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 com.google.common.util.concurrent.Futures;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
23 import java.net.URISyntaxException;
24 import java.text.ParseException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Collections;
28 import java.util.List;
30 import java.util.concurrent.Future;
31 import javax.ws.rs.client.Entity;
32 import javax.ws.rs.core.Application;
33 import javax.ws.rs.core.MediaType;
34 import org.glassfish.jersey.server.ResourceConfig;
35 import org.glassfish.jersey.test.JerseyTest;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
40 import org.opendaylight.controller.sal.common.util.RpcErrors;
41 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
42 import org.opendaylight.controller.sal.core.api.mount.MountService;
43 import org.opendaylight.controller.sal.rest.api.Draft02;
44 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
45 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
46 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
47 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
48 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
49 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
50 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
51 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
52 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
53 import org.opendaylight.yangtools.yang.common.QName;
54 import org.opendaylight.yangtools.yang.common.RpcError;
55 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
56 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
59 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
60 import org.opendaylight.yangtools.yang.model.api.Module;
61 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
63 public class RestPostOperationTest extends JerseyTest {
65 private static String xmlDataAbsolutePath;
66 private static String xmlDataInterfaceAbsolutePath;
67 private static String xmlDataRpcInput;
68 private static String xmlBlockData;
69 private static String xmlTestInterface;
70 private static CompositeNodeWrapper cnSnDataOutput;
71 private static String xmlData3;
72 private static String xmlData4;
74 private static ControllerContext controllerContext;
75 private static BrokerFacade brokerFacade;
76 private static RestconfImpl restconfImpl;
77 private static SchemaContext schemaContextYangsIetf;
78 private static SchemaContext schemaContextTestModule;
79 private static SchemaContext schemaContext;
81 private static MountService mountService;
84 public static void init() throws URISyntaxException, IOException {
85 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
86 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
87 controllerContext = ControllerContext.getInstance();
88 brokerFacade = mock(BrokerFacade.class);
89 restconfImpl = RestconfImpl.getInstance();
90 restconfImpl.setBroker(brokerFacade);
91 restconfImpl.setControllerContext(controllerContext);
93 Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
94 schemaContext = TestUtils.loadSchemaContext(modules);
100 protected Application configure() {
101 /* enable/disable Jersey logs to console */
102 // enable(TestProperties.LOG_TRAFFIC);
103 // enable(TestProperties.DUMP_ENTITY);
104 // enable(TestProperties.RECORD_LOG_LEVEL);
105 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
106 ResourceConfig resourceConfig = new ResourceConfig();
107 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
108 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
109 JsonToCompositeNodeProvider.INSTANCE);
110 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
111 return resourceConfig;
115 public void postOperationsStatusCodes() throws IOException {
116 controllerContext.setSchemas(schemaContextTestModule);
117 mockInvokeRpc(cnSnDataOutput, true);
118 String uri = "/operations/test-module:rpc-test";
119 assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
121 mockInvokeRpc(null, true);
122 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
124 mockInvokeRpc(null, false);
125 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
127 List<RpcError> rpcErrors = new ArrayList<>();
128 rpcErrors.add( RpcErrors.getRpcError("applicationTag1", "tag1", "info1", ErrorSeverity.ERROR, "message1", ErrorType.RPC, null));
129 rpcErrors.add( RpcErrors.getRpcError("applicationTag2", "tag2", "info2", ErrorSeverity.WARNING, "message2", ErrorType.PROTOCOL, null));
130 mockInvokeRpc(null, false, rpcErrors);
131 assertEquals(500,post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
133 uri = "/operations/test-module:rpc-wrongtest";
134 assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
138 public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
139 controllerContext.setSchemas(schemaContextYangsIetf);
140 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
141 String uri = "/config";
142 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
144 mockCommitConfigurationDataPostMethod(null);
145 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
147 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
148 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
152 public void postConfigStatusCodes() throws UnsupportedEncodingException {
153 controllerContext.setSchemas(schemaContextYangsIetf);
154 mockCommitConfigurationDataPostMethod(TransactionStatus.COMMITED);
155 String uri = "/config/ietf-interfaces:interfaces";
156 assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
158 mockCommitConfigurationDataPostMethod(null);
159 assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
161 mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
162 assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
164 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
168 public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
169 controllerContext.setSchemas(schemaContextYangsIetf);
170 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
171 TransactionStatus.COMMITED).build();
172 Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
175 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
176 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
178 MountInstance mountInstance = mock(MountInstance.class);
179 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
180 MountService mockMountService = mock(MountService.class);
181 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
183 ControllerContext.getInstance().setMountService(mockMountService);
185 String uri = "/config/ietf-interfaces:interfaces/interface/0/";
186 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
187 uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
188 assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
190 assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
193 private void mockInvokeRpc(CompositeNode result, boolean sucessful, Collection<RpcError> errors) {
195 DummyRpcResult.Builder<CompositeNode> builder = new DummyRpcResult.Builder<CompositeNode>().result(result)
196 .isSuccessful(sucessful);
197 if (!errors.isEmpty()) {
198 builder.errors(errors);
200 RpcResult<CompositeNode> rpcResult = builder.build();
201 when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class)))
202 .thenReturn(Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
205 private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
206 mockInvokeRpc(result, sucessful, Collections.<RpcError> emptyList());
209 private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
210 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
212 Future<RpcResult<TransactionStatus>> dummyFuture = null;
213 if (statusName != null) {
214 dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(rpcResult).build();
216 dummyFuture = new DummyFuture.Builder<TransactionStatus>().build();
219 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
220 .thenReturn(dummyFuture);
224 public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
226 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
227 TransactionStatus.COMMITED).build();
228 Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
231 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
232 .thenReturn(dummyFuture);
234 ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
235 ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
237 String URI_1 = "/config";
238 assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
239 verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
240 String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
241 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
243 String URI_2 = "/config/test-interface:interfaces";
244 assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
245 verify(brokerFacade, times(2))
246 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
247 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]";
248 assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
252 public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
255 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
258 String URI_1 = "/config";
259 assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
261 String URI_2 = "/config/test-interface:interfaces";
262 assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
265 private static void initMocking() {
266 controllerContext = ControllerContext.getInstance();
267 controllerContext.setSchemas(schemaContext);
268 mountService = mock(MountService.class);
269 controllerContext.setMountService(mountService);
270 brokerFacade = mock(BrokerFacade.class);
271 restconfImpl = RestconfImpl.getInstance();
272 restconfImpl.setBroker(brokerFacade);
273 restconfImpl.setControllerContext(controllerContext);
276 private int post(String uri, String mediaType, String data) {
277 return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
280 private static void loadData() throws IOException, URISyntaxException {
281 InputStream xmlStream = RestconfImplTest.class
282 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
283 xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
284 xmlStream = RestconfImplTest.class
285 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
286 xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
287 String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
289 xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
290 String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
291 xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
292 String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
294 xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
295 cnSnDataOutput = prepareCnSnRpcOutput();
296 String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
297 xmlData3 = TestUtils.loadTextFile(data3Input);
298 String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
299 xmlData4 = TestUtils.loadTextFile(data4Input);
302 private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
303 CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
304 CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
305 cnSnDataOutput.addValue(cont);
306 cnSnDataOutput.unwrap();
307 return cnSnDataOutput;