Custom mailbox that is bounded and instrumented.
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / test / java / org / opendaylight / controller / xml / codec / 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.xml.codec;
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.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;
29
30 import javax.xml.parsers.DocumentBuilderFactory;
31
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;
37
38
39 public class XmlUtilsTest {
40
41   private static final DocumentBuilderFactory BUILDERFACTORY;
42
43   static {
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;
50   }
51
52   private SchemaContext schema;
53   private RpcDefinition testRpc;
54
55   public static final String XML_CONTENT = "<add-flow xmlns=\"urn:opendaylight:controller:rpc:test\"><input xmlns=\"urn:opendaylight:controller:rpc:test\">" +
56       "<id>flowid</id>" +
57       "<flow xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:node/ltha:node1[ltha:id='3@java.lang.Short']</flow>" +
58       "</input></add-flow>";
59
60   @Before
61   public void setUp() throws Exception {
62     final ByteSource byteSource = new ByteSource() {
63       @Override
64       public InputStream openStream() throws IOException {
65         return XmlUtilsTest.this.getClass().getResourceAsStream("rpcTest.yang");
66       }
67     };
68     schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
69     final Module rpcTestModule = schema.getModules().iterator().next();
70     testRpc = rpcTestModule.getRpcs().iterator().next();
71   }
72
73   @Test
74   public void testNullInputXmlToComposite() {
75     CompositeNode node = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), null, schema);
76     Assert.assertNull(node);
77   }
78
79   @Test
80   public void testNullRpcXmlToComposite() {
81     CompositeNode node = XmlUtils.inputXmlToCompositeNode(null, XML_CONTENT, schema);
82     Assert.assertNull(node);
83   }
84
85   @Test
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);
90
91     Assert.assertEquals("id", firstNode.getNodeType().getLocalName());
92     Assert.assertEquals("flowid", firstNode.getValue());
93
94     SimpleNode secondNode = input.get(1);
95     Assert.assertEquals("flow", secondNode.getNodeType().getLocalName());
96
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();
104     Short expected = 3;
105     Assert.assertEquals(expected, value);
106   }
107
108   @Test
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"));
118   }
119
120   @Test
121   public void testNullCompositeNodeToXml(){
122     String xml = XmlUtils.inputCompositeNodeToXml(null, schema);
123     Assert.assertTrue(xml.isEmpty());
124   }
125
126   @Test
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());
130   }
131
132
133 }