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.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.spy;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
21 import com.google.common.base.Optional;
22 import com.google.common.util.concurrent.Futures;
23 import java.io.FileNotFoundException;
24 import java.text.ParseException;
26 import javax.ws.rs.core.MultivaluedHashMap;
27 import javax.ws.rs.core.UriInfo;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
32 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
33 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
34 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
35 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
36 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
37 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
38 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
39 import org.opendaylight.yangtools.yang.common.QName;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
44 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
45 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
48 * @See {@link InvokeRpcMethodTest}
51 public class RestconfImplTest {
53 private RestconfImpl restconfImpl = null;
54 private static ControllerContext controllerContext = null;
57 public static void init() throws FileNotFoundException {
58 final Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
59 assertNotNull(allModules);
60 final SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
61 controllerContext = spy(ControllerContext.getInstance());
62 controllerContext.setSchemas(schemaContext);
67 public void initMethod() {
68 restconfImpl = RestconfImpl.getInstance();
69 restconfImpl.setControllerContext(controllerContext);
72 @SuppressWarnings("unchecked")
74 public void testExample() throws FileNotFoundException, ParseException {
75 @SuppressWarnings("rawtypes")
77 NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
78 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
79 when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
80 assertEquals(normalizedNodeData,
81 brokerFacade.readOperationalData(null));
85 public void testRpcForMountpoint() throws Exception {
86 final UriInfo uriInfo = mock(UriInfo.class);
87 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
89 final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
90 final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
91 doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
92 final SchemaNode schemaNode = mock(SchemaNode.class);
93 doReturn(schemaNode).when(iiCtx).getSchemaNode();
94 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
95 doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
97 final DOMMountPoint mount = mock(DOMMountPoint.class);
98 doReturn(mount).when(iiCtx).getMountPoint();
99 final DOMRpcService rpcService = mock(DOMRpcService.class);
100 doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
101 doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
102 restconfImpl.invokeRpc("randomId", ctx, uriInfo);
103 restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
104 verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));