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.util;
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;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.common.RpcResult;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 * Unit tests for Util.
27 public class UtilTest {
30 public void testResultFor() throws Exception {
32 final String expectedResult = "dummy string";
33 RpcResult<String> rpcResult = Util.resultRpcSuccessFor(expectedResult).get();
34 assertEquals(expectedResult, rpcResult.getResult());
35 assertTrue(rpcResult.isSuccessful());
36 assertTrue(rpcResult.getErrors().isEmpty());
39 final Integer expectedResult = 42;
40 RpcResult<Integer> rpcResult = Util.resultRpcSuccessFor(expectedResult).get();
41 assertEquals(expectedResult, rpcResult.getResult());
42 assertTrue(rpcResult.isSuccessful());
43 assertTrue(rpcResult.getErrors().isEmpty());
48 public void testExpandQname() throws Exception {
49 // match no path because the list of the allowed paths is empty
51 final List<SchemaPath> paths = new ArrayList<>();
52 final Pattern regexPattern = Pattern.compile(".*"); // match everything
53 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
54 assertTrue(matchingPaths.isEmpty());
57 // match no path because of regex pattern
59 final List<SchemaPath> paths = createSchemaPathList();
60 final Pattern regexPattern = Pattern.compile("^@.*");
61 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
62 assertTrue(matchingPaths.isEmpty());
67 final List<SchemaPath> paths = createSchemaPathList();
68 final Pattern regexPattern = Pattern.compile(".*");
69 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
70 assertTrue(matchingPaths.contains(paths.get(0)));
71 assertTrue(matchingPaths.contains(paths.get(1)));
72 assertEquals(paths.size(), matchingPaths.size());
75 // match one path only
77 final List<SchemaPath> paths = createSchemaPathList();
78 final Pattern regexPattern = Pattern.compile(".*yyy$");
79 final List<SchemaPath> matchingPaths = Util.expandQname(paths, regexPattern);
80 assertTrue(matchingPaths.contains(paths.get(1)));
81 assertEquals(1, matchingPaths.size());
85 private static List<SchemaPath> createSchemaPathList() {
86 final QName qname1 = QName.create("urn:odl:xxx", "2015-01-01", "localName");
87 final QName qname2 = QName.create("urn:odl:yyy", "2015-01-01", "localName");
88 final SchemaPath path1 = SchemaPath.create(true, qname1);
89 final SchemaPath path2 = SchemaPath.create(true, qname2);
90 return Arrays.asList(path1, path2);