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 testResultFor() throws Exception {
31 final String expectedResult = "dummy string";
32 RpcResult<String> rpcResult = Util.resultRpcSuccessFor(expectedResult).get();
33 assertEquals(expectedResult, rpcResult.getResult());
34 assertTrue(rpcResult.isSuccessful());
35 assertTrue(rpcResult.getErrors().isEmpty());
38 final Integer expectedResult = 42;
39 RpcResult<Integer> rpcResult = Util.resultRpcSuccessFor(expectedResult).get();
40 assertEquals(expectedResult, rpcResult.getResult());
41 assertTrue(rpcResult.isSuccessful());
42 assertTrue(rpcResult.getErrors().isEmpty());
47 public void testExpandQname() throws Exception {
48 // match no path because the list of the allowed paths is empty
50 final List<SchemaPath> paths = new ArrayList<>();
51 final Pattern regexPattern = Pattern.compile(".*"); // match everything
52 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
53 assertTrue(matchingPaths.isEmpty());
56 // match no path because of regex pattern
58 final List<SchemaPath> paths = createSchemaPathList();
59 final Pattern regexPattern = Pattern.compile("^@.*");
60 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
61 assertTrue(matchingPaths.isEmpty());
66 final List<SchemaPath> paths = createSchemaPathList();
67 final Pattern regexPattern = Pattern.compile(".*");
68 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
69 assertTrue(matchingPaths.contains(paths.get(0)));
70 assertTrue(matchingPaths.contains(paths.get(1)));
71 assertEquals(paths.size(), matchingPaths.size());
74 // match one path only
76 final List<SchemaPath> paths = createSchemaPathList();
77 final Pattern regexPattern = Pattern.compile(".*yyy$");
78 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
79 assertTrue(matchingPaths.contains(paths.get(1)));
80 assertEquals(1, matchingPaths.size());
84 private static List<SchemaPath> createSchemaPathList() {
85 final QName qname1 = QName.create("urn:odl:xxx", "2015-01-01", "localName");
86 final QName qname2 = QName.create("urn:odl:yyy", "2015-01-01", "localName");
87 final SchemaPath path1 = SchemaPath.create(true, qname1);
88 final SchemaPath path2 = SchemaPath.create(true, qname2);
89 return Arrays.asList(path1, path2);