7aebb8fbdd6ac14919c9b1c8d6d5ec8327559f5c
[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 testResultFor() throws Exception {
30         {
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());
36         }
37         {
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());
43         }
44     }
45
46     @Test
47     public void testExpandQname() throws Exception {
48         // match no path because the list of the allowed paths is empty
49         {
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());
54         }
55
56         // match no path because of regex pattern
57         {
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());
62         }
63
64         // match all paths
65         {
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());
72         }
73
74         // match one path only
75         {
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());
81         }
82     }
83
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);
90     }
91 }