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;
17 import javax.ws.rs.WebApplicationException;
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;
30 public class CnSnToJsonWithDataFromSeveralModulesTest extends YangAndXmlAndDataSchemaLoader {
33 public static void initialize() {
34 dataLoad("/xml-to-cnsn/data-of-several-modules/yang", 2, "module1", "cont_m1");
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);
44 // String.format("\"data\" : {\n" +
45 // "\t\"cont_m1\" : {\n" +
46 // "\t\t\"lf1_m1\" : \"lf1 m1 value\"\n" +
48 // "\t\"cont_m2\" : {\n" +
49 // "\t\t\"lf1_m2\" : \"lf1 m2 value\"\n" +
53 StringBuilder regex = new StringBuilder();
56 regex.append(".*\"data\"");
58 regex.append(".*\\{");
60 regex.append(".*\"cont_m1\"");
62 regex.append(".*\\{");
63 regex.append(".*\\}");
65 regex.append(".*\"contB_m1\"");
67 regex.append(".*\\{");
68 regex.append(".*\\}");
70 regex.append(".*\"cont_m2\"");
72 regex.append(".*\\{");
73 regex.append(".*\\}");
75 regex.append(".*\"contB_m2\"");
77 regex.append(".*\\{");
78 regex.append(".*\\}");
80 regex.append(".*\\}");
85 Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
86 Matcher matcher = ptrn.matcher(output);
88 assertTrue(matcher.find());
92 private CompositeNode prepareCnSn() throws URISyntaxException {
93 String uri1 = "module:one";
94 String rev1 = "2014-01-17";
96 MutableCompositeNode data = NodeFactory.createMutableCompositeNode(
97 TestUtils.buildQName("data", "urn:ietf:params:xml:ns:netconf:base:1.0", "2000-01-01"), null, null,
100 MutableCompositeNode cont_m1 = NodeFactory.createMutableCompositeNode(
101 TestUtils.buildQName("cont_m1", uri1, rev1), data, null, null, null);
102 data.getChildren().add(cont_m1);
104 MutableSimpleNode<?> lf1_m1 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1_m1", uri1, rev1),
105 cont_m1, "lf1 m1 value", null, null);
106 cont_m1.getChildren().add(lf1_m1);
109 MutableCompositeNode contB_m1 = NodeFactory.createMutableCompositeNode(
110 TestUtils.buildQName("contB_m1", uri1, rev1), data, null, null, null);
111 data.getChildren().add(contB_m1);
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.getChildren().add(cont_m2);
120 MutableSimpleNode<?> lf1_m2 = NodeFactory.createMutableSimpleNode(TestUtils.buildQName("lf1_m2", uri2, rev2),
121 cont_m1, "lf1 m2 value", null, null);
122 cont_m2.getChildren().add(lf1_m2);
125 MutableCompositeNode contB_m2 = NodeFactory.createMutableCompositeNode(
126 TestUtils.buildQName("contB_m2", uri2, rev2), data, null, null, null);
127 data.getChildren().add(contB_m2);