Merge "Fixed for bug : 1171 - issue while creating subnet"
[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
12 import java.io.IOException;
13 import java.net.URISyntaxException;
14 import javax.ws.rs.WebApplicationException;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
18 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.Node;
21 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
22
23 public class XmlAndJsonToCnSnLeafRefTest extends YangAndXmlAndDataSchemaLoader {
24
25     @BeforeClass
26     public static void initialize() {
27         dataLoad("/leafref/yang", 2, "leafref-module", "cont");
28     }
29
30     @Test
31     public void loadXmlToCnSn() throws WebApplicationException, IOException, URISyntaxException {
32         CompositeNode cnSn = TestUtils.readInputToCnSn("/leafref/xml/xmldata.xml", XmlToCompositeNodeProvider.INSTANCE);
33         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
34         verifyContPredicate(cnSn, "/ns:cont/ns:lf1", "/cont/lf1", "/ns:cont/ns:lf1", "../lf1");
35     }
36
37     @Test
38     public void loadJsonToCnSn() throws WebApplicationException, IOException, URISyntaxException {
39         CompositeNode cnSn = TestUtils.readInputToCnSn("/leafref/json/jsondata.json",
40                 JsonToCompositeNodeProvider.INSTANCE);
41         TestUtils.normalizeCompositeNode(cnSn, modules, schemaNodePath);
42         verifyContPredicate(cnSn, "/leafref-module:cont/leafref-module:lf1", "/leafref-module:cont/leafref-module:lf1",
43                 "/referenced-module:cont/referenced-module:lf1", "/leafref-module:cont/leafref-module:lf1");
44     }
45
46     private void verifyContPredicate(CompositeNode cnSn, String... values) throws URISyntaxException {
47         Object lf2Value = null;
48         Object lf3Value = null;
49         Object lf4Value = null;
50         Object lf5Value = null;
51
52         for (Node<?> node : cnSn.getValue()) {
53             if (node.getNodeType().getLocalName().equals("lf2")) {
54                 lf2Value = ((SimpleNode<?>) node).getValue();
55             } else if (node.getNodeType().getLocalName().equals("lf3")) {
56                 lf3Value = ((SimpleNode<?>) node).getValue();
57             } else if (node.getNodeType().getLocalName().equals("lf4")) {
58                 lf4Value = ((SimpleNode<?>) node).getValue();
59             } else if (node.getNodeType().getLocalName().equals("lf5")) {
60                 lf5Value = ((SimpleNode<?>) node).getValue();
61             }
62         }
63         assertEquals(values[0], lf2Value);
64         assertEquals(values[1], lf3Value);
65         assertEquals(values[2], lf4Value);
66         assertEquals(values[3], lf5Value);
67     }
68
69 }