BUG-868: migrate users of CompositeNode.getChildren()
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / CnSnToJsonWithDataFromSeveralModulesTest.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.assertTrue;
11
12 import java.io.IOException;
13 import java.net.URISyntaxException;
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.restconf.impl.test.TestUtils;
23 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.MutableCompositeNode;
26 import org.opendaylight.yangtools.yang.data.api.MutableSimpleNode;
27 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 public class CnSnToJsonWithDataFromSeveralModulesTest extends YangAndXmlAndDataSchemaLoader {
31
32     @BeforeClass
33     public static void initialize() {
34         dataLoad("/xml-to-cnsn/data-of-several-modules/yang", 2, "module1", "cont_m1");
35     }
36
37     @Test
38     public void dataFromSeveralModulesToJsonTest() throws WebApplicationException, IOException, URISyntaxException {
39         SchemaContext schemaContext = TestUtils.loadSchemaContext(modules);
40         String output = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCnSn(), modules, schemaContext,
41                 StructuredDataToJsonProvider.INSTANCE);
42
43         // String output =
44         // String.format("\"data\"   :   {\n" +
45         // "\t\"cont_m1\"   :  {\n" +
46         // "\t\t\"lf1_m1\"   :  \"lf1 m1 value\"\n" +
47         // "\t}\n" +
48         // "\t\"cont_m2\"   :  {\n" +
49         // "\t\t\"lf1_m2\"   :  \"lf1 m2 value\"\n" +
50         // "\t}\n" +
51         // "}");
52
53         StringBuilder regex = new StringBuilder();
54         regex.append("^");
55
56         regex.append(".*\"data\"");
57         regex.append(".*:");
58         regex.append(".*\\{");
59
60         regex.append(".*\"cont_m1\"");
61         regex.append(".*:");
62         regex.append(".*\\{");
63         regex.append(".*\\}");
64
65         regex.append(".*\"contB_m1\"");
66         regex.append(".*:");
67         regex.append(".*\\{");
68         regex.append(".*\\}");
69
70         regex.append(".*\"cont_m2\"");
71         regex.append(".*:");
72         regex.append(".*\\{");
73         regex.append(".*\\}");
74
75         regex.append(".*\"contB_m2\"");
76         regex.append(".*:");
77         regex.append(".*\\{");
78         regex.append(".*\\}");
79
80         regex.append(".*\\}");
81
82         regex.append(".*");
83         regex.append("$");
84
85         Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
86         Matcher matcher = ptrn.matcher(output);
87
88         assertTrue(matcher.find());
89
90     }
91
92     private CompositeNode prepareCnSn() throws URISyntaxException {
93         String uri1 = "module:one";
94         String rev1 = "2014-01-17";
95
96         MutableCompositeNode data = NodeFactory.createMutableCompositeNode(
97                 TestUtils.buildQName("data", "urn:ietf:params:xml:ns:netconf:base:1.0", "2000-01-01"), null, null,
98                 null, null);
99
100         MutableCompositeNode cont_m1 = NodeFactory.createMutableCompositeNode(
101                 TestUtils.buildQName("cont_m1", uri1, rev1), data, null, null, null);
102         data.getValue().add(cont_m1);
103
104         MutableSimpleNode<?> lf1_m1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1_m1", uri1, rev1),
105                 cont_m1, "lf1 m1 value", null, null);
106         cont_m1.getValue().add(lf1_m1);
107         cont_m1.init();
108
109         MutableCompositeNode contB_m1 = NodeFactory.createMutableCompositeNode(
110                 TestUtils.buildQName("contB_m1", uri1, rev1), data, null, null, null);
111         data.getValue().add(contB_m1);
112         contB_m1.init();
113
114         String uri2 = "module:two";
115         String rev2 = "2014-01-17";
116         MutableCompositeNode cont_m2 = NodeFactory.createMutableCompositeNode(
117                 TestUtils.buildQName("cont_m2", uri2, rev2), data, null, null, null);
118         data.getValue().add(cont_m2);
119
120         MutableSimpleNode<?> lf1_m2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1_m2", uri2, rev2),
121                 cont_m1, "lf1 m2 value", null, null);
122         cont_m2.getValue().add(lf1_m2);
123         cont_m2.init();
124
125         MutableCompositeNode contB_m2 = NodeFactory.createMutableCompositeNode(
126                 TestUtils.buildQName("contB_m2", uri2, rev2), data, null, null, null);
127         data.getValue().add(contB_m2);
128         contB_m2.init();
129
130         data.init();
131         return data;
132     }
133
134 }