d997a8afb19d60854f58bb2802ee4f5ad850da61
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestGetOperationTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.mockito.Matchers.any;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
7 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.JSON;
8 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
9 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
10
11 import java.io.FileNotFoundException;
12 import java.io.UnsupportedEncodingException;
13 import java.net.URI;
14 import java.net.URISyntaxException;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.logging.Level;
18
19 import javax.ws.rs.core.Application;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22
23 import org.glassfish.jersey.server.ResourceConfig;
24 import org.glassfish.jersey.test.JerseyTest;
25 import org.glassfish.jersey.test.TestProperties;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.opendaylight.controller.sal.core.api.mount.MountService;
30 import org.opendaylight.controller.sal.rest.api.Draft01;
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.controller.sal.restconf.impl.BrokerFacade;
38 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
39 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
40 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
41 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
42 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
43 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.Node;
45 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46
47 public class RestGetOperationTest extends JerseyTest {
48
49     private static BrokerFacade brokerFacade;
50     private static RestconfImpl restconfImpl;
51     private static SchemaContext schemaContextYangsIetf;
52     private static SchemaContext schemaContextTestModule;
53
54     @BeforeClass
55     public static void init() throws FileNotFoundException {
56         schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
57         schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
58         ControllerContext controllerContext = ControllerContext.getInstance();
59         controllerContext.setSchemas(schemaContextYangsIetf);
60         brokerFacade = mock(BrokerFacade.class);
61         restconfImpl = RestconfImpl.getInstance();
62         restconfImpl.setBroker(brokerFacade);
63         restconfImpl.setControllerContext(controllerContext);
64     }
65
66     @Before
67     public void logs() {
68         /* enable/disable Jersey logs to console */
69         /*
70          * List<LogRecord> loggedRecords = getLoggedRecords(); for (LogRecord l
71          * : loggedRecords) { System.out.println(l.getMessage()); }
72          */
73     }
74
75     @Override
76     protected Application configure() {
77         /* enable/disable Jersey logs to console */
78         /*
79          * enable(TestProperties.LOG_TRAFFIC);
80          */
81         enable(TestProperties.DUMP_ENTITY);
82         enable(TestProperties.RECORD_LOG_LEVEL);
83         set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
84
85         ResourceConfig resourceConfig = new ResourceConfig();
86         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
87                 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
88                 JsonToCompositeNodeProvider.INSTANCE);
89         return resourceConfig;
90     }
91
92     /**
93      * Tests {@link RestconfImpl#readData() readAllData()} method of
94      * RestconfImpl with url {@code "/datastore/ identifier}"}. Status codes 200
95      * is tested.
96      */
97     @Test
98     public void getDatastoreDataViaUrlTest200() throws FileNotFoundException, UnsupportedEncodingException {
99         mockReadOperationalDataMethod();
100         getDataWithUrl("/datastore/", Draft01.MediaTypes.DATA + JSON, 200);
101         getDataWithUrl("/datastore/", Draft01.MediaTypes.DATA + XML, 200);
102         getDataWithUrl("/datastore/", MediaType.APPLICATION_JSON, 200);
103         getDataWithUrl("/datastore/", MediaType.APPLICATION_XML, 200);
104         getDataWithUrl("/datastore/", MediaType.TEXT_XML, 200);
105     }
106
107     /**
108      * Tests {@link RestconfImpl#readData() readAllData()} method of
109      * RestconfImpl with url {@code "/datastore/ identifier}"}. Status codes 400
110      * is tested.
111      */
112     @Test
113     public void getDatastoreDataViaUrlTest400() throws FileNotFoundException, UnsupportedEncodingException {
114         mockReadOperationalDataMethod();
115         getDataWithUrl("/datastore/", Draft01.MediaTypes.DATA + JSON, 400);
116         getDataWithUrl("/datastore/", Draft01.MediaTypes.DATA + XML, 400);
117         getDataWithUrl("/datastore/", MediaType.APPLICATION_JSON, 400);
118         getDataWithUrl("/datastore/", MediaType.APPLICATION_XML, 400);
119         getDataWithUrl("/datastore/", MediaType.TEXT_XML, 400);
120     }
121
122     /**
123      * Tests {@link RestconfImpl#readOperationalData(String)
124      * readOperationalData(String)} method of RestconfImpl with url
125      * {@code "/operational/...identifier..."}. Status codes 200 is tested.
126      */
127     @Test
128     public void getOperationalDataViaUrl200() throws UnsupportedEncodingException {
129         mockReadOperationalDataMethod();
130         getDataWithUrl("/operational/", Draft02.MediaTypes.DATA + JSON, 200);
131         getDataWithUrl("/operational/", Draft02.MediaTypes.DATA + XML, 200);
132         getDataWithUrl("/operational/", MediaType.APPLICATION_JSON, 200);
133         getDataWithUrl("/operational/", MediaType.APPLICATION_XML, 200);
134         getDataWithUrl("/operational/", MediaType.TEXT_XML, 200);
135     }
136
137     /**
138      * Tests {@link RestconfImpl#readOperationalData(String)
139      * readOperationalData(String)} method of RestconfImpl with url
140      * {@code "/operational/...identifier..."}. Status codes 400 is tested.
141      */
142     @Test
143     public void getOperationalDataViaUrl400() throws UnsupportedEncodingException {
144         mockReadOperationalDataMethod();
145         getDataWithUrl("/operational/", Draft02.MediaTypes.DATA + JSON, 400);
146         getDataWithUrl("/operational/", Draft02.MediaTypes.DATA + XML, 400);
147         getDataWithUrl("/operational/", MediaType.APPLICATION_JSON, 400);
148         getDataWithUrl("/operational/", MediaType.APPLICATION_XML, 400);
149         getDataWithUrl("/operational/", MediaType.TEXT_XML, 400);
150     }
151
152     /**
153      * Tests {@link RestconfImpl#readOperationalData
154      * #readConfigurationData(String) readConfigurationData(String)} method of
155      * RestconfImpl with url {@code "/config/...identifier..."}. Status codes
156      * 200 is tested.
157      */
158     @Test
159     public void getConfigDataViaUrl200() throws UnsupportedEncodingException {
160         mockReadConfigurationDataMethod();
161         getDataWithUrl("/config/", Draft02.MediaTypes.DATA + JSON, 200);
162         getDataWithUrl("/config/", Draft02.MediaTypes.DATA + XML, 200);
163         getDataWithUrl("/config/", MediaType.APPLICATION_JSON, 200);
164         getDataWithUrl("/config/", MediaType.APPLICATION_XML, 200);
165         getDataWithUrl("/config/", MediaType.TEXT_XML, 200);
166     }
167
168     /**
169      * Tests {@link RestconfImpl#readOperationalData
170      * #readConfigurationData(String) readConfigurationData(String)} method of
171      * RestconfImpl with url {@code "/config/...identifier..."}. Status codes
172      * 400 is tested.
173      */
174     @Test
175     public void getConfigDataViaUrl400() throws UnsupportedEncodingException {
176         mockReadConfigurationDataMethod();
177         getDataWithUrl("/config/", Draft02.MediaTypes.DATA + JSON, 400);
178         getDataWithUrl("/config/", Draft02.MediaTypes.DATA + XML, 400);
179         getDataWithUrl("/config/", MediaType.APPLICATION_JSON, 400);
180         getDataWithUrl("/config/", MediaType.APPLICATION_XML, 400);
181         getDataWithUrl("/config/", MediaType.TEXT_XML, 400);
182     }
183
184     /**
185      * Tests {@link RestconfImpl#readAllData() readAllData()} method of
186      * RestconfImpl with url {@code "/datastore"}. Currently the method isn't
187      * supported so it returns 500
188      */
189     @Test
190     public void getDatastoreDataAllTest500() throws UnsupportedEncodingException {
191         getDatastoreAllDataTest(Draft01.MediaTypes.DATASTORE + XML);
192         getDatastoreAllDataTest(Draft01.MediaTypes.DATASTORE + JSON);
193     }
194
195     /**
196      * 
197      * Tests {@link RestconfImpl#getModules getModules} method of RestconfImpl
198      * with uri {@code "/modules"}. Currently the method isn't supported so it
199      * returns 500
200      */
201     @Test
202     public void getModulesDataTest500() throws UnsupportedEncodingException {
203         getModulesDataTest(Draft01.MediaTypes.API + JSON);
204         getModulesDataTest(Draft01.MediaTypes.API + XML);
205         getModulesDataTest(Draft02.MediaTypes.API + JSON);
206         getModulesDataTest(Draft02.MediaTypes.API + XML);
207     }
208
209     /**
210      * Test of request for not existing data. Returning status code 404
211      */
212     @Test
213     public void getDataWithUrlNoExistingDataTest404() throws UnsupportedEncodingException, URISyntaxException {
214         String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
215
216         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
217
218         Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
219         assertEquals(404, response.getStatus());
220     }
221
222     /**
223      * MountPoint test. URI represents mount point.
224      */
225     @Test
226     public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, FileNotFoundException,
227             URISyntaxException {
228         when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(
229                 prepareCnDataForMountPointTest());
230
231         MountService mockMountService = mock(MountService.class);
232
233         when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(
234                 new DummyMountInstanceImpl.Builder().setSchemaContext(schemaContextTestModule).build());
235
236         ControllerContext.getInstance().setMountService(mockMountService);
237
238         String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/test-module:cont/cont1");
239         Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).get();
240         assertEquals(200, response.getStatus());
241     }
242
243     private void getDataWithUrl(String mediaTypePrefix, String mediaType, int statusCode)
244             throws UnsupportedEncodingException {
245         String uri = null;
246         switch (statusCode) {
247         case 400:
248             uri = createUri(mediaTypePrefix, "wrong-module:interfaces/interface/eth0");
249             break;
250         case 200:
251             uri = createUri(mediaTypePrefix, "ietf-interfaces:interfaces/interface/eth0");
252             break;
253         }
254         Response response = target(uri).request(mediaType).get();
255         assertEquals("Status is incorrect for media type " + mediaType + ".", statusCode, response.getStatus());
256
257     }
258
259     private void getModulesDataTest(String mediaType) throws UnsupportedEncodingException {
260         String uri = createUri("/modules", "");
261         Response response = target(uri).request(mediaType).get();
262
263         assertEquals("Status is incorrect for media type " + mediaType + ".", 500, response.getStatus());
264     }
265
266     private void getDatastoreAllDataTest(String mediaType) throws UnsupportedEncodingException {
267         String uri = createUri("/datastore", "");
268         Response response = target(uri).request(mediaType).get();
269
270         assertEquals(500, response.getStatus());
271     }
272
273     private CompositeNode prepareCnDataForMountPointTest() throws URISyntaxException {
274         CompositeNodeWrapper cont1 = new CompositeNodeWrapper(new URI("test:module"), "cont1");
275         SimpleNodeWrapper lf11 = new SimpleNodeWrapper(new URI("test:module"), "lf11", "lf11 value");
276         cont1.addValue(lf11);
277         return cont1.unwrap();
278     }
279
280     private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
281         CompositeNode intface;
282         try {
283             intface = new CompositeNodeWrapper(new URI("interface"), "interface");
284             List<Node<?>> childs = new ArrayList<>();
285
286             childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
287             childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
288             childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
289             childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
290             intface.setValue(childs);
291             return intface;
292         } catch (URISyntaxException e) {
293         }
294
295         return null;
296     }
297
298     private void mockReadOperationalDataMethod() {
299         CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
300         when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
301     }
302
303     private void mockReadConfigurationDataMethod() {
304         CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
305         when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
306     }
307 }