BUG-1140: inPort disappeared from match
[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
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.UnsupportedEncodingException;
22
23 import javax.ws.rs.client.Entity;
24 import javax.ws.rs.core.Application;
25 import javax.ws.rs.core.MediaType;
26
27 import org.glassfish.jersey.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.opendaylight.controller.sal.rest.api.Draft02;
32 import org.opendaylight.controller.sal.rest.api.RestconfService;
33 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
34 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
35 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
36 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
37 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
38
39 public class MediaTypesTest extends JerseyTest {
40
41     private static RestconfService restconfService;
42     private static String jsonData;
43     private static String xmlData;
44
45     @BeforeClass
46     public static void init() throws IOException {
47         restconfService = mock(RestconfService.class);
48         String jsonPath = RestconfImplTest.class.getResource("/parts/ietf-interfaces_interfaces.json").getPath();
49         jsonData = TestUtils.loadTextFile(jsonPath);
50         InputStream xmlStream = 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         ResourceConfig resourceConfig = new ResourceConfig();
62         resourceConfig = resourceConfig.registerInstances(restconfService, StructuredDataToXmlProvider.INSTANCE,
63                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
64                 JsonToCompositeNodeProvider.INSTANCE);
65         return resourceConfig;
66     }
67
68   @Test
69   public void testPostOperationsWithInputDataMediaTypes() throws UnsupportedEncodingException {
70       String uriPrefix = "/operations/";
71       String uriPath = "ietf-interfaces:interfaces";
72       String uri = uriPrefix + uriPath;
73       when(restconfService.invokeRpc(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
74       post(uri, Draft02.MediaTypes.OPERATION+JSON, Draft02.MediaTypes.OPERATION+JSON, jsonData);
75       verify(restconfService, times(1)).invokeRpc(eq(uriPath), any(CompositeNode.class));
76       post(uri, Draft02.MediaTypes.OPERATION+XML, Draft02.MediaTypes.OPERATION+XML, xmlData);
77       verify(restconfService, times(2)).invokeRpc(eq(uriPath), any(CompositeNode.class));
78       post(uri, MediaType.APPLICATION_JSON, MediaType.APPLICATION_JSON, jsonData);
79       verify(restconfService, times(3)).invokeRpc(eq(uriPath), any(CompositeNode.class));
80       post(uri, MediaType.APPLICATION_XML, MediaType.APPLICATION_XML, xmlData);
81       verify(restconfService, times(4)).invokeRpc(eq(uriPath), any(CompositeNode.class));
82       post(uri, MediaType.TEXT_XML, MediaType.TEXT_XML, xmlData);
83       verify(restconfService, times(5)).invokeRpc(eq(uriPath), any(CompositeNode.class));
84       post(uri, null, MediaType.TEXT_XML, xmlData);
85       verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
86
87       // negative tests
88       post(uri, MediaType.TEXT_PLAIN, MediaType.TEXT_XML, xmlData);
89       verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
90       post(uri, MediaType.TEXT_XML, MediaType.TEXT_PLAIN, xmlData);
91       verify(restconfService, times(6)).invokeRpc(eq(uriPath), any(CompositeNode.class));
92   }
93
94     @Test
95     public void testGetConfigMediaTypes() throws UnsupportedEncodingException {
96         String uriPrefix = "/config/";
97         String uriPath = "ietf-interfaces:interfaces";
98         String uri = uriPrefix + uriPath;
99         when(restconfService.readConfigurationData(uriPath)).thenReturn(null);
100         get(uri, Draft02.MediaTypes.DATA+JSON);
101         verify(restconfService, times(1)).readConfigurationData(uriPath);
102         get(uri, Draft02.MediaTypes.DATA+XML);
103         verify(restconfService, times(2)).readConfigurationData(uriPath);
104         get(uri, MediaType.APPLICATION_JSON);
105         verify(restconfService, times(3)).readConfigurationData(uriPath);
106         get(uri, MediaType.APPLICATION_XML);
107         verify(restconfService, times(4)).readConfigurationData(uriPath);
108         get(uri, MediaType.TEXT_XML);
109         verify(restconfService, times(5)).readConfigurationData(uriPath);
110
111         // negative tests
112         get(uri, MediaType.TEXT_PLAIN);
113         verify(restconfService, times(5)).readConfigurationData(uriPath);
114     }
115
116     @Test
117     public void testGetOperationalMediaTypes() throws UnsupportedEncodingException {
118         String uriPrefix = "/operational/";
119         String uriPath = "ietf-interfaces:interfaces";
120         String uri = uriPrefix + uriPath;
121         when(restconfService.readOperationalData(uriPath)).thenReturn(null);
122         get(uri, Draft02.MediaTypes.DATA+JSON);
123         verify(restconfService, times(1)).readOperationalData(uriPath);
124         get(uri, Draft02.MediaTypes.DATA+XML);
125         verify(restconfService, times(2)).readOperationalData(uriPath);
126         get(uri, MediaType.APPLICATION_JSON);
127         verify(restconfService, times(3)).readOperationalData(uriPath);
128         get(uri, MediaType.APPLICATION_XML);
129         verify(restconfService, times(4)).readOperationalData(uriPath);
130         get(uri, MediaType.TEXT_XML);
131         verify(restconfService, times(5)).readOperationalData(uriPath);
132
133         // negative tests
134         get(uri, MediaType.TEXT_PLAIN);
135         verify(restconfService, times(5)).readOperationalData(uriPath);
136     }
137
138     @Test
139     public void testPutConfigMediaTypes() throws UnsupportedEncodingException {
140         String uriPrefix = "/config/";
141         String uriPath = "ietf-interfaces:interfaces";
142         String uri = uriPrefix + uriPath;
143         when(restconfService.updateConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
144         put(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
145         verify(restconfService, times(1)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
146         put(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
147         verify(restconfService, times(2)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
148         put(uri, null, MediaType.APPLICATION_JSON, jsonData);
149         verify(restconfService, times(3)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
150         put(uri, null, MediaType.APPLICATION_XML, xmlData);
151         verify(restconfService, times(4)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
152         put(uri, null, MediaType.TEXT_XML, xmlData);
153         verify(restconfService, times(5)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
154         put(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
155         verify(restconfService, times(6)).updateConfigurationData(eq(uriPath), any(CompositeNode.class));
156     }
157
158     @Test
159     public void testPostConfigWithPathMediaTypes() throws UnsupportedEncodingException {
160         String uriPrefix = "/config/";
161         String uriPath = "ietf-interfaces:interfaces";
162         String uri = uriPrefix + uriPath;
163         when(restconfService.createConfigurationData(eq(uriPath), any(CompositeNode.class))).thenReturn(null);
164         post(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
165         verify(restconfService, times(1)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
166         post(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
167         verify(restconfService, times(2)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
168         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
169         verify(restconfService, times(3)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
170         post(uri, null, MediaType.APPLICATION_XML, xmlData);
171         verify(restconfService, times(4)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
172         post(uri, null, MediaType.TEXT_XML, xmlData);
173         verify(restconfService, times(5)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
174         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
175         verify(restconfService, times(6)).createConfigurationData(eq(uriPath), any(CompositeNode.class));
176     }
177
178     @Test
179     public void testPostConfigMediaTypes() throws UnsupportedEncodingException {
180         String uriPrefix = "/config/";
181         String uri = uriPrefix;
182         when(restconfService.createConfigurationData(any(CompositeNode.class))).thenReturn(null);
183         post(uri, null, Draft02.MediaTypes.DATA+JSON, jsonData);
184         verify(restconfService, times(1)).createConfigurationData(any(CompositeNode.class));
185         post(uri, null, Draft02.MediaTypes.DATA+XML, xmlData);
186         verify(restconfService, times(2)).createConfigurationData(any(CompositeNode.class));
187         post(uri, null, MediaType.APPLICATION_JSON, jsonData);
188         verify(restconfService, times(3)).createConfigurationData(any(CompositeNode.class));
189         post(uri, null, MediaType.APPLICATION_XML, xmlData);
190         verify(restconfService, times(4)).createConfigurationData(any(CompositeNode.class));
191         post(uri, null, MediaType.TEXT_XML, xmlData);
192         verify(restconfService, times(5)).createConfigurationData(any(CompositeNode.class));
193         post(uri, "fooMediaType", MediaType.TEXT_XML, xmlData);
194         verify(restconfService, times(6)).createConfigurationData(any(CompositeNode.class));
195     }
196
197     @Test
198     public void testDeleteConfigMediaTypes() throws UnsupportedEncodingException {
199         String uriPrefix = "/config/";
200         String uriPath = "ietf-interfaces:interfaces";
201         String uri = uriPrefix + uriPath;
202         when(restconfService.deleteConfigurationData(eq(uriPath))).thenReturn(null);
203         target(uri).request("fooMediaType").delete();
204         verify(restconfService, times(1)).deleteConfigurationData(uriPath);
205     }
206
207     private int get(String uri, String acceptMediaType) {
208         return target(uri).request(acceptMediaType).get().getStatus();
209     }
210
211     private int put(String uri, String acceptMediaType, String contentTypeMediaType, String data) {
212         if (acceptMediaType == null) {
213             return target(uri).request().put(Entity.entity(data, contentTypeMediaType)).getStatus();
214         }
215         return target(uri).request(acceptMediaType).put(Entity.entity(data, contentTypeMediaType)).getStatus();
216     }
217
218     private int post(String uri, String acceptMediaType, String contentTypeMediaType, String data) {
219         if (acceptMediaType == null) {
220             if (contentTypeMediaType == null || data == null) {
221                 return target(uri).request().post(null).getStatus();
222             }
223             return target(uri).request().post(Entity.entity(data, contentTypeMediaType)).getStatus();
224         }
225         if (contentTypeMediaType == null || data == null) {
226             return target(uri).request(acceptMediaType).post(null).getStatus();
227         }
228         return target(uri).request(acceptMediaType).post(Entity.entity(data, contentTypeMediaType)).getStatus();
229     }
230
231 }