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
9 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import java.util.Collections;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.MockitoAnnotations;
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.restconf.nb.rfc8040.references.SchemaContextRef;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
31 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
32 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
38 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
41 public class CreateStreamUtilTest {
43 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/streams";
45 private NormalizedNodeContext payload;
46 private SchemaContextRef refSchemaCtx;
49 public void setUp() throws Exception {
50 MockitoAnnotations.initMocks(this);
51 this.refSchemaCtx = new SchemaContextRef(
52 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
56 public void createStreamTest() {
57 this.payload = prepareDomPayload("create-data-change-event-subscription", "input", "toaster", "path");
58 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
59 assertEquals(result.getErrors(), Collections.emptyList());
60 final NormalizedNode<?, ?> testedNn = result.getResult();
61 assertNotNull(testedNn);
62 final NormalizedNodeContext contextRef = prepareDomPayload("create-data-change-event-subscription", "output",
63 "data-change-event-subscription/toaster:toaster/datastore=CONFIGURATION/scope=BASE", "stream-name");
64 assertEquals(contextRef.getData(), testedNn);
67 @Test(expected = RestconfDocumentedException.class)
68 public void createStreamWrongValueTest() {
69 this.payload = prepareDomPayload("create-data-change-event-subscription", "input", "String value", "path");
70 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
71 assertEquals(result.getErrors(), Collections.emptyList());
74 @Test(expected = RestconfDocumentedException.class)
75 public void createStreamWrongInputRpcTest() {
76 this.payload = prepareDomPayload("create-data-change-event-subscription2", "input", "toaster", "path2");
77 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
78 assertEquals(result.getErrors(), Collections.emptyList());
81 private NormalizedNodeContext prepareDomPayload(final String rpcName, final String inputOutput,
82 final String toasterValue, final String inputOutputName) {
83 final EffectiveModelContext schema = this.refSchemaCtx.get();
84 final Module rpcModule = schema.findModules("sal-remote").iterator().next();
85 final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
86 final QName rpcInputQName = QName.create(rpcModule.getQNameModule(), inputOutput);
87 ContainerSchemaNode rpcInputSchemaNode = null;
88 for (final RpcDefinition rpc : rpcModule.getRpcs()) {
89 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
90 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
94 assertNotNull(rpcInputSchemaNode);
96 final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container =
97 Builders.containerBuilder(rpcInputSchemaNode);
99 final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
100 final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.getDataChildByName(lfQName);
102 assertTrue(lfSchemaNode instanceof LeafSchemaNode);
105 if ("toaster".equals(toasterValue)) {
106 final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
107 o = YangInstanceIdentifier.builder().node(rpcQname).build();
111 final LeafNode<Object> lfNode = Builders.leafBuilder((LeafSchemaNode) lfSchemaNode)
112 .withValue(o).build();
113 container.withChild(lfNode);
115 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema),