Merge "BUG 1082 Migrate sal-rest-connector to Async Data Broker API"
[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.doReturn;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
19
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.CheckedFuture;
22 import com.google.common.util.concurrent.Futures;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.UnsupportedEncodingException;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.text.ParseException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Set;
34 import javax.ws.rs.client.Entity;
35 import javax.ws.rs.core.Application;
36 import javax.ws.rs.core.MediaType;
37 import org.glassfish.jersey.server.ResourceConfig;
38 import org.glassfish.jersey.test.JerseyTest;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.mockito.ArgumentCaptor;
42 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
43 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
44 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
45 import org.opendaylight.controller.sal.rest.api.Draft02;
46 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
47 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
48 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
49 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
50 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
51 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
52 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
53 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
54 import org.opendaylight.controller.sal.restconf.impl.RestconfDocumentedException;
55 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
56 import org.opendaylight.yangtools.yang.common.QName;
57 import org.opendaylight.yangtools.yang.common.RpcError;
58 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
59 import org.opendaylight.yangtools.yang.common.RpcResult;
60 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
61 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
62 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
63 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
64 import org.opendaylight.yangtools.yang.model.api.Module;
65 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
66
67 public class RestPostOperationTest extends JerseyTest {
68
69     private static String xmlDataAbsolutePath;
70     private static String xmlDataInterfaceAbsolutePath;
71     private static String xmlDataRpcInput;
72     private static String xmlBlockData;
73     private static String xmlTestInterface;
74     private static CompositeNodeWrapper cnSnDataOutput;
75     private static String xmlData3;
76     private static String xmlData4;
77
78     private static ControllerContext controllerContext;
79     private static BrokerFacade brokerFacade;
80     private static RestconfImpl restconfImpl;
81     private static SchemaContext schemaContextYangsIetf;
82     private static SchemaContext schemaContextTestModule;
83     private static SchemaContext schemaContext;
84
85     private static DOMMountPointService mountService;
86
87     @BeforeClass
88     public static void init() throws URISyntaxException, IOException {
89         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
90         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
91         controllerContext = ControllerContext.getInstance();
92         brokerFacade = mock(BrokerFacade.class);
93         restconfImpl = RestconfImpl.getInstance();
94         restconfImpl.setBroker(brokerFacade);
95         restconfImpl.setControllerContext(controllerContext);
96
97         Set<Module> modules = TestUtils.loadModulesFrom("/test-config-data/yang1");
98         schemaContext = TestUtils.loadSchemaContext(modules);
99
100         loadData();
101     }
102
103     @Override
104     protected Application configure() {
105         /* enable/disable Jersey logs to console */
106         // enable(TestProperties.LOG_TRAFFIC);
107         // enable(TestProperties.DUMP_ENTITY);
108         // enable(TestProperties.RECORD_LOG_LEVEL);
109         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
110         ResourceConfig resourceConfig = new ResourceConfig();
111         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
112                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
113                 JsonToCompositeNodeProvider.INSTANCE);
114         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
115         return resourceConfig;
116     }
117
118     @Test
119     public void postOperationsStatusCodes() throws IOException {
120         controllerContext.setSchemas(schemaContextTestModule);
121         mockInvokeRpc(cnSnDataOutput, true);
122         String uri = "/operations/test-module:rpc-test";
123         assertEquals(200, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
124
125         mockInvokeRpc(null, true);
126         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
127
128         mockInvokeRpc(null, false);
129         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
130
131         List<RpcError> rpcErrors = new ArrayList<>();
132         rpcErrors.add(RpcResultBuilder.newError(ErrorType.RPC, "tag1", "message1", "applicationTag1", "info1", null));
133         rpcErrors.add(RpcResultBuilder.newWarning(ErrorType.PROTOCOL, "tag2", "message2", "applicationTag2", "info2",
134                 null));
135         mockInvokeRpc(null, false, rpcErrors);
136         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
137
138         uri = "/operations/test-module:rpc-wrongtest";
139         assertEquals(400, post(uri, MediaType.APPLICATION_XML, xmlDataRpcInput));
140     }
141
142     @Test
143     public void postConfigOnlyStatusCodes() throws UnsupportedEncodingException {
144         controllerContext.setSchemas(schemaContextYangsIetf);
145         String uri = "/config";
146         mockCommitConfigurationDataPostMethod(true);
147         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
148
149         mockCommitConfigurationDataPostMethod(false);
150         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataAbsolutePath));
151
152         assertEquals(400, post(uri, MediaType.APPLICATION_XML, ""));
153     }
154
155     @Test
156     public void postConfigStatusCodes() throws UnsupportedEncodingException {
157         controllerContext.setSchemas(schemaContextYangsIetf);
158         String uri = "/config/ietf-interfaces:interfaces";
159
160         mockCommitConfigurationDataPostMethod(true);
161         assertEquals(204, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
162
163         mockCommitConfigurationDataPostMethod(false);
164         assertEquals(500, post(uri, MediaType.APPLICATION_XML, xmlDataInterfaceAbsolutePath));
165
166         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
167     }
168
169     @Test
170     public void postDataViaUrlMountPoint() throws UnsupportedEncodingException {
171         controllerContext.setSchemas(schemaContextYangsIetf);
172         when(
173                 brokerFacade.commitConfigurationDataPost(any(DOMMountPoint.class), any(YangInstanceIdentifier.class),
174                         any(NormalizedNode.class))).thenReturn(mock(CheckedFuture.class));
175
176         DOMMountPoint mountInstance = mock(DOMMountPoint.class);
177         when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
178         DOMMountPointService mockMountService = mock(DOMMountPointService.class);
179         when(mockMountService.getMountPoint(any(YangInstanceIdentifier.class))).thenReturn(Optional.of(mountInstance));
180
181         ControllerContext.getInstance().setMountService(mockMountService);
182
183         String uri = "/config/ietf-interfaces:interfaces/interface/0/";
184         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData4));
185         uri = "/config/ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont";
186         assertEquals(204, post(uri, Draft02.MediaTypes.DATA + XML, xmlData3));
187
188         assertEquals(400, post(uri, MediaType.APPLICATION_JSON, ""));
189     }
190
191     private void mockInvokeRpc(CompositeNode result, boolean sucessful, Collection<RpcError> errors) {
192
193         DummyRpcResult.Builder<CompositeNode> builder = new DummyRpcResult.Builder<CompositeNode>().result(result)
194                 .isSuccessful(sucessful);
195         if (!errors.isEmpty()) {
196             builder.errors(errors);
197         }
198         RpcResult<CompositeNode> rpcResult = builder.build();
199         when(brokerFacade.invokeRpc(any(QName.class), any(CompositeNode.class))).thenReturn(
200                 Futures.<RpcResult<CompositeNode>> immediateFuture(rpcResult));
201     }
202
203     private void mockInvokeRpc(CompositeNode result, boolean sucessful) {
204         mockInvokeRpc(result, sucessful, Collections.<RpcError> emptyList());
205     }
206
207     private void mockCommitConfigurationDataPostMethod(final boolean succesfulComit) {
208         if (succesfulComit) {
209             doReturn(mock(CheckedFuture.class)).when(brokerFacade).commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
210         } else {
211             doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataPost(
212                     any(YangInstanceIdentifier.class), any(NormalizedNode.class));
213         }
214     }
215
216     @Test
217     public void createConfigurationDataTest() throws UnsupportedEncodingException, ParseException {
218         initMocking();
219         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
220                 TransactionStatus.COMMITED).build();
221
222         when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
223                 .thenReturn(mock(CheckedFuture.class));
224
225         ArgumentCaptor<YangInstanceIdentifier> instanceIdCaptor = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
226         ArgumentCaptor<NormalizedNode> compNodeCaptor = ArgumentCaptor.forClass(NormalizedNode.class);
227
228         String URI_1 = "/config";
229         assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
230         verify(brokerFacade).commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
231         String identifier = "[(urn:ietf:params:xml:ns:yang:test-interface?revision=2014-07-01)interfaces]";
232         assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
233
234         String URI_2 = "/config/test-interface:interfaces";
235         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
236         verify(brokerFacade, times(2))
237                 .commitConfigurationDataPost(instanceIdCaptor.capture(), compNodeCaptor.capture());
238         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]";
239         assertEquals(identifier, instanceIdCaptor.getValue().getPath().toString());
240     }
241
242     @Test
243     public void createConfigurationDataNullTest() throws UnsupportedEncodingException {
244         initMocking();
245
246         when(brokerFacade.commitConfigurationDataPost(any(YangInstanceIdentifier.class), any(NormalizedNode.class)))
247                 .thenReturn(null);
248
249         String URI_1 = "/config";
250         assertEquals(204, post(URI_1, Draft02.MediaTypes.DATA + XML, xmlTestInterface));
251
252         String URI_2 = "/config/test-interface:interfaces";
253         assertEquals(204, post(URI_2, Draft02.MediaTypes.DATA + XML, xmlBlockData));
254     }
255
256     private static void initMocking() {
257         controllerContext = ControllerContext.getInstance();
258         controllerContext.setSchemas(schemaContext);
259         mountService = mock(DOMMountPointService.class);
260         controllerContext.setMountService(mountService);
261         brokerFacade = mock(BrokerFacade.class);
262         restconfImpl = RestconfImpl.getInstance();
263         restconfImpl.setBroker(brokerFacade);
264         restconfImpl.setControllerContext(controllerContext);
265     }
266
267     private int post(String uri, String mediaType, String data) {
268         return target(uri).request(mediaType).post(Entity.entity(data, mediaType)).getStatus();
269     }
270
271     private static void loadData() throws IOException, URISyntaxException {
272         InputStream xmlStream = RestconfImplTest.class
273                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_absolute_path.xml");
274         xmlDataAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
275         xmlStream = RestconfImplTest.class
276                 .getResourceAsStream("/parts/ietf-interfaces_interfaces_interface_absolute_path.xml");
277         xmlDataInterfaceAbsolutePath = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
278         String xmlPathRpcInput = RestconfImplTest.class.getResource("/full-versions/test-data2/data-rpc-input.xml")
279                 .getPath();
280         xmlDataRpcInput = TestUtils.loadTextFile(xmlPathRpcInput);
281         String xmlPathBlockData = RestconfImplTest.class.getResource("/test-config-data/xml/block-data.xml").getPath();
282         xmlBlockData = TestUtils.loadTextFile(xmlPathBlockData);
283         String xmlPathTestInterface = RestconfImplTest.class.getResource("/test-config-data/xml/test-interface.xml")
284                 .getPath();
285         xmlTestInterface = TestUtils.loadTextFile(xmlPathTestInterface);
286         cnSnDataOutput = prepareCnSnRpcOutput();
287         String data3Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data3.xml").getPath();
288         xmlData3 = TestUtils.loadTextFile(data3Input);
289         String data4Input = RestconfImplTest.class.getResource("/full-versions/test-data2/data7.xml").getPath();
290         xmlData4 = TestUtils.loadTextFile(data4Input);
291     }
292
293     private static CompositeNodeWrapper prepareCnSnRpcOutput() throws URISyntaxException {
294         CompositeNodeWrapper cnSnDataOutput = new CompositeNodeWrapper(new URI("test:module"), "output");
295         CompositeNodeWrapper cont = new CompositeNodeWrapper(new URI("test:module"), "cont-output");
296         cnSnDataOutput.addValue(cont);
297         cnSnDataOutput.unwrap();
298         return cnSnDataOutput;
299     }
300 }