2 * Copyright (c) 2013 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
8 package org.opendaylight.yangtools.yang.data.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
14 import java.io.FileInputStream;
15 import java.io.InputStream;
16 import java.util.List;
18 import javax.xml.stream.XMLStreamException;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.Node;
24 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
27 * The class <code>XmlTreeBuilderTest</code> contains tests for the class
28 * {@link <code>XmlTreeBuilder</code>}
30 * @author Lukas Sedlak
32 public class XmlTreeBuilderTest {
34 private InputStream inputStream;
37 * Perform pre-test initialization
42 public void setUp() throws Exception {
43 final String inputFile = getClass().getResource("/rpc-getDeviceEquipment.xml").getPath();
44 inputStream = new FileInputStream(inputFile);
48 * Run the Node<?> buildDataTree(InputStream) method test
50 @SuppressWarnings("unchecked")
52 public void testBuildDataTree()
54 Node<?> rootNode = null;
56 rootNode = XmlTreeBuilder.buildDataTree(inputStream);
57 } catch (XMLStreamException e) {
60 assertNotNull(rootNode);
61 assertTrue(rootNode instanceof CompositeNode);
63 CompositeNode compRootNode = (CompositeNode)rootNode;
64 assertNotNull(compRootNode.getChildren());
66 SimpleNode<String> methodName = null;
67 SimpleNode<String> emptyTag = null;
68 CompositeNode params = null;
69 for (final Node<?> childNode : compRootNode.getChildren()) {
70 if (childNode instanceof SimpleNode) {
71 if ("emptyTag".equals(childNode.getNodeType().getLocalName())) {
72 emptyTag = (SimpleNode<String>) childNode;
73 } else if ("methodName".equals(childNode.getNodeType().getLocalName())) {
74 methodName = (SimpleNode<String>) childNode;
77 } else if (childNode instanceof CompositeNode) {
78 params = (CompositeNode) childNode;
82 assertNotNull(methodName);
83 assertNotNull(params);
84 assertTrue(emptyTag.getValue().isEmpty());
85 assertEquals(methodName.getValue(), "getDeviceEquipment");
87 String deviceId = null;
88 String deviceIP = null;
89 for (final Node<?> param : params.getChildren()) {
90 if (param instanceof CompositeNode) {
91 final Node<?> valueNode = ((CompositeNode) param).getChildren().get(0);
93 assertTrue(valueNode instanceof CompositeNode);
94 final CompositeNode value = (CompositeNode) valueNode;
95 final Node<?> stringNode = value.getChildren().get(0);
96 assertTrue(stringNode instanceof SimpleNode);
98 final SimpleNode<String> string = (SimpleNode<String>) stringNode;
99 if ("DeviceID123".equals(string.getValue())) {
100 deviceId = string.getValue();
101 } else if ("172.23.218.75".equals(string.getValue())) {
102 deviceIP = string.getValue();
107 assertNotNull(deviceId);
108 assertNotNull(deviceIP);
112 public void nodeMapInCompositeNodeTest() {
113 Node<?> rootNode = null;
115 rootNode = XmlTreeBuilder.buildDataTree(inputStream);
116 } catch (XMLStreamException e) {
120 CompositeNode compRootNode = (CompositeNode)rootNode;
121 List<CompositeNode> params = compRootNode.getCompositesByName("params");
122 assertEquals(1, params.size());
123 List<CompositeNode> compositesByName = params.get(0).getCompositesByName("param");
124 assertEquals(2, compositesByName.size());