Merge "BUG-2329 Add test for anyxmls inside rpc resonse for netcfon-connector"
[controller.git] / opendaylight / md-sal / sal-netconf-connector / src / test / java / org / opendaylight / controller / sal / connect / netconf / schema / mapping / NetconfMessageTransformerTest.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.sal.connect.netconf.schema.mapping;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
14 import static org.junit.Assert.assertTrue;
15
16 import java.io.InputStream;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Set;
20 import org.hamcrest.CoreMatchers;
21 import org.junit.Test;
22 import org.opendaylight.controller.netconf.api.NetconfMessage;
23 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
24 import org.opendaylight.controller.sal.connect.netconf.NetconfToRpcRequestTest;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
28 import org.opendaylight.yangtools.yang.data.api.Node;
29 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
33
34 public class NetconfMessageTransformerTest {
35
36     private static final QName COMMIT_Q_NAME = QName.create("namespace", "2012-12-12", "commit");
37
38     @Test
39     public void testToRpcRequestNoSchemaForRequest() throws Exception {
40         final NetconfMessageTransformer netconfMessageTransformer = getTransformer();
41         final NetconfMessage netconfMessage = netconfMessageTransformer.toRpcRequest(COMMIT_Q_NAME,
42                 NodeFactory.createImmutableCompositeNode(COMMIT_Q_NAME, null, Collections.<Node<?>>emptyList()));
43         assertThat(XmlUtil.toString(netconfMessage.getDocument()), CoreMatchers.containsString("<commit"));
44     }
45
46     private NetconfMessageTransformer getTransformer() {
47         final NetconfMessageTransformer netconfMessageTransformer = new NetconfMessageTransformer();
48         netconfMessageTransformer.onGlobalContextUpdated(getSchema());
49         return netconfMessageTransformer;
50     }
51
52     @Test
53     public void testToRpcResultNoSchemaForResult() throws Exception {
54         final NetconfMessageTransformer netconfMessageTransformer = getTransformer();
55         final NetconfMessage response = new NetconfMessage(XmlUtil.readXmlToDocument(
56                 "<rpc-reply><ok/></rpc-reply>"
57         ));
58         final RpcResult<CompositeNode> compositeNodeRpcResult = netconfMessageTransformer.toRpcResult(response, COMMIT_Q_NAME);
59         assertTrue(compositeNodeRpcResult.isSuccessful());
60         assertEquals("ok", compositeNodeRpcResult.getResult().getValue().get(0).getKey().getLocalName());
61     }
62
63     public SchemaContext getSchema() {
64         final List<InputStream> modelsToParse = Collections
65                 .singletonList(NetconfToRpcRequestTest.class.getResourceAsStream("/schemas/rpc-notification-subscription.yang"));
66         final YangParserImpl parser = new YangParserImpl();
67         final Set<Module> configModules = parser.parseYangModelsFromStreams(modelsToParse);
68         final SchemaContext cfgCtx = parser.resolveSchemaContext(configModules);
69         assertNotNull(cfgCtx);
70         return cfgCtx;
71     }
72 }