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