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