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.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.UnsupportedEncodingException;
22 import java.net.URISyntaxException;
23 import java.text.ParseException;
25 import java.util.concurrent.Future;
27 import javax.ws.rs.client.Entity;
28 import javax.ws.rs.core.Application;
29 import javax.ws.rs.core.MediaType;
31 import org.glassfish.jersey.server.ResourceConfig;
32 import org.glassfish.jersey.test.JerseyTest;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.mockito.ArgumentCaptor;
36 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
37 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
38 import org.opendaylight.controller.sal.core.api.mount.MountService;
39 import org.opendaylight.controller.sal.rest.api.Draft02;
40 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
41 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
42 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
43 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
44 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
45 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
46 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
47 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
48 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
49 import org.opendaylight.yangtools.yang.common.QName;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
52 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
53 import org.opendaylight.yangtools.yang.model.api.Module;
54 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import com.google.common.util.concurrent.Futures;
58 public class RestPostOperationTest extends JerseyTest {
60 private static String xmlDataAbsolutePath;
61 private static String xmlDataInterfaceAbsolutePath;
62 private static String xmlDataRpcInput;
63 private static String xmlBlockData;
64 private static String xmlTestInterface;
65 private static CompositeNodeWrapper cnSnDataOutput;
66 private static String xmlData3;
67 private static String xmlData4;
69 private static ControllerContext controllerContext;
70 private static BrokerFacade brokerFacade;
71 private static RestconfImpl restconfImpl;
72 private static SchemaContext schemaContextYangsIetf;
73 private static SchemaContext schemaContextTestModule;
74 private static SchemaContext schemaContext;
76 private static MountService mountService;
79 public static void init() throws URISyntaxException, IOException {
80 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
81 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
82 controllerContext = ControllerContext.getInstance();
83 brokerFacade = mock(BrokerFacade.class);
84 restconfImpl = RestconfImpl.getInstance();
85 restconfImpl.setBroker(brokerFacade);
86 restconfImpl.setControllerContext(controllerContext);
88 Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
89 schemaContext = TestUtils.loadSchemaContext(modules);
95 protected Application configure() {
96 /* enable/disable Jersey logs to console */
97 // enable(TestProperties.LOG_TRAFFIC);
98 // enable(TestProperties.DUMP_ENTITY);
99 // enable(TestProperties.RECORD_LOG_LEVEL);
100 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
101 ResourceConfig resourceConfig = new ResourceConfig();
102 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
103 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
104 JsonToCompositeNodeProvider.INSTANCE);
105 resourceConfig.registerClasses( RestconfDocumentedExceptionMapper.class );
106 return resourceConfig;
110 public void postOperationsStatusCodes() throws UnsupportedEncodingException {
111 controllerContext.setSchemas(schemaContextTestModule);
112 mockInvokeRpc(cnSnDataOutput, true);
113 String uri = "/operations/test-module:rpc-test";
114 assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
116 mockInvokeRpc(null, true);
117 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
119 mockInvokeRpc(null, false);
120 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
122 uri = "/operations/test-module:rpc-wrongtest";
123 assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
127 public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
128 controllerContext.setSchemas(schemaContextYangsIetf);
129 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
130 String uri = "/config";
131 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
133 mockCommitConfigurationDataPostMethod(null);
134 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
136 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
137 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
141 public void postConfigStatusCodes() throws UnsupportedEncodingException {
142 controllerContext.setSchemas(schemaContextYangsIetf);
143 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
144 String uri = "/config/ietf-interfaces:interfaces";
145 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
147 mockCommitConfigurationDataPostMethod(null);
148 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
150 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
151 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
155 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
156 controllerContext.setSchemas(schemaContextYangsIetf);
157 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
158 TransactionStatus.COMMITED).build();
159 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
161 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
162 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
164 MountInstance mountInstance = mock(MountInstance.class);
165 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
166 MountService mockMountService = mock(MountService.class);
167 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
169 ControllerContext.getInstance().setMountService(mockMountService);
171 String uri = "/config/ietf-interfaces:interfaces/interface/0/";
172 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
173 uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
174 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
177 private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
178 RpcResult<CompositeNode> rpcResult = new DummyRpcResult.Builder<CompositeNode>().result(result)
179 .isSuccessful(sucessful).build();
180 when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class)))
181 .thenReturn(Futures.<RpcResult<CompositeNode>>immediateFuture( rpcResult ));
184 private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
185 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
187 Future<RpcResult<TransactionStatus>> dummyFuture = null;
188 if (statusName != null) {
189 dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
191 dummyFuture = DummyFuture.builder().build();
194 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
195 .thenReturn(dummyFuture);
199 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
201 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
202 TransactionStatus.COMMITED).build();
203 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
205 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
206 .thenReturn(dummyFuture);
208 ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
209 ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
211 String URI_1 = "/config";
212 assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
213 verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
214 String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
215 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
217 String URI_2 = "/config/test-interface:interfaces";
218 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
219 verify(brokerFacade, times(2))
220 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
221 identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces, (urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)block]";
222 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
226 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
229 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
232 String URI_1 = "/config";
233 assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
235 String URI_2 = "/config/test-interface:interfaces";
236 assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
239 private static void initMocking() {
240 controllerContext = ControllerContext.getInstance();
241 controllerContext.setSchemas(schemaContext);
242 mountService = mock(MountService.class);
243 controllerContext.setMountService(mountService);
244 brokerFacade = mock(BrokerFacade.class);
245 restconfImpl = RestconfImpl.getInstance();
246 restconfImpl.setBroker(brokerFacade);
247 restconfImpl.setControllerContext(controllerContext);
250 private int post(String uri, String mediaType, String data) {
251 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
254 private static void loadData() throws IOException, URISyntaxException {
255 InputStream xmlStream = RestconfImplTest.class
256 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
257 xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
258 xmlStream = RestconfImplTest.class
259 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
260 xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
261 String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
263 xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
264 String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
265 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
266 String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
268 xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
269 cnSnDataOutput = prepareCnSnRpcOutput();
270 String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
271 xmlData3 = TestUtils.loadTextFile(data3Input);
272 String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
273 xmlData4 = TestUtils.loadTextFile(data4Input);
276 private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
277 CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
278 CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
279 cnSnDataOutput.addValue(cont);
280 cnSnDataOutput.unwrap();
281 return cnSnDataOutput;