BUG-650: remove executor abstraction
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / XmlAndJsonToCnSnInstanceIdentifierTest.java
1 /*
2  * Copyright (c) 2014 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.controller.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.List;
17 import java.util.Map;
18 import javax.ws.rs.WebApplicationException;
19 import org.junit.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
22 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
29 import org.opendaylight.yangtools.yang.data.api.Node;
30 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
31
32 public class XmlAndJsonToCnSnInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
33
34     @BeforeClass
35     public static void initialize() {
36         dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont");
37     }
38
39     @Test
40     public void loadXmlToCnSn() throws WebApplicationException, IOException, URISyntaxException {
41         Node<?> node = TestUtils.readInputToCnSn("/instanceidentifier/xml/xmldata.xml",
42                 XmlToCompositeNodeProvider.INSTANCE);
43
44         assertTrue(node instanceof CompositeNode);
45         CompositeNode cnSn = (CompositeNode)node;
46         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
47         verifyListPredicate(cnSn);
48     }
49
50     @Test
51     public void loadXmlLeafListToCnSn() throws WebApplicationException, IOException, URISyntaxException {
52         Node<?> node = TestUtils.readInputToCnSn("/instanceidentifier/xml/xmldata_leaf_list.xml",
53                 XmlToCompositeNodeProvider.INSTANCE);
54
55         assertTrue(node instanceof CompositeNode);
56         CompositeNode cnSn = (CompositeNode)node;
57         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
58         verifyLeafListPredicate(cnSn);
59     }
60
61     @Test
62     public void loadJsonToCnSn() throws WebApplicationException, IOException, URISyntaxException {
63         Node<?> node = TestUtils.readInputToCnSn("/instanceidentifier/json/jsondata.json",
64                 JsonToCompositeNodeProvider.INSTANCE);
65
66         assertTrue(node instanceof CompositeNode);
67         CompositeNode cnSn = (CompositeNode)node;
68         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
69         verifyListPredicate(cnSn);
70     }
71
72     @Test
73     public void loadJsonLeafListToCnSn() throws WebApplicationException, IOException, URISyntaxException {
74         Node<?> node = TestUtils.readInputToCnSn("/instanceidentifier/json/jsondata_leaf_list.json",
75                 JsonToCompositeNodeProvider.INSTANCE);
76         assertTrue(node instanceof CompositeNode);
77         CompositeNode cnSn = (CompositeNode)node;
78
79         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
80         verifyLeafListPredicate(cnSn);
81     }
82
83     private void verifyLeafListPredicate(CompositeNode cnSn) throws URISyntaxException {
84         SimpleNode<?> lf11 = getSnWithInstanceIdentifierWhenLeafList(cnSn);
85         Object value = lf11.getValue();
86         assertTrue(value instanceof YangInstanceIdentifier);
87
88         YangInstanceIdentifier instanceIdentifier = (YangInstanceIdentifier) value;
89         List<PathArgument> pathArguments = instanceIdentifier.getPath();
90         assertEquals(3, pathArguments.size());
91         String revisionDate = "2014-01-17";
92         assertEquals(TestUtils.buildQName("cont", "instance:identifier:module", revisionDate), pathArguments.get(0)
93                 .getNodeType());
94         assertEquals(TestUtils.buildQName("cont1", "instance:identifier:module", revisionDate), pathArguments.get(1)
95                 .getNodeType());
96         assertEquals(TestUtils.buildQName("lflst11", "augment:module:leaf:list", "2014-01-27"), pathArguments.get(2)
97                 .getNodeType());
98
99         assertTrue(pathArguments.get(2) instanceof NodeWithValue);
100         assertEquals("lflst11_1", ((NodeWithValue) pathArguments.get(2)).getValue());
101
102     }
103
104     private void verifyListPredicate(CompositeNode cnSn) throws URISyntaxException {
105         SimpleNode<?> lf111 = getSnWithInstanceIdentifierWhenList(cnSn);
106         Object value = lf111.getValue();
107         assertTrue(value instanceof YangInstanceIdentifier);
108
109         YangInstanceIdentifier instanceIdentifier = (YangInstanceIdentifier) value;
110         List<PathArgument> pathArguments = instanceIdentifier.getPath();
111         assertEquals(4, pathArguments.size());
112         String revisionDate = "2014-01-17";
113         assertEquals(TestUtils.buildQName("cont", "instance:identifier:module", revisionDate), pathArguments.get(0)
114                 .getNodeType());
115         assertEquals(TestUtils.buildQName("cont1", "instance:identifier:module", revisionDate), pathArguments.get(1)
116                 .getNodeType());
117         assertEquals(TestUtils.buildQName("lst11", "augment:module", revisionDate), pathArguments.get(2).getNodeType());
118         assertEquals(TestUtils.buildQName("lf112", "augment:augment:module", revisionDate), pathArguments.get(3)
119                 .getNodeType());
120
121         assertTrue(pathArguments.get(2) instanceof NodeIdentifierWithPredicates);
122         Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArguments.get(2)).getKeyValues();
123         assertEquals(2, predicates.size());
124         assertEquals("value1", predicates.get(TestUtils.buildQName("keyvalue111", "augment:module", revisionDate)));
125         assertEquals("value2", predicates.get(TestUtils.buildQName("keyvalue112", "augment:module", revisionDate)));
126     }
127
128     private SimpleNode<?> getSnWithInstanceIdentifierWhenList(CompositeNode cnSn) throws URISyntaxException {
129         CompositeNode cont1 = cnSn.getFirstCompositeByName(TestUtils.buildQName("cont1", "instance:identifier:module",
130                 "2014-01-17"));
131         assertNotNull(cont1);
132         CompositeNode lst11 = cont1.getFirstCompositeByName(TestUtils.buildQName("lst11", "augment:module",
133                 "2014-01-17"));
134         assertNotNull(lst11);
135         SimpleNode<?> lf111 = lst11.getFirstSimpleByName(TestUtils.buildQName("lf111", "augment:augment:module",
136                 "2014-01-17"));
137         assertNotNull(lf111);
138         return lf111;
139     }
140
141     private SimpleNode<?> getSnWithInstanceIdentifierWhenLeafList(CompositeNode cnSn) throws URISyntaxException {
142         CompositeNode cont1 = cnSn.getFirstCompositeByName(TestUtils.buildQName("cont1", "instance:identifier:module",
143                 "2014-01-17"));
144         assertNotNull(cont1);
145         SimpleNode<?> lf11 = cont1.getFirstSimpleByName(TestUtils.buildQName("lf11", "augment:module:leaf:list",
146                 "2014-01-27"));
147         assertNotNull(lf11);
148         return lf11;
149     }
150
151 }