BUG 2412 - restconf @POST createConfigurationData(payload) method migration
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / MediaTypesTest.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.mockito.Matchers.any;
11 import static org.mockito.Matchers.eq;
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.JSON;
17 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;
21 import javax.ws.rs.client.Entity;
22 import javax.ws.rs.core.Application;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.UriInfo;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.glassfish.jersey.test.JerseyTest;
27 import org.junit.BeforeClass;
28 import org.junit.Ignore;
29 import org.junit.Test;
30 import org.opendaylight.controller.sal.rest.api.Draft02;
31 import org.opendaylight.controller.sal.rest.api.RestconfService;
32 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
33 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
34 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter;
35 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeXmlBodyWriter;
36 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
37 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
38 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
39 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
40 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
41 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
42
43 public class MediaTypesTest extends JerseyTest {
44
45     private static RestconfService restconfService;
46     private static String jsonData;
47     private static String xmlData;
48
49     @BeforeClass
50     public static void init() throws IOException {
51         restconfService = mock(RestconfService.class);
52         final String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
53         jsonData = TestUtils.loadTextFile(jsonPath);
54         final InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
55         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
56     }
57
58     @Override
59     protected Application configure() {
60         /* enable/disable Jersey logs to console */
61         // enable(TestProperties.LOG_TRAFFIC);
62         // enable(TestProperties.DUMP_ENTITY);
63         // enable(TestProperties.RECORD_LOG_LEVEL);
64         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
65         ResourceConfig resourceConfig = new ResourceConfig();
66         resourceConfig = resourceConfig.registerInstances(restconfService, StructuredDataToXmlProvider.INSTANCE,
67                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
68                 JsonToCompositeNodeProvider.INSTANCE, new NormalizedNodeJsonBodyWriter(), new NormalizedNodeXmlBodyWriter(),
69                 new JsonNormalizedNodeBodyReader(), new XmlNormalizedNodeBodyReader());
70         return resourceConfig;
71     }
72
73     @Test
74     public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
75         final String uriPrefix = "/operations/";
76         final String uriPath = "ietf-interfaces:interfaces";
77         final String uri = uriPrefix + uriPath;
78         when(restconfService.invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class))).thenReturn(null);
79         post(uri, Draft02.MediaTypes.OPERATION + JSON, Draft02.MediaTypes.OPERATION + JSON, jsonData);
80         verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
81         post(uri, Draft02.MediaTypes.OPERATION + XML, Draft02.MediaTypes.OPERATION + XML, xmlData);
82         verify(restconfService, times(2)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
83         post(uri, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, jsonData);
84         verify(restconfService, times(3)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
85         post(uri, MediaType.APPLICATION_XML, MediaType.APPLICATION_XML, xmlData);
86         verify(restconfService, times(4)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
87         post(uri, MediaType.TEXT_XML, MediaType.TEXT_XML, xmlData);
88         verify(restconfService, times(5)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
89         post(uri, null, MediaType.TEXT_XML, xmlData);
90         verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
91
92         // negative tests
93         post(uri, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, xmlData);
94         verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
95         post(uri, MediaType.TEXT_XML, MediaType.TEXT_PLAIN, xmlData);
96         verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
97     }
98
99     @Test
100     public void testGetConfigMediaTypes() throws UnsupportedEncodingException {
101         final String uriPrefix = "/config/";
102         final String uriPath = "ietf-interfaces:interfaces";
103         final String uri = uriPrefix + uriPath;
104         when(restconfService.readConfigurationData(eq(uriPath), any(UriInfo.class))).thenReturn(null);
105         get(uri, Draft02.MediaTypes.DATA + JSON);
106         verify(restconfService, times(1)).readConfigurationData(eq(uriPath), any(UriInfo.class));
107         get(uri, Draft02.MediaTypes.DATA + XML);
108         verify(restconfService, times(2)).readConfigurationData(eq(uriPath), any(UriInfo.class));
109         get(uri, MediaType.APPLICATION_JSON);
110         verify(restconfService, times(3)).readConfigurationData(eq(uriPath), any(UriInfo.class));
111         get(uri, MediaType.APPLICATION_XML);
112         verify(restconfService, times(4)).readConfigurationData(eq(uriPath), any(UriInfo.class));
113         get(uri, MediaType.TEXT_XML);
114         verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
115
116         // negative tests
117         get(uri, MediaType.TEXT_PLAIN);
118         verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
119     }
120
121     @Test
122     public void testGetOperationalMediaTypes() throws UnsupportedEncodingException {
123         final String uriPrefix = "/operational/";
124         final String uriPath = "ietf-interfaces:interfaces";
125         final String uri = uriPrefix + uriPath;
126         when(restconfService.readOperationalData(eq(uriPath), any(UriInfo.class))).thenReturn(null);
127         get(uri, Draft02.MediaTypes.DATA + JSON);
128         verify(restconfService, times(1)).readOperationalData(eq(uriPath), any(UriInfo.class));
129         get(uri, Draft02.MediaTypes.DATA + XML);
130         verify(restconfService, times(2)).readOperationalData(eq(uriPath), any(UriInfo.class));
131         get(uri, MediaType.APPLICATION_JSON);
132         verify(restconfService, times(3)).readOperationalData(eq(uriPath), any(UriInfo.class));
133         get(uri, MediaType.APPLICATION_XML);
134         verify(restconfService, times(4)).readOperationalData(eq(uriPath), any(UriInfo.class));
135         get(uri, MediaType.TEXT_XML);
136         verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
137
138         // negative tests
139         get(uri, MediaType.TEXT_PLAIN);
140         verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
141     }
142
143     @Test
144     @Ignore
145     public void testPutConfigMediaTypes() throws UnsupportedEncodingException {
146         final String uriPrefix = "/config/";
147         final String uriPath = "ietf-interfaces:interfaces";
148         final String uri = uriPrefix + uriPath;
149         when(restconfService.updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class))).thenReturn(null);
150         put(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
151         verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
152         put(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
153         verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
154         put(uri, null, MediaType.APPLICATION_JSON, jsonData);
155         verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
156         put(uri, null, MediaType.APPLICATION_XML, xmlData);
157         verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
158         put(uri, null, MediaType.TEXT_XML, xmlData);
159         verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
160         put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
161         verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
162     }
163
164     @Test
165     @Ignore
166     public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
167         final String uriPrefix = "/config/";
168         final String uriPath = "ietf-interfaces:interfaces";
169         final String uri = uriPrefix + uriPath;
170         when(restconfService.createConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
171                 any(UriInfo.class))).thenReturn(null);
172         post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
173         verify(restconfService, times(1)).createConfigurationData(eq(uriPath),
174                 any(NormalizedNodeContext.class), any(UriInfo.class));
175         post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
176         verify(restconfService, times(2)).createConfigurationData(eq(uriPath),
177                 any(NormalizedNodeContext.class), any(UriInfo.class));
178         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
179         verify(restconfService, times(3)).createConfigurationData(eq(uriPath),
180                 any(NormalizedNodeContext.class), any(UriInfo.class));
181         post(uri, null, MediaType.APPLICATION_XML, xmlData);
182         verify(restconfService, times(4)).createConfigurationData(eq(uriPath),
183                 any(NormalizedNodeContext.class), any(UriInfo.class));
184         post(uri, null, MediaType.TEXT_XML, xmlData);
185         verify(restconfService, times(5)).createConfigurationData(eq(uriPath),
186                 any(NormalizedNodeContext.class), any(UriInfo.class));
187         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
188         verify(restconfService, times(6)).createConfigurationData(eq(uriPath),
189                 any(NormalizedNodeContext.class), any(UriInfo.class));
190     }
191
192     @Test
193     @Ignore
194     public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
195         final String uriPrefix = "/config/";
196         final String uri = uriPrefix;
197         when(restconfService.createConfigurationData(any(NormalizedNodeContext.class),
198                 any(UriInfo.class))).thenReturn(null);
199         post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
200         verify(restconfService, times(1)).createConfigurationData(
201                 any(NormalizedNodeContext.class), any(UriInfo.class));
202         post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
203         verify(restconfService, times(2)).createConfigurationData(
204                 any(NormalizedNodeContext.class), any(UriInfo.class));
205         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
206         verify(restconfService, times(3)).createConfigurationData(
207                 any(NormalizedNodeContext.class), any(UriInfo.class));
208         post(uri, null, MediaType.APPLICATION_XML, xmlData);
209         verify(restconfService, times(4)).createConfigurationData(
210                 any(NormalizedNodeContext.class), any(UriInfo.class));
211         post(uri, null, MediaType.TEXT_XML, xmlData);
212         verify(restconfService, times(5)).createConfigurationData(
213                 any(NormalizedNodeContext.class), any(UriInfo.class));
214         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
215         verify(restconfService, times(6)).createConfigurationData(
216                 any(NormalizedNodeContext.class), any(UriInfo.class));
217     }
218
219     @Test
220     public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
221         final String uriPrefix = "/config/";
222         final String uriPath = "ietf-interfaces:interfaces";
223         final String uri = uriPrefix + uriPath;
224         when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
225         target(uri).request("fooMediaType").delete();
226         verify(restconfService, times(1)).deleteConfigurationData(uriPath);
227     }
228
229     private int get(final String uri, final String acceptMediaType) {
230         return target(uri).request(acceptMediaType).get().getStatus();
231     }
232
233     private int put(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) {
234         if (acceptMediaType == null) {
235             return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
236         }
237         return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
238     }
239
240     private int post(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) {
241         if (acceptMediaType == null) {
242             if (contentTypeMediaType == null || data == null) {
243                 return target(uri).request().post(null).getStatus();
244             }
245             return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
246         }
247         if (contentTypeMediaType == null || data == null) {
248             return target(uri).request(acceptMediaType).post(null).getStatus();
249         }
250         return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();
251     }
252
253 }