2ff4654155402231c43e1722374ee7fa0088b452
[controller.git] / opendaylight / md-sal / messagebus-impl / src / test / java / org / opendaylight / controller / messagebus / app / impl / UtilTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.messagebus.app.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.regex.Pattern;
17
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;
22
23 public class UtilTest {
24
25     @Test
26     public void testMD5Hash() throws Exception {
27         // empty string
28         createAndAssertHash("", "d41d8cd98f00b204e9800998ecf8427e");
29
30         // non-empty string
31         createAndAssertHash("The Guardian", "69b929ae473ed732d5fb8e0a55a8dc8d");
32
33         // the same hash for the same string
34         createAndAssertHash("The Independent", "db793706d70c37dcc16454fa8eb21b1c");
35         createAndAssertHash("The Independent", "db793706d70c37dcc16454fa8eb21b1c"); // one more time
36
37         // different strings must have different hashes
38         createAndAssertHash("orange", "fe01d67a002dfa0f3ac084298142eccd");
39         createAndAssertHash("yellow", "d487dd0b55dfcacdd920ccbdaeafa351");
40     }
41
42     //TODO: IllegalArgumentException would be better
43     @Test(expected = RuntimeException.class)
44     public void testMD5HashInvalidInput() throws Exception {
45         Util.md5String(null);
46     }
47
48     @Test
49     public void testWildcardToRegex() throws Exception {
50         // empty wildcard string
51         createAndAssertRegex("", "^$");
52
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);
62         }
63
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\\$$");
70     }
71
72     @Test
73     public void testResultFor() throws Exception {
74         {
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());
80         }
81         {
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());
87         }
88     }
89
90     @Test
91     public void testExpandQname() throws Exception {
92         // match no path because the list of the allowed paths is empty
93         {
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());
98         }
99
100         // match no path because of regex pattern
101         {
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());
106         }
107
108         // match all paths
109         {
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());
116         }
117
118         // match one path only
119         {
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());
125         }
126     }
127
128     private static void createAndAssertHash(final String inString, final String expectedHash) {
129         assertEquals("Incorrect hash.", expectedHash, Util.md5String(inString));
130     }
131
132     private static void createAndAssertRegex(final String wildcardStr, final String expectedRegex) {
133         assertEquals("Incorrect regex string.", expectedRegex, Util.wildcardToRegex(wildcardStr));
134     }
135
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);
142     }
143 }