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