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.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.JsonToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
34 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
35 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
36 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
38 public class MediaTypesTest extends JerseyTest {
40 private static RestconfService restconfService;
41 private static String jsonData;
42 private static String xmlData;
45 public static void init() throws IOException {
46 restconfService = mock(RestconfService.class);
47 String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
48 jsonData = TestUtils.loadTextFile(jsonPath);
49 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
50 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
54 protected Application configure() {
55 /* enable/disable Jersey logs to console */
56 // enable(TestProperties.LOG_TRAFFIC);
57 // enable(TestProperties.DUMP_ENTITY);
58 // enable(TestProperties.RECORD_LOG_LEVEL);
59 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
60 ResourceConfig resourceConfig = new ResourceConfig();
61 resourceConfig = resourceConfig.registerInstances(restconfService, StructuredDataToXmlProvider.INSTANCE,
62 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
63 JsonToCompositeNodeProvider.INSTANCE);
64 return resourceConfig;
68 public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
69 String uriPrefix = "/operations/";
70 String uriPath = "ietf-interfaces:interfaces";
71 String uri = uriPrefix + uriPath;
72 when(restconfService.invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class))).thenReturn(null);
73 post(uri, Draft02.MediaTypes.OPERATION + JSON, Draft02.MediaTypes.OPERATION + JSON, jsonData);
74 verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
75 post(uri, Draft02.MediaTypes.OPERATION + XML, Draft02.MediaTypes.OPERATION + XML, xmlData);
76 verify(restconfService, times(2)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
77 post(uri, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, jsonData);
78 verify(restconfService, times(3)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
79 post(uri, MediaType.APPLICATION_XML, MediaType.APPLICATION_XML, xmlData);
80 verify(restconfService, times(4)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
81 post(uri, MediaType.TEXT_XML, MediaType.TEXT_XML, xmlData);
82 verify(restconfService, times(5)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
83 post(uri, null, MediaType.TEXT_XML, xmlData);
84 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
87 post(uri, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, xmlData);
88 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
89 post(uri, MediaType.TEXT_XML, MediaType.TEXT_PLAIN, xmlData);
90 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class), any(UriInfo.class));
94 public void testGetConfigMediaTypes() throws UnsupportedEncodingException {
95 String uriPrefix = "/config/";
96 String uriPath = "ietf-interfaces:interfaces";
97 String uri = uriPrefix + uriPath;
98 when(restconfService.readConfigurationData(eq(uriPath), any(UriInfo.class))).thenReturn(null);
99 get(uri, Draft02.MediaTypes.DATA + JSON);
100 verify(restconfService, times(1)).readConfigurationData(eq(uriPath), any(UriInfo.class));
101 get(uri, Draft02.MediaTypes.DATA + XML);
102 verify(restconfService, times(2)).readConfigurationData(eq(uriPath), any(UriInfo.class));
103 get(uri, MediaType.APPLICATION_JSON);
104 verify(restconfService, times(3)).readConfigurationData(eq(uriPath), any(UriInfo.class));
105 get(uri, MediaType.APPLICATION_XML);
106 verify(restconfService, times(4)).readConfigurationData(eq(uriPath), any(UriInfo.class));
107 get(uri, MediaType.TEXT_XML);
108 verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
111 get(uri, MediaType.TEXT_PLAIN);
112 verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
116 public void testGetOperationalMediaTypes() throws UnsupportedEncodingException {
117 String uriPrefix = "/operational/";
118 String uriPath = "ietf-interfaces:interfaces";
119 String uri = uriPrefix + uriPath;
120 when(restconfService.readOperationalData(eq(uriPath), any(UriInfo.class))).thenReturn(null);
121 get(uri, Draft02.MediaTypes.DATA + JSON);
122 verify(restconfService, times(1)).readOperationalData(eq(uriPath), any(UriInfo.class));
123 get(uri, Draft02.MediaTypes.DATA + XML);
124 verify(restconfService, times(2)).readOperationalData(eq(uriPath), any(UriInfo.class));
125 get(uri, MediaType.APPLICATION_JSON);
126 verify(restconfService, times(3)).readOperationalData(eq(uriPath), any(UriInfo.class));
127 get(uri, MediaType.APPLICATION_XML);
128 verify(restconfService, times(4)).readOperationalData(eq(uriPath), any(UriInfo.class));
129 get(uri, MediaType.TEXT_XML);
130 verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
133 get(uri, MediaType.TEXT_PLAIN);
134 verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
138 public void testPutConfigMediaTypes() throws UnsupportedEncodingException {
139 String uriPrefix = "/config/";
140 String uriPath = "ietf-interfaces:interfaces";
141 String uri = uriPrefix + uriPath;
142 when(restconfService.updateConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
143 put(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
144 verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
145 put(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
146 verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
147 put(uri, null, MediaType.APPLICATION_JSON, jsonData);
148 verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
149 put(uri, null, MediaType.APPLICATION_XML, xmlData);
150 verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
151 put(uri, null, MediaType.TEXT_XML, xmlData);
152 verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
153 put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
154 verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
158 public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
159 String uriPrefix = "/config/";
160 String uriPath = "ietf-interfaces:interfaces";
161 String uri = uriPrefix + uriPath;
162 when(restconfService.createConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
163 post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
164 verify(restconfService, times(1)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
165 post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
166 verify(restconfService, times(2)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
167 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
168 verify(restconfService, times(3)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
169 post(uri, null, MediaType.APPLICATION_XML, xmlData);
170 verify(restconfService, times(4)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
171 post(uri, null, MediaType.TEXT_XML, xmlData);
172 verify(restconfService, times(5)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
173 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
174 verify(restconfService, times(6)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
178 public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
179 String uriPrefix = "/config/";
180 String uri = uriPrefix;
181 when(restconfService.createConfigurationData(any(CompositeNode.class))).thenReturn(null);
182 post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
183 verify(restconfService, times(1)).createConfigurationData(any(CompositeNode.class));
184 post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
185 verify(restconfService, times(2)).createConfigurationData(any(CompositeNode.class));
186 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
187 verify(restconfService, times(3)).createConfigurationData(any(CompositeNode.class));
188 post(uri, null, MediaType.APPLICATION_XML, xmlData);
189 verify(restconfService, times(4)).createConfigurationData(any(CompositeNode.class));
190 post(uri, null, MediaType.TEXT_XML, xmlData);
191 verify(restconfService, times(5)).createConfigurationData(any(CompositeNode.class));
192 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
193 verify(restconfService, times(6)).createConfigurationData(any(CompositeNode.class));
197 public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
198 String uriPrefix = "/config/";
199 String uriPath = "ietf-interfaces:interfaces";
200 String uri = uriPrefix + uriPath;
201 when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
202 target(uri).request("fooMediaType").delete();
203 verify(restconfService, times(1)).deleteConfigurationData(uriPath);
206 private int get(String uri, String acceptMediaType) {
207 return target(uri).request(acceptMediaType).get().getStatus();
210 private int put(String uri, String acceptMediaType, String contentTypeMediaType, String data) {
211 if (acceptMediaType == null) {
212 return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
214 return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
217 private int post(String uri, String acceptMediaType, String contentTypeMediaType, String data) {
218 if (acceptMediaType == null) {
219 if (contentTypeMediaType == null || data == null) {
220 return target(uri).request().post(null).getStatus();
222 return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
224 if (contentTypeMediaType == null || data == null) {
225 return target(uri).request(acceptMediaType).post(null).getStatus();
227 return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();