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