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.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;
43 public class MediaTypesTest extends JerseyTest {
45 private static RestconfService restconfService;
46 private static String jsonData;
47 private static String xmlData;
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));
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;
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));
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));
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 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));
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));
193 public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
194 final String uriPrefix = "/config/";
195 final String uri = uriPrefix;
196 when(restconfService.createConfigurationData(any(CompositeNode.class), any(UriInfo.class))).thenReturn(null);
197 post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
198 verify(restconfService, times(1)).createConfigurationData(any(CompositeNode.class), any(UriInfo.class));
199 post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
200 verify(restconfService, times(2)).createConfigurationData(any(CompositeNode.class), any(UriInfo.class));
201 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
202 verify(restconfService, times(3)).createConfigurationData(any(CompositeNode.class), any(UriInfo.class));
203 post(uri, null, MediaType.APPLICATION_XML, xmlData);
204 verify(restconfService, times(4)).createConfigurationData(any(CompositeNode.class), any(UriInfo.class));
205 post(uri, null, MediaType.TEXT_XML, xmlData);
206 verify(restconfService, times(5)).createConfigurationData(any(CompositeNode.class), any(UriInfo.class));
207 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
208 verify(restconfService, times(6)).createConfigurationData(any(CompositeNode.class), any(UriInfo.class));
212 public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
213 final String uriPrefix = "/config/";
214 final String uriPath = "ietf-interfaces:interfaces";
215 final String uri = uriPrefix + uriPath;
216 when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
217 target(uri).request("fooMediaType").delete();
218 verify(restconfService, times(1)).deleteConfigurationData(uriPath);
221 private int get(final String uri, final String acceptMediaType) {
222 return target(uri).request(acceptMediaType).get().getStatus();
225 private int put(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) {
226 if (acceptMediaType == null) {
227 return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
229 return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
232 private int post(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) {
233 if (acceptMediaType == null) {
234 if (contentTypeMediaType == null || data == null) {
235 return target(uri).request().post(null).getStatus();
237 return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
239 if (contentTypeMediaType == null || data == null) {
240 return target(uri).request(acceptMediaType).post(null).getStatus();
242 return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();