2a8ab7a344964b8a99e952a6d9336d1145b35e30
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / MediaTypesTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
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;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.UnsupportedEncodingException;
23
24 import javax.ws.rs.client.Entity;
25 import javax.ws.rs.core.Application;
26 import javax.ws.rs.core.MediaType;
27
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;
39
40 public class MediaTypesTest extends JerseyTest {
41     
42     private static RestconfService restconfService;
43     private static String jsonData;
44     private static String xmlData;
45     
46     @BeforeClass
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));
53     }
54     
55     @Override
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;
67     }
68     
69   @Test
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.DATA+JSON, Draft02.MediaTypes.DATA+JSON, jsonData);
76       verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(CompositeNode.class));
77       post(uri, Draft02.MediaTypes.DATA+XML, Draft02.MediaTypes.DATA+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));
87       
88       // negative tests
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));
93   }
94   
95     @Test
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);
111         
112         // negative tests
113         get(uri, MediaType.TEXT_PLAIN);
114         verify(restconfService, times(5)).readConfigurationData(uriPath);
115     }
116     
117     @Test
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);
133         
134         // negative tests
135         get(uri, MediaType.TEXT_PLAIN);
136         verify(restconfService, times(5)).readOperationalData(uriPath);
137     }
138     
139     @Test
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));
157     }
158     
159     @Test
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));
177     }
178     
179     @Test
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));
196     }
197     
198     @Test
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);
206     }
207     
208     private int get(String uri, String acceptMediaType) {
209         return target(uri).request(acceptMediaType).get().getStatus();
210     }
211     
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();
215         }
216         return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
217     }
218     
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();
223             }
224             return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
225         }
226         if (contentTypeMediaType == null || data == null) {
227             return target(uri).request(acceptMediaType).post(null).getStatus();
228         }
229         return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();
230     }
231
232 }