Merge "Bug 9092: revert to org.json temporarily"
[netconf.git] / restconf / 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
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22 import javax.ws.rs.client.Entity;
23 import javax.ws.rs.core.Application;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.UriInfo;
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.glassfish.jersey.test.JerseyTest;
28 import org.junit.BeforeClass;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.opendaylight.netconf.sal.rest.api.Draft02;
33 import org.opendaylight.netconf.sal.rest.api.RestconfService;
34 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
35 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
36 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
37 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
38 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
39 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
40
41 public class MediaTypesTest extends JerseyTest {
42
43     private static RestconfService restconfService;
44     private static String jsonData;
45     private static String xmlData;
46
47     @BeforeClass
48     public static void init() throws IOException {
49         restconfService = mock(RestconfService.class);
50         final String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
51         jsonData = TestUtils.loadTextFile(jsonPath);
52         final InputStream xmlStream =
53                 RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
54         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
55     }
56
57     @Override
58     protected Application configure() {
59         /* enable/disable Jersey logs to console */
60         // enable(TestProperties.LOG_TRAFFIC);
61         // enable(TestProperties.DUMP_ENTITY);
62         // enable(TestProperties.RECORD_LOG_LEVEL);
63         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
64         ResourceConfig resourceConfig = new ResourceConfig();
65         resourceConfig = resourceConfig.registerInstances(restconfService,  new NormalizedNodeJsonBodyWriter(),
66             new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
67         resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
68         return resourceConfig;
69     }
70
71     @Test
72     @Ignore
73     public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
74         final String uriPrefix = "/operations/";
75         final String uriPath = "ietf-interfaces:interfaces";
76         final String uri = uriPrefix + uriPath;
77         when(restconfService.invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class)))
78                 .thenReturn(null);
79         post(uri, Draft02.MediaTypes.OPERATION + JSON, Draft02.MediaTypes.OPERATION + JSON, jsonData);
80         verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.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(NormalizedNodeContext.class), any(UriInfo.class));
83         post(uri, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, jsonData);
84         verify(restconfService, times(3)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
85         post(uri, MediaType.APPLICATION_XML, MediaType.APPLICATION_XML, xmlData);
86         verify(restconfService, times(4)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
87         post(uri, MediaType.TEXT_XML, MediaType.TEXT_XML, xmlData);
88         verify(restconfService, times(5)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
89         post(uri, null, MediaType.TEXT_XML, xmlData);
90         verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.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(NormalizedNodeContext.class), any(UriInfo.class));
95         post(uri, MediaType.TEXT_XML, MediaType.TEXT_PLAIN, xmlData);
96         verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.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         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
150         when(restconfService.updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class), uriInfo))
151                 .thenReturn(null);
152         put(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
153         verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
154                 uriInfo);
155         put(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
156         verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
157                 uriInfo);
158         put(uri, null, MediaType.APPLICATION_JSON, jsonData);
159         verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
160                 uriInfo);
161         put(uri, null, MediaType.APPLICATION_XML, xmlData);
162         verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
163                 uriInfo);
164         put(uri, null, MediaType.TEXT_XML, xmlData);
165         verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
166                 uriInfo);
167         put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
168         verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
169                 uriInfo);
170     }
171
172     @Test
173     @Ignore
174     public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
175         final String uriPrefix = "/config/";
176         final String uriPath = "ietf-interfaces:interfaces";
177         final String uri = uriPrefix + uriPath;
178         when(restconfService.createConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
179                 any(UriInfo.class))).thenReturn(null);
180         post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
181         verify(restconfService, times(1)).createConfigurationData(eq(uriPath),
182                 any(NormalizedNodeContext.class), any(UriInfo.class));
183         post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
184         verify(restconfService, times(2)).createConfigurationData(eq(uriPath),
185                 any(NormalizedNodeContext.class), any(UriInfo.class));
186         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
187         verify(restconfService, times(3)).createConfigurationData(eq(uriPath),
188                 any(NormalizedNodeContext.class), any(UriInfo.class));
189         post(uri, null, MediaType.APPLICATION_XML, xmlData);
190         verify(restconfService, times(4)).createConfigurationData(eq(uriPath),
191                 any(NormalizedNodeContext.class), any(UriInfo.class));
192         post(uri, null, MediaType.TEXT_XML, xmlData);
193         verify(restconfService, times(5)).createConfigurationData(eq(uriPath),
194                 any(NormalizedNodeContext.class), any(UriInfo.class));
195         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
196         verify(restconfService, times(6)).createConfigurationData(eq(uriPath),
197                 any(NormalizedNodeContext.class), any(UriInfo.class));
198     }
199
200     @Test
201     @Ignore
202     public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
203         final String uriPrefix = "/config/";
204         final String uri = uriPrefix;
205         when(restconfService.createConfigurationData(any(NormalizedNodeContext.class),
206                 any(UriInfo.class))).thenReturn(null);
207         post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
208         verify(restconfService, times(1)).createConfigurationData(
209                 any(NormalizedNodeContext.class), any(UriInfo.class));
210         post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
211         verify(restconfService, times(2)).createConfigurationData(
212                 any(NormalizedNodeContext.class), any(UriInfo.class));
213         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
214         verify(restconfService, times(3)).createConfigurationData(
215                 any(NormalizedNodeContext.class), any(UriInfo.class));
216         post(uri, null, MediaType.APPLICATION_XML, xmlData);
217         verify(restconfService, times(4)).createConfigurationData(
218                 any(NormalizedNodeContext.class), any(UriInfo.class));
219         post(uri, null, MediaType.TEXT_XML, xmlData);
220         verify(restconfService, times(5)).createConfigurationData(
221                 any(NormalizedNodeContext.class), any(UriInfo.class));
222         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
223         verify(restconfService, times(6)).createConfigurationData(
224                 any(NormalizedNodeContext.class), any(UriInfo.class));
225     }
226
227     @Test
228     public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
229         final String uriPrefix = "/config/";
230         final String uriPath = "ietf-interfaces:interfaces";
231         final String uri = uriPrefix + uriPath;
232         when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
233         target(uri).request("fooMediaType").delete();
234         verify(restconfService, times(1)).deleteConfigurationData(uriPath);
235     }
236
237     private int get(final String uri, final String acceptMediaType) {
238         return target(uri).request(acceptMediaType).get().getStatus();
239     }
240
241     private int put(final String uri, final String acceptMediaType, final String contentTypeMediaType,
242                     final String data) {
243         if (acceptMediaType == null) {
244             return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
245         }
246         return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
247     }
248
249     private int post(final String uri, final String acceptMediaType, final String contentTypeMediaType,
250                      final String data) {
251         if (acceptMediaType == null) {
252             if ((contentTypeMediaType == null) || (data == null)) {
253                 return target(uri).request().post(null).getStatus();
254             }
255             return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
256         }
257         if ((contentTypeMediaType == null) || (data == null)) {
258             return target(uri).request(acceptMediaType).post(null).getStatus();
259         }
260         return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();
261     }
262
263 }