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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.