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;
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.api.Draft01;
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.*;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.data.api.*;
38 import org.opendaylight.yangtools.yang.data.impl.NodeFactory;
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 XmlProvidersTest extends JerseyTest {
46 private static ControllerContext controllerContext;
47 private static BrokerFacade brokerFacade;
48 private static RestconfImpl restconfImpl;
49 private static final MediaType MEDIA_TYPE = new MediaType("application", "vnd.yang.data+xml");
50 private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
53 public static void init() throws FileNotFoundException {
54 Set<Module> allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs")
56 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
57 controllerContext = ControllerContext.getInstance();
58 controllerContext.setSchemas(schemaContext);
59 brokerFacade = mock(BrokerFacade.class);
60 restconfImpl = RestconfImpl.getInstance();
61 restconfImpl.setBroker(brokerFacade);
62 restconfImpl.setControllerContext(controllerContext);
67 List<LogRecord> loggedRecords = getLoggedRecords();
68 for (LogRecord l : loggedRecords) {
69 System.out.println(l.getMessage());
74 public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException {
75 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
77 CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
78 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
80 Response response = target(uri).request(MEDIA_TYPE).get();
81 assertEquals(200, response.getStatus());
84 private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
85 CompositeNode intface;
87 intface = new CompositeNodeWrapper(new URI("interface"), "interface");
88 List<Node<?>> childs = new ArrayList<>();
90 childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
91 childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
92 childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
93 childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
94 intface.setValue(childs);
96 } catch (URISyntaxException e) {
103 public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException {
104 String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0");
106 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
107 Entity.entity("<SimpleNode/>", MEDIA_TYPE));
108 assertEquals(400, response.getStatus());
110 response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
111 Entity.entity("<SimpleNode>", MEDIA_TYPE));
112 assertEquals(400, response.getStatus());
116 public void testXmlToCompositeNode404NotFound() throws UnsupportedEncodingException, URISyntaxException {
117 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
119 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
121 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
122 assertEquals(404, response.getStatus());
126 public void testXmlToCompositeNode400() throws UnsupportedEncodingException, URISyntaxException {
127 String uri = createUri("/datastore/", "simple-nodes:user/name");
129 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
131 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
132 assertEquals(400, response.getStatus());
136 public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException {
137 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
138 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
139 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
140 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
141 TransactionStatus.COMMITED).build();
142 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
143 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
144 .thenReturn(dummyFuture);
145 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
146 .thenReturn(dummyFuture);
148 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
149 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
150 assertEquals(200, response.getStatus());
151 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
152 assertEquals(204, response.getStatus());
154 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
155 response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
156 assertEquals(200, response.getStatus());
157 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
158 assertEquals(204, response.getStatus());
160 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
161 response = target(uri).request(MEDIA_TYPE).put(entity);
162 assertEquals(200, response.getStatus());
163 response = target(uri).request(MEDIA_TYPE).post(entity);
164 assertEquals(204, response.getStatus());
168 public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException {
169 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
170 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
171 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
172 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
173 TransactionStatus.FAILED).build();
174 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
175 when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
176 .thenReturn(dummyFuture);
177 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
178 .thenReturn(dummyFuture);
180 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
181 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
182 assertEquals(500, response.getStatus());
183 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
184 assertEquals(500, response.getStatus());
186 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
187 response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
188 assertEquals(500, response.getStatus());
189 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
190 assertEquals(500, response.getStatus());
192 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
193 response = target(uri).request(MEDIA_TYPE).put(entity);
194 assertEquals(500, response.getStatus());
195 response = target(uri).request(MEDIA_TYPE).post(entity);
196 assertEquals(500, response.getStatus());
199 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
200 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
204 protected Application configure() {
205 enable(TestProperties.LOG_TRAFFIC);
206 enable(TestProperties.DUMP_ENTITY);
207 enable(TestProperties.RECORD_LOG_LEVEL);
208 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
210 ResourceConfig resourceConfig = new ResourceConfig();
211 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
212 XmlToCompositeNodeProvider.INSTANCE);
213 return resourceConfig;