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
9 package org.opendaylight.controller.remote.rpc.utils;
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.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
20 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
21 import org.opendaylight.yangtools.yang.data.api.Node;
22 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
30 import javax.xml.parsers.DocumentBuilderFactory;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
39 public class XmlUtilsTest {
41 private static final DocumentBuilderFactory BUILDERFACTORY;
44 final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
45 factory.setNamespaceAware(true);
46 factory.setCoalescing(true);
47 factory.setIgnoringElementContentWhitespace(true);
48 factory.setIgnoringComments(true);
49 BUILDERFACTORY = factory;
52 private SchemaContext schema;
53 private RpcDefinition testRpc;
55 public static final String XML_CONTENT = "<add-flow xmlns=\"urn:opendaylight:controller:rpc:test\"><input xmlns=\"urn:opendaylight:controller:rpc:test\">" +
57 "<flow xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:node/ltha:node1[ltha:id='3@java.lang.Short']</flow>" +
58 "</input></add-flow>";
61 public void setUp() throws Exception {
62 final ByteSource byteSource = new ByteSource() {
64 public InputStream openStream() throws IOException {
65 return XmlUtilsTest.this.getClass().getResourceAsStream("rpcTest.yang");
68 schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
69 final Module rpcTestModule = schema.getModules().iterator().next();
70 testRpc = rpcTestModule.getRpcs().iterator().next();
74 public void testNullInputXmlToComposite() {
75 CompositeNode node = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), null, schema);
76 Assert.assertNull(node);
80 public void testNullRpcXmlToComposite() {
81 CompositeNode node = XmlUtils.inputXmlToCompositeNode(null, XML_CONTENT, schema);
82 Assert.assertNull(node);
86 public void testInputXmlToCompositeNode() {
87 CompositeNode node = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), XML_CONTENT, schema);
88 ImmutableList<SimpleNode> input = (ImmutableList)node.getValue().get(0).getValue();
89 SimpleNode firstNode = input.get(0);
91 Assert.assertEquals("id", firstNode.getNodeType().getLocalName());
92 Assert.assertEquals("flowid", firstNode.getValue());
94 SimpleNode secondNode = input.get(1);
95 Assert.assertEquals("flow", secondNode.getNodeType().getLocalName());
97 YangInstanceIdentifier instance = (YangInstanceIdentifier) secondNode.getValue();
98 Iterable<YangInstanceIdentifier.PathArgument> iterable = instance.getPathArguments();
99 Iterator it = iterable.iterator();
100 YangInstanceIdentifier.NodeIdentifier firstPath = (YangInstanceIdentifier.NodeIdentifier) it.next();
101 Assert.assertEquals("node", firstPath.getNodeType().getLocalName());
102 YangInstanceIdentifier.NodeIdentifierWithPredicates secondPath = (YangInstanceIdentifier.NodeIdentifierWithPredicates)it.next();
103 Short value = (Short)secondPath.getKeyValues().values().iterator().next();
105 Assert.assertEquals(expected, value);
109 public void testInputCompositeNodeToXML() {
110 CompositeNode input = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), XML_CONTENT, schema);
111 List<Node<?>> childNodes = new ArrayList();
112 childNodes.add(input);
113 QName rpcQName = schema.getOperations().iterator().next().getQName();
114 CompositeNode node = new ImmutableCompositeNode(rpcQName, input.getValue(), ModifyAction.REPLACE);
115 String xml = XmlUtils.inputCompositeNodeToXml(node, schema);
116 Assert.assertNotNull(xml);
117 Assert.assertTrue(xml.contains("3@java.lang.Short"));
121 public void testNullCompositeNodeToXml(){
122 String xml = XmlUtils.inputCompositeNodeToXml(null, schema);
123 Assert.assertTrue(xml.isEmpty());
127 public void testNullSchemaCompositeNodeToXml(){
128 String xml = XmlUtils.inputCompositeNodeToXml(new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE), null);
129 Assert.assertTrue(xml.isEmpty());