Merge "Increase timeout for waiting for broker service in sal-binding-it."
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / CnSnToJsonLeafrefType.java
1 /*
2  * Copyright (c) 2014 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.sal.restconf.impl.cnsn.to.json.test;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.IOException;
14 import java.util.regex.Matcher;
15 import java.util.regex.Pattern;
16
17 import javax.ws.rs.WebApplicationException;
18
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
22 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
23 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
24 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
25 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
26
27 /**
28  * 
29  * All tests are commented now because leafref isn't supported now
30  * 
31  */
32
33 public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader {
34
35     @BeforeClass
36     public static void initialization() {
37         dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont");
38     }
39
40     @Test
41     public void leafrefAbsolutePathToExistingLeafTest() {
42         String json = toJson("/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
43         validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
44     }
45
46     @Test
47     public void leafrefRelativePathToExistingLeafTest() {
48         String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
49         validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
50     }
51
52     /**
53      * Tests case when reference to not existing element is present. In this
54      * case value from single node is printed as string.
55      */
56     @Test
57     public void leafrefToNonExistingLeafTest() {
58         String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
59         validateJson(".*\"lf5\":\\p{Blank}*\"137\".*", json);
60     }
61
62     /**
63      * Tests case when non leaf element is referenced. In this case value from
64      * single node is printed as string.
65      */
66     @Test
67     public void leafrefToNotLeafTest() {
68         String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
69         validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*", json);
70     }
71
72     /**
73      * Tests case when leaflist element is refers to leaf.
74      */
75     @Test
76     public void leafrefFromLeafListToLeafTest() {
77         String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
78         validateJson(
79                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"345\",\\p{Space}*\"346\",\\p{Space}*\"347\".*",
80                 json);
81     }
82
83     /**
84      * Tests case when leaflist element is refers to leaf.
85      */
86     @Test
87     public void leafrefFromLeafrefToLeafrefTest() {
88         String json = toJson("/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
89         validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*", json);
90     }
91
92     private void validateJson(String regex, String value) {
93         assertNotNull(value);
94         Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
95         Matcher mtch = ptrn.matcher(value);
96         assertTrue(mtch.matches());
97     }
98
99     private String toJson(String xmlDataPath) {
100         try {
101             CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlDataPath, XmlToCompositeNodeProvider.INSTANCE);
102             TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName);
103             return TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
104                     StructuredDataToJsonProvider.INSTANCE);
105         } catch (WebApplicationException | IOException e) {
106         }
107         return "";
108     }
109
110 }