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.XML;
8 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
10 import java.io.FileNotFoundException;
11 import java.io.UnsupportedEncodingException;
13 import java.net.URISyntaxException;
14 import java.util.ArrayList;
15 import java.util.List;
17 import javax.ws.rs.core.Application;
18 import javax.ws.rs.core.MediaType;
19 import javax.ws.rs.core.Response;
21 import org.glassfish.jersey.server.ResourceConfig;
22 import org.glassfish.jersey.test.JerseyTest;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
26 import org.opendaylight.controller.sal.core.api.mount.MountService;
27 import org.opendaylight.controller.sal.rest.api.Draft02;
28 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
29 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
30 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
31 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
32 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
33 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
34 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
35 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
37 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
38 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.Node;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 public class RestGetOperationTest extends JerseyTest {
44 private static BrokerFacade brokerFacade;
45 private static RestconfImpl restconfImpl;
46 private static SchemaContext schemaContextYangsIetf;
47 private static SchemaContext schemaContextTestModule;
48 private static CompositeNode answerFromGet;
51 public static void init() throws FileNotFoundException {
52 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
53 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
54 ControllerContext controllerContext = ControllerContext.getInstance();
55 controllerContext.setSchemas(schemaContextYangsIetf);
56 brokerFacade = mock(BrokerFacade.class);
57 restconfImpl = RestconfImpl.getInstance();
58 restconfImpl.setBroker(brokerFacade);
59 restconfImpl.setControllerContext(controllerContext);
60 answerFromGet = prepareCompositeNodeWithIetfInterfacesInterfacesData();
64 protected Application configure() {
65 /* enable/disable Jersey logs to console */
66 // enable(TestProperties.LOG_TRAFFIC);
67 // enable(TestProperties.DUMP_ENTITY);
68 // enable(TestProperties.RECORD_LOG_LEVEL);
69 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
70 ResourceConfig resourceConfig = new ResourceConfig();
71 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
72 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
73 JsonToCompositeNodeProvider.INSTANCE);
74 return resourceConfig;
78 * Tests of status codes for "/datastore/{identifier}".
81 public void getDatastoreStatusCodes() throws FileNotFoundException, UnsupportedEncodingException {
82 mockReadOperationalDataMethod();
83 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
84 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
86 uri = createUri("/datastore/", "wrong-module:interfaces/interface/eth0");
87 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
89 // Test of request for not existing data. Returning status code 404
90 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
91 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
92 assertEquals(404, get(uri, MediaType.APPLICATION_XML));
96 * Tests of status codes for "/operational/{identifier}".
99 public void getOperationalStatusCodes() throws UnsupportedEncodingException {
100 mockReadOperationalDataMethod();
101 String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
102 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
104 uri = createUri("/operational/", "wrong-module:interfaces/interface/eth0");
105 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
109 * Tests of status codes for "/config/{identifier}".
112 public void getConfigStatusCodes() throws UnsupportedEncodingException {
113 mockReadConfigurationDataMethod();
114 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
115 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
117 uri = createUri("/config/", "wrong-module:interfaces/interface/eth0");
118 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
122 * MountPoint test. URI represents mount point.
125 public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException {
126 when(brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
127 any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
128 MountInstance mountInstance = mock(MountInstance.class);
129 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
130 MountService mockMountService = mock(MountService.class);
131 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
133 ControllerContext.getInstance().setMountService(mockMountService);
135 String uri = createUri("/config/",
136 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1");
137 Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).get();
138 assertEquals(200, response.getStatus());
140 uri = createUri("/config/",
141 "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1");
142 response = target(uri).request(Draft02.MediaTypes.DATA + XML).get();
143 assertEquals(200, response.getStatus());
146 private int get(String uri, String mediaType) {
147 return target(uri).request(mediaType).get().getStatus();
150 private CompositeNode prepareCnDataForMountPointTest() throws URISyntaxException {
151 CompositeNodeWrapper cont1 = new CompositeNodeWrapper(new URI("test:module"), "cont1");
152 SimpleNodeWrapper lf11 = new SimpleNodeWrapper(new URI("test:module"), "lf11", "lf11 value");
153 cont1.addValue(lf11);
154 return cont1.unwrap();
157 private void mockReadOperationalDataMethod() {
158 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
161 private void mockReadConfigurationDataMethod() {
162 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
165 private static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
166 CompositeNode intface;
168 intface = new CompositeNodeWrapper(new URI("interface"), "interface");
169 List<Node<?>> childs = new ArrayList<>();
171 childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
172 childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
173 childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
174 childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
175 intface.setValue(childs);
177 } catch (URISyntaxException e) {