Merge "BUG-190 Simplify reconnect logic in protocol-framework."
[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.SimpleNode;
30
31 public class XmlAndJsonToCnSnInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
32
33     @BeforeClass
34     public static void initialize() {
35         dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont");
36     }
37
38     @Test
39     public void loadXmlToCnSn() throws WebApplicationException, IOException, URISyntaxException {
40         CompositeNode cnSn = TestUtils.readInputToCnSn("/instanceidentifier/xml/xmldata.xml",
41                 XmlToCompositeNodeProvider.INSTANCE);
42         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
43         verifyListPredicate(cnSn);
44     }
45
46     @Test
47     public void loadXmlLeafListToCnSn() throws WebApplicationException, IOException, URISyntaxException {
48         CompositeNode cnSn = TestUtils.readInputToCnSn("/instanceidentifier/xml/xmldata_leaf_list.xml",
49                 XmlToCompositeNodeProvider.INSTANCE);
50         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
51         verifyLeafListPredicate(cnSn);
52     }
53
54     @Test
55     public void loadJsonToCnSn() throws WebApplicationException, IOException, URISyntaxException {
56         CompositeNode cnSn = TestUtils.readInputToCnSn("/instanceidentifier/json/jsondata.json",
57                 JsonToCompositeNodeProvider.INSTANCE);
58         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
59         verifyListPredicate(cnSn);
60     }
61
62     @Test
63     public void loadJsonLeafListToCnSn() throws WebApplicationException, IOException, URISyntaxException {
64         CompositeNode cnSn = TestUtils.readInputToCnSn("/instanceidentifier/json/jsondata_leaf_list.json",
65                 JsonToCompositeNodeProvider.INSTANCE);
66         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
67         verifyLeafListPredicate(cnSn);
68     }
69
70     private void verifyLeafListPredicate(CompositeNode cnSn) throws URISyntaxException {
71         SimpleNode<?> lf11 = getSnWithInstanceIdentifierWhenLeafList(cnSn);
72         Object value = lf11.getValue();
73         assertTrue(value instanceof YangInstanceIdentifier);
74
75         YangInstanceIdentifier instanceIdentifier = (YangInstanceIdentifier) value;
76         List<PathArgument> pathArguments = instanceIdentifier.getPath();
77         assertEquals(3, pathArguments.size());
78         String revisionDate = "2014-01-17";
79         assertEquals(TestUtils.buildQName("cont", "instance:identifier:module", revisionDate), pathArguments.get(0)
80                 .getNodeType());
81         assertEquals(TestUtils.buildQName("cont1", "instance:identifier:module", revisionDate), pathArguments.get(1)
82                 .getNodeType());
83         assertEquals(TestUtils.buildQName("lflst11", "augment:module:leaf:list", "2014-01-27"), pathArguments.get(2)
84                 .getNodeType());
85
86         assertTrue(pathArguments.get(2) instanceof NodeWithValue);
87         assertEquals("lflst11_1", ((NodeWithValue) pathArguments.get(2)).getValue());
88
89     }
90
91     private void verifyListPredicate(CompositeNode cnSn) throws URISyntaxException {
92         SimpleNode<?> lf111 = getSnWithInstanceIdentifierWhenList(cnSn);
93         Object value = lf111.getValue();
94         assertTrue(value instanceof YangInstanceIdentifier);
95
96         YangInstanceIdentifier instanceIdentifier = (YangInstanceIdentifier) value;
97         List<PathArgument> pathArguments = instanceIdentifier.getPath();
98         assertEquals(4, pathArguments.size());
99         String revisionDate = "2014-01-17";
100         assertEquals(TestUtils.buildQName("cont", "instance:identifier:module", revisionDate), pathArguments.get(0)
101                 .getNodeType());
102         assertEquals(TestUtils.buildQName("cont1", "instance:identifier:module", revisionDate), pathArguments.get(1)
103                 .getNodeType());
104         assertEquals(TestUtils.buildQName("lst11", "augment:module", revisionDate), pathArguments.get(2).getNodeType());
105         assertEquals(TestUtils.buildQName("lf112", "augment:augment:module", revisionDate), pathArguments.get(3)
106                 .getNodeType());
107
108         assertTrue(pathArguments.get(2) instanceof NodeIdentifierWithPredicates);
109         Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArguments.get(2)).getKeyValues();
110         assertEquals(2, predicates.size());
111         assertEquals("value1", predicates.get(TestUtils.buildQName("keyvalue111", "augment:module", revisionDate)));
112         assertEquals("value2", predicates.get(TestUtils.buildQName("keyvalue112", "augment:module", revisionDate)));
113     }
114
115     private SimpleNode<?> getSnWithInstanceIdentifierWhenList(CompositeNode cnSn) throws URISyntaxException {
116         CompositeNode cont1 = cnSn.getFirstCompositeByName(TestUtils.buildQName("cont1", "instance:identifier:module",
117                 "2014-01-17"));
118         assertNotNull(cont1);
119         CompositeNode lst11 = cont1.getFirstCompositeByName(TestUtils.buildQName("lst11", "augment:module",
120                 "2014-01-17"));
121         assertNotNull(lst11);
122         SimpleNode<?> lf111 = lst11.getFirstSimpleByName(TestUtils.buildQName("lf111", "augment:augment:module",
123                 "2014-01-17"));
124         assertNotNull(lf111);
125         return lf111;
126     }
127
128     private SimpleNode<?> getSnWithInstanceIdentifierWhenLeafList(CompositeNode cnSn) throws URISyntaxException {
129         CompositeNode cont1 = cnSn.getFirstCompositeByName(TestUtils.buildQName("cont1", "instance:identifier:module",
130                 "2014-01-17"));
131         assertNotNull(cont1);
132         SimpleNode<?> lf11 = cont1.getFirstSimpleByName(TestUtils.buildQName("lf11", "augment:module:leaf:list",
133                 "2014-01-27"));
134         assertNotNull(lf11);
135         return lf11;
136     }
137
138 }