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.*;
19 import org.glassfish.jersey.server.ResourceConfig;
20 import org.glassfish.jersey.test.JerseyTest;
21 import org.glassfish.jersey.test.TestProperties;
23 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
24 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
25 import org.opendaylight.controller.sal.restconf.impl.*;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import com.google.common.base.Charsets;
33 public class ReadConfAndOperDataTest extends JerseyTest {
35 private static ControllerContext controllerContext;
36 private static BrokerFacade brokerFacade;
37 private static RestconfImpl restconfImpl;
38 private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
41 public static void init() throws FileNotFoundException {
42 Set<Module> allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs")
44 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
45 controllerContext = ControllerContext.getInstance();
46 controllerContext.setSchemas(schemaContext);
47 brokerFacade = mock(BrokerFacade.class);
48 restconfImpl = RestconfImpl.getInstance();
49 restconfImpl.setBroker(brokerFacade);
50 restconfImpl.setControllerContext(controllerContext);
55 List<LogRecord> loggedRecords = getLoggedRecords();
56 for (LogRecord l : loggedRecords) {
57 System.out.println(l.getMessage());
62 public void testReadConfigurationData() throws UnsupportedEncodingException, FileNotFoundException {
64 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
66 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode("/parts/ietf-interfaces_interfaces.xml", true);
67 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
69 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
70 assertEquals(200, response.getStatus());
72 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
73 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
75 response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
76 assertEquals(404, response.getStatus());
81 public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException {
82 String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
84 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode("/parts/ietf-interfaces_interfaces.xml");
85 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
87 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
88 assertEquals(200, response.getStatus());
90 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
91 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
93 response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
94 assertEquals(404, response.getStatus());
97 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
98 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
102 protected Application configure() {
103 enable(TestProperties.LOG_TRAFFIC);
104 enable(TestProperties.DUMP_ENTITY);
105 enable(TestProperties.RECORD_LOG_LEVEL);
106 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
108 ResourceConfig resourceConfig = new ResourceConfig();
109 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
110 XmlToCompositeNodeProvider.INSTANCE);
111 return resourceConfig;