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