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.URISyntaxException;
13 import java.net.URLEncoder;
14 import java.util.List;
16 import java.util.concurrent.Future;
17 import java.util.logging.Level;
18 import java.util.logging.LogRecord;
20 import javax.ws.rs.client.Entity;
21 import javax.ws.rs.core.Application;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.glassfish.jersey.test.JerseyTest;
27 import org.glassfish.jersey.test.TestProperties;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
32 import org.opendaylight.controller.sal.rest.api.RestconfService;
33 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
34 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
35 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
36 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
37 import org.opendaylight.controller.sal.restconf.impl.MediaTypes;
38 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
41 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.model.api.Module;
43 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
45 import com.google.common.base.Charsets;
47 public class XmlProvidersTest extends JerseyTest {
49 private static ControllerContext controllerContext;
50 private static BrokerFacade brokerFacade;
51 private static RestconfImpl restconfImpl;
52 private static final MediaType MEDIA_TYPE = new MediaType("application", "vnd.yang.api+xml");
55 public static void init() throws FileNotFoundException {
56 Set<Module> allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath());
57 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
58 controllerContext = ControllerContext.getInstance();
59 controllerContext.setSchemas(schemaContext);
60 brokerFacade = mock(BrokerFacade.class);
61 restconfImpl = RestconfImpl.getInstance();
62 restconfImpl.setBroker(brokerFacade);
63 restconfImpl.setControllerContext(controllerContext);
68 List<LogRecord> loggedRecords = getLoggedRecords();
69 for (LogRecord l : loggedRecords) {
70 System.out.println(l.getMessage());
75 public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException {
76 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
78 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
79 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
80 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
82 Response response = target(uri).request(MEDIA_TYPE).get();
83 assertEquals(200, response.getStatus());
87 public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException {
88 String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0");
90 Response response = target(uri).request(MediaTypes.API + RestconfService.XML).post(
91 Entity.entity("<SimpleNode/>", MEDIA_TYPE));
92 assertEquals(400, response.getStatus());
94 response = target(uri).request(MediaTypes.API + RestconfService.XML).post(
95 Entity.entity("<SimpleNode>", MEDIA_TYPE));
96 assertEquals(400, response.getStatus());
100 public void testXmlToCompositeNode404NotFound() throws UnsupportedEncodingException, URISyntaxException {
101 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
103 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
105 Response response = target(uri).request(MediaTypes.API+RestconfService.XML).get();
106 assertEquals(404, response.getStatus());
110 public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException {
111 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
112 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
113 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE);
114 RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build();
115 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
116 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
117 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
119 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
120 Response response = target(uri).request(MEDIA_TYPE).put(entity);
121 assertEquals(200, response.getStatus());
122 response = target(uri).request(MEDIA_TYPE).post(entity);
123 assertEquals(204, response.getStatus());
125 uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
126 response = target(uri).request(MEDIA_TYPE).put(entity);
127 assertEquals(200, response.getStatus());
128 response = target(uri).request(MEDIA_TYPE).post(entity);
129 assertEquals(204, response.getStatus());
131 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
132 response = target(uri).request(MEDIA_TYPE).put(entity);
133 assertEquals(200, response.getStatus());
134 response = target(uri).request(MEDIA_TYPE).post(entity);
135 assertEquals(204, response.getStatus());
139 public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException {
140 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
141 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
142 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE);
143 RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.FAILED).build();
144 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
145 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
146 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
148 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
149 Response response = target(uri).request(MEDIA_TYPE).put(entity);
150 assertEquals(500, response.getStatus());
151 response = target(uri).request(MEDIA_TYPE).post(entity);
152 assertEquals(500, response.getStatus());
154 uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
155 response = target(uri).request(MEDIA_TYPE).put(entity);
156 assertEquals(500, response.getStatus());
157 response = target(uri).request(MEDIA_TYPE).post(entity);
158 assertEquals(500, response.getStatus());
160 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
161 response = target(uri).request(MEDIA_TYPE).put(entity);
162 assertEquals(500, response.getStatus());
163 response = target(uri).request(MEDIA_TYPE).post(entity);
164 assertEquals(500, response.getStatus());
167 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
168 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
172 protected Application configure() {
173 enable(TestProperties.LOG_TRAFFIC);
174 enable(TestProperties.DUMP_ENTITY);
175 enable(TestProperties.RECORD_LOG_LEVEL);
176 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
178 ResourceConfig resourceConfig = new ResourceConfig();
179 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
180 return resourceConfig;