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;
16 import java.util.logging.Level;
18 import javax.ws.rs.core.Application;
19 import javax.ws.rs.core.MediaType;
20 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.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
29 import org.opendaylight.controller.sal.core.api.mount.MountService;
30 import org.opendaylight.controller.sal.rest.api.Draft02;
31 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
32 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
33 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
34 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
35 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
36 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
37 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
38 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
39 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
40 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
41 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.Node;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 public class RestGetOperationTest extends JerseyTest {
47 private static BrokerFacade brokerFacade;
48 private static RestconfImpl restconfImpl;
49 private static SchemaContext schemaContextYangsIetf;
50 private static SchemaContext schemaContextTestModule;
51 private static CompositeNode answerFromGet;
54 public static void init() throws FileNotFoundException {
55 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
56 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
57 ControllerContext controllerContext = ControllerContext.getInstance();
58 controllerContext.setSchemas(schemaContextYangsIetf);
59 brokerFacade = mock(BrokerFacade.class);
60 restconfImpl = RestconfImpl.getInstance();
61 restconfImpl.setBroker(brokerFacade);
62 restconfImpl.setControllerContext(controllerContext);
63 answerFromGet = prepareCompositeNodeWithIetfInterfacesInterfacesData();
67 protected Application configure() {
68 /* enable/disable Jersey logs to console */
69 // enable(TestProperties.LOG_TRAFFIC);
70 // enable(TestProperties.DUMP_ENTITY);
71 // enable(TestProperties.RECORD_LOG_LEVEL);
72 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
73 ResourceConfig resourceConfig = new ResourceConfig();
74 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
75 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
76 JsonToCompositeNodeProvider.INSTANCE);
77 return resourceConfig;
81 * Tests of status codes for "/datastore/{identifier}".
84 public void getDatastoreStatusCodes() throws FileNotFoundException, UnsupportedEncodingException {
85 mockReadOperationalDataMethod();
86 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
87 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
89 uri = createUri("/datastore/", "wrong-module:interfaces/interface/eth0");
90 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
92 // Test of request for not existing data. Returning status code 404
93 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
94 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
95 assertEquals(404, get(uri, MediaType.APPLICATION_XML));
99 * Tests of status codes for "/operational/{identifier}".
102 public void getOperationalStatusCodes() throws UnsupportedEncodingException {
103 mockReadOperationalDataMethod();
104 String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
105 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
107 uri = createUri("/operational/", "wrong-module:interfaces/interface/eth0");
108 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
112 * Tests of status codes for "/config/{identifier}".
115 public void getConfigStatusCodes() throws UnsupportedEncodingException {
116 mockReadConfigurationDataMethod();
117 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
118 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
120 uri = createUri("/config/", "wrong-module:interfaces/interface/eth0");
121 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
125 * MountPoint test. URI represents mount point.
128 public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException {
130 brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
131 any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
132 MountInstance mountInstance = mock(MountInstance.class);
133 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
134 MountService mockMountService = mock(MountService.class);
135 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
137 ControllerContext.getInstance().setMountService(mockMountService);
139 String uri = createUri("/config/",
140 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1");
141 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
143 uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1");
144 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
148 public void getDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
149 when(brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
150 any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
151 MountInstance mountInstance = mock(MountInstance.class);
152 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
153 MountService mockMountService = mock(MountService.class);
154 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
156 ControllerContext.getInstance().setMountService(mockMountService);
158 String uri = createUri("/config/",
159 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/");
160 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
163 private int get(String uri, String mediaType) {
164 return target(uri).request(mediaType).get().getStatus();
167 private CompositeNode prepareCnDataForMountPointTest() throws URISyntaxException {
168 CompositeNodeWrapper cont1 = new CompositeNodeWrapper(new URI("test:module"), "cont1");
169 SimpleNodeWrapper lf11 = new SimpleNodeWrapper(new URI("test:module"), "lf11", "lf11 value");
170 cont1.addValue(lf11);
171 return cont1.unwrap();
174 private void mockReadOperationalDataMethod() {
175 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
178 private void mockReadConfigurationDataMethod() {
179 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
182 private static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
183 CompositeNode intface;
185 intface = new CompositeNodeWrapper(new URI("interface"), "interface");
186 List<Node<?>> childs = new ArrayList<>();
188 childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
189 childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
190 childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
191 childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
192 intface.setValue(childs);
194 } catch (URISyntaxException e) {