Merge "Bug 8153: Enforce check-style rules for netconf - mdsal-netconf-notification"
[netconf.git] / netconf / sal-netconf-connector / src / test / java / org / opendaylight / netconf / sal / connect / netconf / util / SchemalessRpcStructureTransformerTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.netconf.sal.connect.netconf.util;
9
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;
18 import java.util.Map;
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;
39
40 @RunWith(Parameterized.class)
41 public class SchemalessRpcStructureTransformerTest {
42
43     private static final String NAMESPACE = "http://example.com/schema/1.2/config";
44
45     @Rule
46     public final ExpectedException thrown = ExpectedException.none();
47     private final Class<? extends Exception> expectedException;
48
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;
54
55     private final SchemalessRpcStructureTransformer adapter = new SchemalessRpcStructureTransformer();
56     private final String testDataset;
57
58     public SchemalessRpcStructureTransformerTest(YangInstanceIdentifier path, String testDataset, Class<? extends Exception> expectedException) throws IOException, SAXException, URISyntaxException {
59         this.path = path;
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())));
66     }
67
68     @Parameterized.Parameters
69     public static Collection parameters() {
70         Object[][] params = {
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}
90         };
91         return Arrays.asList(params);
92     }
93
94     @BeforeClass
95     public static void suiteSetup() {
96         XMLUnit.setIgnoreWhitespace(true);
97     }
98
99     @Test
100     public void testCreateEditConfigStructure() throws Exception {
101         if(expectedException != null) {
102             thrown.expect(expectedException);
103         }
104         AnyXmlNode data = Builders.anyXmlBuilder()
105                 .withNodeIdentifier(createNodeId(path.getLastPathArgument().getNodeType().getLocalName()))
106                 .withValue(source)
107                 .build();
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());
112     }
113
114     @Test
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());
120     }
121
122     @Test
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()))
127                 .build();
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());
134     }
135
136     private static YangInstanceIdentifier.NodeIdentifier createNodeId(String name) {
137         return new YangInstanceIdentifier.NodeIdentifier(QName.create(NAMESPACE, name));
138     }
139
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);
142     }
143
144     private static YangInstanceIdentifier.NodeIdentifierWithPredicates createListNodeId(String nodeName, Map<QName, Object> keys) {
145         return new YangInstanceIdentifier.NodeIdentifierWithPredicates(QName.create(NAMESPACE, nodeName), keys);
146     }
147 }