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