Migrate Collections references
[netconf.git] / plugins / netconf-client-mdsal / src / test / java / org / opendaylight / netconf / client / mdsal / impl / FieldsSchemalessRpcStructureTransformerTest.java
1 /*
2  * Copyright © 2020 FRINX s.r.o. 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.client.mdsal.impl;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.opendaylight.netconf.client.mdsal.impl.NetconfMessageTransformUtil.toId;
12
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15 import java.nio.file.Files;
16 import java.nio.file.Paths;
17 import java.util.List;
18 import java.util.Map;
19 import org.custommonkey.xmlunit.Diff;
20 import org.junit.Test;
21 import org.opendaylight.netconf.api.xml.XmlUtil;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.QNameModule;
24 import org.opendaylight.yangtools.yang.common.Revision;
25 import org.opendaylight.yangtools.yang.common.XMLNamespace;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
29 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
30 import org.w3c.dom.Element;
31 import org.xml.sax.SAXException;
32
33 public class FieldsSchemalessRpcStructureTransformerTest {
34
35     /**
36      * Sample YANG structure.<br>
37      * container c-1 {
38      *     leaf leaf-1 {
39      *         type string;
40      *     }
41      *     list list-1 {
42      *         leaf key-1 {
43      *             type string;
44      *         }
45      *         leaf key-2 {
46      *             type string;
47      *         }
48      *         container c-2 {
49      *             leaf leaf-3 {
50      *                 type string;
51      *             }
52      *             leaf leaf-4 {
53      *                 type string;
54      *             }
55      *         }
56      *     }
57      * }
58      * container c-x {
59      *     leaf l-x {
60      *         type boolean;
61      *     }
62      *     leaf l-y {
63      *         type boolean;
64      *     }
65      * }
66      */
67     private static final QNameModule TEST_MODULE = QNameModule.create(
68             XMLNamespace.of("test-namespace"), Revision.of("2020-10-25"));
69
70     private static final NodeIdentifier C1_NID = toId(QName.create(TEST_MODULE, "c-1"));
71     private static final NodeIdentifier C2_NID = toId(QName.create(TEST_MODULE, "c-2"));
72     private static final NodeIdentifier CX_NID = toId(QName.create(TEST_MODULE, "c-x"));
73
74     private static final NodeIdentifier LEAF1_NID = toId(QName.create(TEST_MODULE, "leaf-1"));
75     private static final NodeIdentifier LEAF2_NID = toId(QName.create(TEST_MODULE, "leaf-2"));
76     private static final NodeIdentifier LEAF3_NID = toId(QName.create(TEST_MODULE, "leaf-3"));
77     private static final NodeIdentifier LX_NID = toId(QName.create(TEST_MODULE, "l-x"));
78
79     private static final QName KEY1_QNAME = QName.create(TEST_MODULE, "key-1");
80     private static final QName KEY2_QNAME = QName.create(TEST_MODULE, "key-2");
81     private static final NodeIdentifier KEY1_NID = toId(KEY1_QNAME);
82
83     private static final QName LIST1_QNAME = QName.create(TEST_MODULE, "list-1");
84     private static final NodeIdentifier LIST1_NID = toId(LIST1_QNAME);
85
86     private final SchemalessRpcStructureTransformer transformer = new SchemalessRpcStructureTransformer();
87
88     @Test
89     public void toFilterStructureWithSingleRootTest() throws SAXException, IOException, URISyntaxException {
90         final YangInstanceIdentifier rootPath = YangInstanceIdentifier.of(C1_NID);
91         final YangInstanceIdentifier leaf1Field = YangInstanceIdentifier.of(LEAF1_NID);
92         final YangInstanceIdentifier leaf3Field = YangInstanceIdentifier.of(NodeIdentifierWithPredicates.of(
93                 LIST1_QNAME, Map.of(KEY1_QNAME, "key1", KEY2_QNAME, "key2")), C2_NID, LEAF3_NID);
94         final YangInstanceIdentifier key1Field = YangInstanceIdentifier.of(NodeIdentifierWithPredicates.of(
95                 LIST1_QNAME, Map.of(KEY1_QNAME, "key1", KEY2_QNAME, "key2")), KEY1_NID);
96         final FieldsFilter filter = FieldsFilter.of(rootPath, List.of(leaf1Field, leaf3Field, key1Field));
97
98         final DOMSourceAnyxmlNode filterStructure = (DOMSourceAnyxmlNode) transformer.toFilterStructure(
99                 List.of(filter));
100         final Diff diff = getDiff("one-root-filter.xml", filterStructure);
101         assertTrue(diff.similar());
102     }
103
104     @Test
105     public void toFilterStructureWithTwoRootContainersTest() throws SAXException, IOException, URISyntaxException {
106         final YangInstanceIdentifier c1RootPath = YangInstanceIdentifier.of(C1_NID);
107         final YangInstanceIdentifier cxRootPath = YangInstanceIdentifier.of(CX_NID);
108         final YangInstanceIdentifier c2Field = YangInstanceIdentifier.of(LIST1_NID, C2_NID);
109         final YangInstanceIdentifier leaf2Field = YangInstanceIdentifier.of(LIST1_NID, C2_NID, LEAF2_NID);
110         final YangInstanceIdentifier lxField = YangInstanceIdentifier.of(LX_NID);
111
112         final FieldsFilter filter1 = FieldsFilter.of(c1RootPath, List.of(c2Field, leaf2Field));
113         final FieldsFilter filter2 = FieldsFilter.of(cxRootPath, List.of(lxField));
114         final DOMSourceAnyxmlNode filterStructure = (DOMSourceAnyxmlNode) transformer.toFilterStructure(
115                 List.of(filter1, filter2));
116         final Diff diff = getDiff("two-roots-filter.xml", filterStructure);
117         assertTrue(diff.similar());
118     }
119
120     private static Diff getDiff(final String filterFileName, final DOMSourceAnyxmlNode filterStructure)
121             throws IOException, SAXException, URISyntaxException {
122         final String body = XmlUtil.toString((Element) filterStructure.body().getNode());
123         final String expectedBody = new String(Files.readAllBytes(Paths.get(FieldsSchemalessRpcStructureTransformerTest
124                 .class.getResource("/schemaless/filter/" + filterFileName).toURI())));
125         return new Diff(expectedBody, body);
126     }
127 }