d428819833619b98612c9d07656ee4fc41d25aa4
[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.test.util.YangParserTestUtils;
43
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class CreateStreamUtilTest {
46     private static EffectiveModelContext SCHEMA_CTX;
47
48     @BeforeClass
49     public static void setUp() throws Exception {
50         SCHEMA_CTX = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/streams"));
51     }
52
53     @Test
54     public void createStreamTest() {
55         final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(
56             prepareDomPayload("create-data-change-event-subscription", RpcDefinition::getInput, "toaster", "path"),
57             SCHEMA_CTX);
58         assertEquals(List.of(), result.getErrors());
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
68     public void createStreamWrongValueTest() {
69         final var payload = prepareDomPayload("create-data-change-event-subscription", RpcDefinition::getInput,
70             "String value", "path");
71         final var errors = assertThrows(RestconfDocumentedException.class,
72             () -> CreateStreamUtil.createDataChangeNotifiStream(payload, SCHEMA_CTX)).getErrors();
73         assertEquals(1, errors.size());
74         final var error = errors.get(0);
75         assertEquals(ErrorType.APPLICATION, error.getErrorType());
76         assertEquals(ErrorTag.OPERATION_FAILED, error.getErrorTag());
77         assertEquals("Instance identifier was not normalized correctly", error.getErrorMessage());
78     }
79
80     @Test
81     public void createStreamWrongInputRpcTest() {
82         final var payload = prepareDomPayload("create-data-change-event-subscription2", RpcDefinition::getInput,
83             "toaster", "path2");
84         final var errors = assertThrows(RestconfDocumentedException.class,
85             () -> CreateStreamUtil.createDataChangeNotifiStream(payload, SCHEMA_CTX)).getErrors();
86         assertEquals(1, errors.size());
87         final var error = errors.get(0);
88         assertEquals(ErrorType.APPLICATION, error.getErrorType());
89         assertEquals(ErrorTag.OPERATION_FAILED, error.getErrorTag());
90         assertEquals("Instance identifier was not normalized correctly", error.getErrorMessage());
91     }
92
93     private static NormalizedNodePayload prepareDomPayload(final String rpcName,
94             final Function<RpcDefinition, ContainerLike> rpcToContainer, final String toasterValue,
95             final String inputOutputName) {
96         final Module rpcModule = SCHEMA_CTX.findModules("sal-remote").iterator().next();
97         final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
98         ContainerLike rpcInputSchemaNode = null;
99         for (final RpcDefinition rpc : rpcModule.getRpcs()) {
100             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
101                 rpcInputSchemaNode = rpcToContainer.apply(rpc);
102                 break;
103             }
104         }
105         assertNotNull(rpcInputSchemaNode);
106
107         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container =
108             SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
109
110         final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
111         final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.findDataChildByName(lfQName).orElseThrow();
112
113         assertThat(lfSchemaNode, instanceOf(LeafSchemaNode.class));
114
115         final Object o;
116         if ("toaster".equals(toasterValue)) {
117             final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
118             o = YangInstanceIdentifier.builder().node(rpcQname).build();
119         } else {
120             o = toasterValue;
121         }
122         final LeafNode<Object> lfNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) lfSchemaNode)
123                 .withValue(o).build();
124         container.withChild(lfNode);
125
126         return NormalizedNodePayload.of(
127             InstanceIdentifierContext.ofDataSchemaNode(SCHEMA_CTX, rpcInputSchemaNode, null), container.build());
128     }
129 }