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.base.Optional;
11 import com.google.common.collect.ImmutableMap;
12 import java.io.IOException;
13 import java.net.URISyntaxException;
14 import java.nio.file.Files;
15 import java.nio.file.Paths;
16 import java.util.Arrays;
17 import java.util.Collection;
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.controller.config.util.xml.XmlElement;
30 import org.opendaylight.controller.config.util.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(YangInstanceIdentifier path, String testDataset, Class<? extends Exception> expectedException) throws IOException, SAXException, URISyntaxException {
60 this.testDataset = testDataset;
61 this.expectedException = expectedException;
62 this.source = new DOMSource(XmlUtil.readXmlToDocument(getClass().getResourceAsStream("/schemaless/data/" + testDataset)).getDocumentElement());
63 this.expectedConfig = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/edit-config/" + testDataset).toURI())));
64 this.expectedFilter = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/filter/" + testDataset).toURI())));
65 this.getConfigData = new String(Files.readAllBytes(Paths.get(getClass().getResource("/schemaless/get-config/" + testDataset).toURI())));
68 @Parameterized.Parameters
69 public static Collection parameters() {
71 {YangInstanceIdentifier.builder()
72 .node(createNodeId("top"))
73 .node(createNodeId("users"))
74 .build(), "container.xml", null},
75 {YangInstanceIdentifier.builder()
76 .node(createNodeId("top"))
77 .node(createNodeId("users"))
78 .node(createListNodeId("user", "key", "k1"))
79 .build(), "keyed-list.xml", null},
80 {YangInstanceIdentifier.builder()
81 .node(createNodeId("top"))
82 .node(createNodeId("users"))
83 .node(createListNodeId("user", ImmutableMap.of(QName.create(NAMESPACE, "key1"), "k1", QName.create(NAMESPACE, "key2"), "k2")))
84 .build(), "keyed-list-compound-key.xml", null},
85 {YangInstanceIdentifier.builder()
86 .node(createNodeId("top"))
87 .node(createNodeId("users"))
88 .node(createListNodeId("user", "key", "k2"))
89 .build(), "keyed-list-bad-key.xml", IllegalStateException.class}
91 return Arrays.asList(params);
95 public static void suiteSetup() {
96 XMLUnit.setIgnoreWhitespace(true);
100 public void testCreateEditConfigStructure() throws Exception {
101 if(expectedException != null) {
102 thrown.expect(expectedException);
104 AnyXmlNode data = Builders.anyXmlBuilder()
105 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
108 final AnyXmlNode anyXmlNode = adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(ModifyAction.REPLACE));
109 final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
110 Diff diff = new Diff(expectedConfig, s);
111 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
115 public void testToFilterStructure() throws Exception {
116 final AnyXmlNode anyXmlNode = (AnyXmlNode) adapter.toFilterStructure(path);
117 final String s = XmlUtil.toString((Element) anyXmlNode.getValue().getNode());
118 Diff diff = new Diff(expectedFilter, s);
119 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
123 public void testSelectFromDataStructure() throws Exception {
124 AnyXmlNode data = Builders.anyXmlBuilder()
125 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
126 .withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement()))
128 final AnyXmlNode dataStructure = (AnyXmlNode) adapter.selectFromDataStructure(data, path).get();
129 final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.getValue().getNode());
130 final String dataFromReply = XmlUtil.toString(s.getOnlyChildElement().getDomElement());
131 final String expectedData = XmlUtil.toString((Element) source.getNode());
132 Diff diff = new Diff(expectedData, dataFromReply);
133 Assert.assertTrue(String.format("Input %s: %s", testDataset, diff.toString()), diff.similar());
136 private static YangInstanceIdentifier.NodeIdentifier createNodeId(String name) {
137 return new YangInstanceIdentifier.NodeIdentifier(QName.create(NAMESPACE, name));
140 private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, String keyName, String id) {
141 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), QName.create(NAMESPACE, keyName), id);
144 private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, Map<QName, Object> keys) {
145 return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), keys);