1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.mockito.Matchers.any;
4 import static org.mockito.Matchers.eq;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.times;
7 import static org.mockito.Mockito.verify;
8 import static org.mockito.Mockito.when;
9 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.JSON;
10 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
11 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.UnsupportedEncodingException;
17 import javax.ws.rs.client.Entity;
18 import javax.ws.rs.core.Application;
19 import javax.ws.rs.core.MediaType;
21 import org.glassfish.jersey.server.ResourceConfig;
22 import org.glassfish.jersey.test.JerseyTest;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.sal.rest.api.Draft02;
26 import org.opendaylight.controller.sal.rest.api.RestconfService;
27 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
28 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
29 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
30 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
31 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33 public class MediaTypesTest extends JerseyTest {
35 private static RestconfService restconfService;
36 private static String jsonData;
37 private static String xmlData;
40 public static void init() throws IOException {
41 restconfService = mock(RestconfService.class);
42 String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
43 jsonData = TestUtils.loadTextFile(jsonPath);
44 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
45 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
49 protected Application configure() {
50 /* enable/disable Jersey logs to console */
51 // enable(TestProperties.LOG_TRAFFIC);
52 // enable(TestProperties.DUMP_ENTITY);
53 // enable(TestProperties.RECORD_LOG_LEVEL);
54 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
55 ResourceConfig resourceConfig = new ResourceConfig();
56 resourceConfig = resourceConfig.registerInstances(restconfService, StructuredDataToXmlProvider.INSTANCE,
57 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
58 JsonToCompositeNodeProvider.INSTANCE);
59 return resourceConfig;
63 public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
64 String uriPrefix = "/operations/";
65 String uriPath = "ietf-interfaces:interfaces";
66 String uri = createUri(uriPrefix, uriPath);
67 when(restconfService.invokeRpc(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
68 post(uri, Draft02.MediaTypes.DATA+JSON, Draft02.MediaTypes.DATA+JSON, jsonData);
69 verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(CompositeNode.class));
70 post(uri, Draft02.MediaTypes.DATA+XML, Draft02.MediaTypes.DATA+XML, xmlData);
71 verify(restconfService, times(2)).invokeRpc(eq(uriPath), any(CompositeNode.class));
72 post(uri, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, jsonData);
73 verify(restconfService, times(3)).invokeRpc(eq(uriPath), any(CompositeNode.class));
74 post(uri, MediaType.APPLICATION_XML, MediaType.APPLICATION_XML, xmlData);
75 verify(restconfService, times(4)).invokeRpc(eq(uriPath), any(CompositeNode.class));
76 post(uri, MediaType.TEXT_XML, MediaType.TEXT_XML, xmlData);
77 verify(restconfService, times(5)).invokeRpc(eq(uriPath), any(CompositeNode.class));
78 post(uri, null, MediaType.TEXT_XML, xmlData);
79 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
82 post(uri, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, xmlData);
83 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
84 post(uri, MediaType.TEXT_XML, MediaType.TEXT_PLAIN, xmlData);
85 verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
89 public void testGetConfigMediaTypes() throws UnsupportedEncodingException {
90 String uriPrefix = "/config/";
91 String uriPath = "ietf-interfaces:interfaces";
92 String uri = createUri(uriPrefix, uriPath);
93 when(restconfService.readConfigurationData(uriPath)).thenReturn(null);
94 get(uri, Draft02.MediaTypes.DATA+JSON);
95 verify(restconfService, times(1)).readConfigurationData(uriPath);
96 get(uri, Draft02.MediaTypes.DATA+XML);
97 verify(restconfService, times(2)).readConfigurationData(uriPath);
98 get(uri, MediaType.APPLICATION_JSON);
99 verify(restconfService, times(3)).readConfigurationData(uriPath);
100 get(uri, MediaType.APPLICATION_XML);
101 verify(restconfService, times(4)).readConfigurationData(uriPath);
102 get(uri, MediaType.TEXT_XML);
103 verify(restconfService, times(5)).readConfigurationData(uriPath);
106 get(uri, MediaType.TEXT_PLAIN);
107 verify(restconfService, times(5)).readConfigurationData(uriPath);
111 public void testGetOperationalMediaTypes() throws UnsupportedEncodingException {
112 String uriPrefix = "/operational/";
113 String uriPath = "ietf-interfaces:interfaces";
114 String uri = createUri(uriPrefix, uriPath);
115 when(restconfService.readOperationalData(uriPath)).thenReturn(null);
116 get(uri, Draft02.MediaTypes.DATA+JSON);
117 verify(restconfService, times(1)).readOperationalData(uriPath);
118 get(uri, Draft02.MediaTypes.DATA+XML);
119 verify(restconfService, times(2)).readOperationalData(uriPath);
120 get(uri, MediaType.APPLICATION_JSON);
121 verify(restconfService, times(3)).readOperationalData(uriPath);
122 get(uri, MediaType.APPLICATION_XML);
123 verify(restconfService, times(4)).readOperationalData(uriPath);
124 get(uri, MediaType.TEXT_XML);
125 verify(restconfService, times(5)).readOperationalData(uriPath);
128 get(uri, MediaType.TEXT_PLAIN);
129 verify(restconfService, times(5)).readOperationalData(uriPath);
133 public void testPutConfigMediaTypes() throws UnsupportedEncodingException {
134 String uriPrefix = "/config/";
135 String uriPath = "ietf-interfaces:interfaces";
136 String uri = createUri(uriPrefix, uriPath);
137 when(restconfService.updateConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
138 put(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
139 verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
140 put(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
141 verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
142 put(uri, null, MediaType.APPLICATION_JSON, jsonData);
143 verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
144 put(uri, null, MediaType.APPLICATION_XML, xmlData);
145 verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
146 put(uri, null, MediaType.TEXT_XML, xmlData);
147 verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
148 put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
149 verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
153 public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
154 String uriPrefix = "/config/";
155 String uriPath = "ietf-interfaces:interfaces";
156 String uri = createUri(uriPrefix, uriPath);
157 when(restconfService.createConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
158 post(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
159 verify(restconfService, times(1)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
160 post(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
161 verify(restconfService, times(2)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
162 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
163 verify(restconfService, times(3)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
164 post(uri, null, MediaType.APPLICATION_XML, xmlData);
165 verify(restconfService, times(4)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
166 post(uri, null, MediaType.TEXT_XML, xmlData);
167 verify(restconfService, times(5)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
168 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
169 verify(restconfService, times(6)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
173 public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
174 String uriPrefix = "/config/";
175 String uri = createUri(uriPrefix, "");
176 when(restconfService.createConfigurationData(any(CompositeNode.class))).thenReturn(null);
177 post(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
178 verify(restconfService, times(1)).createConfigurationData(any(CompositeNode.class));
179 post(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
180 verify(restconfService, times(2)).createConfigurationData(any(CompositeNode.class));
181 post(uri, null, MediaType.APPLICATION_JSON, jsonData);
182 verify(restconfService, times(3)).createConfigurationData(any(CompositeNode.class));
183 post(uri, null, MediaType.APPLICATION_XML, xmlData);
184 verify(restconfService, times(4)).createConfigurationData(any(CompositeNode.class));
185 post(uri, null, MediaType.TEXT_XML, xmlData);
186 verify(restconfService, times(5)).createConfigurationData(any(CompositeNode.class));
187 post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
188 verify(restconfService, times(6)).createConfigurationData(any(CompositeNode.class));
192 public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
193 String uriPrefix = "/config/";
194 String uriPath = "ietf-interfaces:interfaces";
195 String uri = createUri(uriPrefix, uriPath);
196 when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
197 target(uri).request("fooMediaType").delete();
198 verify(restconfService, times(1)).deleteConfigurationData(uriPath);
201 private int get(String uri, String acceptMediaType) {
202 return target(uri).request(acceptMediaType).get().getStatus();
205 private int put(String uri, String acceptMediaType, String contentTypeMediaType, String data) {
206 if (acceptMediaType == null) {
207 return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
209 return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
212 private int post(String uri, String acceptMediaType, String contentTypeMediaType, String data) {
213 if (acceptMediaType == null) {
214 if (contentTypeMediaType == null || data == null) {
215 return target(uri).request().post(null).getStatus();
217 return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
219 if (contentTypeMediaType == null || data == null) {
220 return target(uri).request(acceptMediaType).post(null).getStatus();
222 return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();