2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
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.NormalizedNodeJsonBodyWriter;
34 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeXmlBodyWriter;
35 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
36 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
37 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
39 public class MediaTypesTest extends JerseyTest {
41 private static RestconfService restconfService;
42 private static String jsonData;
43 private static String xmlData;
46 public static void init() throws IOException {
47 restconfService = mock(RestconfService.class);
48 final String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
49 jsonData = TestUtils.loadTextFile(jsonPath);
50 final InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
51 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
55 protected Application configure() {
56 /* enable/disable Jersey logs to console */
57 // enable(TestProperties.LOG_TRAFFIC);
58 // enable(TestProperties.DUMP_ENTITY);
59 // enable(TestProperties.RECORD_LOG_LEVEL);
60 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
61 ResourceConfig resourceConfig = new ResourceConfig();
62 resourceConfig = resourceConfig.registerInstances(restconfService, new NormalizedNodeJsonBodyWriter(),
63 new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
64 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
65 return resourceConfig;
70 public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
71 final String uriPrefix = "/operations/";
72 final String uriPath = "ietf-interfaces:interfaces";
73 final String uri = uriPrefix + uriPath;
74 when(restconfService.invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class))).thenReturn(null);
75 post(uri, Draft02.MediaTypes.OPERATION + JSON, Draft02.MediaTypes.OPERATION + JSON, jsonData);
76 verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
77 post(uri, Draft02.MediaTypes.OPERATION + XML, Draft02.MediaTypes.OPERATION + XML, xmlData);
78 verify(restconfService, times(2)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
79 post(uri, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, jsonData);
80 verify(restconfService, times(3)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
81 post(uri, MediaType.APPLICATION_XML, MediaType.APPLICATION_XML, xmlData);
82 verify(restconfService, times(4)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
83 post(uri, MediaType.TEXT_XML, MediaType.TEXT_XML, xmlData);
84 verify(restconfService, times(5)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
85 post(uri, null, MediaType.TEXT_XML, xmlData);
86 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
89 post(uri, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, xmlData);
90 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
91 post(uri, MediaType.TEXT_XML, MediaType.TEXT_PLAIN, xmlData);
92 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
96 public void testGetConfigMediaTypes() throws UnsupportedEncodingException {
97 final String uriPrefix = "/config/";
98 final String uriPath = "ietf-interfaces:interfaces";
99 final String uri = uriPrefix + uriPath;
100 when(restconfService.readConfigurationData(eq(uriPath), any(UriInfo.class))).thenReturn(null);
101 get(uri, Draft02.MediaTypes.DATA + JSON);
102 verify(restconfService, times(1)).readConfigurationData(eq(uriPath), any(UriInfo.class));
103 get(uri, Draft02.MediaTypes.DATA + XML);
104 verify(restconfService, times(2)).readConfigurationData(eq(uriPath), any(UriInfo.class));
105 get(uri, MediaType.APPLICATION_JSON);
106 verify(restconfService, times(3)).readConfigurationData(eq(uriPath), any(UriInfo.class));
107 get(uri, MediaType.APPLICATION_XML);
108 verify(restconfService, times(4)).readConfigurationData(eq(uriPath), any(UriInfo.class));
109 get(uri, MediaType.TEXT_XML);
110 verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
113 get(uri, MediaType.TEXT_PLAIN);
114 verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
118 public void testGetOperationalMediaTypes() throws UnsupportedEncodingException {
119 final String uriPrefix = "/operational/";
120 final String uriPath = "ietf-interfaces:interfaces";
121 final String uri = uriPrefix + uriPath;
122 when(restconfService.readOperationalData(eq(uriPath), any(UriInfo.class))).thenReturn(null);
123 get(uri, Draft02.MediaTypes.DATA + JSON);
124 verify(restconfService, times(1)).readOperationalData(eq(uriPath), any(UriInfo.class));
125 get(uri, Draft02.MediaTypes.DATA + XML);
126 verify(restconfService, times(2)).readOperationalData(eq(uriPath), any(UriInfo.class));
127 get(uri, MediaType.APPLICATION_JSON);
128 verify(restconfService, times(3)).readOperationalData(eq(uriPath), any(UriInfo.class));
129 get(uri, MediaType.APPLICATION_XML);
130 verify(restconfService, times(4)).readOperationalData(eq(uriPath), any(UriInfo.class));
131 get(uri, MediaType.TEXT_XML);
132 verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
135 get(uri, MediaType.TEXT_PLAIN);
136 verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
141 public void testPutConfigMediaTypes() throws UnsupportedEncodingException {
142 final String uriPrefix = "/config/";
143 final String uriPath = "ietf-interfaces:interfaces";
144 final String uri = uriPrefix + uriPath;
145 when(restconfService.updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class))).thenReturn(null);
146 put(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
147 verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
148 put(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
149 verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
150 put(uri, null, MediaType.APPLICATION_JSON, jsonData);
151 verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
152 put(uri, null, MediaType.APPLICATION_XML, xmlData);
153 verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
154 put(uri, null, MediaType.TEXT_XML, xmlData);
155 verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
156 put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
157 verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class));
162 public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
163 final String uriPrefix = "/config/";
164 final String uriPath = "ietf-interfaces:interfaces";
165 final String uri = uriPrefix + uriPath;
166 when(restconfService.createConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
167 any(UriInfo.class))).thenReturn(null);
168 post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
169 verify(restconfService, times(1)).createConfigurationData(eq(uriPath),
170 any(NormalizedNodeContext.class), any(UriInfo.class));
171 post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
172 verify(restconfService, times(2)).createConfigurationData(eq(uriPath),
173 any(NormalizedNodeContext.class), any(UriInfo.class));
174 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
175 verify(restconfService, times(3)).createConfigurationData(eq(uriPath),
176 any(NormalizedNodeContext.class), any(UriInfo.class));
177 post(uri, null, MediaType.APPLICATION_XML, xmlData);
178 verify(restconfService, times(4)).createConfigurationData(eq(uriPath),
179 any(NormalizedNodeContext.class), any(UriInfo.class));
180 post(uri, null, MediaType.TEXT_XML, xmlData);
181 verify(restconfService, times(5)).createConfigurationData(eq(uriPath),
182 any(NormalizedNodeContext.class), any(UriInfo.class));
183 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
184 verify(restconfService, times(6)).createConfigurationData(eq(uriPath),
185 any(NormalizedNodeContext.class), any(UriInfo.class));
190 public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
191 final String uriPrefix = "/config/";
192 final String uri = uriPrefix;
193 when(restconfService.createConfigurationData(any(NormalizedNodeContext.class),
194 any(UriInfo.class))).thenReturn(null);
195 post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
196 verify(restconfService, times(1)).createConfigurationData(
197 any(NormalizedNodeContext.class), any(UriInfo.class));
198 post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
199 verify(restconfService, times(2)).createConfigurationData(
200 any(NormalizedNodeContext.class), any(UriInfo.class));
201 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
202 verify(restconfService, times(3)).createConfigurationData(
203 any(NormalizedNodeContext.class), any(UriInfo.class));
204 post(uri, null, MediaType.APPLICATION_XML, xmlData);
205 verify(restconfService, times(4)).createConfigurationData(
206 any(NormalizedNodeContext.class), any(UriInfo.class));
207 post(uri, null, MediaType.TEXT_XML, xmlData);
208 verify(restconfService, times(5)).createConfigurationData(
209 any(NormalizedNodeContext.class), any(UriInfo.class));
210 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
211 verify(restconfService, times(6)).createConfigurationData(
212 any(NormalizedNodeContext.class), any(UriInfo.class));
216 public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
217 final String uriPrefix = "/config/";
218 final String uriPath = "ietf-interfaces:interfaces";
219 final String uri = uriPrefix + uriPath;
220 when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
221 target(uri).request("fooMediaType").delete();
222 verify(restconfService, times(1)).deleteConfigurationData(uriPath);
225 private int get(final String uri, final String acceptMediaType) {
226 return target(uri).request(acceptMediaType).get().getStatus();
229 private int put(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) {
230 if (acceptMediaType == null) {
231 return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
233 return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
236 private int post(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) {
237 if (acceptMediaType == null) {
238 if (contentTypeMediaType == null || data == null) {
239 return target(uri).request().post(null).getStatus();
241 return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
243 if (contentTypeMediaType == null || data == null) {
244 return target(uri).request(acceptMediaType).post(null).getStatus();
246 return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();