Merge changes I114cbac1,I45c2e7cd
[controller.git] / opendaylight / md-sal / messagebus-impl / src / test / java / org / opendaylight / controller / messagebus / app / impl / UtilTest.java
1 /*
2  * Copyright (c) 2015 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  * @author ppalmar
24  *
25  */
26 public class UtilTest {
27
28     @Test
29     public void testWildcardToRegex() throws Exception {
30         // empty wildcard string
31         createAndAssertRegex("", "^$");
32
33         // wildcard string is a char to be replaced
34         createAndAssertRegex("*", "^.*$");
35         createAndAssertRegex("?", "^.$");
36         final String relevantChars = "()[]$^.{}|\\";
37         for (final char c : relevantChars.toCharArray()) {
38             final char oneChar[] = {c};
39             final String wildcardStr = new String(oneChar);
40             final String expectedRegex = "^\\" + c + "$";
41             createAndAssertRegex(wildcardStr, expectedRegex);
42         }
43
44         // wildcard string consists of more chars
45         createAndAssertRegex("a", "^a$");
46         createAndAssertRegex("aBc", "^aBc$");
47         createAndAssertRegex("a1b2C34", "^a1b2C34$");
48         createAndAssertRegex("*?()[]$^.{}|\\X", "^.*.\\(\\)\\[\\]\\$\\^\\.\\{\\}\\|\\\\X$");
49         createAndAssertRegex("a*BB?37|42$", "^a.*BB.37\\|42\\$$");
50     }
51
52     @Test
53     public void testResultFor() throws Exception {
54         {
55             final String expectedResult = "dummy string";
56             RpcResult<String> rpcResult = Util.resultRpcSuccessFor(expectedResult).get();
57             assertEquals(expectedResult, rpcResult.getResult());
58             assertTrue(rpcResult.isSuccessful());
59             assertTrue(rpcResult.getErrors().isEmpty());
60         }
61         {
62             final Integer expectedResult = 42;
63             RpcResult<Integer> rpcResult = Util.resultRpcSuccessFor(expectedResult).get();
64             assertEquals(expectedResult, rpcResult.getResult());
65             assertTrue(rpcResult.isSuccessful());
66             assertTrue(rpcResult.getErrors().isEmpty());
67         }
68     }
69
70     @Test
71     public void testExpandQname() throws Exception {
72         // match no path because the list of the allowed paths is empty
73         {
74             final List<SchemaPath> paths = new ArrayList<>();
75             final Pattern regexPattern = Pattern.compile(".*"); // match everything
76             final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
77             assertTrue(matchingPaths.isEmpty());
78         }
79
80         // match no path because of regex pattern
81         {
82             final List<SchemaPath> paths = createSchemaPathList();
83             final Pattern regexPattern = Pattern.compile("^@.*");
84             final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
85             assertTrue(matchingPaths.isEmpty());
86         }
87
88         // match all paths
89         {
90             final List<SchemaPath> paths = createSchemaPathList();
91             final Pattern regexPattern = Pattern.compile(".*");
92             final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
93             assertTrue(matchingPaths.contains(paths.get(0)));
94             assertTrue(matchingPaths.contains(paths.get(1)));
95             assertEquals(paths.size(), matchingPaths.size());
96         }
97
98         // match one path only
99         {
100             final List<SchemaPath> paths = createSchemaPathList();
101             final Pattern regexPattern = Pattern.compile(".*yyy$");
102             final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
103             assertTrue(matchingPaths.contains(paths.get(1)));
104             assertEquals(1, matchingPaths.size());
105         }
106     }
107
108     private static void createAndAssertRegex(final String wildcardStr, final String expectedRegex) {
109         assertEquals("Incorrect regex string.", expectedRegex, Util.wildcardToRegex(wildcardStr));
110     }
111
112     private static List<SchemaPath> createSchemaPathList() {
113         final QName qname1 = QName.create("urn:odl:xxx", "2015-01-01", "localName");
114         final QName qname2 = QName.create("urn:odl:yyy", "2015-01-01", "localName");
115         final SchemaPath path1 = SchemaPath.create(true, qname1);
116         final SchemaPath path2 = SchemaPath.create(true, qname2);
117         return Arrays.asList(path1, path2);
118     }
119 }