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 static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.UnsupportedEncodingException;
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.Application;
26 import javax.ws.rs.core.MediaType;
28 import org.glassfish.jersey.server.ResourceConfig;
29 import org.glassfish.jersey.test.JerseyTest;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.opendaylight.controller.sal.rest.api.Draft02;
33 import org.opendaylight.controller.sal.rest.api.RestconfService;
34 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
35 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
36 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
37 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
38 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
40 public class MediaTypesTest extends JerseyTest {
42 private static RestconfService restconfService;
43 private static String jsonData;
44 private static String xmlData;
47 public static void init() throws IOException {
48 restconfService = mock(RestconfService.class);
49 String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
50 jsonData = TestUtils.loadTextFile(jsonPath);
51 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
52 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
56 protected Application configure() {
57 /* enable/disable Jersey logs to console */
58 // enable(TestProperties.LOG_TRAFFIC);
59 // enable(TestProperties.DUMP_ENTITY);
60 // enable(TestProperties.RECORD_LOG_LEVEL);
61 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
62 ResourceConfig resourceConfig = new ResourceConfig();
63 resourceConfig = resourceConfig.registerInstances(restconfService, StructuredDataToXmlProvider.INSTANCE,
64 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
65 JsonToCompositeNodeProvider.INSTANCE);
66 return resourceConfig;
70 public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
71 String uriPrefix = "/operations/";
72 String uriPath = "ietf-interfaces:interfaces";
73 String uri = createUri(uriPrefix, uriPath);
74 when(restconfService.invokeRpc(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
75 post(uri, Draft02.MediaTypes.OPERATION+JSON, Draft02.MediaTypes.OPERATION+JSON, jsonData);
76 verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(CompositeNode.class));
77 post(uri, Draft02.MediaTypes.OPERATION+XML, Draft02.MediaTypes.OPERATION+XML, xmlData);
78 verify(restconfService, times(2)).invokeRpc(eq(uriPath), any(CompositeNode.class));
79 post(uri, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, jsonData);
80 verify(restconfService, times(3)).invokeRpc(eq(uriPath), any(CompositeNode.class));
81 post(uri, MediaType.APPLICATION_XML, MediaType.APPLICATION_XML, xmlData);
82 verify(restconfService, times(4)).invokeRpc(eq(uriPath), any(CompositeNode.class));
83 post(uri, MediaType.TEXT_XML, MediaType.TEXT_XML, xmlData);
84 verify(restconfService, times(5)).invokeRpc(eq(uriPath), any(CompositeNode.class));
85 post(uri, null, MediaType.TEXT_XML, xmlData);
86 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
89 post(uri, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, xmlData);
90 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
91 post(uri, MediaType.TEXT_XML, MediaType.TEXT_PLAIN, xmlData);
92 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
96 public void testGetConfigMediaTypes() throws UnsupportedEncodingException {
97 String uriPrefix = "/config/";
98 String uriPath = "ietf-interfaces:interfaces";
99 String uri = createUri(uriPrefix, uriPath);
100 when(restconfService.readConfigurationData(uriPath)).thenReturn(null);
101 get(uri, Draft02.MediaTypes.DATA+JSON);
102 verify(restconfService, times(1)).readConfigurationData(uriPath);
103 get(uri, Draft02.MediaTypes.DATA+XML);
104 verify(restconfService, times(2)).readConfigurationData(uriPath);
105 get(uri, MediaType.APPLICATION_JSON);
106 verify(restconfService, times(3)).readConfigurationData(uriPath);
107 get(uri, MediaType.APPLICATION_XML);
108 verify(restconfService, times(4)).readConfigurationData(uriPath);
109 get(uri, MediaType.TEXT_XML);
110 verify(restconfService, times(5)).readConfigurationData(uriPath);
113 get(uri, MediaType.TEXT_PLAIN);
114 verify(restconfService, times(5)).readConfigurationData(uriPath);
118 public void testGetOperationalMediaTypes() throws UnsupportedEncodingException {
119 String uriPrefix = "/operational/";
120 String uriPath = "ietf-interfaces:interfaces";
121 String uri = createUri(uriPrefix, uriPath);
122 when(restconfService.readOperationalData(uriPath)).thenReturn(null);
123 get(uri, Draft02.MediaTypes.DATA+JSON);
124 verify(restconfService, times(1)).readOperationalData(uriPath);
125 get(uri, Draft02.MediaTypes.DATA+XML);
126 verify(restconfService, times(2)).readOperationalData(uriPath);
127 get(uri, MediaType.APPLICATION_JSON);
128 verify(restconfService, times(3)).readOperationalData(uriPath);
129 get(uri, MediaType.APPLICATION_XML);
130 verify(restconfService, times(4)).readOperationalData(uriPath);
131 get(uri, MediaType.TEXT_XML);
132 verify(restconfService, times(5)).readOperationalData(uriPath);
135 get(uri, MediaType.TEXT_PLAIN);
136 verify(restconfService, times(5)).readOperationalData(uriPath);
140 public void testPutConfigMediaTypes() throws UnsupportedEncodingException {
141 String uriPrefix = "/config/";
142 String uriPath = "ietf-interfaces:interfaces";
143 String uri = createUri(uriPrefix, uriPath);
144 when(restconfService.updateConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
145 put(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
146 verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
147 put(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
148 verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
149 put(uri, null, MediaType.APPLICATION_JSON, jsonData);
150 verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
151 put(uri, null, MediaType.APPLICATION_XML, xmlData);
152 verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
153 put(uri, null, MediaType.TEXT_XML, xmlData);
154 verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
155 put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
156 verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
160 public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
161 String uriPrefix = "/config/";
162 String uriPath = "ietf-interfaces:interfaces";
163 String uri = createUri(uriPrefix, uriPath);
164 when(restconfService.createConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
165 post(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
166 verify(restconfService, times(1)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
167 post(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
168 verify(restconfService, times(2)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
169 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
170 verify(restconfService, times(3)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
171 post(uri, null, MediaType.APPLICATION_XML, xmlData);
172 verify(restconfService, times(4)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
173 post(uri, null, MediaType.TEXT_XML, xmlData);
174 verify(restconfService, times(5)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
175 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
176 verify(restconfService, times(6)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
180 public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
181 String uriPrefix = "/config/";
182 String uri = createUri(uriPrefix, "");
183 when(restconfService.createConfigurationData(any(CompositeNode.class))).thenReturn(null);
184 post(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
185 verify(restconfService, times(1)).createConfigurationData(any(CompositeNode.class));
186 post(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
187 verify(restconfService, times(2)).createConfigurationData(any(CompositeNode.class));
188 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
189 verify(restconfService, times(3)).createConfigurationData(any(CompositeNode.class));
190 post(uri, null, MediaType.APPLICATION_XML, xmlData);
191 verify(restconfService, times(4)).createConfigurationData(any(CompositeNode.class));
192 post(uri, null, MediaType.TEXT_XML, xmlData);
193 verify(restconfService, times(5)).createConfigurationData(any(CompositeNode.class));
194 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
195 verify(restconfService, times(6)).createConfigurationData(any(CompositeNode.class));
199 public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
200 String uriPrefix = "/config/";
201 String uriPath = "ietf-interfaces:interfaces";
202 String uri = createUri(uriPrefix, uriPath);
203 when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
204 target(uri).request("fooMediaType").delete();
205 verify(restconfService, times(1)).deleteConfigurationData(uriPath);
208 private int get(String uri, String acceptMediaType) {
209 return target(uri).request(acceptMediaType).get().getStatus();
212 private int put(String uri, String acceptMediaType, String contentTypeMediaType, String data) {
213 if (acceptMediaType == null) {
214 return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
216 return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
219 private int post(String uri, String acceptMediaType, String contentTypeMediaType, String data) {
220 if (acceptMediaType == null) {
221 if (contentTypeMediaType == null || data == null) {
222 return target(uri).request().post(null).getStatus();
224 return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
226 if (contentTypeMediaType == null || data == null) {
227 return target(uri).request(acceptMediaType).post(null).getStatus();
229 return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();