2 * Copyright (c) 2015 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;
26 public class UtilTest {
29 public void testWildcardToRegex() throws Exception {
30 // empty wildcard string
31 createAndAssertRegex("", "^$");
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);
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\\$$");
53 public void testResultFor() throws Exception {
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());
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());
71 public void testExpandQname() throws Exception {
72 // match no path because the list of the allowed paths is empty
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());
80 // match no path because of regex pattern
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());
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());
98 // match one path only
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());
108 private static void createAndAssertRegex(final String wildcardStr, final String expectedRegex) {
109 assertEquals("Incorrect regex string.", expectedRegex, Util.wildcardToRegex(wildcardStr));
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);