Add 'features/protocol-framework/' from commit 'cb42405784db97d0ce2c5991d12a89b46d185949'
[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 public class RestconfImplTest {
66
67     private RestconfImpl restconfImpl = null;
68     private static ControllerContext controllerContext = null;
69
70     @BeforeClass
71     public static void init() throws FileNotFoundException, ReactorException {
72         final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
73
74         final Set<Module> allModules = schemaContext.getModules();
75         assertNotNull(allModules);
76
77         controllerContext = ControllerContext.getInstance();
78         controllerContext.setSchemas(schemaContext);
79     }
80
81     @Before
82     public void initMethod() {
83         this.restconfImpl = RestconfImpl.getInstance();
84         this.restconfImpl.setControllerContext(controllerContext);
85     }
86
87     @SuppressWarnings("unchecked")
88     @Test
89     public void testExample() throws FileNotFoundException, ParseException {
90         @SuppressWarnings("rawtypes")
91         final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
92         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
93         when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
94         assertEquals(normalizedNodeData,
95                 brokerFacade.readOperationalData(null));
96     }
97
98     @Test
99     public void testRpcForMountpoint() throws Exception {
100         final UriInfo uriInfo = mock(UriInfo.class);
101         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
102
103         final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
104         final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
105         doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
106         final SchemaNode schemaNode = mock(SchemaNode.class);
107         doReturn(schemaNode).when(iiCtx).getSchemaNode();
108         doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
109         doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
110
111         final DOMMountPoint mount = mock(DOMMountPoint.class);
112         doReturn(mount).when(iiCtx).getMountPoint();
113         final DOMRpcService rpcService = mock(DOMRpcService.class);
114         doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
115         doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService)
116                 .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
117         this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
118         this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
119         verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
120     }
121
122     /**
123      * Create notification stream for toaster module.
124      */
125     @Test
126     public void createNotificationStreamTest() {
127         final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
128         final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
129         doReturn(iiCtx).when(payload).getInstanceIdentifierContext();
130
131         final SchemaNode schemaNode = mock(SchemaNode.class,
132                 Mockito.withSettings().extraInterfaces(RpcDefinition.class));
133         doReturn(schemaNode).when(iiCtx).getSchemaNode();
134         doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
135
136         doReturn(QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
137                 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
138         doReturn(null).when(iiCtx).getMountPoint();
139
140         final Set<DataContainerChild<?, ?>> children = Sets.newHashSet();
141         final DataContainerChild<?, ?> child = mock(DataContainerChild.class,
142                 Mockito.withSettings().extraInterfaces(LeafSetNode.class));
143
144         final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
145         when(entryNode.getValue()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
146         when(((LeafSetNode) child).getValue()).thenReturn(Sets.newHashSet(entryNode));
147         children.add(child);
148
149         final NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class,
150                 Mockito.withSettings().extraInterfaces(ContainerNode.class));
151         doReturn(normalizedNode).when(payload).getData();
152         doReturn(children).when(normalizedNode).getValue();
153
154         // register notification
155         final NormalizedNodeContext context = this.restconfImpl
156                 .invokeRpc("sal-remote:create-notification-stream", payload, null);
157         assertNotNull(context);
158     }
159
160     /**
161      * Subscribe for notification stream of toaster module.
162      */
163     @Test
164     public void subscribeToNotificationStreamTest() throws Exception {
165         final String identifier = "create-notification-stream/toaster:toastDone";
166
167         // register test notification stream
168         final SchemaPath path = SchemaPath.create(
169                 true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
170         Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML");
171
172         final UriInfo uriInfo = mock(UriInfo.class);
173         final UriBuilder uriBuilder = mock(UriBuilder.class);
174         when(uriBuilder.port(8181)).thenReturn(uriBuilder);
175         when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
176         when(uriBuilder.build()).thenReturn(new URI(""));
177         when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
178         when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
179         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
180         final Set<Entry<String, List<String>>> set = new HashSet<>();
181         when(map.entrySet()).thenReturn(set);
182         when(uriInfo.getQueryParameters()).thenReturn(map);
183
184         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
185         this.restconfImpl.setBroker(brokerFacade);
186
187         // subscribe to stream and verify response
188         final NormalizedNodeContext response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
189
190         // remove test notification stream
191         Notificator.removeAllListeners();
192     }
193 }