/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.data.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.net.URI; import java.util.List; import javax.xml.stream.XMLStreamException; import org.junit.Before; import org.junit.Test; import org.opendaylight.yangtools.yang.data.api.CompositeNode; import org.opendaylight.yangtools.yang.data.api.Node; import org.opendaylight.yangtools.yang.data.api.SimpleNode; /** * The class XmlTreeBuilderTest contains tests for the class * {@link XmlTreeBuilder} * * @author Lukas Sedlak */ public class XmlTreeBuilderTest { private InputStream inputStream; /** * Perform pre-test initialization * * @throws Exception */ @Before public void setUp() throws Exception { final URI inputFile = getClass().getResource("/rpc-getDeviceEquipment.xml").toURI(); inputStream = new FileInputStream(new File(inputFile)); } /** * Run the Node buildDataTree(InputStream) method test */ @SuppressWarnings("unchecked") @Test public void testBuildDataTree() { Node rootNode = null; try { rootNode = XmlTreeBuilder.buildDataTree(inputStream); } catch (XMLStreamException e) { e.printStackTrace(); } assertNotNull(rootNode); assertTrue(rootNode instanceof CompositeNode); CompositeNode compRootNode = (CompositeNode)rootNode; assertNotNull(compRootNode.getValue()); SimpleNode methodName = null; SimpleNode emptyTag = null; CompositeNode params = null; for (final Node childNode : compRootNode.getValue()) { if (childNode instanceof SimpleNode) { if ("emptyTag".equals(childNode.getNodeType().getLocalName())) { emptyTag = (SimpleNode) childNode; } else if ("methodName".equals(childNode.getNodeType().getLocalName())) { methodName = (SimpleNode) childNode; } } else if (childNode instanceof CompositeNode) { params = (CompositeNode) childNode; } } assertNotNull(methodName); assertNotNull(params); assertTrue(emptyTag.getValue().isEmpty()); assertEquals(methodName.getValue(), "getDeviceEquipment"); String deviceId = null; String deviceIP = null; for (final Node param : params.getValue()) { if (param instanceof CompositeNode) { final Node valueNode = ((CompositeNode) param).getValue().get(0); assertTrue(valueNode instanceof CompositeNode); final CompositeNode value = (CompositeNode) valueNode; final Node stringNode = value.getValue().get(0); assertTrue(stringNode instanceof SimpleNode); final SimpleNode string = (SimpleNode) stringNode; if ("DeviceID123".equals(string.getValue())) { deviceId = string.getValue(); } else if ("172.23.218.75".equals(string.getValue())) { deviceIP = string.getValue(); } } } assertNotNull(deviceId); assertNotNull(deviceIP); } @Test public void nodeMapInCompositeNodeTest() { Node rootNode = null; try { rootNode = XmlTreeBuilder.buildDataTree(inputStream); } catch (XMLStreamException e) { e.printStackTrace(); } CompositeNode compRootNode = (CompositeNode)rootNode; List params = compRootNode.getCompositesByName("params"); assertEquals(1, params.size()); List compositesByName = params.get(0).getCompositesByName("param"); assertEquals(2, compositesByName.size()); } }