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