Merge "Remove unused isMaster variable in ClusteredNetconfDevice"
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / util / SchemalessRpcStructureTransformerTest.java
1 package org.opendaylight.netconf.sal.connect.netconf.util;
2
3 import com.google.common.base.Optional;
4 import com.google.common.collect.ImmutableMap;
5 import java.io.IOException;
6 import java.net.URISyntaxException;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
9 import java.util.Arrays;
10 import java.util.Collection;
11 import java.util.Map;
12 import javax.xml.transform.dom.DOMSource;
13 import org.custommonkey.xmlunit.Diff;
14 import org.custommonkey.xmlunit.XMLUnit;
15 import org.junit.Assert;
16 import org.junit.BeforeClass;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.ExpectedException;
20 import org.junit.runner.RunWith;
21 import org.junit.runners.Parameterized;
22 import org.opendaylight.controller.config.util.xml.XmlElement;
23 import org.opendaylight.controller.config.util.xml.XmlUtil;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.xml.sax.SAXException;
32
33 @RunWith(Parameterized.class)
34 public class SchemalessRpcStructureTransformerTest {
35
36     private static final String NAMESPACE = "http://example.com/schema/1.2/config";
37
38     @Rule
39     public final ExpectedException thrown = ExpectedException.none();
40     private final Class<? extends Exception> expectedException;
41
42     private final String expectedConfig;
43     private final String expectedFilter;
44     private final String getConfigData;
45     private final YangInstanceIdentifier path;
46     private final DOMSource source;
47
48     private final SchemalessRpcStructureTransformer adapter = new SchemalessRpcStructureTransformer();
49     private final String testDataset;
50
51     public SchemalessRpcStructureTransformerTest(YangInstanceIdentifier path, String testDataset, Class<? extends Exception> expectedException) throws IOException, SAXException, URISyntaxException {
52         this.path = path;
53         this.testDataset = testDataset;
54         this.expectedException = expectedException;
55         this.source = new DOMSource(XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/schemaless/data/" + testDataset)).getDocumentElement());
56         this.expectedConfig = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/edit-config/" + testDataset).toURI())));
57         this.expectedFilter = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/filter/" + testDataset).toURI())));
58         this.getConfigData = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/get-config/" + testDataset).toURI())));
59     }
60
61     @Parameterized.Parameters
62     public static Collection parameters() {
63         Object[][] params = {
64                 {YangInstanceIdentifier.builder()
65                         .node(createNodeId("top"))
66                         .node(createNodeId("users"))
67                         .build(), "container.xml", null},
68                 {YangInstanceIdentifier.builder()
69                         .node(createNodeId("top"))
70                         .node(createNodeId("users"))
71                         .node(createListNodeId("user", "key", "k1"))
72                         .build(), "keyed-list.xml", null},
73                 {YangInstanceIdentifier.builder()
74                         .node(createNodeId("top"))
75                         .node(createNodeId("users"))
76                         .node(createListNodeId("user", ImmutableMap.of(QName.create(NAMESPACE, "key1"), "k1", QName.create(NAMESPACE, "key2"), "k2")))
77                         .build(), "keyed-list-compound-key.xml", null},
78                 {YangInstanceIdentifier.builder()
79                         .node(createNodeId("top"))
80                         .node(createNodeId("users"))
81                         .node(createListNodeId("user", "key", "k2"))
82                         .build(), "keyed-list-bad-key.xml", IllegalStateException.class}
83         };
84         return Arrays.asList(params);
85     }
86
87     @BeforeClass
88     public static void suiteSetup() {
89         XMLUnit.setIgnoreWhitespace(true);
90     }
91
92     @Test
93     public void testCreateEditConfigStructure() throws Exception {
94         if(expectedException != null) {
95             thrown.expect(expectedException);
96         }
97         AnyXmlNode data = Builders.anyXmlBuilder()
98                 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
99                 .withValue(source)
100                 .build();
101         final AnyXmlNode anyXmlNode = adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE));
102         final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
103         Diff diff = new Diff(expectedConfig, s);
104         Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
105     }
106
107     @Test
108     public void testToFilterStructure() throws Exception {
109         final AnyXmlNode anyXmlNode = (AnyXmlNode) adapter.toFilterStructure(path);
110         final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
111         Diff diff = new Diff(expectedFilter, s);
112         Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
113     }
114
115     @Test
116     public void testSelectFromDataStructure() throws Exception {
117         AnyXmlNode data = Builders.anyXmlBuilder()
118                 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
119                 .withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement()))
120                 .build();
121         final AnyXmlNode dataStructure = (AnyXmlNode) adapter.selectFromDataStructure(data, path).get();
122         final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.getValue().getNode());
123         final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
124         final String expectedData = XmlUtil.toString((Element) source.getNode());
125         Diff diff = new Diff(expectedData, dataFromReply);
126         Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
127     }
128
129     private static YangInstanceIdentifier.NodeIdentifier createNodeId(String name) {
130         return new YangInstanceIdentifier.NodeIdentifier(QName.create(NAMESPACE, name));
131     }
132
133     private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, String keyName, String id) {
134         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), QName.create(NAMESPACE, keyName), id);
135     }
136
137     private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, Map<QName, Object> keys) {
138         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), keys);
139     }
140 }