1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.mockito.Matchers.any;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
9 import java.io.FileNotFoundException;
10 import java.io.UnsupportedEncodingException;
12 import java.net.URLEncoder;
13 import java.util.List;
15 import java.util.logging.Level;
16 import java.util.logging.LogRecord;
18 import javax.ws.rs.core.Application;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
22 import org.glassfish.jersey.server.ResourceConfig;
23 import org.glassfish.jersey.test.JerseyTest;
24 import org.glassfish.jersey.test.TestProperties;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
29 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
30 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
31 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
32 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
34 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import com.google.common.base.Charsets;
40 public class ReadConfAndOperDataTest extends JerseyTest {
42 private static ControllerContext controllerContext;
43 private static BrokerFacade brokerFacade;
44 private static RestconfImpl restconfImpl;
45 private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
48 public static void init() throws FileNotFoundException {
49 Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
50 assertNotNull(allModules);
51 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
52 controllerContext = ControllerContext.getInstance();
53 controllerContext.setSchemas(schemaContext);
54 brokerFacade = mock(BrokerFacade.class);
55 restconfImpl = RestconfImpl.getInstance();
56 restconfImpl.setBroker(brokerFacade);
57 restconfImpl.setControllerContext(controllerContext);
62 List<LogRecord> loggedRecords = getLoggedRecords();
63 for (LogRecord l : loggedRecords) {
64 System.out.println(l.getMessage());
69 public void testReadConfigurationData() throws UnsupportedEncodingException, FileNotFoundException {
71 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
73 CompositeNode loadedCompositeNode = TestUtils.readInputToCnSn("/parts/ietf-interfaces_interfaces.xml", true,
74 XmlToCompositeNodeProvider.INSTANCE);
75 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
77 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
78 assertEquals(200, response.getStatus());
80 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
81 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
83 response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
84 assertEquals(404, response.getStatus());
88 public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException {
89 String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
91 CompositeNode loadedCompositeNode = TestUtils.readInputToCnSn("/parts/ietf-interfaces_interfaces.xml", true,
92 XmlToCompositeNodeProvider.INSTANCE);
94 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
96 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
97 assertEquals(200, response.getStatus());
99 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
100 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
102 response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
103 assertEquals(404, response.getStatus());
106 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
107 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
111 protected Application configure() {
112 enable(TestProperties.LOG_TRAFFIC);
113 enable(TestProperties.DUMP_ENTITY);
114 enable(TestProperties.RECORD_LOG_LEVEL);
115 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
117 ResourceConfig resourceConfig = new ResourceConfig();
118 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
119 XmlToCompositeNodeProvider.INSTANCE);
120 return resourceConfig;