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