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.createUri;
16 import java.io.FileNotFoundException;
17 import java.io.UnsupportedEncodingException;
19 import java.net.URISyntaxException;
20 import java.util.ArrayList;
21 import java.util.List;
23 import javax.ws.rs.core.Application;
24 import javax.ws.rs.core.MediaType;
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.glassfish.jersey.test.JerseyTest;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
31 import org.opendaylight.controller.sal.core.api.mount.MountService;
32 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
34 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
35 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
36 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
37 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
38 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
39 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
40 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
41 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
42 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.Node;
44 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
46 public class RestGetOperationTest extends JerseyTest {
48 private static BrokerFacade brokerFacade;
49 private static RestconfImpl restconfImpl;
50 private static SchemaContext schemaContextYangsIetf;
51 private static SchemaContext schemaContextTestModule;
52 private static CompositeNode answerFromGet;
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 answerFromGet = prepareCompositeNodeWithIetfInterfacesInterfacesData();
68 protected Application configure() {
69 /* enable/disable Jersey logs to console */
70 // enable(TestProperties.LOG_TRAFFIC);
71 // enable(TestProperties.DUMP_ENTITY);
72 // enable(TestProperties.RECORD_LOG_LEVEL);
73 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
74 ResourceConfig resourceConfig = new ResourceConfig();
75 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
76 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
77 JsonToCompositeNodeProvider.INSTANCE);
78 return resourceConfig;
82 * Tests of status codes for "/operational/{identifier}".
85 public void getOperationalStatusCodes() throws UnsupportedEncodingException {
86 mockReadOperationalDataMethod();
87 String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
88 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
90 uri = createUri("/operational/", "wrong-module:interfaces/interface/eth0");
91 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
95 * Tests of status codes for "/config/{identifier}".
98 public void getConfigStatusCodes() throws UnsupportedEncodingException {
99 mockReadConfigurationDataMethod();
100 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
101 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
103 uri = createUri("/config/", "wrong-module:interfaces/interface/eth0");
104 assertEquals(400, get(uri, MediaType.APPLICATION_XML));
108 * MountPoint test. URI represents mount point.
111 public void getDataWithUrlMountPoint() throws UnsupportedEncodingException, URISyntaxException {
113 brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
114 any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
115 MountInstance mountInstance = mock(MountInstance.class);
116 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
117 MountService mockMountService = mock(MountService.class);
118 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
120 ControllerContext.getInstance().setMountService(mockMountService);
122 String uri = createUri("/config/",
123 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont/cont1");
124 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
126 uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1");
127 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
131 public void getDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
132 when(brokerFacade.readConfigurationDataBehindMountPoint(any(MountInstance.class),
133 any(InstanceIdentifier.class))).thenReturn(prepareCnDataForMountPointTest());
134 MountInstance mountInstance = mock(MountInstance.class);
135 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
136 MountService mockMountService = mock(MountService.class);
137 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
139 ControllerContext.getInstance().setMountService(mockMountService);
141 String uri = createUri("/config/",
142 "ietf-interfaces:interfaces/interface/0/yang-ext:mount/");
143 assertEquals(200, get(uri, MediaType.APPLICATION_XML));
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) {