Merge "Add missing copyright to pom files"
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / restful / utils / CreateStreamUtilTest.java
1 /*
2  * Copyright (c) 2016 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.restconf.restful.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.Collections;
16 import java.util.Set;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
22 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
23 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
24 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
25 import org.opendaylight.restconf.common.references.SchemaContextRef;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
40
41 public class CreateStreamUtilTest {
42
43     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/streams";
44
45     private NormalizedNodeContext payload;
46     private SchemaContextRef refSchemaCtx;
47
48     @Before
49     public void setUp() throws Exception {
50         MockitoAnnotations.initMocks(this);
51         refSchemaCtx = new SchemaContextRef(TestRestconfUtils.loadSchemaContext(PATH_FOR_NEW_SCHEMA_CONTEXT));
52     }
53
54     @Test
55     public void createStreamTest() {
56         payload = prepareDomPayload("create-data-change-event-subscription", "input", "toaster", "path");
57         final DOMRpcResult result = CreateStreamUtil.createStream(payload, refSchemaCtx);
58         assertEquals(result.getErrors(), Collections.emptyList());
59         final NormalizedNode<?, ?> testedNn = result.getResult();
60         assertNotNull(testedNn);
61         final NormalizedNodeContext contextRef = prepareDomPayload("create-data-change-event-subscription", "output", "toaster:toaster/datastore=CONFIGURATION/scope=BASE", "stream-name");
62         assertEquals(contextRef.getData(), testedNn);
63     }
64
65     @Test(expected = RestconfDocumentedException.class)
66     public void createStreamWrongValueTest() {
67         payload = prepareDomPayload("create-data-change-event-subscription", "input", "String value", "path");
68         final DOMRpcResult result = CreateStreamUtil.createStream(payload, refSchemaCtx);
69         assertEquals(result.getErrors(), Collections.emptyList());
70     }
71
72     @Test(expected = RestconfDocumentedException.class)
73     public void createStreamWrongInputRpcTest() {
74         payload = prepareDomPayload("create-data-change-event-subscription2", "input", "toaster", "path2");
75         final DOMRpcResult result = CreateStreamUtil.createStream(payload, refSchemaCtx);
76         assertEquals(result.getErrors(), Collections.emptyList());
77     }
78
79     private NormalizedNodeContext prepareDomPayload(final String rpcName, final String inputOutput, final String toasterValue, final String inputOutputName) {
80         final SchemaContext schema = refSchemaCtx.get();
81         final Module rpcModule = schema.findModuleByName("sal-remote", null);
82         assertNotNull(rpcModule);
83         final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
84         final QName rpcInputQName = QName.create(rpcModule.getQNameModule(), inputOutput);
85         final Set<RpcDefinition> setRpcs = rpcModule.getRpcs();
86         ContainerSchemaNode rpcInputSchemaNode = null;
87         for (final RpcDefinition rpc : setRpcs) {
88             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
89                 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
90                 break;
91             }
92         }
93         assertNotNull(rpcInputSchemaNode);
94
95         final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container = Builders.containerBuilder(rpcInputSchemaNode);
96
97         final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
98         final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.getDataChildByName(lfQName);
99
100         assertTrue(lfSchemaNode instanceof LeafSchemaNode);
101
102         final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
103         final Object o;
104         if ("toaster".equals(toasterValue)) {
105             o = YangInstanceIdentifier.builder().node(rpcQname).build();
106         } else {
107             o = toasterValue;
108         }
109         final LeafNode<Object> lfNode = (Builders.leafBuilder((LeafSchemaNode) lfSchemaNode)
110                 .withValue(o)).build();
111         container.withChild(lfNode);
112
113         return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema), container.build());
114     }
115 }