Merge "Make idle timeout configurable in ssh proxy server"
[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 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.Lists;
13 import com.google.common.io.ByteSource;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.util.ArrayList;
17 import java.util.Iterator;
18 import java.util.List;
19 import javax.xml.parsers.DocumentBuilderFactory;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
25 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
26 import org.opendaylight.yangtools.yang.data.api.Node;
27 import org.opendaylight.yangtools.yang.data.api.SimpleNode;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
34
35 public class XmlUtilsTest {
36
37   private static final DocumentBuilderFactory BUILDERFACTORY;
38
39   static {
40     final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
41     factory.setNamespaceAware(true);
42     factory.setCoalescing(true);
43     factory.setIgnoringElementContentWhitespace(true);
44     factory.setIgnoringComments(true);
45     BUILDERFACTORY = factory;
46   }
47
48   private SchemaContext schema;
49   private RpcDefinition testRpc;
50
51   public static final String XML_CONTENT = "<add-flow xmlns=\"urn:opendaylight:controller:rpc:test\"><input xmlns=\"urn:opendaylight:controller:rpc:test\">" +
52       "<id>flowid</id>" +
53       "<flow xmlns:ltha=\"urn:opendaylight:controller:rpc:test\">/ltha:node/ltha:node1[ltha:id='3@java.lang.Short']</flow>" +
54       "</input></add-flow>";
55
56   @Before
57   public void setUp() throws Exception {
58     final ByteSource byteSource = new ByteSource() {
59       @Override
60       public InputStream openStream() throws IOException {
61         return XmlUtilsTest.this.getClass().getResourceAsStream("rpcTest.yang");
62       }
63     };
64     schema = new YangParserImpl().parseSources(Lists.newArrayList(byteSource));
65     final Module rpcTestModule = schema.getModules().iterator().next();
66     testRpc = rpcTestModule.getRpcs().iterator().next();
67   }
68
69   @Test
70   public void testNullInputXmlToComposite() {
71     CompositeNode node = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), null, schema);
72     Assert.assertNull(node);
73   }
74
75   @Test
76   public void testNullRpcXmlToComposite() {
77     CompositeNode node = XmlUtils.inputXmlToCompositeNode(null, XML_CONTENT, schema);
78     Assert.assertNull(node);
79   }
80
81   @Test
82   public void testInputXmlToCompositeNode() {
83     CompositeNode node = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), XML_CONTENT, schema);
84     ImmutableList<SimpleNode<?>> input = (ImmutableList<SimpleNode<?>>)node.getValue().get(0).getValue();
85     SimpleNode<?> firstNode = input.get(0);
86
87     Assert.assertEquals("id", firstNode.getNodeType().getLocalName());
88     Assert.assertEquals("flowid", firstNode.getValue());
89
90     SimpleNode<?> secondNode = input.get(1);
91     Assert.assertEquals("flow", secondNode.getNodeType().getLocalName());
92
93     YangInstanceIdentifier instance = (YangInstanceIdentifier) secondNode.getValue();
94     Iterable<YangInstanceIdentifier.PathArgument> iterable = instance.getPathArguments();
95     Iterator it = iterable.iterator();
96     YangInstanceIdentifier.NodeIdentifier firstPath = (YangInstanceIdentifier.NodeIdentifier) it.next();
97     Assert.assertEquals("node", firstPath.getNodeType().getLocalName());
98     YangInstanceIdentifier.NodeIdentifierWithPredicates secondPath = (YangInstanceIdentifier.NodeIdentifierWithPredicates)it.next();
99     Short value = (Short)secondPath.getKeyValues().values().iterator().next();
100     Short expected = 3;
101     Assert.assertEquals(expected, value);
102   }
103
104   @Test
105   public void testInputCompositeNodeToXML() {
106     CompositeNode input = XmlUtils.inputXmlToCompositeNode(testRpc.getQName(), XML_CONTENT, schema);
107     List<Node<?>> childNodes = new ArrayList<>();
108     childNodes.add(input);
109     QName rpcQName = schema.getOperations().iterator().next().getQName();
110     CompositeNode node = new ImmutableCompositeNode(rpcQName, input.getValue(), ModifyAction.REPLACE);
111     String xml = XmlUtils.inputCompositeNodeToXml(node, schema);
112     Assert.assertNotNull(xml);
113     Assert.assertTrue(xml.contains("3@java.lang.Short"));
114   }
115
116   @Test
117   public void testNullCompositeNodeToXml(){
118     String xml = XmlUtils.inputCompositeNodeToXml(null, schema);
119     Assert.assertTrue(xml.isEmpty());
120   }
121
122   @Test
123   public void testNullSchemaCompositeNodeToXml(){
124     String xml = XmlUtils.inputCompositeNodeToXml(new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE), null);
125     Assert.assertTrue(xml.isEmpty());
126   }
127
128
129 }