Enable modernizer in restconf-nb-bierman02
[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.impl.schema.Builders;
45 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeBuilder;
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.model.util.SchemaNodeUtils;
56 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
57
58 public class URIParametersParsing {
59
60     private RestconfImpl restconf;
61     private BrokerFacade mockedBrokerFacade;
62     private ControllerContext controllerContext;
63
64     @Before
65     public void init() throws FileNotFoundException, ReactorException {
66         this.mockedBrokerFacade = mock(BrokerFacade.class);
67         this.controllerContext = TestRestconfUtils.newControllerContext(
68                 TestUtils.loadSchemaContext("/datastore-and-scope-specification"));
69         this.restconf = RestconfImpl.newInstance(mockedBrokerFacade, controllerContext);
70     }
71
72     @Test
73     public void resolveURIParametersConcreteValues() {
74         resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, DataChangeScope.SUBTREE);
75     }
76
77     @Test
78     public void resolveURIParametersDefaultValues() {
79         resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE);
80     }
81
82     private void resolveURIParameters(final String datastore, final String scope,
83             final LogicalDatastoreType datastoreExpected, final DataChangeScope scopeExpected) {
84
85         final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
86         iiBuilder.node(QName.create("", "dummyStreamName"));
87
88         final String datastoreValue = datastore == null ? "CONFIGURATION" : datastore;
89         final String scopeValue = scope == null ? "BASE" : scope + "";
90         Notificator.createListener(iiBuilder.build(), "dummyStreamName/datastore=" + datastoreValue + "/scope="
91                 + scopeValue, NotificationOutputType.XML, controllerContext);
92
93         final UriInfo mockedUriInfo = mock(UriInfo.class);
94         @SuppressWarnings("unchecked")
95         final MultivaluedMap<String, String> mockedMultivaluedMap = mock(MultivaluedMap.class);
96         when(mockedMultivaluedMap.getFirst(eq("datastore"))).thenReturn(datastoreValue);
97         when(mockedMultivaluedMap.getFirst(eq("scope"))).thenReturn(scopeValue);
98
99         when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
100
101         final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
102         when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
103
104         this.restconf.invokeRpc("sal-remote:create-data-change-event-subscription",
105             prepareDomRpcNode(datastoreValue, scopeValue), mockedUriInfo);
106
107         final ListenerAdapter listener =
108                 Notificator.getListenerFor("data-change-event-subscription/opendaylight-inventory:nodes/datastore="
109                 + datastoreValue + "/scope=" + scopeValue);
110         assertNotNull(listener);
111     }
112
113     private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
114         final EffectiveModelContext schema = this.controllerContext.getGlobalSchema();
115         final Module rpcSalRemoteModule = schema.findModule("sal-remote", Revision.of("2014-01-14")).get();
116         final QName rpcQName =
117                 QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
118         final QName rpcInputQName =
119                 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "input");
120         final RpcDefinition rpcDef = Mockito.mock(RpcDefinition.class);
121         ContainerLike rpcInputSchemaNode = null;
122         for (final RpcDefinition rpc : rpcSalRemoteModule.getRpcs()) {
123             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
124                 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
125                 break;
126             }
127         }
128         assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
129
130         final DataContainerNodeBuilder<NodeIdentifier, ContainerNode> container =
131                 Builders.containerBuilder(rpcInputSchemaNode);
132
133         final QName pathQName =
134                 QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
135         final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
136         assertTrue(pathSchemaNode instanceof LeafSchemaNode);
137         final LeafNode<Object> pathNode = Builders.leafBuilder((LeafSchemaNode) pathSchemaNode)
138                 .withValue(YangInstanceIdentifier.builder()
139                         .node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build()).build();
140         container.withChild(pathNode);
141
142         final AugmentationSchemaNode augmentationSchema = requireNonNull(rpcInputSchemaNode.getAvailableAugmentations()
143                 .iterator().next());
144         final DataContainerNodeBuilder<AugmentationIdentifier, AugmentationNode> augmentationBuilder =
145                 Builders.augmentationBuilder(augmentationSchema);
146
147         final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "datastore");
148         final DataSchemaNode dsSchemaNode = augmentationSchema.getDataChildByName(dataStoreQName);
149         assertTrue(dsSchemaNode instanceof LeafSchemaNode);
150         final LeafNode<Object> dsNode = Builders.leafBuilder((LeafSchemaNode) dsSchemaNode)
151                 .withValue(datastore).build();
152         augmentationBuilder.withChild(dsNode);
153
154         final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "scope");
155         final DataSchemaNode scopeSchemaNode = augmentationSchema.getDataChildByName(scopeQName);
156         assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
157         final LeafNode<Object> scopeNode = Builders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
158                 .withValue(scope).build();
159         augmentationBuilder.withChild(scopeNode);
160
161         final QName outputQName =
162                 QName.create("urn:sal:restconf:event:subscription", "2014-07-08", "notification-output-type");
163         final DataSchemaNode outputSchemaNode = augmentationSchema.getDataChildByName(outputQName);
164         assertTrue(outputSchemaNode instanceof LeafSchemaNode);
165         final LeafNode<Object> outputNode =
166                 Builders.leafBuilder((LeafSchemaNode) outputSchemaNode).withValue("XML").build();
167         augmentationBuilder.withChild(outputNode);
168
169         container.withChild(augmentationBuilder.build());
170
171         when(rpcDef.getInput()).thenReturn((InputSchemaNode) rpcInputSchemaNode);
172         when(rpcDef.getPath()).thenReturn(SchemaPath.create(true, rpcQName));
173         when(rpcDef.getQName()).thenReturn(rpcQName);
174
175         return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcDef, null, schema),
176                 container.build());
177     }
178 }