2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
15 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
17 import java.io.FileNotFoundException;
18 import java.io.UnsupportedEncodingException;
20 import java.net.URISyntaxException;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.logging.Level;
25 import javax.ws.rs.core.Application;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
30 import org.glassfish.jersey.server.ResourceConfig;
31 import org.glassfish.jersey.test.JerseyTest;
32 import org.glassfish.jersey.test.TestProperties;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
36 import org.opendaylight.controller.sal.core.api.mount.MountService;
37 import org.opendaylight.controller.sal.rest.api.Draft02;
38 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
39 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
40 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
41 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
42 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
43 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
44 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
45 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
46 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
47 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
48 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.Node;
50 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
52 public class RestGetOperationTest extends JerseyTest {
54 private static BrokerFacade brokerFacade;
55 private static RestconfImpl restconfImpl;
56 private static SchemaContext schemaContextYangsIetf;
57 private static SchemaContext schemaContextTestModule;
58 private static CompositeNode answerFromGet;
61 public static void init() throws FileNotFoundException {
62 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
63 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
64 ControllerContext controllerContext = ControllerContext.getInstance();
65 controllerContext.setSchemas(schemaContextYangsIetf);
66 brokerFacade = mock(BrokerFacade.class);
67 restconfImpl = RestconfImpl.getInstance();
68 restconfImpl.setBroker(brokerFacade);
69 restconfImpl.setControllerContext(controllerContext);
70 answerFromGet = prepareCompositeNodeWithIetfInterfacesInterfacesData();
74 protected Application configure() {
75 /* enable/disable Jersey logs to console */
76 // enable(TestProperties.LOG_TRAFFIC);
77 // enable(TestProperties.DUMP_ENTITY);
78 // enable(TestProperties.RECORD_LOG_LEVEL);
79 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
80 ResourceConfig resourceConfig = new ResourceConfig();
81 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
82 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
83 JsonToCompositeNodeProvider.INSTANCE);
84 return resourceConfig;
88 * Tests of status codes for "/datastore/{identifier}".
91 public void getDatastoreStatusCodes() throws FileNotFoundException, UnsupportedEncodingException {
92 mockReadOperationalDataMethod();
93 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
94 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
96 uri = createUri("/datastore/", "wrong-module:interfaces/interface/eth0");
97 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
99 // Test of request for not existing data. Returning status code 404
100 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
101 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
102 assertEquals(404, get(uri, MediaType.APPLICATION_XML));
106 * Tests of status codes for "/operational/{identifier}".
109 public void getOperationalStatusCodes() throws UnsupportedEncodingException {
110 mockReadOperationalDataMethod();
111 String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
112 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
114 uri = createUri("/operational/", "wrong-module:interfaces/interface/eth0");
115 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
119 * Tests of status codes for "/config/{identifier}".
122 public void getConfigStatusCodes() throws UnsupportedEncodingException {
123 mockReadConfigurationDataMethod();
124 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
125 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
127 uri = createUri("/config/", "wrong-module:interfaces/interface/eth0");
128 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
132 * MountPoint test. URI represents mount point.
135 public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException {
137 brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
138 any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
139 MountInstance mountInstance = mock(MountInstance.class);
140 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
141 MountService mockMountService = mock(MountService.class);
142 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
144 ControllerContext.getInstance().setMountService(mockMountService);
146 String uri = createUri("/config/",
147 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1");
148 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
150 uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1");
151 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
155 public void getDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
156 when(brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
157 any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
158 MountInstance mountInstance = mock(MountInstance.class);
159 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
160 MountService mockMountService = mock(MountService.class);
161 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
163 ControllerContext.getInstance().setMountService(mockMountService);
165 String uri = createUri("/config/",
166 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/");
167 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
170 private int get(String uri, String mediaType) {
171 return target(uri).request(mediaType).get().getStatus();
174 private CompositeNode prepareCnDataForMountPointTest() throws URISyntaxException {
175 CompositeNodeWrapper cont1 = new CompositeNodeWrapper(new URI("test:module"), "cont1");
176 SimpleNodeWrapper lf11 = new SimpleNodeWrapper(new URI("test:module"), "lf11", "lf11 value");
177 cont1.addValue(lf11);
178 return cont1.unwrap();
181 private void mockReadOperationalDataMethod() {
182 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
185 private void mockReadConfigurationDataMethod() {
186 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(answerFromGet);
189 private static CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
190 CompositeNode intface;
192 intface = new CompositeNodeWrapper(new URI("interface"), "interface");
193 List<Node<?>> childs = new ArrayList<>();
195 childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
196 childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
197 childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
198 childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
199 intface.setValue(childs);
201 } catch (URISyntaxException e) {