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;
14 import java.net.URISyntaxException;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import javax.ws.rs.WebApplicationException;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
23 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
24 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
25 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
26 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
27 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
28 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 public class CnSnToJsonWithDataFromSeveralModulesTest extends YangAndXmlAndDataSchemaLoader {
34 public static void initialize() {
35 dataLoad("/xml-to-cnsn/data-of-several-modules/yang",2,"module1","cont_m1");
39 public void dataFromSeveralModulesToJsonTest() throws WebApplicationException, IOException, URISyntaxException {
40 SchemaContext schemaContext = TestUtils.loadSchemaContext(modules);
41 String output = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCnSn(), modules, schemaContext,
42 StructuredDataToJsonProvider.INSTANCE);
45 // String.format("\"data\" : {\n" +
46 // "\t\"cont_m1\" : {\n" +
47 // "\t\t\"lf1_m1\" : \"lf1 m1 value\"\n" +
49 // "\t\"cont_m2\" : {\n" +
50 // "\t\t\"lf1_m2\" : \"lf1 m2 value\"\n" +
54 StringBuilder regex = new StringBuilder();
57 regex.append(".*\"data\"");
59 regex.append(".*\\{");
61 regex.append(".*\"contB_m1\"");
63 regex.append(".*\\{");
64 regex.append(".*\\}");
66 regex.append(".*\"cont_m1\"");
68 regex.append(".*\\{");
69 regex.append(".*\\}");
71 regex.append(".*\"contB_m2\"");
73 regex.append(".*\\{");
74 regex.append(".*\\}");
76 regex.append(".*\"cont_m2\"");
78 regex.append(".*\\{");
79 regex.append(".*\\}");
81 regex.append(".*\\}");
86 Pattern ptrn = Pattern.compile(regex.toString(), Pattern.DOTALL);
87 Matcher matcher = ptrn.matcher(output);
89 assertTrue(matcher.find());
93 private CompositeNode prepareCnSn() throws URISyntaxException {
94 CompositeNodeWrapper data = new CompositeNodeWrapper(new URI("urn:ietf:params:xml:ns:netconf:base:1.0"), "data");
96 URI uriModule1 = new URI("module:one");
97 CompositeNodeWrapper cont_m1 = new CompositeNodeWrapper(uriModule1, "cont_m1");
98 SimpleNodeWrapper lf1_m1 = new SimpleNodeWrapper(uriModule1, "lf1_m1", "lf1 m1 value");
99 cont_m1.addValue(lf1_m1);
100 CompositeNodeWrapper contB_m1 = new CompositeNodeWrapper(uriModule1, "contB_m1");
102 data.addValue(contB_m1);
103 data.addValue(cont_m1);
105 URI uriModule2 = new URI("module:two");
106 CompositeNodeWrapper cont_m2 = new CompositeNodeWrapper(uriModule2, "cont_m2");
107 SimpleNodeWrapper lf1_m2 = new SimpleNodeWrapper(uriModule2, "lf1_m2", "lf1 m2 value");
108 cont_m2.addValue(lf1_m2);
109 CompositeNodeWrapper contB_m2 = new CompositeNodeWrapper(uriModule2, "contB_m2");
110 data.addValue(contB_m2);
111 data.addValue(cont_m2);