Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / URIParametersParsing.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import static java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
16
17 import java.io.FileNotFoundException;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.core.UriBuilder;
20 import javax.ws.rs.core.UriInfo;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
27 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
29 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
30 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
31 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
32 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
33 import org.opendaylight.restconf.common.util.DataChangeScope;
34 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.common.Revision;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
42 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.impl.schema.SchemaAwareBuilders;
46 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.ContainerLike;
48 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
49 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
50 import org.opendaylight.yangtools.yang.model.api.InputSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
52 import org.opendaylight.yangtools.yang.model.api.Module;
53 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
54 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
55 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
56
57 public class URIParametersParsing {
58
59     private RestconfImpl restconf;
60     private BrokerFacade mockedBrokerFacade;
61     private ControllerContext controllerContext;
62
63     @Before
64     public void init() throws FileNotFoundException, ReactorException {
65         this.mockedBrokerFacade = mock(BrokerFacade.class);
66         this.controllerContext = TestRestconfUtils.newControllerContext(
67                 TestUtils.loadSchemaContext("/datastore-and-scope-specification"));
68         this.restconf = RestconfImpl.newInstance(mockedBrokerFacade, controllerContext);
69     }
70
71     @Test
72     public void resolveURIParametersConcreteValues() {
73         resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, DataChangeScope.SUBTREE);
74     }
75
76     @Test
77     public void resolveURIParametersDefaultValues() {
78         resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE);
79     }
80
81     private void resolveURIParameters(final String datastore, final String scope,
82             final LogicalDatastoreType datastoreExpected, final DataChangeScope scopeExpected) {
83
84         final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
85         iiBuilder.node(QName.create("", "dummyStreamName"));
86
87         final String datastoreValue = datastore == null ? "CONFIGURATION" : datastore;
88         final String scopeValue = scope == null ? "BASE" : scope + "";
89         Notificator.createListener(iiBuilder.build(), "dummyStreamName/datastore=" + datastoreValue + "/scope="
90                 + scopeValue, NotificationOutputType.XML, controllerContext);
91
92         final UriInfo mockedUriInfo = mock(UriInfo.class);
93         @SuppressWarnings("unchecked")
94         final MultivaluedMap<String, String> mockedMultivaluedMap = mock(MultivaluedMap.class);
95         when(mockedMultivaluedMap.getFirst(eq("datastore"))).thenReturn(datastoreValue);
96         when(mockedMultivaluedMap.getFirst(eq("scope"))).thenReturn(scopeValue);
97
98         when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
99
100         final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
101         when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
102
103         this.restconf.invokeRpc("sal-remote:create-data-change-event-subscription",
104             prepareDomRpcNode(datastoreValue, scopeValue), mockedUriInfo);
105
106         final ListenerAdapter listener =
107                 Notificator.getListenerFor("data-change-event-subscription/opendaylight-inventory:nodes/datastore="
108                 + datastoreValue + "/scope=" + scopeValue);
109         assertNotNull(listener);
110     }
111
112     private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
113         final EffectiveModelContext schema = this.controllerContext.getGlobalSchema();
114         final Module rpcSalRemoteModule = schema.findModule("sal-remote", Revision.of("2014-01-14")).get();
115         final QName rpcQName =
116                 QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
117         final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
118         ContainerLike rpcInputSchemaNode = null;
119         for (final RpcDefinition rpc : rpcSalRemoteModule.getRpcs()) {
120             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
121                 rpcInputSchemaNode = rpc.getInput();
122                 break;
123             }
124         }
125         assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
126
127         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container =
128                 SchemaAwareBuilders.containerBuilder(rpcInputSchemaNode);
129
130         final QName pathQName =
131                 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
132         final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
133         assertTrue(pathSchemaNode instanceof LeafSchemaNode);
134         final LeafNode<Object> pathNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) pathSchemaNode)
135                 .withValue(YangInstanceIdentifier.builder()
136                         .node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build()).build();
137         container.withChild(pathNode);
138
139         final AugmentationSchemaNode augmentationSchema = requireNonNull(rpcInputSchemaNode.getAvailableAugmentations()
140                 .iterator().next());
141         final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder =
142                 SchemaAwareBuilders.augmentationBuilder(augmentationSchema);
143
144         final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "datastore");
145         final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
146         assertTrue(dsSchemaNode instanceof LeafSchemaNode);
147         final LeafNode<Object> dsNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) dsSchemaNode)
148                 .withValue(datastore).build();
149         augmentationBuilder.withChild(dsNode);
150
151         final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "scope");
152         final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
153         assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
154         final LeafNode<Object> scopeNode = SchemaAwareBuilders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
155                 .withValue(scope).build();
156         augmentationBuilder.withChild(scopeNode);
157
158         final QName outputQName =
159                 QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "notification-output-type");
160         final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
161         assertTrue(outputSchemaNode instanceof LeafSchemaNode);
162         final LeafNode<Object> outputNode =
163             SchemaAwareBuilders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML").build();
164         augmentationBuilder.withChild(outputNode);
165
166         container.withChild(augmentationBuilder.build());
167
168         when(rpcDef.getInput()).thenReturn((InputSchemaNode) rpcInputSchemaNode);
169         when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
170         when(rpcDef.getQName()).thenReturn(rpcQName);
171
172         return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcDef, null, schema),
173                 container.build());
174     }
175 }