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.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.AnyXmlNode;
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";
48 public final ExpectedException thrown = ExpectedException.none();
49 private final Class<? extends Exception> expectedException;
51 private final String expectedConfig;
52 private final String expectedFilter;
53 private final String getConfigData;
54 private final YangInstanceIdentifier path;
55 private final DOMSource source;
57 private final SchemalessRpcStructureTransformer adapter = new SchemalessRpcStructureTransformer();
58 private final String testDataset;
60 public SchemalessRpcStructureTransformerTest(
61 final YangInstanceIdentifier path, final String testDataset,
62 final Class<? extends Exception> expectedException) throws IOException, SAXException, URISyntaxException {
64 this.testDataset = testDataset;
65 this.expectedException = expectedException;
66 this.source = new DOMSource(XmlUtil.readXmlToDocument(getClass()
67 .getResourceAsStream("/schemaless/data/" + testDataset)).getDocumentElement());
68 this.expectedConfig = new String(Files.readAllBytes(
69 Paths.get(getClass().getResource("/schemaless/edit-config/" + testDataset).toURI())));
70 this.expectedFilter = new String(Files.readAllBytes(
71 Paths.get(getClass().getResource("/schemaless/filter/" + testDataset).toURI())));
72 this.getConfigData = new String(Files.readAllBytes(
73 Paths.get(getClass().getResource("/schemaless/get-config/" + testDataset).toURI())));
76 @Parameterized.Parameters
77 public static Collection parameters() {
79 {YangInstanceIdentifier.builder()
80 .node(createNodeId("top"))
81 .node(createNodeId("users"))
82 .build(), "container.xml", null},
83 {YangInstanceIdentifier.builder()
84 .node(createNodeId("top"))
85 .node(createNodeId("users"))
86 .node(createListNodeId("user", "key", "k1"))
87 .build(), "keyed-list.xml", null},
88 {YangInstanceIdentifier.builder()
89 .node(createNodeId("top"))
90 .node(createNodeId("users"))
91 .node(createListNodeId("user", ImmutableMap.of(QName.create(NAMESPACE, "key1"), "k1",
92 QName.create(NAMESPACE, "key2"), "k2")))
93 .build(), "keyed-list-compound-key.xml", null},
94 {YangInstanceIdentifier.builder()
95 .node(createNodeId("top"))
96 .node(createNodeId("users"))
97 .node(createListNodeId("user", "key", "k2"))
98 .build(), "keyed-list-bad-key.xml", IllegalStateException.class}
100 return Arrays.asList(params);
104 public static void suiteSetup() {
105 XMLUnit.setIgnoreWhitespace(true);
109 public void testCreateEditConfigStructure() throws Exception {
110 if (expectedException != null) {
111 thrown.expect(expectedException);
113 AnyXmlNode data = Builders.anyXmlBuilder()
114 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
117 final AnyXmlNode anyXmlNode =
118 adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE));
119 final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
120 Diff diff = new Diff(expectedConfig, s);
121 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
125 public void testToFilterStructure() throws Exception {
126 final AnyXmlNode anyXmlNode = (AnyXmlNode) adapter.toFilterStructure(path);
127 final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
128 Diff diff = new Diff(expectedFilter, s);
129 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
133 public void testSelectFromDataStructure() throws Exception {
134 AnyXmlNode data = Builders.anyXmlBuilder()
135 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
136 .withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement()))
138 final AnyXmlNode dataStructure = (AnyXmlNode) adapter.selectFromDataStructure(data, path).get();
139 final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.getValue().getNode());
140 final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
141 final String expectedData = XmlUtil.toString((Element) source.getNode());
142 Diff diff = new Diff(expectedData, dataFromReply);
143 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
146 private static NodeIdentifier createNodeId(final String name) {
147 return new NodeIdentifier(QName.create(NAMESPACE, name));
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);
155 private static NodeIdentifierWithPredicates createListNodeId(final String nodeName, final Map<QName, Object> keys) {
156 return NodeIdentifierWithPredicates.of(QName.create(NAMESPACE, nodeName), keys);