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.InputStream;
10 import java.io.UnsupportedEncodingException;
12 import java.net.URLEncoder;
13 import java.util.List;
15 import java.util.concurrent.Future;
16 import java.util.logging.Level;
17 import java.util.logging.LogRecord;
19 import javax.ws.rs.client.Entity;
20 import javax.ws.rs.core.Application;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.Response;
24 import org.glassfish.jersey.server.ResourceConfig;
25 import org.glassfish.jersey.test.JerseyTest;
26 import org.glassfish.jersey.test.TestProperties;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
31 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
32 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
34 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
35 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
38 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import com.google.common.base.Charsets;
44 public class ReadConfAndOperDataTest extends JerseyTest {
46 private static ControllerContext controllerContext;
47 private static BrokerFacade brokerFacade;
48 private static RestconfImpl restconfImpl;
49 private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
52 public static void init() throws FileNotFoundException {
53 Set<Module> allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs")
55 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
56 controllerContext = ControllerContext.getInstance();
57 controllerContext.setSchemas(schemaContext);
58 brokerFacade = mock(BrokerFacade.class);
59 restconfImpl = RestconfImpl.getInstance();
60 restconfImpl.setBroker(brokerFacade);
61 restconfImpl.setControllerContext(controllerContext);
66 List<LogRecord> loggedRecords = getLoggedRecords();
67 for (LogRecord l : loggedRecords) {
68 System.out.println(l.getMessage());
73 public void testReadConfigurationData() throws UnsupportedEncodingException, FileNotFoundException {
75 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
77 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
78 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
79 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
81 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
82 assertEquals(200, response.getStatus());
84 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
85 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
87 response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
88 assertEquals(404, response.getStatus());
92 public void testReadOperationalData() throws UnsupportedEncodingException, FileNotFoundException {
93 String uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
95 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
96 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
97 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
99 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
100 assertEquals(200, response.getStatus());
102 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/example");
103 when(brokerFacade.readConfigurationData(any(InstanceIdentifier.class))).thenReturn(null);
105 response = target(uri).request(MEDIA_TYPE_DRAFT02).get();
106 assertEquals(404, response.getStatus());
109 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
110 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
114 protected Application configure() {
115 enable(TestProperties.LOG_TRAFFIC);
116 enable(TestProperties.DUMP_ENTITY);
117 enable(TestProperties.RECORD_LOG_LEVEL);
118 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
120 ResourceConfig resourceConfig = new ResourceConfig();
121 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
122 XmlToCompositeNodeProvider.INSTANCE);
123 return resourceConfig;