Instance identifier support
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / XmlAndJsonToCnSnInstanceIdentifierTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.IOException;
8 import java.net.URISyntaxException;
9 import java.util.List;
10 import java.util.Map;
11
12 import javax.ws.rs.WebApplicationException;
13
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
17 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
23 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
24
25 public class XmlAndJsonToCnSnInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
26
27     @BeforeClass
28     public static void initialize() {
29         dataLoad("/instanceidentifier/yang", 3, "instance-identifier-module", "cont");
30     }
31
32     @Test
33     public void loadXmlToCnSn() throws WebApplicationException, IOException, URISyntaxException {
34         CompositeNode cnSn = TestUtils.readInputToCnSn("/instanceidentifier/xml/xmldata.xml",
35                 XmlToCompositeNodeProvider.INSTANCE);
36         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
37         verify(cnSn);
38     }
39
40     @Test
41     public void loadJsonToCnSn() throws WebApplicationException, IOException, URISyntaxException {
42         CompositeNode cnSn = TestUtils.readInputToCnSn("/instanceidentifier/json/jsondata.json",
43                 JsonToCompositeNodeProvider.INSTANCE);
44         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
45         verify(cnSn);
46     }
47
48     private void verify(CompositeNode cnSn) throws URISyntaxException {
49         SimpleNode<?> lf111 = getSnWithInstanceIdentifier(cnSn);
50         Object value = lf111.getValue();
51         assertTrue(value instanceof InstanceIdentifier);
52
53         InstanceIdentifier instanceIdentifier = (InstanceIdentifier) value;
54         List<PathArgument> pathArguments = instanceIdentifier.getPath();
55         assertEquals(4, pathArguments.size());
56         String revisionDate = "2014-01-17";
57         assertEquals(TestUtils.buildQName("cont", "instance:identifier:module", revisionDate), pathArguments.get(0)
58                 .getNodeType());
59         assertEquals(TestUtils.buildQName("cont1", "instance:identifier:module", revisionDate), pathArguments.get(1)
60                 .getNodeType());
61         assertEquals(TestUtils.buildQName("lst11", "augment:module", revisionDate), pathArguments.get(2).getNodeType());
62         assertEquals(TestUtils.buildQName("lf112", "augment:augment:module", revisionDate), pathArguments.get(3)
63                 .getNodeType());
64
65         assertTrue(pathArguments.get(2) instanceof NodeIdentifierWithPredicates);
66         Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArguments.get(2)).getKeyValues();
67         assertEquals(2, predicates.size());
68         assertEquals("value1", predicates.get(TestUtils.buildQName("keyvalue111", "augment:module", revisionDate)));
69         assertEquals("value2", predicates.get(TestUtils.buildQName("keyvalue112", "augment:module", revisionDate)));
70     }
71
72     private SimpleNode<?> getSnWithInstanceIdentifier(CompositeNode cnSn) throws URISyntaxException {
73         CompositeNode cont1 = cnSn.getFirstCompositeByName(TestUtils.buildQName("cont1", "instance:identifier:module",
74                 "2014-01-17"));
75         assertNotNull(cont1);
76         CompositeNode lst11 = cont1.getFirstCompositeByName(TestUtils.buildQName("lst11", "augment:module",
77                 "2014-01-17"));
78         assertNotNull(lst11);
79         SimpleNode<?> lf111 = lst11.getFirstSimpleByName(TestUtils.buildQName("lf111", "augment:augment:module",
80                 "2014-01-17"));
81         assertNotNull(lf111);
82         return lf111;
83     }
84
85 }