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