72b5b3d9f7e131cd2d59e506175317db174b14b6
[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.spy;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
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;
25 import java.util.Set;
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;
46
47 /**
48  * @See {@link InvokeRpcMethodTest}
49  *
50  */
51 public class RestconfImplTest {
52
53     private RestconfImpl restconfImpl = null;
54     private static ControllerContext controllerContext = null;
55
56     @BeforeClass
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);
63
64     }
65
66     @Before
67     public void initMethod() {
68         restconfImpl = RestconfImpl.getInstance();
69         restconfImpl.setControllerContext(controllerContext);
70     }
71
72     @SuppressWarnings("unchecked")
73     @Test
74     public void testExample() throws FileNotFoundException, ParseException {
75         @SuppressWarnings("rawtypes")
76         final
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));
82     }
83
84     @Test
85     public void testRpcForMountpoint() throws Exception {
86         final UriInfo uriInfo = mock(UriInfo.class);
87         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
88
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();
96
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));
105     }
106 }