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