Bug 2358: Fixed warnings in Restconf
[controller.git] / opendaylight / md-sal / sal-rest-connector / 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 org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15 import static org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil.getRevisionFormat;
16
17 import java.io.FileNotFoundException;
18 import java.text.ParseException;
19 import java.util.Date;
20 import java.util.Set;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.core.UriBuilder;
23 import javax.ws.rs.core.UriInfo;
24 import org.junit.Before;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
30 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.controller.sal.restconf.impl.InstanceIdentifierContext;
32 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
33 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
34 import org.opendaylight.controller.sal.streams.listeners.ListenerAdapter;
35 import org.opendaylight.controller.sal.streams.listeners.Notificator;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.InstanceIdentifierBuilder;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
41 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
42 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
43 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.DataContainerNodeAttrBuilder;
44 import org.opendaylight.yangtools.yang.model.api.ContainerSchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
46 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
47 import org.opendaylight.yangtools.yang.model.api.Module;
48 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
49 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
51
52 public class URIParametersParsing {
53
54     private RestconfImpl restconf;
55     private BrokerFacade mockedBrokerFacade;
56     private ControllerContext controllerContext;
57
58     @Before
59     public void init() throws FileNotFoundException {
60         restconf = RestconfImpl.getInstance();
61         mockedBrokerFacade = mock(BrokerFacade.class);
62         controllerContext = ControllerContext.getInstance();
63         controllerContext.setSchemas(TestUtils.loadSchemaContext("/datastore-and-scope-specification"));
64         restconf.setControllerContext(controllerContext);
65         restconf.setBroker(mockedBrokerFacade);
66     }
67
68     @Test
69     @Ignore // URI parsing test - not able to catch a motivation + bad mocking response now - it needs to change Controller RPC table holder approach
70     public void resolveURIParametersConcreteValues() {
71         resolveURIParameters("OPERATIONAL", "SUBTREE", LogicalDatastoreType.OPERATIONAL, DataChangeScope.SUBTREE);
72     }
73
74     @Test
75     @Ignore // URI parsing test - not able to catch a motivation + bad mocking response now - it needs to change Controller RPC table holder approach
76     public void resolveURIParametersDefaultValues() {
77         resolveURIParameters(null, null, LogicalDatastoreType.CONFIGURATION, DataChangeScope.BASE);
78     }
79
80     private void resolveURIParameters(final String datastore, final String scope,
81             final LogicalDatastoreType datastoreExpected, final DataChangeScope scopeExpected) {
82
83         final InstanceIdentifierBuilder iiBuilder = YangInstanceIdentifier.builder();
84         iiBuilder.node(QName.create("dummyStreamName"));
85
86         final String datastoreValue = datastore == null ? "CONFIGURATION" : datastore;
87         final String scopeValue = scope == null ? "BASE" : scope + "";
88         Notificator.createListener(iiBuilder.build(), "dummyStreamName/datastore=" + datastoreValue + "/scope="
89                 + scopeValue);
90
91         final UriInfo mockedUriInfo = mock(UriInfo.class);
92         @SuppressWarnings("unchecked")
93         final MultivaluedMap<String, String> mockedMultivaluedMap = mock(MultivaluedMap.class);
94         when(mockedMultivaluedMap.getFirst(eq("datastore"))).thenReturn(datastoreValue);
95         when(mockedMultivaluedMap.getFirst(eq("scope"))).thenReturn(scopeValue);
96
97         when(mockedUriInfo.getQueryParameters(eq(false))).thenReturn(mockedMultivaluedMap);
98
99          final UriBuilder uriBuilder = UriBuilder.fromUri("www.whatever.com");
100          when(mockedUriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
101
102 //       when(mockedBrokerFacade.invokeRpc(any(SchemaPath.class), any(NormalizedNode.class)))
103 //       .thenReturn(Futures.<DOMRpcResult, DOMRpcException> immediateCheckedFuture(new DefaultDOMRpcResult(Builders.containerBuilder().build())));
104
105         restconf.invokeRpc("sal-remote:create-data-change-event-subscription", prepareDomRpcNode(datastore, scope),
106                 mockedUriInfo);
107
108         final ListenerAdapter listener = Notificator.getListenerFor("opendaylight-inventory:nodes/datastore="
109                 + datastoreValue + "/scope=" + scopeValue);
110         assertNotNull(listener);
111
112     }
113
114     private NormalizedNodeContext prepareDomRpcNode(final String datastore, final String scope) {
115         final SchemaContext schema = controllerContext.getGlobalSchema();
116         final Date revDate;
117         try {
118             revDate = getRevisionFormat().parse("2014-01-14");
119         }
120         catch (final ParseException e) {
121             throw new IllegalStateException(e);
122         }
123         final Module rpcSalRemoteModule = schema.findModuleByName("sal-remote", revDate);
124         final Set<RpcDefinition> setRpcs = rpcSalRemoteModule.getRpcs();
125         final QName rpcQName = QName.create(rpcSalRemoteModule.getQNameModule(), "create-data-change-event-subscription");
126         final QName rpcInputQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote","2014-01-14","input");
127         ContainerSchemaNode rpcInputSchemaNode = null;
128         for (final RpcDefinition rpc : setRpcs) {
129             if (rpcQName.isEqualWithoutRevision(rpc.getQName())) {
130                 rpcInputSchemaNode = SchemaNodeUtils.getRpcDataSchema(rpc, rpcInputQName);
131                 break;
132             }
133         }
134         assertNotNull("RPC ContainerSchemaNode was not found!", rpcInputSchemaNode);
135
136         final DataContainerNodeAttrBuilder<NodeIdentifier, ContainerNode> container = Builders.containerBuilder(rpcInputSchemaNode);
137
138         final QName pathQName = QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote", "2014-01-14", "path");
139         final DataSchemaNode pathSchemaNode = rpcInputSchemaNode.getDataChildByName(pathQName);
140         assertTrue(pathSchemaNode instanceof LeafSchemaNode);
141         final LeafNode<Object> pathNode = (Builders.leafBuilder((LeafSchemaNode) pathSchemaNode)
142                 .withValue(YangInstanceIdentifier.builder().node(QName.create("urn:opendaylight:inventory", "2013-08-19", "nodes")).build())).build();
143         container.withChild(pathNode);
144
145         final QName dataStoreQName = QName.create("urn:sal:restconf:event:subscription", "2014-7-8", "datastore");
146         final DataSchemaNode dsSchemaNode = rpcInputSchemaNode.getDataChildByName(dataStoreQName);
147         assertTrue(dsSchemaNode instanceof LeafSchemaNode);
148         final LeafNode<Object> dsNode = (Builders.leafBuilder((LeafSchemaNode) dsSchemaNode)
149                 .withValue(datastore)).build();
150         container.withChild(dsNode);
151
152         final QName scopeQName = QName.create("urn:sal:restconf:event:subscription", "2014-7-8", "scope");
153         final DataSchemaNode scopeSchemaNode = rpcInputSchemaNode.getDataChildByName(scopeQName);
154         assertTrue(scopeSchemaNode instanceof LeafSchemaNode);
155         final LeafNode<Object> scopeNode = (Builders.leafBuilder((LeafSchemaNode) scopeSchemaNode)
156                 .withValue(scope)).build();
157         container.withChild(scopeNode);
158
159         return new NormalizedNodeContext(new InstanceIdentifierContext<>(null, rpcInputSchemaNode, null, schema), container.build());
160     }
161 }