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