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").getPath());
58 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
59 controllerContext = ControllerContext.getInstance();
60 controllerContext.setSchemas(schemaContext);
61 brokerFacade = mock(BrokerFacade.class);
62 restconfImpl = RestconfImpl.getInstance();
63 restconfImpl.setBroker(brokerFacade);
64 restconfImpl.setControllerContext(controllerContext);
69 List<LogRecord> loggedRecords = getLoggedRecords();
70 for (LogRecord l : loggedRecords) {
71 System.out.println(l.getMessage());
76 public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException {
77 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
79 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
80 CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream);
81 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
83 Response response = target(uri).request(MEDIA_TYPE).get();
84 assertEquals(200, response.getStatus());
88 public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException {
89 String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0");
91 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
92 Entity.entity("<SimpleNode/>", MEDIA_TYPE));
93 assertEquals(400, response.getStatus());
95 response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
96 Entity.entity("<SimpleNode>", MEDIA_TYPE));
97 assertEquals(400, response.getStatus());
101 public void testXmlToCompositeNode404NotFound() throws UnsupportedEncodingException, URISyntaxException {
102 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
104 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
106 Response response = target(uri).request(Draft01.MediaTypes.DATA+RestconfService.XML).get();
107 assertEquals(404, response.getStatus());
111 public void testXmlToCompositeNode400() throws UnsupportedEncodingException, URISyntaxException {
112 String uri = createUri("/datastore/", "simple-nodes:user/name");
114 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
116 Response response = target(uri).request(Draft01.MediaTypes.DATA+RestconfService.XML).get();
117 assertEquals(400, response.getStatus());
121 public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException {
122 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
123 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
124 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
125 RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build();
126 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
127 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
128 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
130 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
131 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
132 assertEquals(204, response.getStatus());
133 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
134 assertEquals(200, response.getStatus());
136 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
137 response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
138 assertEquals(204, response.getStatus());
139 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
140 assertEquals(200, response.getStatus());
142 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
143 response = target(uri).request(MEDIA_TYPE).put(entity);
144 assertEquals(204, response.getStatus());
145 response = target(uri).request(MEDIA_TYPE).post(entity);
146 assertEquals(200, response.getStatus());
150 public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException {
151 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
152 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
153 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
154 RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.FAILED).build();
155 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
156 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
157 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
159 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
160 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
161 assertEquals(500, response.getStatus());
162 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
163 assertEquals(500, response.getStatus());
165 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
166 response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
167 assertEquals(500, response.getStatus());
168 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
169 assertEquals(500, response.getStatus());
171 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
172 response = target(uri).request(MEDIA_TYPE).put(entity);
173 assertEquals(500, response.getStatus());
174 response = target(uri).request(MEDIA_TYPE).post(entity);
175 assertEquals(500, response.getStatus());
178 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
179 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
183 protected Application configure() {
184 enable(TestProperties.LOG_TRAFFIC);
185 enable(TestProperties.DUMP_ENTITY);
186 enable(TestProperties.RECORD_LOG_LEVEL);
187 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
189 ResourceConfig resourceConfig = new ResourceConfig();
190 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
191 return resourceConfig;