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.restful.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;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
21 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
22 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
23 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
24 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
25 import org.opendaylight.restconf.common.references.SchemaContextRef;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
32 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
33 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
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.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 public class CreateStreamUtilTest {
44 private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/streams";
46 private NormalizedNodeContext payload;
47 private SchemaContextRef refSchemaCtx;
50 public void setUp() throws Exception {
51 MockitoAnnotations.initMocks(this);
52 this.refSchemaCtx = new SchemaContextRef(
53 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT)));
57 public void createStreamTest() {
58 this.payload = prepareDomPayload("create-data-change-event-subscription", "input", "toaster", "path");
59 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
60 assertEquals(result.getErrors(), Collections.emptyList());
61 final NormalizedNode<?, ?> testedNn = result.getResult();
62 assertNotNull(testedNn);
63 final NormalizedNodeContext contextRef = prepareDomPayload("create-data-change-event-subscription", "output",
64 "data-change-event-subscription/toaster:toaster/datastore=CONFIGURATION/scope=BASE", "stream-name");
65 assertEquals(contextRef.getData(), testedNn);
68 @Test(expected = RestconfDocumentedException.class)
69 public void createStreamWrongValueTest() {
70 this.payload = prepareDomPayload("create-data-change-event-subscription", "input", "String value", "path");
71 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
72 assertEquals(result.getErrors(), Collections.emptyList());
75 @Test(expected = RestconfDocumentedException.class)
76 public void createStreamWrongInputRpcTest() {
77 this.payload = prepareDomPayload("create-data-change-event-subscription2", "input", "toaster", "path2");
78 final DOMRpcResult result = CreateStreamUtil.createDataChangeNotifiStream(this.payload, this.refSchemaCtx);
79 assertEquals(result.getErrors(), Collections.emptyList());
82 private NormalizedNodeContext prepareDomPayload(final String rpcName, final String inputOutput,
83 final String toasterValue, final String inputOutputName) {
84 final SchemaContext schema = this.refSchemaCtx.get();
85 final Module rpcModule = schema.findModuleByName("sal-remote", null);
86 assertNotNull(rpcModule);
87 final QName rpcQName = QName.create(rpcModule.getQNameModule(), rpcName);
88 final QName rpcInputQName = QName.create(rpcModule.getQNameModule(), inputOutput);
89 final Set<RpcDefinition> setRpcs = rpcModule.getRpcs();
90 ContainerSchemaNode rpcInputSchemaNode = null;
91 for (final RpcDefinition rpc : setRpcs) {
92 if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
93 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
97 assertNotNull(rpcInputSchemaNode);
99 final DataContainerNodeAttrBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> container =
100 Builders.containerBuilder(rpcInputSchemaNode);
102 final QName lfQName = QName.create(rpcModule.getQNameModule(), inputOutputName);
103 final DataSchemaNode lfSchemaNode = rpcInputSchemaNode.getDataChildByName(lfQName);
105 assertTrue(lfSchemaNode instanceof LeafSchemaNode);
108 if ("toaster".equals(toasterValue)) {
109 final QName rpcQname = QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", toasterValue);
110 o = YangInstanceIdentifier.builder().node(rpcQname).build();
114 final LeafNode<Object> lfNode = (Builders.leafBuilder((LeafSchemaNode) lfSchemaNode)
115 .withValue(o)).build();
116 container.withChild(lfNode);
118 return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema),