Merge "Bug-2277 : Isolated Leader Implementation"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / XmlAndJsonToCnSnLeafRefTest.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.assertTrue;
12
13 import java.io.IOException;
14 import java.net.URISyntaxException;
15
16 import javax.ws.rs.WebApplicationException;
17
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
21 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
24 import org.opendaylight.yangtools.yang.data.api.Node;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26
27 public class XmlAndJsonToCnSnLeafRefTest extends YangAndXmlAndDataSchemaLoader {
28
29     final QName refContQName = QName.create("referenced:module", "2014-04-17", "cont");
30     final QName refLf1QName = QName.create(refContQName, "lf1");
31     final QName contQName = QName.create("leafref:module", "2014-04-17", "cont");
32     final QName lf1QName = QName.create(contQName, "lf1");
33     final QName lf2QName = QName.create(contQName, "lf2");
34     final QName lf3QName = QName.create(contQName, "lf3");
35
36     @BeforeClass
37     public static void initialize() {
38         dataLoad("/leafref/yang", 2, "leafref-module", "cont");
39     }
40
41     @Test
42     public void loadXmlToCnSn() throws WebApplicationException, IOException, URISyntaxException {
43         Node<?> node = TestUtils.readInputToCnSn("/leafref/xml/xmldata.xml", XmlToCompositeNodeProvider.INSTANCE);
44
45         assertTrue(node instanceof CompositeNode);
46         CompositeNode cnSn = (CompositeNode)node;
47
48         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
49
50         verifyContPredicate(cnSn, "lf4", YangInstanceIdentifier.builder().node(refContQName).node(refLf1QName).build());
51         verifyContPredicate(cnSn, "lf2", YangInstanceIdentifier.builder().node(contQName).node(lf1QName).build());
52         verifyContPredicate(cnSn, "lf3", YangInstanceIdentifier.builder().node(contQName).node(lf2QName).build());
53         verifyContPredicate(cnSn, "lf5", YangInstanceIdentifier.builder().node(contQName).node(lf3QName).build());
54     }
55
56     @Test
57     public void loadJsonToCnSn() throws WebApplicationException, IOException, URISyntaxException {
58         Node<?> node = TestUtils.readInputToCnSn("/leafref/json/jsondata.json",
59                 JsonToCompositeNodeProvider.INSTANCE);
60         assertTrue(node instanceof CompositeNode);
61         CompositeNode cnSn = (CompositeNode)node;
62
63         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
64
65         verifyContPredicate(cnSn, "lf4", YangInstanceIdentifier.builder().node(refContQName).node(refLf1QName).build());
66         verifyContPredicate(cnSn, "lf2", YangInstanceIdentifier.builder().node(contQName).node(lf1QName).build());
67         verifyContPredicate(cnSn, "lf3", YangInstanceIdentifier.builder().node(contQName).node(lf2QName).build());
68         verifyContPredicate(cnSn, "lf5", YangInstanceIdentifier.builder().node(contQName).node(lf3QName).build());
69     }
70
71     private void verifyContPredicate(CompositeNode cnSn, String leafName, Object value) throws URISyntaxException {
72         Object parsed = null;
73
74         for (final Node<?> node : cnSn.getValue()) {
75             if (node.getNodeType().getLocalName().equals(leafName)) {
76                 parsed = node.getValue();
77             }
78         }
79
80         assertEquals(value, parsed);
81     }
82
83 }