2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyBoolean;
14 import static org.mockito.ArgumentMatchers.isNull;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Sets;
24 import java.io.FileNotFoundException;
26 import java.text.ParseException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.HashSet;
30 import java.util.List;
32 import java.util.Map.Entry;
33 import java.util.Optional;
35 import javax.ws.rs.core.MultivaluedHashMap;
36 import javax.ws.rs.core.MultivaluedMap;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
44 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
45 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
46 import org.opendaylight.mdsal.dom.api.DOMRpcService;
47 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
48 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
49 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
50 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
51 import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
52 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
53 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
54 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
55 import org.opendaylight.restconf.common.errors.RestconfError;
56 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
57 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
60 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
61 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
62 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
63 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
64 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
65 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
66 import org.opendaylight.yangtools.yang.model.api.Module;
67 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
68 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
69 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
70 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
71 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
74 * See {@link InvokeRpcMethodTest}.
76 public class RestconfImplTest {
78 private static SchemaContext schemaContext;
80 private final BrokerFacade brokerFacade = mock(BrokerFacade.class);
81 private final ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
82 private final RestconfImpl restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
85 public static void init() throws FileNotFoundException, ReactorException {
86 schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
87 final Set<Module> allModules = schemaContext.getModules();
88 assertNotNull(allModules);
92 public static void cleanUp() {
93 WebSocketServer.destroyInstance(); // NETCONF-604
97 public void binaryKeyTest() {
98 final List<Byte> al = new ArrayList<>();
100 binaryKeyTest(al, al);
103 private static void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {
105 final QName keyDef = QName.create("test:key:binary", "2017-08-14", "b1");
107 final Map<QName, Object> uriKeyValues = new HashMap<>();
108 uriKeyValues.put(keyDef, al.toArray());
110 final MapEntryNode payload = mock(MapEntryNode.class);
111 final NodeIdentifierWithPredicates nodeIdWithPred =
112 new NodeIdentifierWithPredicates(keyDef, keyDef, al2.toArray());
113 when(payload.getIdentifier()).thenReturn(nodeIdWithPred);
115 final List<QName> keyDefinitions = new ArrayList<>();
116 keyDefinitions.add(keyDef);
117 RestconfImpl.isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions);
121 public void binaryKeyFailTest() {
122 final List<Byte> al = new ArrayList<>();
124 final List<Byte> al2 = new ArrayList<>();
126 binaryKeyTest(al, al2);
127 } catch (final RestconfDocumentedException e) {
128 final RestconfError err = e.getErrors().iterator().next();
129 assertEquals(ErrorType.PROTOCOL, err.getErrorType());
130 assertEquals(ErrorTag.INVALID_VALUE, err.getErrorTag());
134 @SuppressWarnings("unchecked")
136 public void testExample() throws FileNotFoundException, ParseException {
137 @SuppressWarnings("rawtypes")
138 final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
139 when(brokerFacade.readOperationalData(isNull())).thenReturn(normalizedNodeData);
140 assertEquals(normalizedNodeData,
141 brokerFacade.readOperationalData(null));
145 public void testRpcForMountpoint() throws Exception {
146 final UriInfo uriInfo = mock(UriInfo.class);
147 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
149 final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
150 final InstanceIdentifierContext<?> iiCtx = mock(InstanceIdentifierContext.class);
151 doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
152 final SchemaNode schemaNode = mock(SchemaNode.class);
153 doReturn(schemaNode).when(iiCtx).getSchemaNode();
154 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
155 doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
157 final DOMMountPoint mount = mock(DOMMountPoint.class);
158 doReturn(mount).when(iiCtx).getMountPoint();
159 final DOMRpcService rpcService = mock(DOMRpcService.class);
160 doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
161 doReturn(immediateFluentFuture(mock(DOMRpcResult.class))).when(rpcService)
162 .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
163 this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
164 this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
165 verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), isNull());
169 * Create notification stream for toaster module.
172 public void createNotificationStreamTest() {
173 final NormalizedNodeContext payload = mock(NormalizedNodeContext.class);
174 final InstanceIdentifierContext<?> iiCtx = mock(InstanceIdentifierContext.class);
175 doReturn(iiCtx).when(payload).getInstanceIdentifierContext();
177 final SchemaNode schemaNode = mock(SchemaNode.class,
178 Mockito.withSettings().extraInterfaces(RpcDefinition.class));
179 doReturn(schemaNode).when(iiCtx).getSchemaNode();
180 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
182 doReturn(QName.create("urn:opendaylight:params:xml:ns:yang:controller:md:sal:remote",
183 "2014-01-14", "create-notification-stream")).when(schemaNode).getQName();
184 doReturn(null).when(iiCtx).getMountPoint();
186 final Set<DataContainerChild<?, ?>> children = new HashSet<>();
187 final DataContainerChild<?, ?> child = mock(DataContainerChild.class,
188 Mockito.withSettings().extraInterfaces(LeafSetNode.class));
190 final LeafSetEntryNode entryNode = mock(LeafSetEntryNode.class);
191 when(entryNode.getValue()).thenReturn("(http://netconfcentral.org/ns/toaster?revision=2009-11-20)toastDone");
192 when(((LeafSetNode) child).getValue()).thenReturn(Sets.newHashSet(entryNode));
195 final NormalizedNode<?, ?> normalizedNode = mock(NormalizedNode.class,
196 Mockito.withSettings().extraInterfaces(ContainerNode.class));
197 doReturn(normalizedNode).when(payload).getData();
198 doReturn(children).when(normalizedNode).getValue();
200 // register notification
201 final NormalizedNodeContext context = this.restconfImpl
202 .invokeRpc("sal-remote:create-notification-stream", payload, null);
203 assertNotNull(context);
207 * Subscribe for notification stream of toaster module.
210 public void subscribeToNotificationStreamTest() throws Exception {
211 final String identifier = "create-notification-stream/toaster:toastDone";
213 // register test notification stream
214 final SchemaPath path = SchemaPath.create(
215 true, QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toastDone"));
216 Notificator.createNotificationListener(Lists.newArrayList(path), identifier, "XML", controllerContext);
218 final UriInfo uriInfo = mock(UriInfo.class);
219 final UriBuilder uriBuilder = mock(UriBuilder.class);
220 when(uriBuilder.port(8181)).thenReturn(uriBuilder);
221 when(uriBuilder.replacePath(identifier)).thenReturn(uriBuilder);
222 when(uriBuilder.build()).thenReturn(new URI(""));
223 when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
224 when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
225 final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
226 final Set<Entry<String, List<String>>> set = new HashSet<>();
227 when(map.entrySet()).thenReturn(set);
228 when(uriInfo.getQueryParameters()).thenReturn(map);
230 // subscribe to stream and verify response
231 final NormalizedNodeContext response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
233 // remove test notification stream
234 Notificator.removeAllListeners();