Convert NetconfSalKeystoreService into a component
[netconf.git] / plugins / 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.EffectiveOperation;
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         source = new DOMSource(XmlUtil.readXmlToDocument(getClass()
98                 .getResourceAsStream("/schemaless/data/" + testDataset)).getDocumentElement());
99         expectedConfig = Files.readString(
100                 Paths.get(getClass().getResource("/schemaless/edit-config/" + testDataset).toURI()));
101         expectedFilter = Files.readString(
102                 Paths.get(getClass().getResource("/schemaless/filter/" + testDataset).toURI()));
103         getConfigData = Files.readString(
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,
117                     Optional.of(EffectiveOperation.REPLACE)));
118             return;
119         }
120
121         final DOMSourceAnyxmlNode anyXmlNode =
122                 adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(EffectiveOperation.REPLACE));
123         final String s = XmlUtil.toString((Element) anyXmlNode.body().getNode());
124         Diff diff = new Diff(expectedConfig, s);
125         assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
126     }
127
128     @Test
129     public void testToFilterStructure() throws Exception {
130         final DOMSourceAnyxmlNode anyXmlNode = (DOMSourceAnyxmlNode) adapter.toFilterStructure(path);
131         final String s = XmlUtil.toString((Element) anyXmlNode.body().getNode());
132         Diff diff = new Diff(expectedFilter, s);
133         assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
134     }
135
136     @Test
137     public void testSelectFromDataStructure() throws Exception {
138         DOMSourceAnyxmlNode data = Builders.anyXmlBuilder()
139                 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
140                 .withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement()))
141                 .build();
142         final DOMSourceAnyxmlNode dataStructure = (DOMSourceAnyxmlNode) adapter.selectFromDataStructure(data, path)
143                 .get();
144         final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.body().getNode());
145         final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
146         final String expectedData = XmlUtil.toString((Element) source.getNode());
147         Diff diff = new Diff(expectedData, dataFromReply);
148         assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
149     }
150
151     private static NodeIdentifier createNodeId(final String name) {
152         return new NodeIdentifier(QName.create(NAMESPACE, name));
153     }
154
155     private static NodeIdentifierWithPredicates createListNodeId(
156             final String nodeName, final String keyName, final String id) {
157         return NodeIdentifierWithPredicates.of(QName.create(NAMESPACE, nodeName), QName.create(NAMESPACE, keyName), id);
158     }
159
160     private static NodeIdentifierWithPredicates createListNodeId(final String nodeName, final Map<QName, Object> keys) {
161         return NodeIdentifierWithPredicates.of(QName.create(NAMESPACE, nodeName), keys);
162     }
163 }