2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
10 import static org.junit.Assert.assertTrue;
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;
28 public class CnSnToJsonWithDataFromSeveralModulesTest extends YangAndXmlAndDataSchemaLoader {
31 public static void initialize() {
32 dataLoad("/xml-to-cnsn/data-of-several-modules/yang", 2, "module1", "cont_m1");
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);
42 // String.format("\"data\" : {\n" +
43 // "\t\"cont_m1\" : {\n" +
44 // "\t\t\"lf1_m1\" : \"lf1 m1 value\"\n" +
46 // "\t\"cont_m2\" : {\n" +
47 // "\t\t\"lf1_m2\" : \"lf1 m2 value\"\n" +
51 StringBuilder regex = new StringBuilder();
54 regex.append(".*\"data\"");
56 regex.append(".*\\{");
58 regex.append(".*\"cont_m1\"");
60 regex.append(".*\\{");
61 regex.append(".*\\}");
63 regex.append(".*\"contB_m1\"");
65 regex.append(".*\\{");
66 regex.append(".*\\}");
68 regex.append(".*\"cont_m2\"");
70 regex.append(".*\\{");
71 regex.append(".*\\}");
73 regex.append(".*\"contB_m2\"");
75 regex.append(".*\\{");
76 regex.append(".*\\}");
78 regex.append(".*\\}");
83 Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
84 Matcher matcher = ptrn.matcher(output);
86 assertTrue(matcher.find());
90 private CompositeNode prepareCnSn() throws URISyntaxException {
91 String uri1 = "module:one";
92 String rev1 = "2014-01-17";
94 MutableCompositeNode data = NodeFactory.createMutableCompositeNode(
95 TestUtils.buildQName("data", "urn:ietf:params:xml:ns:netconf:base:1.0", "2000-01-01"), null, null,
98 MutableCompositeNode cont_m1 = NodeFactory.createMutableCompositeNode(
99 TestUtils.buildQName("cont_m1", uri1, rev1), data, null, null, null);
100 data.getValue().add(cont_m1);
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);
107 MutableCompositeNode contB_m1 = NodeFactory.createMutableCompositeNode(
108 TestUtils.buildQName("contB_m1", uri1, rev1), data, null, null, null);
109 data.getValue().add(contB_m1);
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);
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);
123 MutableCompositeNode contB_m2 = NodeFactory.createMutableCompositeNode(
124 TestUtils.buildQName("contB_m2", uri2, rev2), data, null, null, null);
125 data.getValue().add(contB_m2);