2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.util.List;
19 import javax.ws.rs.WebApplicationException;
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;
33 public class XmlAndJsonToCnSnInstanceIdentifierTest extends YangAndXmlAndDataSchemaLoader {
36 public static void initialize() {
37 dataLoad("/instanceidentifier/yang", 4, "instance-identifier-module", "cont");
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);
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);
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);
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);
72 private void verifyLeafListPredicate(CompositeNode cnSn) throws URISyntaxException {
73 SimpleNode<?> lf11 = getSnWithInstanceIdentifierWhenLeafList(cnSn);
74 Object value = lf11.getValue();
75 assertTrue(value instanceof InstanceIdentifier);
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)
83 assertEquals(TestUtils.buildQName("cont1", "instance:identifier:module", revisionDate), pathArguments.get(1)
85 assertEquals(TestUtils.buildQName("lflst11", "augment:module:leaf:list", "2014-01-27"), pathArguments.get(2)
88 assertTrue(pathArguments.get(2) instanceof NodeWithValue);
89 assertEquals("lflst11_1", ((NodeWithValue) pathArguments.get(2)).getValue());
93 private void verifyListPredicate(CompositeNode cnSn) throws URISyntaxException {
94 SimpleNode<?> lf111 = getSnWithInstanceIdentifierWhenList(cnSn);
95 Object value = lf111.getValue();
96 assertTrue(value instanceof InstanceIdentifier);
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)
104 assertEquals(TestUtils.buildQName("cont1", "instance:identifier:module", revisionDate), pathArguments.get(1)
106 assertEquals(TestUtils.buildQName("lst11", "augment:module", revisionDate), pathArguments.get(2).getNodeType());
107 assertEquals(TestUtils.buildQName("lf112", "augment:augment:module", revisionDate), pathArguments.get(3)
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)));
117 private SimpleNode<?> getSnWithInstanceIdentifierWhenList(CompositeNode cnSn) throws URISyntaxException {
118 CompositeNode cont1 = cnSn.getFirstCompositeByName(TestUtils.buildQName("cont1", "instance:identifier:module",
120 assertNotNull(cont1);
121 CompositeNode lst11 = cont1.getFirstCompositeByName(TestUtils.buildQName("lst11", "augment:module",
123 assertNotNull(lst11);
124 SimpleNode<?> lf111 = lst11.getFirstSimpleByName(TestUtils.buildQName("lf111", "augment:augment:module",
126 assertNotNull(lf111);
130 private SimpleNode<?> getSnWithInstanceIdentifierWhenLeafList(CompositeNode cnSn) throws URISyntaxException {
131 CompositeNode cont1 = cnSn.getFirstCompositeByName(TestUtils.buildQName("cont1", "instance:identifier:module",
133 assertNotNull(cont1);
134 SimpleNode<?> lf11 = cont1.getFirstSimpleByName(TestUtils.buildQName("lf11", "augment:module:leaf:list",