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;
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.Futures;
22 import java.io.FileNotFoundException;
23 import java.text.ParseException;
25 import javax.ws.rs.core.MultivaluedHashMap;
26 import javax.ws.rs.core.UriInfo;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
31 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
32 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
33 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
34 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
35 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
36 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
37 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
38 import org.opendaylight.yangtools.yang.common.QName;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
44 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
45 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
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, ReactorException {
58 final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
60 final Set<Module> allModules = schemaContext.getModules();
61 assertNotNull(allModules);
63 controllerContext = spy(ControllerContext.getInstance());
64 controllerContext.setSchemas(schemaContext);
69 public void initMethod() {
70 this.restconfImpl = RestconfImpl.getInstance();
71 this.restconfImpl.setControllerContext(controllerContext);
74 @SuppressWarnings("unchecked")
76 public void testExample() throws FileNotFoundException, ParseException {
77 @SuppressWarnings("rawtypes")
79 NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
80 final BrokerFacade brokerFacade = mock(BrokerFacade.class);
81 when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
82 assertEquals(normalizedNodeData,
83 brokerFacade.readOperationalData(null));
87 public void testRpcForMountpoint() throws Exception {
88 final UriInfo uriInfo = mock(UriInfo.class);
89 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
91 final NormalizedNodeContext ctx = mock(NormalizedNodeContext.class);
92 final InstanceIdentifierContext iiCtx = mock(InstanceIdentifierContext.class);
93 doReturn(iiCtx).when(ctx).getInstanceIdentifierContext();
94 final SchemaNode schemaNode = mock(SchemaNode.class);
95 doReturn(schemaNode).when(iiCtx).getSchemaNode();
96 doReturn(mock(SchemaPath.class)).when(schemaNode).getPath();
97 doReturn(QName.create("namespace", "2010-10-10", "localname")).when(schemaNode).getQName();
99 final DOMMountPoint mount = mock(DOMMountPoint.class);
100 doReturn(mount).when(iiCtx).getMountPoint();
101 final DOMRpcService rpcService = mock(DOMRpcService.class);
102 doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
103 doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
104 this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
105 this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
106 verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));