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.client.mdsal.impl;
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertThrows;
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.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.junit.runners.Parameterized;
26 import org.opendaylight.netconf.api.EffectiveOperation;
27 import org.opendaylight.netconf.api.xml.XmlElement;
28 import org.opendaylight.netconf.api.xml.XmlUtil;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
33 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
34 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
35 import org.w3c.dom.Document;
36 import org.xml.sax.SAXException;
37 import org.xmlunit.builder.DiffBuilder;
39 @RunWith(Parameterized.class)
40 public class SchemalessRpcStructureTransformerTest {
41 private static final String NAMESPACE = "http://example.com/schema/1.2/config";
43 @Parameterized.Parameters
44 public static Collection<Object[]> parameters() {
45 return Arrays.asList(new Object[][] {
46 { YangInstanceIdentifier.builder()
47 .node(createNodeId("top"))
48 .node(createNodeId("users"))
49 .build(), "container.xml", null
51 { YangInstanceIdentifier.builder()
52 .node(createNodeId("top"))
53 .node(createNodeId("users"))
54 .node(createListNodeId("user", "key", "k1"))
55 .build(), "keyed-list.xml", null
57 { YangInstanceIdentifier.builder()
58 .node(createNodeId("top"))
59 .node(createNodeId("users"))
60 .node(createListNodeId("user",
61 ImmutableMap.of(QName.create(NAMESPACE, "key1"), "k1", QName.create(NAMESPACE, "key2"), "k2")))
62 .build(), "keyed-list-compound-key.xml", null
64 { YangInstanceIdentifier.builder()
65 .node(createNodeId("top"))
66 .node(createNodeId("users"))
67 .node(createListNodeId("user", "key", "k2"))
68 .build(), "keyed-list-bad-key.xml", IllegalStateException.class
72 private final Class<? extends Exception> expectedException;
74 private final String expectedConfig;
75 private final String expectedFilter;
76 private final String getConfigData;
77 private final YangInstanceIdentifier path;
78 private final DOMSource source;
80 private final SchemalessRpcStructureTransformer adapter = new SchemalessRpcStructureTransformer();
82 public SchemalessRpcStructureTransformerTest(final YangInstanceIdentifier path, final String testDataset,
83 final Class<? extends Exception> expectedException) throws IOException, SAXException, URISyntaxException {
85 this.expectedException = expectedException;
86 source = new DOMSource(XmlUtil.readXmlToDocument(getClass()
87 .getResourceAsStream("/schemaless/data/" + testDataset)).getDocumentElement());
88 expectedConfig = Files.readString(
89 Paths.get(getClass().getResource("/schemaless/edit-config/" + testDataset).toURI()));
90 expectedFilter = Files.readString(
91 Paths.get(getClass().getResource("/schemaless/filter/" + testDataset).toURI()));
92 getConfigData = Files.readString(
93 Paths.get(getClass().getResource("/schemaless/get-config/" + testDataset).toURI()));
97 public void testCreateEditConfigStructure() throws Exception {
98 final var data = Builders.anyXmlBuilder()
99 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
103 if (expectedException != null) {
104 assertThrows(expectedException, () -> adapter.createEditConfigStructure(Optional.of(data), path,
105 Optional.of(EffectiveOperation.REPLACE)));
109 final var anyXmlNode =
110 adapter.createEditConfigStructure(Optional.of(data), path, Optional.of(EffectiveOperation.REPLACE));
111 final var diff = DiffBuilder.compare(expectedConfig)
112 .withTest(anyXmlNode.body().getNode())
116 assertFalse(diff.toString(), diff.hasDifferences());
120 public void testToFilterStructure() throws Exception {
121 final var anyXmlNode = (DOMSourceAnyxmlNode) adapter.toFilterStructure(path);
122 final var diff = DiffBuilder.compare(expectedFilter)
123 .withTest(anyXmlNode.body().getNode())
127 assertFalse(diff.toString(), diff.hasDifferences());
131 public void testSelectFromDataStructure() throws Exception {
132 final var data = Builders.anyXmlBuilder()
133 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
134 .withValue(new DOMSource(XmlUtil.readXmlToDocument(getConfigData).getDocumentElement()))
136 final var dataStructure = (DOMSourceAnyxmlNode) adapter.selectFromDataStructure(data, path).orElseThrow();
137 final XmlElement s = XmlElement.fromDomDocument((Document) dataStructure.body().getNode());
138 final var diff = DiffBuilder.compare(source.getNode())
139 .withTest(s.getOnlyChildElement().getDomElement())
143 assertFalse(diff.toString(), diff.hasDifferences());
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);