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 testXmlToCompositeNode400() throws UnsupportedEncodingException, URISyntaxException {
111 String uri = createUri("/datastore/", "simple-nodes:user/name");
113 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
115 Response response = target(uri).request(MediaTypes.API+RestconfService.XML).get();
116 assertEquals(400, response.getStatus());
120 public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException {
121 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
122 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
123 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE);
124 RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build();
125 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
126 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
127 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
129 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
130 Response response = target(uri).request(MEDIA_TYPE).put(entity);
131 assertEquals(200, response.getStatus());
132 response = target(uri).request(MEDIA_TYPE).post(entity);
133 assertEquals(204, response.getStatus());
135 uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
136 response = target(uri).request(MEDIA_TYPE).put(entity);
137 assertEquals(200, response.getStatus());
138 response = target(uri).request(MEDIA_TYPE).post(entity);
139 assertEquals(204, response.getStatus());
141 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
142 response = target(uri).request(MEDIA_TYPE).put(entity);
143 assertEquals(200, response.getStatus());
144 response = target(uri).request(MEDIA_TYPE).post(entity);
145 assertEquals(204, response.getStatus());
149 public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException {
150 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
151 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
152 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE);
153 RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.FAILED).build();
154 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
155 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
156 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
158 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
159 Response response = target(uri).request(MEDIA_TYPE).put(entity);
160 assertEquals(500, response.getStatus());
161 response = target(uri).request(MEDIA_TYPE).post(entity);
162 assertEquals(500, response.getStatus());
164 uri = createUri("/operational/", "ietf-interfaces:interfaces/interface/eth0");
165 response = target(uri).request(MEDIA_TYPE).put(entity);
166 assertEquals(500, response.getStatus());
167 response = target(uri).request(MEDIA_TYPE).post(entity);
168 assertEquals(500, response.getStatus());
170 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
171 response = target(uri).request(MEDIA_TYPE).put(entity);
172 assertEquals(500, response.getStatus());
173 response = target(uri).request(MEDIA_TYPE).post(entity);
174 assertEquals(500, response.getStatus());
177 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
178 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
182 protected Application configure() {
183 enable(TestProperties.LOG_TRAFFIC);
184 enable(TestProperties.DUMP_ENTITY);
185 enable(TestProperties.RECORD_LOG_LEVEL);
186 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
188 ResourceConfig resourceConfig = new ResourceConfig();
189 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE);
190 return resourceConfig;