Bulk-add copyright headers to java files
[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.PathArgument;
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", 3, "instance-identifier-module", "cont");
37     }
38
39     @Test
40     public void loadXmlToCnSn() throws WebApplicationException, IOException, URISyntaxException {
41         CompositeNode cnSn = TestUtils.readInputToCnSn("/instanceidentifier/xml/xmldata.xml",
42                 XmlToCompositeNodeProvider.INSTANCE);
43         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
44         verify(cnSn);
45     }
46
47     @Test
48     public void loadJsonToCnSn() throws WebApplicationException, IOException, URISyntaxException {
49         CompositeNode cnSn = TestUtils.readInputToCnSn("/instanceidentifier/json/jsondata.json",
50                 JsonToCompositeNodeProvider.INSTANCE);
51         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
52         verify(cnSn);
53     }
54
55     private void verify(CompositeNode cnSn) throws URISyntaxException {
56         SimpleNode<?> lf111 = getSnWithInstanceIdentifier(cnSn);
57         Object value = lf111.getValue();
58         assertTrue(value instanceof InstanceIdentifier);
59
60         InstanceIdentifier instanceIdentifier = (InstanceIdentifier) value;
61         List<PathArgument> pathArguments = instanceIdentifier.getPath();
62         assertEquals(4, pathArguments.size());
63         String revisionDate = "2014-01-17";
64         assertEquals(TestUtils.buildQName("cont", "instance:identifier:module", revisionDate), pathArguments.get(0)
65                 .getNodeType());
66         assertEquals(TestUtils.buildQName("cont1", "instance:identifier:module", revisionDate), pathArguments.get(1)
67                 .getNodeType());
68         assertEquals(TestUtils.buildQName("lst11", "augment:module", revisionDate), pathArguments.get(2).getNodeType());
69         assertEquals(TestUtils.buildQName("lf112", "augment:augment:module", revisionDate), pathArguments.get(3)
70                 .getNodeType());
71
72         assertTrue(pathArguments.get(2) instanceof NodeIdentifierWithPredicates);
73         Map<QName, Object> predicates = ((NodeIdentifierWithPredicates) pathArguments.get(2)).getKeyValues();
74         assertEquals(2, predicates.size());
75         assertEquals("value1", predicates.get(TestUtils.buildQName("keyvalue111", "augment:module", revisionDate)));
76         assertEquals("value2", predicates.get(TestUtils.buildQName("keyvalue112", "augment:module", revisionDate)));
77     }
78
79     private SimpleNode<?> getSnWithInstanceIdentifier(CompositeNode cnSn) throws URISyntaxException {
80         CompositeNode cont1 = cnSn.getFirstCompositeByName(TestUtils.buildQName("cont1", "instance:identifier:module",
81                 "2014-01-17"));
82         assertNotNull(cont1);
83         CompositeNode lst11 = cont1.getFirstCompositeByName(TestUtils.buildQName("lst11", "augment:module",
84                 "2014-01-17"));
85         assertNotNull(lst11);
86         SimpleNode<?> lf111 = lst11.getFirstSimpleByName(TestUtils.buildQName("lf111", "augment:augment:module",
87                 "2014-01-17"));
88         assertNotNull(lf111);
89         return lf111;
90     }
91
92 }