2bddec25a343af1a2565dc8ac63321fe1dac76fe
[netconf.git] / restconf / restconf-nb-bierman02 / 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.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.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
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.Ignore;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.opendaylight.netconf.sal.rest.api.Draft02;
33 import org.opendaylight.netconf.sal.rest.api.RestconfService;
34 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
35 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
36 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
37
38 public class MediaTypesTest extends JerseyTest {
39
40     private static String jsonData;
41     private static String xmlData;
42
43     private RestconfService restconfService;
44
45     @BeforeClass
46     public static void init() throws IOException {
47         final String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
48         jsonData = TestUtils.loadTextFile(jsonPath);
49         final InputStream xmlStream =
50                 RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
51         xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
52     }
53
54     @Override
55     protected Application configure() {
56         /* enable/disable Jersey logs to console */
57         // enable(TestProperties.LOG_TRAFFIC);
58         // enable(TestProperties.DUMP_ENTITY);
59         // enable(TestProperties.RECORD_LOG_LEVEL);
60         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());'
61         restconfService = mock(RestconfService.class);
62         ResourceConfig resourceConfig = new ResourceConfig();
63         resourceConfig = resourceConfig.registerInstances(restconfService,  new NormalizedNodeJsonBodyWriter(),
64             new NormalizedNodeXmlBodyWriter());
65         return resourceConfig;
66     }
67
68     @Test
69     @Ignore
70     public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
71         final String uriPrefix = "/operations/";
72         final String uriPath = "ietf-interfaces:interfaces";
73         final String uri = uriPrefix + uriPath;
74         when(restconfService.invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class)))
75                 .thenReturn(null);
76         post(uri, Draft02.MediaTypes.OPERATION + JSON, Draft02.MediaTypes.OPERATION + JSON, jsonData);
77         verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
78         post(uri, Draft02.MediaTypes.OPERATION + XML, Draft02.MediaTypes.OPERATION + XML, xmlData);
79         verify(restconfService, times(2)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
80         post(uri, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, jsonData);
81         verify(restconfService, times(3)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
82         post(uri, MediaType.APPLICATION_XML, MediaType.APPLICATION_XML, xmlData);
83         verify(restconfService, times(4)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
84         post(uri, MediaType.TEXT_XML, MediaType.TEXT_XML, xmlData);
85         verify(restconfService, times(5)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
86         post(uri, null, MediaType.TEXT_XML, xmlData);
87         verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
88
89         // negative tests
90         post(uri, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, xmlData);
91         verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
92         post(uri, MediaType.TEXT_XML, MediaType.TEXT_PLAIN, xmlData);
93         verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(NormalizedNodeContext.class), any(UriInfo.class));
94     }
95
96     @Test
97     public void testGetConfigMediaTypes() throws UnsupportedEncodingException {
98         final String uriPrefix = "/config/";
99         final String uriPath = "ietf-interfaces:interfaces";
100         final String uri = uriPrefix + uriPath;
101         when(restconfService.readConfigurationData(eq(uriPath), any(UriInfo.class))).thenReturn(null);
102         get(uri, Draft02.MediaTypes.DATA + JSON);
103         verify(restconfService, times(1)).readConfigurationData(eq(uriPath), any(UriInfo.class));
104         get(uri, Draft02.MediaTypes.DATA + XML);
105         verify(restconfService, times(2)).readConfigurationData(eq(uriPath), any(UriInfo.class));
106         get(uri, MediaType.APPLICATION_JSON);
107         verify(restconfService, times(3)).readConfigurationData(eq(uriPath), any(UriInfo.class));
108         get(uri, MediaType.APPLICATION_XML);
109         verify(restconfService, times(4)).readConfigurationData(eq(uriPath), any(UriInfo.class));
110         get(uri, MediaType.TEXT_XML);
111         verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
112
113         // negative tests
114         get(uri, MediaType.TEXT_PLAIN);
115         verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
116     }
117
118     @Test
119     public void testGetOperationalMediaTypes() throws UnsupportedEncodingException {
120         final String uriPrefix = "/operational/";
121         final String uriPath = "ietf-interfaces:interfaces";
122         final String uri = uriPrefix + uriPath;
123         when(restconfService.readOperationalData(eq(uriPath), any(UriInfo.class))).thenReturn(null);
124         get(uri, Draft02.MediaTypes.DATA + JSON);
125         verify(restconfService, times(1)).readOperationalData(eq(uriPath), any(UriInfo.class));
126         get(uri, Draft02.MediaTypes.DATA + XML);
127         verify(restconfService, times(2)).readOperationalData(eq(uriPath), any(UriInfo.class));
128         get(uri, MediaType.APPLICATION_JSON);
129         verify(restconfService, times(3)).readOperationalData(eq(uriPath), any(UriInfo.class));
130         get(uri, MediaType.APPLICATION_XML);
131         verify(restconfService, times(4)).readOperationalData(eq(uriPath), any(UriInfo.class));
132         get(uri, MediaType.TEXT_XML);
133         verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
134
135         // negative tests
136         get(uri, MediaType.TEXT_PLAIN);
137         verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
138     }
139
140     @Test
141     @Ignore
142     public void testPutConfigMediaTypes() throws UnsupportedEncodingException {
143         final String uriPrefix = "/config/";
144         final String uriPath = "ietf-interfaces:interfaces";
145         final String uri = uriPrefix + uriPath;
146         final UriInfo uriInfo = Mockito.mock(UriInfo.class);
147         when(restconfService.updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class), uriInfo))
148                 .thenReturn(null);
149         put(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
150         verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
151                 uriInfo);
152         put(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
153         verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
154                 uriInfo);
155         put(uri, null, MediaType.APPLICATION_JSON, jsonData);
156         verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
157                 uriInfo);
158         put(uri, null, MediaType.APPLICATION_XML, xmlData);
159         verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
160                 uriInfo);
161         put(uri, null, MediaType.TEXT_XML, xmlData);
162         verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
163                 uriInfo);
164         put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
165         verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
166                 uriInfo);
167     }
168
169     @Test
170     @Ignore
171     public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
172         final String uriPrefix = "/config/";
173         final String uriPath = "ietf-interfaces:interfaces";
174         final String uri = uriPrefix + uriPath;
175         when(restconfService.createConfigurationData(eq(uriPath), any(NormalizedNodeContext.class),
176                 any(UriInfo.class))).thenReturn(null);
177         post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
178         verify(restconfService, times(1)).createConfigurationData(eq(uriPath),
179                 any(NormalizedNodeContext.class), any(UriInfo.class));
180         post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
181         verify(restconfService, times(2)).createConfigurationData(eq(uriPath),
182                 any(NormalizedNodeContext.class), any(UriInfo.class));
183         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
184         verify(restconfService, times(3)).createConfigurationData(eq(uriPath),
185                 any(NormalizedNodeContext.class), any(UriInfo.class));
186         post(uri, null, MediaType.APPLICATION_XML, xmlData);
187         verify(restconfService, times(4)).createConfigurationData(eq(uriPath),
188                 any(NormalizedNodeContext.class), any(UriInfo.class));
189         post(uri, null, MediaType.TEXT_XML, xmlData);
190         verify(restconfService, times(5)).createConfigurationData(eq(uriPath),
191                 any(NormalizedNodeContext.class), any(UriInfo.class));
192         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
193         verify(restconfService, times(6)).createConfigurationData(eq(uriPath),
194                 any(NormalizedNodeContext.class), any(UriInfo.class));
195     }
196
197     @Test
198     @Ignore
199     public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
200         final String uriPrefix = "/config/";
201         final String uri = uriPrefix;
202         when(restconfService.createConfigurationData(any(NormalizedNodeContext.class),
203                 any(UriInfo.class))).thenReturn(null);
204         post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
205         verify(restconfService, times(1)).createConfigurationData(
206                 any(NormalizedNodeContext.class), any(UriInfo.class));
207         post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
208         verify(restconfService, times(2)).createConfigurationData(
209                 any(NormalizedNodeContext.class), any(UriInfo.class));
210         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
211         verify(restconfService, times(3)).createConfigurationData(
212                 any(NormalizedNodeContext.class), any(UriInfo.class));
213         post(uri, null, MediaType.APPLICATION_XML, xmlData);
214         verify(restconfService, times(4)).createConfigurationData(
215                 any(NormalizedNodeContext.class), any(UriInfo.class));
216         post(uri, null, MediaType.TEXT_XML, xmlData);
217         verify(restconfService, times(5)).createConfigurationData(
218                 any(NormalizedNodeContext.class), any(UriInfo.class));
219         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
220         verify(restconfService, times(6)).createConfigurationData(
221                 any(NormalizedNodeContext.class), any(UriInfo.class));
222     }
223
224     @Test
225     public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
226         final String uriPrefix = "/config/";
227         final String uriPath = "ietf-interfaces:interfaces";
228         final String uri = uriPrefix + uriPath;
229         when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
230         target(uri).request("fooMediaType").delete();
231         verify(restconfService, times(1)).deleteConfigurationData(uriPath);
232     }
233
234     private int get(final String uri, final String acceptMediaType) {
235         return target(uri).request(acceptMediaType).get().getStatus();
236     }
237
238     private int put(final String uri, final String acceptMediaType, final String contentTypeMediaType,
239                     final String data) {
240         if (acceptMediaType == null) {
241             return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
242         }
243         return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
244     }
245
246     private int post(final String uri, final String acceptMediaType, final String contentTypeMediaType,
247                      final String data) {
248         if (acceptMediaType == null) {
249             if (contentTypeMediaType == null || data == null) {
250                 return target(uri).request().post(null).getStatus();
251             }
252             return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
253         }
254         if (contentTypeMediaType == null || data == null) {
255             return target(uri).request(acceptMediaType).post(null).getStatus();
256         }
257         return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();
258     }
259
260 }