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.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;
26 import javax.xml.parsers.DocumentBuilderFactory;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.util.Iterator;
33 public class XmlUtilsTest {
35 private static final DocumentBuilderFactory BUILDERFACTORY;
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;
46 private SchemaContext schema;
47 private RpcDefinition testRpc;
49 public static final String XML_CONTENT = "<input xmlns=\"urn:opendaylight:controller:rpc:test\">" +
51 "<flow xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:node/ltha:node1[ltha:id='3@java.lang.Short']</flow>" +
55 public void setUp() throws Exception {
56 final ByteSource byteSource = new ByteSource() {
58 public InputStream openStream() throws IOException {
59 return XmlUtilsTest.this.getClass().getResourceAsStream("rpcTest.yang");
62 schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
63 final Module rpcTestModule = schema.getModules().iterator().next();
64 testRpc = rpcTestModule.getRpcs().iterator().next();
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);
73 Assert.assertEquals("id", firstNode.getNodeType().getLocalName());
74 Assert.assertEquals("flowid", firstNode.getValue());
76 SimpleNode secondNode = input.get(1);
77 Assert.assertEquals("flow", secondNode.getNodeType().getLocalName());
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();
87 Assert.assertEquals(expected, value);