1c57effe4590a7bd2760907a100d255b687faf82
[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 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.Futures;
22 import java.io.FileNotFoundException;
23 import java.text.ParseException;
24 import java.util.Set;
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;
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, ReactorException {
58         final SchemaContext schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
59
60         final Set<Module> allModules = schemaContext.getModules();
61         assertNotNull(allModules);
62
63         controllerContext = spy(ControllerContext.getInstance());
64         controllerContext.setSchemas(schemaContext);
65
66     }
67
68     @Before
69     public void initMethod() {
70         this.restconfImpl = RestconfImpl.getInstance();
71         this.restconfImpl.setControllerContext(controllerContext);
72     }
73
74     @SuppressWarnings("unchecked")
75     @Test
76     public void testExample() throws FileNotFoundException, ParseException {
77         @SuppressWarnings("rawtypes")
78         final
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));
84     }
85
86     @Test
87     public void testRpcForMountpoint() throws Exception {
88         final UriInfo uriInfo = mock(UriInfo.class);
89         doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters(anyBoolean());
90
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();
98
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));
107     }
108 }