Bump upstreams to SNAPSHOTs
[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 import static org.junit.Assert.assertThrows;
15
16 import java.util.List;
17 import java.util.function.Function;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.junit.runner.RunWith;
21 import org.mockito.junit.MockitoJUnitRunner;
22 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
23 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
24 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
25 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
26 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
27 import org.opendaylight.yangtools.yang.common.ErrorTag;
28 import org.opendaylight.yangtools.yang.common.ErrorType;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
32 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
35 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
36 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
37 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
38 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
39 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
42 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
43 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
44 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45
46 @RunWith(MockitoJUnitRunner.StrictStubs.class)
47 public class CreateStreamUtilTest {
48     private static EffectiveModelContext SCHEMA_CTX;
49
50     @BeforeClass
51     public static void setUp() throws Exception {
52         SCHEMA_CTX = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/streams"));
53     }
54
55     @Test
56     public void createStreamTest() {
57         final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(
58             prepareDomPayload("create-data-change-event-subscription", RpcDefinition::getInput, "toaster", "path"),
59             SCHEMA_CTX);
60         assertEquals(List.of(), result.getErrors());
61         final NormalizedNode testedNn = result.getResult();
62         assertNotNull(testedNn);
63         final NormalizedNodePayload contextRef = prepareDomPayload("create-data-change-event-subscription",
64             RpcDefinition::getOutput,
65             "data-change-event-subscription/toaster:toaster/datastore=CONFIGURATION/scope=BASE", "stream-name");
66         assertEquals(contextRef.getData(), testedNn);
67     }
68
69     @Test
70     public void createStreamWrongValueTest() {
71         final var payload = prepareDomPayload("create-data-change-event-subscription", RpcDefinition::getInput,
72             "String value", "path");
73         final var errors = assertThrows(RestconfDocumentedException.class,
74             () -> CreateStreamUtil.createDataChangeNotifiStream(payload, SCHEMA_CTX)).getErrors();
75         assertEquals(1, errors.size());
76         final var error = errors.get(0);
77         assertEquals(ErrorType.APPLICATION, error.getErrorType());
78         assertEquals(ErrorTag.OPERATION_FAILED, error.getErrorTag());
79         assertEquals("Instance identifier was not normalized correctly", error.getErrorMessage());
80     }
81
82     @Test
83     public void createStreamWrongInputRpcTest() {
84         final var payload = prepareDomPayload("create-data-change-event-subscription2", RpcDefinition::getInput,
85             "toaster", "path2");
86         final var errors = assertThrows(RestconfDocumentedException.class,
87             () -> CreateStreamUtil.createDataChangeNotifiStream(payload, SCHEMA_CTX)).getErrors();
88         assertEquals(1, errors.size());
89         final var error = errors.get(0);
90         assertEquals(ErrorType.APPLICATION, error.getErrorType());
91         assertEquals(ErrorTag.OPERATION_FAILED, error.getErrorTag());
92         assertEquals("Instance identifier was not normalized correctly", error.getErrorMessage());
93     }
94
95     private static NormalizedNodePayload prepareDomPayload(final String rpcName,
96             final Function<RpcDefinition, ContainerLike> rpcToContainer, final String toasterValue,
97             final String inputOutputName) {
98         final Module rpcModule = SCHEMA_CTX.findModules("sal-remote").iterator().next();
99         final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
100         ContainerLike rpcInputSchemaNode = null;
101         for (final RpcDefinition rpc : rpcModule.getRpcs()) {
102             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
103                 rpcInputSchemaNode = rpcToContainer.apply(rpc);
104                 break;
105             }
106         }
107         assertNotNull(rpcInputSchemaNode);
108
109         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container =
110             SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
111
112         final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
113         final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.findDataChildByName(lfQName).orElseThrow();
114
115         assertThat(lfSchemaNode, instanceOf(LeafSchemaNode.class));
116
117         final Object o;
118         if ("toaster".equals(toasterValue)) {
119             final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
120             o = YangInstanceIdentifier.builder().node(rpcQname).build();
121         } else {
122             o = toasterValue;
123         }
124         final LeafNode<Object> lfNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) lfSchemaNode)
125                 .withValue(o).build();
126         container.withChild(lfNode);
127
128         return NormalizedNodePayload.of(InstanceIdentifierContext.ofStack(
129             SchemaInferenceStack.of(SCHEMA_CTX, Absolute.of(rpcQName, rpcInputSchemaNode.getQName()))),
130             container.build());
131     }
132 }