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.mockito.MockitoAnnotations;
18 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
19 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
20 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
21 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
22 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
29 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
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.model.util.SchemaNodeUtils;
37 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39 public class CreateStreamUtilTest {
40 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/streams";
42 private NormalizedNodeContext payload;
43 private EffectiveModelContext refSchemaCtx;
46 public void setUp() throws Exception {
47 MockitoAnnotations.initMocks(this);
49 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
53 public void createStreamTest() {
54 this.payload = prepareDomPayload("create-data-change-event-subscription", "input", "toaster", "path");
55 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
56 assertEquals(result.getErrors(), Collections.emptyList());
57 final NormalizedNode<?, ?> testedNn = result.getResult();
58 assertNotNull(testedNn);
59 final NormalizedNodeContext contextRef = prepareDomPayload("create-data-change-event-subscription", "output",
60 "data-change-event-subscription/toaster:toaster/datastore=CONFIGURATION/scope=BASE", "stream-name");
61 assertEquals(contextRef.getData(), testedNn);
64 @Test(expected = RestconfDocumentedException.class)
65 public void createStreamWrongValueTest() {
66 this.payload = prepareDomPayload("create-data-change-event-subscription", "input", "String value", "path");
67 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
68 assertEquals(result.getErrors(), Collections.emptyList());
71 @Test(expected = RestconfDocumentedException.class)
72 public void createStreamWrongInputRpcTest() {
73 this.payload = prepareDomPayload("create-data-change-event-subscription2", "input", "toaster", "path2");
74 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
75 assertEquals(result.getErrors(), Collections.emptyList());
78 private NormalizedNodeContext prepareDomPayload(final String rpcName, final String inputOutput,
79 final String toasterValue, final String inputOutputName) {
80 final EffectiveModelContext schema = this.refSchemaCtx;
81 final Module rpcModule = schema.findModules("sal-remote").iterator().next();
82 final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
83 final QName rpcInputQName = QName.create(rpcModule.getQNameModule(), inputOutput);
84 ContainerLike rpcInputSchemaNode = null;
85 for (final RpcDefinition rpc : rpcModule.getRpcs()) {
86 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
87 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
91 assertNotNull(rpcInputSchemaNode);
93 final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container =
94 Builders.containerBuilder(rpcInputSchemaNode);
96 final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
97 final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.findDataChildByName(lfQName).orElseThrow();
99 assertTrue(lfSchemaNode instanceof LeafSchemaNode);
102 if ("toaster".equals(toasterValue)) {
103 final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
104 o = YangInstanceIdentifier.builder().node(rpcQname).build();
108 final LeafNode<Object> lfNode = Builders.leafBuilder((LeafSchemaNode) lfSchemaNode)
109 .withValue(o).build();
110 container.withChild(lfNode);
112 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema),