2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.streams;
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;
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;
38 @RunWith(MockitoJUnitRunner.StrictStubs.class)
39 public class ListenersBrokerTest {
40 private static EffectiveModelContext SCHEMA_CTX;
42 private final ListenersBroker listenersBroker = new ListenersBroker.ServerSentEvents();
45 public static void setUp() {
46 SCHEMA_CTX = YangParserTestUtils.parseYangResourceDirectory("/streams");
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());
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());
73 public void createStreamWrongInputRpcTest() {
74 final var payload = prepareDomPayload("create-data-change-event-subscription2", RpcDefinition::getInput,
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());
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);
91 ContainerLike containerSchemaNode = null;
92 for (final RpcDefinition rpc : rpcModule.getRpcs()) {
93 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
94 containerSchemaNode = rpcToContainer.apply(rpc);
98 assertNotNull(containerSchemaNode);
100 final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
101 final DataSchemaNode lfSchemaNode = containerSchemaNode.getDataChildByName(lfQName);
102 assertThat(lfSchemaNode, instanceOf(LeafSchemaNode.class));
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);
112 return Builders.containerBuilder()
113 .withNodeIdentifier(new NodeIdentifier(containerSchemaNode.getQName()))
114 .withChild(ImmutableNodes.leafNode(lfQName, o))