BUG 1179 - rpcResult = false but response 200
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestPostOperationTest.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.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;
17
18 import com.google.common.util.concurrent.Futures;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import java.net.URI;
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;
29 import java.util.Set;
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;
62
63 public class RestPostOperationTest extends JerseyTest {
64
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;
73
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;
80
81     private static MountService mountService;
82
83     @BeforeClass
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);
92
93         Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
94         schemaContext = TestUtils.loadSchemaContext(modules);
95
96         loadData();
97     }
98
99     @Override
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;
112     }
113
114     @Test
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));
120
121         mockInvokeRpc(null, true);
122         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
123
124         mockInvokeRpc(null, false);
125         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
126
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));
132
133         uri = "/operations/test-module:rpc-wrongtest";
134         assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
135     }
136
137     @Test
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));
143
144         mockCommitConfigurationDataPostMethod(null);
145         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
146
147         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
148         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
149     }
150
151     @Test
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));
157
158         mockCommitConfigurationDataPostMethod(null);
159         assertEquals(202, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
160
161         mockCommitConfigurationDataPostMethod(TransactionStatus.FAILED);
162         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
163
164         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
165     }
166
167     @Test
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(
173                 rpcResult).build();
174         when(
175                 brokerFacade.commitConfigurationDataPostBehindMountPoint(any(MountInstance.class),
176                         any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
177
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);
182
183         ControllerContext.getInstance().setMountService(mockMountService);
184
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));
189
190         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
191     }
192
193     private void mockInvokeRpc(CompositeNode result, boolean sucessful, Collection<RpcError> errors) {
194
195         DummyRpcResult.Builder<CompositeNode> builder = new DummyRpcResult.Builder<CompositeNode>().result(result)
196                 .isSuccessful(sucessful);
197         if (!errors.isEmpty()) {
198             builder.errors(errors);
199         }
200         RpcResult<CompositeNode> rpcResult = builder.build();
201         when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class)))
202                 .thenReturn(Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
203     }
204
205     private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
206         mockInvokeRpc(result, sucessful, Collections.<RpcError> emptyList());
207     }
208
209     private void mockCommitConfigurationDataPostMethod(TransactionStatus statusName) {
210         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
211                 .build();
212         Future<RpcResult<TransactionStatus>> dummyFuture = null;
213         if (statusName != null) {
214             dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(rpcResult).build();
215         } else {
216             dummyFuture = new DummyFuture.Builder<TransactionStatus>().build();
217         }
218
219         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
220                 .thenReturn(dummyFuture);
221     }
222
223     @Test
224     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
225         initMocking();
226         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
227                 TransactionStatus.COMMITED).build();
228         Future<RpcResult<TransactionStatus>> dummyFuture = new DummyFuture.Builder<TransactionStatus>().rpcResult(
229                 rpcResult).build();
230
231         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
232                 .thenReturn(dummyFuture);
233
234         ArgumentCaptor<InstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(InstanceIdentifier.class);
235         ArgumentCaptor<CompositeNode> compNodeCaptor = ArgumentCaptor.forClass(CompositeNode.class);
236
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());
242
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());
249     }
250
251     @Test
252     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
253         initMocking();
254
255         when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
256                 .thenReturn(null);
257
258         String URI_1 = "/config";
259         assertEquals(202, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
260
261         String URI_2 = "/config/test-interface:interfaces";
262         assertEquals(202, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
263     }
264
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);
274     }
275
276     private int post(String uri, String mediaType, String data) {
277         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
278     }
279
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")
288                 .getPath();
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")
293                 .getPath();
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);
300     }
301
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;
308     }
309 }