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 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;
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.xml.XmlElement;
30 import org.opendaylight.netconf.api.xml.XmlUtil;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
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;
40 @RunWith(Parameterized.class)
41 public class SchemalessRpcStructureTransformerTest {
43 private static final String NAMESPACE = "http://example.com/schema/1.2/config";
46 public final ExpectedException thrown = ExpectedException.none();
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 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 if (expectedException != null) {
109 thrown.expect(expectedException);
111 AnyXmlNode data = Builders.anyXmlBuilder()
112 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
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());
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());
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()))
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());
144 private static YangInstanceIdentifier.NodeIdentifier createNodeId(final String name) {
145 return new YangInstanceIdentifier.NodeIdentifier(QName.create(NAMESPACE, name));
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);
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);