1 package org.opendaylight.netconf.sal.connect.netconf.util;
3 import com.google.common.base.Optional;
4 import com.google.common.collect.ImmutableMap;
5 import java.io.IOException;
6 import java.net.URISyntaxException;
7 import java.nio.file.Files;
8 import java.nio.file.Paths;
9 import java.util.Arrays;
10 import java.util.Collection;
12 import javax.xml.transform.dom.DOMSource;
13 import org.custommonkey.xmlunit.Diff;
14 import org.custommonkey.xmlunit.XMLUnit;
15 import org.junit.Assert;
16 import org.junit.BeforeClass;
17 import org.junit.Rule;
18 import org.junit.Test;
19 import org.junit.rules.ExpectedException;
20 import org.junit.runner.RunWith;
21 import org.junit.runners.Parameterized;
22 import org.opendaylight.controller.config.util.xml.XmlElement;
23 import org.opendaylight.controller.config.util.xml.XmlUtil;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.w3c.dom.Document;
30 import org.w3c.dom.Element;
31 import org.xml.sax.SAXException;
33 @RunWith(Parameterized.class)
34 public class SchemalessRpcStructureTransformerTest {
36 private static final String NAMESPACE = "http://example.com/schema/1.2/config";
39 public final ExpectedException thrown = ExpectedException.none();
40 private final Class<? extends Exception> expectedException;
42 private final String expectedConfig;
43 private final String expectedFilter;
44 private final String getConfigData;
45 private final YangInstanceIdentifier path;
46 private final DOMSource source;
48 private final SchemalessRpcStructureTransformer adapter = new SchemalessRpcStructureTransformer();
49 private final String testDataset;
51 public SchemalessRpcStructureTransformerTest(YangInstanceIdentifier path, String testDataset, Class<? extends Exception> expectedException) throws IOException, SAXException, URISyntaxException {
53 this.testDataset = testDataset;
54 this.expectedException = expectedException;
55 this.source = new DOMSource(XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/schemaless/data/" + testDataset)).getDocumentElement());
56 this.expectedConfig = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/edit-config/" + testDataset).toURI())));
57 this.expectedFilter = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/filter/" + testDataset).toURI())));
58 this.getConfigData = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/get-config/" + testDataset).toURI())));
61 @Parameterized.Parameters
62 public static Collection parameters() {
64 {YangInstanceIdentifier.builder()
65 .node(createNodeId("top"))
66 .node(createNodeId("users"))
67 .build(), "container.xml", null},
68 {YangInstanceIdentifier.builder()
69 .node(createNodeId("top"))
70 .node(createNodeId("users"))
71 .node(createListNodeId("user", "key", "k1"))
72 .build(), "keyed-list.xml", null},
73 {YangInstanceIdentifier.builder()
74 .node(createNodeId("top"))
75 .node(createNodeId("users"))
76 .node(createListNodeId("user", ImmutableMap.of(QName.create(NAMESPACE, "key1"), "k1", QName.create(NAMESPACE, "key2"), "k2")))
77 .build(), "keyed-list-compound-key.xml", null},
78 {YangInstanceIdentifier.builder()
79 .node(createNodeId("top"))
80 .node(createNodeId("users"))
81 .node(createListNodeId("user", "key", "k2"))
82 .build(), "keyed-list-bad-key.xml", IllegalStateException.class}
84 return Arrays.asList(params);
88 public static void suiteSetup() {
89 XMLUnit.setIgnoreWhitespace(true);
93 public void testCreateEditConfigStructure() throws Exception {
94 if(expectedException != null) {
95 thrown.expect(expectedException);
97 AnyXmlNode data = Builders.anyXmlBuilder()
98 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
101 final AnyXmlNode anyXmlNode = adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE));
102 final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
103 Diff diff = new Diff(expectedConfig, s);
104 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
108 public void testToFilterStructure() throws Exception {
109 final AnyXmlNode anyXmlNode = (AnyXmlNode) adapter.toFilterStructure(path);
110 final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
111 Diff diff = new Diff(expectedFilter, s);
112 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
116 public void testSelectFromDataStructure() throws Exception {
117 AnyXmlNode data = Builders.anyXmlBuilder()
118 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
119 .withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement()))
121 final AnyXmlNode dataStructure = (AnyXmlNode) adapter.selectFromDataStructure(data, path).get();
122 final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.getValue().getNode());
123 final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
124 final String expectedData = XmlUtil.toString((Element) source.getNode());
125 Diff diff = new Diff(expectedData, dataFromReply);
126 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
129 private static YangInstanceIdentifier.NodeIdentifier createNodeId(String name) {
130 return new YangInstanceIdentifier.NodeIdentifier(QName.create(NAMESPACE, name));
133 private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, String keyName, String id) {
134 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), QName.create(NAMESPACE, keyName), id);
137 private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, Map<QName, Object> keys) {
138 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), keys);