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.rests.services.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
14 import java.util.Collections;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.junit.MockitoJUnitRunner;
19 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
20 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
21 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
22 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
23 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
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.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
30 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
31 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
32 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
34 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
37 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40 @RunWith(MockitoJUnitRunner.StrictStubs.class)
41 public class CreateStreamUtilTest {
42 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/streams";
44 private NormalizedNodeContext payload;
45 private EffectiveModelContext refSchemaCtx;
48 public void setUp() throws Exception {
50 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
54 public void createStreamTest() {
55 this.payload = prepareDomPayload("create-data-change-event-subscription", "input", "toaster", "path");
56 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
57 assertEquals(result.getErrors(), Collections.emptyList());
58 final NormalizedNode<?, ?> testedNn = result.getResult();
59 assertNotNull(testedNn);
60 final NormalizedNodeContext contextRef = prepareDomPayload("create-data-change-event-subscription", "output",
61 "data-change-event-subscription/toaster:toaster/datastore=CONFIGURATION/scope=BASE", "stream-name");
62 assertEquals(contextRef.getData(), testedNn);
65 @Test(expected = RestconfDocumentedException.class)
66 public void createStreamWrongValueTest() {
67 this.payload = prepareDomPayload("create-data-change-event-subscription", "input", "String value", "path");
68 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
69 assertEquals(result.getErrors(), Collections.emptyList());
72 @Test(expected = RestconfDocumentedException.class)
73 public void createStreamWrongInputRpcTest() {
74 this.payload = prepareDomPayload("create-data-change-event-subscription2", "input", "toaster", "path2");
75 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
76 assertEquals(result.getErrors(), Collections.emptyList());
79 private NormalizedNodeContext prepareDomPayload(final String rpcName, final String inputOutput,
80 final String toasterValue, final String inputOutputName) {
81 final EffectiveModelContext schema = this.refSchemaCtx;
82 final Module rpcModule = schema.findModules("sal-remote").iterator().next();
83 final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
84 final QName rpcInputQName = QName.create(rpcModule.getQNameModule(), inputOutput);
85 ContainerLike rpcInputSchemaNode = null;
86 for (final RpcDefinition rpc : rpcModule.getRpcs()) {
87 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
88 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
92 assertNotNull(rpcInputSchemaNode);
94 final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container =
95 Builders.containerBuilder(rpcInputSchemaNode);
97 final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
98 final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.findDataChildByName(lfQName).orElseThrow();
100 assertTrue(lfSchemaNode instanceof LeafSchemaNode);
103 if ("toaster".equals(toasterValue)) {
104 final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
105 o = YangInstanceIdentifier.builder().node(rpcQname).build();
109 final LeafNode<Object> lfNode = Builders.leafBuilder((LeafSchemaNode) lfSchemaNode)
110 .withValue(o).build();
111 container.withChild(lfNode);
113 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema),