1 package org.opendaylight.controller.sal.restconf.impl.test;
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;
11 import java.io.FileNotFoundException;
12 import java.io.UnsupportedEncodingException;
14 import java.net.URISyntaxException;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.logging.Level;
19 import javax.ws.rs.core.Application;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
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;
47 public class RestGetOperationTest extends JerseyTest {
49 private static BrokerFacade brokerFacade;
50 private static RestconfImpl restconfImpl;
51 private static SchemaContext schemaContextYangsIetf;
52 private static SchemaContext schemaContextTestModule;
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);
68 /* enable/disable Jersey logs to console */
70 * List<LogRecord> loggedRecords = getLoggedRecords(); for (LogRecord l
71 * : loggedRecords) { System.out.println(l.getMessage()); }
76 protected Application configure() {
77 /* enable/disable Jersey logs to console */
79 * enable(TestProperties.LOG_TRAFFIC);
81 enable(TestProperties.DUMP_ENTITY);
82 enable(TestProperties.RECORD_LOG_LEVEL);
83 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
85 ResourceConfig resourceConfig = new ResourceConfig();
86 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
87 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
88 JsonToCompositeNodeProvider.INSTANCE);
89 return resourceConfig;
93 * Tests {@link RestconfImpl#readData() readAllData()} method of
94 * RestconfImpl with url {@code "/datastore/ identifier}"}. Status codes 200
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);
108 * Tests {@link RestconfImpl#readData() readAllData()} method of
109 * RestconfImpl with url {@code "/datastore/ identifier}"}. Status codes 400
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);
123 * Tests {@link RestconfImpl#readOperationalData(String)
124 * readOperationalData(String)} method of RestconfImpl with url
125 * {@code "/operational/...identifier..."}. Status codes 200 is tested.
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);
138 * Tests {@link RestconfImpl#readOperationalData(String)
139 * readOperationalData(String)} method of RestconfImpl with url
140 * {@code "/operational/...identifier..."}. Status codes 400 is tested.
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);
153 * Tests {@link RestconfImpl#readOperationalData
154 * #readConfigurationData(String) readConfigurationData(String)} method of
155 * RestconfImpl with url {@code "/config/...identifier..."}. Status codes
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);
169 * Tests {@link RestconfImpl#readOperationalData
170 * #readConfigurationData(String) readConfigurationData(String)} method of
171 * RestconfImpl with url {@code "/config/...identifier..."}. Status codes
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);
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
190 public void getDatastoreDataAllTest500() throws UnsupportedEncodingException {
191 getDatastoreAllDataTest(Draft01.MediaTypes.DATASTORE + XML);
192 getDatastoreAllDataTest(Draft01.MediaTypes.DATASTORE + JSON);
197 * Tests {@link RestconfImpl#getModules getModules} method of RestconfImpl
198 * with uri {@code "/modules"}. Currently the method isn't supported so it
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);
210 * Test of request for not existing data. Returning status code 404
213 public void getDataWithUrlNoExistingDataTest404() throws UnsupportedEncodingException, URISyntaxException {
214 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
216 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
218 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
219 assertEquals(404, response.getStatus());
223 * MountPoint test. URI represents mount point.
226 public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, FileNotFoundException,
228 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(
229 prepareCnDataForMountPointTest());
231 MountService mockMountService = mock(MountService.class);
233 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(
234 new DummyMountInstanceImpl.Builder().setSchemaContext(schemaContextTestModule).build());
236 ControllerContext.getInstance().setMountService(mockMountService);
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());
243 private void getDataWithUrl(String mediaTypePrefix, String mediaType, int statusCode)
244 throws UnsupportedEncodingException {
246 switch (statusCode) {
248 uri = createUri(mediaTypePrefix, "wrong-module:interfaces/interface/eth0");
251 uri = createUri(mediaTypePrefix, "ietf-interfaces:interfaces/interface/eth0");
254 Response response = target(uri).request(mediaType).get();
255 assertEquals("Status is incorrect for media type " + mediaType + ".", statusCode, response.getStatus());
259 private void getModulesDataTest(String mediaType) throws UnsupportedEncodingException {
260 String uri = createUri("/modules", "");
261 Response response = target(uri).request(mediaType).get();
263 assertEquals("Status is incorrect for media type " + mediaType + ".", 500, response.getStatus());
266 private void getDatastoreAllDataTest(String mediaType) throws UnsupportedEncodingException {
267 String uri = createUri("/datastore", "");
268 Response response = target(uri).request(mediaType).get();
270 assertEquals(500, response.getStatus());
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();
280 private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
281 CompositeNode intface;
283 intface = new CompositeNodeWrapper(new URI("interface"), "interface");
284 List<Node<?>> childs = new ArrayList<>();
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);
292 } catch (URISyntaxException e) {
298 private void mockReadOperationalDataMethod() {
299 CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
300 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
303 private void mockReadConfigurationDataMethod() {
304 CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
305 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);