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.Draft01;
33 import org.opendaylight.controller.sal.rest.api.RestconfService;
34 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
35 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
36 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
37 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
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.data+xml");
53 private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
56 public static void init() throws FileNotFoundException {
57 Set<Module> allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs")
59 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
60 controllerContext = ControllerContext.getInstance();
61 controllerContext.setSchemas(schemaContext);
62 brokerFacade = mock(BrokerFacade.class);
63 restconfImpl = RestconfImpl.getInstance();
64 restconfImpl.setBroker(brokerFacade);
65 restconfImpl.setControllerContext(controllerContext);
70 List<LogRecord> loggedRecords = getLoggedRecords();
71 for (LogRecord l : loggedRecords) {
72 System.out.println(l.getMessage());
77 public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException {
78 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
80 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
81 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
82 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
84 Response response = target(uri).request(MEDIA_TYPE).get();
85 assertEquals(200, response.getStatus());
89 public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException {
90 String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0");
92 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
93 Entity.entity("<SimpleNode/>", MEDIA_TYPE));
94 assertEquals(400, response.getStatus());
96 response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
97 Entity.entity("<SimpleNode>", MEDIA_TYPE));
98 assertEquals(400, response.getStatus());
102 public void testXmlToCompositeNode404NotFound() throws UnsupportedEncodingException, URISyntaxException {
103 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
105 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
107 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
108 assertEquals(404, response.getStatus());
112 public void testXmlToCompositeNode400() throws UnsupportedEncodingException, URISyntaxException {
113 String uri = createUri("/datastore/", "simple-nodes:user/name");
115 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
117 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
118 assertEquals(400, response.getStatus());
122 public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException {
123 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
124 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
125 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
126 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
127 TransactionStatus.COMMITED).build();
128 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
129 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
130 .thenReturn(dummyFuture);
131 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
132 .thenReturn(dummyFuture);
134 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
135 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
136 assertEquals(200, response.getStatus());
137 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
138 assertEquals(204, response.getStatus());
140 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
141 response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
142 assertEquals(200, response.getStatus());
143 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
144 assertEquals(204, response.getStatus());
146 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
147 response = target(uri).request(MEDIA_TYPE).put(entity);
148 assertEquals(200, response.getStatus());
149 response = target(uri).request(MEDIA_TYPE).post(entity);
150 assertEquals(204, response.getStatus());
154 public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException {
155 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
156 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
157 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
158 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
159 TransactionStatus.FAILED).build();
160 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
161 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
162 .thenReturn(dummyFuture);
163 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
164 .thenReturn(dummyFuture);
166 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
167 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
168 assertEquals(500, response.getStatus());
169 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
170 assertEquals(500, response.getStatus());
172 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
173 response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
174 assertEquals(500, response.getStatus());
175 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
176 assertEquals(500, response.getStatus());
178 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
179 response = target(uri).request(MEDIA_TYPE).put(entity);
180 assertEquals(500, response.getStatus());
181 response = target(uri).request(MEDIA_TYPE).post(entity);
182 assertEquals(500, response.getStatus());
185 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
186 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
190 protected Application configure() {
191 enable(TestProperties.LOG_TRAFFIC);
192 enable(TestProperties.DUMP_ENTITY);
193 enable(TestProperties.RECORD_LOG_LEVEL);
194 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
196 ResourceConfig resourceConfig = new ResourceConfig();
197 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
198 XmlToCompositeNodeProvider.INSTANCE);
199 return resourceConfig;