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;
8 import java.io.FileNotFoundException;
9 import java.io.UnsupportedEncodingException;
11 import java.net.URLEncoder;
12 import java.util.List;
14 import java.util.logging.Level;
15 import java.util.logging.LogRecord;
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.glassfish.jersey.test.TestProperties;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
28 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
29 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
30 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
32 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.model.api.Module;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import com.google.common.base.Charsets;
39 public class ReadConfAndOperDataTest extends JerseyTest {
41 private static ControllerContext controllerContext;
42 private static BrokerFacade brokerFacade;
43 private static RestconfImpl restconfImpl;
44 private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
47 public static void init() throws FileNotFoundException {
48 Set<Module> allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs")
50 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
51 controllerContext = ControllerContext.getInstance();
52 controllerContext.setSchemas(schemaContext);
53 brokerFacade = mock(BrokerFacade.class);
54 restconfImpl = RestconfImpl.getInstance();
55 restconfImpl.setBroker(brokerFacade);
56 restconfImpl.setControllerContext(controllerContext);
61 List<LogRecord> loggedRecords = getLoggedRecords();
62 for (LogRecord l : loggedRecords) {
63 System.out.println(l.getMessage());
68 public void testReadConfigurationData() throws UnsupportedEncodingException, FileNotFoundException {
70 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
72 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder("/parts/ietf-interfaces_interfaces.xml");
73 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
75 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
76 assertEquals(200, response.getStatus());
78 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
79 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
81 response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
82 assertEquals(404, response.getStatus());
86 public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException {
87 String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
90 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNodeWithXmlTreeBuilder("/parts/ietf-interfaces_interfaces.xml");
91 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
93 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
94 assertEquals(200, response.getStatus());
96 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
97 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
99 response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
100 assertEquals(404, response.getStatus());
103 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
104 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
108 protected Application configure() {
109 enable(TestProperties.LOG_TRAFFIC);
110 enable(TestProperties.DUMP_ENTITY);
111 enable(TestProperties.RECORD_LOG_LEVEL);
112 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
114 ResourceConfig resourceConfig = new ResourceConfig();
115 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
116 XmlToCompositeNodeProvider.INSTANCE);
117 return resourceConfig;