Merge "Removed `which` dependency, now using proper shell builtin."
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / xml / test / CnSnToXmlWithDataFromSeveralModulesTest.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.xml.test;
9
10 import static org.junit.Assert.assertTrue;
11
12 import java.io.IOException;
13 import java.net.URI;
14 import java.net.URISyntaxException;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import javax.ws.rs.WebApplicationException;
19
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
23 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
24 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
25 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
26 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
27 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29
30 public class CnSnToXmlWithDataFromSeveralModulesTest 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 dataFromSeveralModulesToXmlTest() throws WebApplicationException, IOException, URISyntaxException {
39         SchemaContext schemaContext = TestUtils.loadSchemaContext(modules);
40         String output = TestUtils.writeCompNodeWithSchemaContextToOutput(prepareCnSn(), modules, schemaContext,
41                 StructuredDataToXmlProvider.INSTANCE);
42
43 //         String output =
44 //         String.format("<data>" +
45 //                  "\n<cont_m1>" +
46 //                         "\n\t<lf1_m1>" +
47 //                             "\n\t\tlf1 m1 value" +
48 //                             "\n\t</lf1_m1>" +
49 //                         "\n</cont_m1>" +
50 //                         "\n<cont_m2>" +
51 //                             "\n\t<lf1_m2>" +
52 //                                 "\n\t\tlf1 m2 value" +
53 //                             "\n\t</lf1_m2>" +
54 //                         "\n</cont_m2>" +
55 //                     "\n</data>");
56
57         StringBuilder regex = new StringBuilder();
58         regex.append("^");
59
60         regex.append(".*<data.*");
61         regex.append(".*xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"");
62         regex.append(".*>");
63
64
65         regex.append(".*<contB_m1.*\\/>");
66         regex.append(".*xmlns=\"module:one\"");
67         regex.append(".*>");
68         regex.append(".*<lf1_m1.*>");
69         regex.append(".*<\\/lf1_m1>");
70         regex.append(".*<\\/cont_m1>");
71
72         regex.append(".*<contB_m2.*/>");
73         regex.append(".*<cont_m2.*");
74         regex.append(".*xmlns=\"module:two\"");
75         regex.append(".*>");
76         regex.append(".*<lf1_m2.*>");
77         regex.append(".*<\\/lf1_m2>");
78         regex.append(".*<\\/cont_m2>");
79
80         regex.append(".*<\\/data.*>");
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         CompositeNodeWrapper data = new CompositeNodeWrapper(new URI("urn:ietf:params:xml:ns:netconf:base:1.0"), "data");
94
95         URI uriModule1 = new URI("module:one");
96         CompositeNodeWrapper cont_m1 = new CompositeNodeWrapper(uriModule1, "cont_m1");
97         SimpleNodeWrapper lf1_m1 = new SimpleNodeWrapper(uriModule1, "lf1_m1", "lf1 m1 value");
98         cont_m1.addValue(lf1_m1);
99         CompositeNodeWrapper contB_m1 = new CompositeNodeWrapper(uriModule1, "contB_m1");
100
101         data.addValue(contB_m1);
102         data.addValue(cont_m1);
103
104         URI uriModule2 = new URI("module:two");
105         CompositeNodeWrapper cont_m2 = new CompositeNodeWrapper(uriModule2, "cont_m2");
106         SimpleNodeWrapper lf1_m2 = new SimpleNodeWrapper(uriModule2, "lf1_m2", "lf1 m2 value");
107         cont_m2.addValue(lf1_m2);
108         CompositeNodeWrapper contB_m2 = new CompositeNodeWrapper(uriModule2, "contB_m2");
109         data.addValue(contB_m2);
110         data.addValue(cont_m2);
111         return data;
112     }
113
114 }