Merge "Refactor netconf-util's configuration service"
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfImplTest.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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.anyBoolean;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.when;
19
20 import com.google.common.base.Optional;
21 import com.google.common.collect.Lists;
22 import com.google.common.collect.Sets;
23 import com.google.common.util.concurrent.Futures;
24 import java.io.FileNotFoundException;
25 import java.net.URI;
26 import java.text.ParseException;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.Set;
31 import javax.ws.rs.core.MultivaluedHashMap;
32 import javax.ws.rs.core.MultivaluedMap;
33 import javax.ws.rs.core.UriBuilder;
34 import javax.ws.rs.core.UriInfo;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.mockito.Mockito;
39 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
40 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
41 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
42 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
43 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
44 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
45 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
46 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
47 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
48 import org.opendaylight.yangtools.yang.common.QName;
49 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
50 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
51 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
52 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
54 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
55 import org.opendaylight.yangtools.yang.model.api.Module;
56 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
57 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
58 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
59 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
60 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
61
62 /**
63  * @See {@link InvokeRpcMethodTest}
64  *
65  */
66 public class RestconfImplTest {
67
68     private RestconfImpl restconfImpl = null;
69     private static ControllerContext controllerContext = null;
70
71     @BeforeClass
72     public static void init() throws FileNotFoundException, ReactorException {
73         final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
74
75         final Set<Module> allModules = schemaContext.getModules();
76         assertNotNull(allModules);
77
78         controllerContext = ControllerContext.getInstance();
79         controllerContext.setSchemas(schemaContext);
80     }
81
82     @Before
83     public void initMethod() {
84         this.restconfImpl = RestconfImpl.getInstance();
85         this.restconfImpl.setControllerContext(controllerContext);
86     }
87
88     @SuppressWarnings("unchecked")
89     @Test
90     public void testExample() throws FileNotFoundException, ParseException {
91         @SuppressWarnings("rawtypes")
92         final
93         NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
94         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
95         when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
96         assertEquals(normalizedNodeData,
97                 brokerFacade.readOperationalData(null));
98     }
99
100     @Test
101     public void testRpcForMountpoint() throws Exception {
102         final UriInfo uriInfo = mock(UriInfo.class);
103         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
104
105         final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
106         final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
107         doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
108         final SchemaNode schemaNode = mock(SchemaNode.class);
109         doReturn(schemaNode).when(iiCtx).getSchemaNode();
110         doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
111         doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
112
113         final DOMMountPoint mount = mock(DOMMountPoint.class);
114         doReturn(mount).when(iiCtx).getMountPoint();
115         final DOMRpcService rpcService = mock(DOMRpcService.class);
116         doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
117         doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService)
118                 .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
119         this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
120         this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
121         verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
122     }
123
124     /**
125      * Create notification stream for toaster module
126      */
127     @Test
128     public void createNotificationStreamTest() {
129         final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
130         final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
131         doReturn(iiCtx).when(payload).getInstanceIdentifierContext();
132
133         final SchemaNode schemaNode = mock(SchemaNode.class,
134                 Mockito.withSettings().extraInterfaces(RpcDefinition.class));
135         doReturn(schemaNode).when(iiCtx).getSchemaNode();
136         doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
137
138         doReturn(QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
139                 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
140         doReturn(null).when(iiCtx).getMountPoint();
141
142         final Set<DataContainerChild<?, ?>> children = Sets.newHashSet();
143         final DataContainerChild<?, ?> child = mock(DataContainerChild.class,
144                 Mockito.withSettings().extraInterfaces(LeafSetNode.class));
145
146         final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
147         when(entryNode.getValue()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
148         when(((LeafSetNode) child).getValue()).thenReturn(Sets.newHashSet(entryNode));
149         children.add(child);
150
151         final NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class,
152                 Mockito.withSettings().extraInterfaces(ContainerNode.class));
153         doReturn(normalizedNode).when(payload).getData();
154         doReturn(children).when(normalizedNode).getValue();
155
156         // register notification
157         final NormalizedNodeContext context = this.restconfImpl
158                 .invokeRpc("sal-remote:create-notification-stream", payload, null);
159         assertNotNull(context);
160     }
161
162     /**
163      * Subscribe for notification stream of toaster module
164      */
165     @Test
166     public void subscribeToNotificationStreamTest() throws Exception {
167         final String identifier = "create-notification-stream/toaster:toastDone";
168
169         // register test notification stream
170         final SchemaPath path = SchemaPath.create(
171                 true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
172         Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML");
173
174         final UriInfo uriInfo = mock(UriInfo.class);
175         final UriBuilder uriBuilder = mock(UriBuilder.class);
176         when(uriBuilder.port(8181)).thenReturn(uriBuilder);
177         when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
178         when(uriBuilder.build()).thenReturn(new URI(""));
179         when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
180         when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
181         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
182         final Set<Entry<String, List<String>>> set = new HashSet<>();
183         when(map.entrySet()).thenReturn(set);
184         when(uriInfo.getQueryParameters()).thenReturn(map);
185
186         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
187         this.restconfImpl.setBroker(brokerFacade);
188
189         // subscribe to stream and verify response
190         final NormalizedNodeContext response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
191
192         // remove test notification stream
193         Notificator.removeAllListeners();
194     }
195 }