2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.messagebus.app.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.regex.Pattern;
18 import org.junit.Test;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 public class UtilTest {
26 public void testMD5Hash() throws Exception {
28 createAndAssertHash("", "d41d8cd98f00b204e9800998ecf8427e");
31 createAndAssertHash("The Guardian", "69b929ae473ed732d5fb8e0a55a8dc8d");
33 // the same hash for the same string
34 createAndAssertHash("The Independent", "db793706d70c37dcc16454fa8eb21b1c");
35 createAndAssertHash("The Independent", "db793706d70c37dcc16454fa8eb21b1c"); // one more time
37 // different strings must have different hashes
38 createAndAssertHash("orange", "fe01d67a002dfa0f3ac084298142eccd");
39 createAndAssertHash("yellow", "d487dd0b55dfcacdd920ccbdaeafa351");
42 //TODO: IllegalArgumentException would be better
43 @Test(expected = RuntimeException.class)
44 public void testMD5HashInvalidInput() throws Exception {
49 public void testWildcardToRegex() throws Exception {
50 // empty wildcard string
51 createAndAssertRegex("", "^$");
53 // wildcard string is a char to be replaced
54 createAndAssertRegex("*", "^.*$");
55 createAndAssertRegex("?", "^.$");
56 final String relevantChars = "()[]$^.{}|\\";
57 for (final char c : relevantChars.toCharArray()) {
58 final char oneChar[] = {c};
59 final String wildcardStr = new String(oneChar);
60 final String expectedRegex = "^\\" + c + "$";
61 createAndAssertRegex(wildcardStr, expectedRegex);
64 // wildcard string consists of more chars
65 createAndAssertRegex("a", "^a$");
66 createAndAssertRegex("aBc", "^aBc$");
67 createAndAssertRegex("a1b2C34", "^a1b2C34$");
68 createAndAssertRegex("*?()[]$^.{}|\\X", "^.*.\\(\\)\\[\\]\\$\\^\\.\\{\\}\\|\\\\X$");
69 createAndAssertRegex("a*BB?37|42$", "^a.*BB.37\\|42\\$$");
73 public void testResultFor() throws Exception {
75 final String expectedResult = "dummy string";
76 RpcResult<String> rpcResult = Util.resultFor(expectedResult).get();
77 assertEquals(expectedResult, rpcResult.getResult());
78 assertTrue(rpcResult.isSuccessful());
79 assertTrue(rpcResult.getErrors().isEmpty());
82 final Integer expectedResult = 42;
83 RpcResult<Integer> rpcResult = Util.resultFor(expectedResult).get();
84 assertEquals(expectedResult, rpcResult.getResult());
85 assertTrue(rpcResult.isSuccessful());
86 assertTrue(rpcResult.getErrors().isEmpty());
91 public void testExpandQname() throws Exception {
92 // match no path because the list of the allowed paths is empty
94 final List<SchemaPath> paths = new ArrayList<>();
95 final Pattern regexPattern = Pattern.compile(".*"); // match everything
96 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
97 assertTrue(matchingPaths.isEmpty());
100 // match no path because of regex pattern
102 final List<SchemaPath> paths = createSchemaPathList();
103 final Pattern regexPattern = Pattern.compile("^@.*");
104 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
105 assertTrue(matchingPaths.isEmpty());
110 final List<SchemaPath> paths = createSchemaPathList();
111 final Pattern regexPattern = Pattern.compile(".*");
112 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
113 assertTrue(matchingPaths.contains(paths.get(0)));
114 assertTrue(matchingPaths.contains(paths.get(1)));
115 assertEquals(paths.size(), matchingPaths.size());
118 // match one path only
120 final List<SchemaPath> paths = createSchemaPathList();
121 final Pattern regexPattern = Pattern.compile(".*yyy$");
122 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
123 assertTrue(matchingPaths.contains(paths.get(1)));
124 assertEquals(1, matchingPaths.size());
128 private static void createAndAssertHash(final String inString, final String expectedHash) {
129 assertEquals("Incorrect hash.", expectedHash, Util.md5String(inString));
132 private static void createAndAssertRegex(final String wildcardStr, final String expectedRegex) {
133 assertEquals("Incorrect regex string.", expectedRegex, Util.wildcardToRegex(wildcardStr));
136 private static List<SchemaPath> createSchemaPathList() {
137 final QName qname1 = QName.create("urn:odl:xxx", "2015-01-01", "localName");
138 final QName qname2 = QName.create("urn:odl:yyy", "2015-01-01", "localName");
139 final SchemaPath path1 = SchemaPath.create(true, qname1);
140 final SchemaPath path2 = SchemaPath.create(true, qname2);
141 return Arrays.asList(path1, path2);