BUG 2412 - restconf @PUT updateConfigurationData method migration
[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 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;
42
43 public class MediaTypesTest extends JerseyTest {
44
45     private static RestconfService restconfService;
46     private static String jsonData;
47     private static String xmlData;
48
49     @BeforeClass
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));
56     }
57
58     @Override
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;
71     }
72
73     @Test
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));
91
92         // negative tests
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));
97     }
98
99     @Test
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));
115
116         // negative tests
117         get(uri, MediaType.TEXT_PLAIN);
118         verify(restconfService, times(5)).readConfigurationData(eq(uriPath), any(UriInfo.class));
119     }
120
121     @Test
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));
137
138         // negative tests
139         get(uri, MediaType.TEXT_PLAIN);
140         verify(restconfService, times(5)).readOperationalData(eq(uriPath), any(UriInfo.class));
141     }
142
143     @Test
144     @Ignore
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));
162     }
163
164     @Test
165     public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
166         final String uriPrefix = "/config/";
167         final String uriPath = "ietf-interfaces:interfaces";
168         final String uri = uriPrefix + uriPath;
169         when(restconfService.createConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
170         post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
171         verify(restconfService, times(1)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
172         post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
173         verify(restconfService, times(2)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
174         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
175         verify(restconfService, times(3)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
176         post(uri, null, MediaType.APPLICATION_XML, xmlData);
177         verify(restconfService, times(4)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
178         post(uri, null, MediaType.TEXT_XML, xmlData);
179         verify(restconfService, times(5)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
180         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
181         verify(restconfService, times(6)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
182     }
183
184     @Test
185     public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
186         final String uriPrefix = "/config/";
187         final String uri = uriPrefix;
188         when(restconfService.createConfigurationData(any(CompositeNode.class))).thenReturn(null);
189         post(uri, null, Draft02.MediaTypes.DATA + JSON, jsonData);
190         verify(restconfService, times(1)).createConfigurationData(any(CompositeNode.class));
191         post(uri, null, Draft02.MediaTypes.DATA + XML, xmlData);
192         verify(restconfService, times(2)).createConfigurationData(any(CompositeNode.class));
193         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
194         verify(restconfService, times(3)).createConfigurationData(any(CompositeNode.class));
195         post(uri, null, MediaType.APPLICATION_XML, xmlData);
196         verify(restconfService, times(4)).createConfigurationData(any(CompositeNode.class));
197         post(uri, null, MediaType.TEXT_XML, xmlData);
198         verify(restconfService, times(5)).createConfigurationData(any(CompositeNode.class));
199         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
200         verify(restconfService, times(6)).createConfigurationData(any(CompositeNode.class));
201     }
202
203     @Test
204     public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
205         final String uriPrefix = "/config/";
206         final String uriPath = "ietf-interfaces:interfaces";
207         final String uri = uriPrefix + uriPath;
208         when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
209         target(uri).request("fooMediaType").delete();
210         verify(restconfService, times(1)).deleteConfigurationData(uriPath);
211     }
212
213     private int get(final String uri, final String acceptMediaType) {
214         return target(uri).request(acceptMediaType).get().getStatus();
215     }
216
217     private int put(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) {
218         if (acceptMediaType == null) {
219             return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
220         }
221         return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
222     }
223
224     private int post(final String uri, final String acceptMediaType, final String contentTypeMediaType, final String data) {
225         if (acceptMediaType == null) {
226             if (contentTypeMediaType == null || data == null) {
227                 return target(uri).request().post(null).getStatus();
228             }
229             return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
230         }
231         if (contentTypeMediaType == null || data == null) {
232             return target(uri).request(acceptMediaType).post(null).getStatus();
233         }
234         return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();
235     }
236
237 }