2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf.util;
10 import static org.junit.Assert.assertThrows;
11 import static org.junit.Assert.assertTrue;
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;
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;
42 @RunWith(Parameterized.class)
43 public class SchemalessRpcStructureTransformerTest {
45 private static final String NAMESPACE = "http://example.com/schema/1.2/config";
47 private final Class<? extends Exception> expectedException;
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;
55 private final SchemalessRpcStructureTransformer adapter = new SchemalessRpcStructureTransformer();
56 private final String testDataset;
58 public SchemalessRpcStructureTransformerTest(
59 final YangInstanceIdentifier path, final String testDataset,
60 final Class<? extends Exception> expectedException) throws IOException, SAXException, URISyntaxException {
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())));
74 @Parameterized.Parameters
75 public static Collection<Object[]> parameters() {
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}
98 return Arrays.asList(params);
102 public static void suiteSetup() {
103 XMLUnit.setIgnoreWhitespace(true);
107 public void testCreateEditConfigStructure() throws Exception {
108 DOMSourceAnyxmlNode data = Builders.anyXmlBuilder()
109 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
113 if (expectedException != null) {
114 assertThrows(expectedException,
115 () -> adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE)));
119 final DOMSourceAnyxmlNode anyXmlNode =
120 adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE));
121 final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
122 Diff diff = new Diff(expectedConfig, s);
123 assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
127 public void testToFilterStructure() throws Exception {
128 final DOMSourceAnyxmlNode anyXmlNode = (DOMSourceAnyxmlNode) adapter.toFilterStructure(path);
129 final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
130 Diff diff = new Diff(expectedFilter, s);
131 assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
135 public void testSelectFromDataStructure() throws Exception {
136 DOMSourceAnyxmlNode data = Builders.anyXmlBuilder()
137 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
138 .withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement()))
140 final DOMSourceAnyxmlNode dataStructure = (DOMSourceAnyxmlNode) adapter.selectFromDataStructure(data, path)
142 final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.getValue().getNode());
143 final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
144 final String expectedData = XmlUtil.toString((Element) source.getNode());
145 Diff diff = new Diff(expectedData, dataFromReply);
146 assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
149 private static NodeIdentifier createNodeId(final String name) {
150 return new NodeIdentifier(QName.create(NAMESPACE, name));
153 private static NodeIdentifierWithPredicates createListNodeId(
154 final String nodeName, final String keyName, final String id) {
155 return NodeIdentifierWithPredicates.of(QName.create(NAMESPACE, nodeName), QName.create(NAMESPACE, keyName), id);
158 private static NodeIdentifierWithPredicates createListNodeId(final String nodeName, final Map<QName, Object> keys) {
159 return NodeIdentifierWithPredicates.of(QName.create(NAMESPACE, nodeName), keys);