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;
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;
41 public class MediaTypesTest extends JerseyTest {
43 private static RestconfService restconfService;
44 private static String jsonData;
45 private static String xmlData;
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));
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;
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)))
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));
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));
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));
117 get(uri, MediaType.TEXT_PLAIN);
118 verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
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));
139 get(uri, MediaType.TEXT_PLAIN);
140 verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
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))
152 put(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
153 verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
155 put(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
156 verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
158 put(uri, null, MediaType.APPLICATION_JSON, jsonData);
159 verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
161 put(uri, null, MediaType.APPLICATION_XML, xmlData);
162 verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
164 put(uri, null, MediaType.TEXT_XML, xmlData);
165 verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
167 put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
168 verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
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));
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));
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);
237 private int get(final String uri, final String acceptMediaType) {
238 return target(uri).request(acceptMediaType).get().getStatus();
241 private int put(final String uri, final String acceptMediaType, final String contentTypeMediaType,
243 if (acceptMediaType == null) {
244 return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
246 return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
249 private int post(final String uri, final String acceptMediaType, final String contentTypeMediaType,
251 if (acceptMediaType == null) {
252 if ((contentTypeMediaType == null) || (data == null)) {
253 return target(uri).request().post(null).getStatus();
255 return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
257 if ((contentTypeMediaType == null) || (data == null)) {
258 return target(uri).request(acceptMediaType).post(null).getStatus();
260 return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();