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