Updating cluster RPC code making it work
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / utils / XmlUtilsTest.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
9 package org.opendaylight.controller.remote.rpc.utils;
10
11
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.Lists;
14 import com.google.common.io.ByteSource;
15 import junit.framework.Assert;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
19 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
25
26 import javax.xml.parsers.DocumentBuilderFactory;
27
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Iterator;
31
32
33 public class XmlUtilsTest {
34
35   private static final DocumentBuilderFactory BUILDERFACTORY;
36
37   static {
38     final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
39     factory.setNamespaceAware(true);
40     factory.setCoalescing(true);
41     factory.setIgnoringElementContentWhitespace(true);
42     factory.setIgnoringComments(true);
43     BUILDERFACTORY = factory;
44   }
45
46   private SchemaContext schema;
47   private RpcDefinition testRpc;
48
49   public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">" +
50       "<id>flowid</id>" +
51       "<flow xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:node/ltha:node1[ltha:id='3@java.lang.Short']</flow>" +
52       "</input>";
53
54   @Before
55   public void setUp() throws Exception {
56     final ByteSource byteSource = new ByteSource() {
57       @Override
58       public InputStream openStream() throws IOException {
59         return XmlUtilsTest.this.getClass().getResourceAsStream("rpcTest.yang");
60       }
61     };
62     schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
63     final Module rpcTestModule = schema.getModules().iterator().next();
64     testRpc = rpcTestModule.getRpcs().iterator().next();
65   }
66
67   @Test
68   public void testInputXmlToCompositeNode() {
69     CompositeNode node = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), XML_CONTENT, schema);
70     ImmutableList<SimpleNode> input = (ImmutableList)node.getValue().get(0).getValue();
71     SimpleNode firstNode = input.get(0);
72
73     Assert.assertEquals("id", firstNode.getNodeType().getLocalName());
74     Assert.assertEquals("flowid", firstNode.getValue());
75
76     SimpleNode secondNode = input.get(1);
77     Assert.assertEquals("flow", secondNode.getNodeType().getLocalName());
78
79     YangInstanceIdentifier instance = (YangInstanceIdentifier) secondNode.getValue();
80     Iterable<YangInstanceIdentifier.PathArgument> iterable = instance.getPathArguments();
81     Iterator it = iterable.iterator();
82     YangInstanceIdentifier.NodeIdentifier firstPath = (YangInstanceIdentifier.NodeIdentifier) it.next();
83     Assert.assertEquals("node", firstPath.getNodeType().getLocalName());
84     YangInstanceIdentifier.NodeIdentifierWithPredicates secondPath = (YangInstanceIdentifier.NodeIdentifierWithPredicates)it.next();
85     Short value = (Short)secondPath.getKeyValues().values().iterator().next();
86     Short expected = 3;
87     Assert.assertEquals(expected, value);
88   }
89
90
91 }