Fix checkstyle violations in messagebus
[controller.git] / opendaylight / md-sal / messagebus-util / src / test / java / org / opendaylight / controller / messagebus / app / util / 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.util;
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 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;
21
22 /**
23  * Unit tests for Util.
24  *
25  * @author ppalmar
26  */
27 public class UtilTest {
28
29     @Test
30     public void testResultFor() throws Exception {
31         {
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());
37         }
38         {
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());
44         }
45     }
46
47     @Test
48     public void testExpandQname() throws Exception {
49         // match no path because the list of the allowed paths is empty
50         {
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());
55         }
56
57         // match no path because of regex pattern
58         {
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());
63         }
64
65         // match all paths
66         {
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());
73         }
74
75         // match one path only
76         {
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());
82         }
83     }
84
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);
91     }
92 }