1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.mockito.Matchers.any;
6 import static org.mockito.Mockito.mock;
7 import static org.mockito.Mockito.when;
9 import java.io.FileNotFoundException;
10 import java.io.InputStream;
11 import java.io.UnsupportedEncodingException;
13 import java.net.URISyntaxException;
14 import java.net.URLEncoder;
15 import java.util.ArrayList;
16 import java.util.List;
18 import java.util.concurrent.Future;
19 import java.util.logging.Level;
20 import java.util.logging.LogRecord;
22 import javax.ws.rs.client.Entity;
23 import javax.ws.rs.core.Application;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.Response;
27 import org.glassfish.jersey.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
29 import org.glassfish.jersey.test.TestProperties;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
34 import org.opendaylight.controller.sal.rest.api.Draft01;
35 import org.opendaylight.controller.sal.rest.api.RestconfService;
36 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
37 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
38 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
39 import org.opendaylight.controller.sal.restconf.impl.CompositeNodeWrapper;
40 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
41 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
42 import org.opendaylight.controller.sal.restconf.impl.SimpleNodeWrapper;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
45 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.data.api.Node;
47 import org.opendaylight.yangtools.yang.model.api.Module;
48 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
50 import com.google.common.base.Charsets;
52 public class XmlProvidersTest extends JerseyTest {
54 private static ControllerContext controllerContext;
55 private static BrokerFacade brokerFacade;
56 private static RestconfImpl restconfImpl;
57 private static final MediaType MEDIA_TYPE = new MediaType("application", "vnd.yang.data+xml");
58 private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
61 public static void init() throws FileNotFoundException {
62 Set<Module> allModules = TestUtils.loadModulesFrom("/full-versions/yangs");
63 assertNotNull(allModules);
64 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
65 controllerContext = ControllerContext.getInstance();
66 controllerContext.setSchemas(schemaContext);
67 brokerFacade = mock(BrokerFacade.class);
68 restconfImpl = RestconfImpl.getInstance();
69 restconfImpl.setBroker(brokerFacade);
70 restconfImpl.setControllerContext(controllerContext);
75 List<LogRecord> loggedRecords = getLoggedRecords();
76 for (LogRecord l : loggedRecords) {
77 System.out.println(l.getMessage());
82 public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException {
83 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
85 CompositeNode loadedCompositeNode = prepareCompositeNodeWithIetfInterfacesInterfacesData();
86 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode);
88 Response response = target(uri).request(MEDIA_TYPE).get();
89 assertEquals(200, response.getStatus());
92 private CompositeNode prepareCompositeNodeWithIetfInterfacesInterfacesData() {
93 CompositeNode intface;
95 intface = new CompositeNodeWrapper(new URI("interface"), "interface");
96 List<Node<?>> childs = new ArrayList<>();
98 childs.add(new SimpleNodeWrapper(new URI("name"), "name", "eth0"));
99 childs.add(new SimpleNodeWrapper(new URI("type"), "type", "ethernetCsmacd"));
100 childs.add(new SimpleNodeWrapper(new URI("enabled"), "enabled", Boolean.FALSE));
101 childs.add(new SimpleNodeWrapper(new URI("description"), "description", "some interface"));
102 intface.setValue(childs);
104 } catch (URISyntaxException e) {
111 public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException {
112 String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0");
114 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
115 Entity.entity("<SimpleNode/>", MEDIA_TYPE));
116 assertEquals(400, response.getStatus());
118 response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
119 Entity.entity("<SimpleNode>", MEDIA_TYPE));
120 assertEquals(400, response.getStatus());
124 public void testXmlToCompositeNode404NotFound() throws UnsupportedEncodingException, URISyntaxException {
125 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
127 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
129 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
130 assertEquals(404, response.getStatus());
134 public void testXmlToCompositeNode400() throws UnsupportedEncodingException, URISyntaxException {
135 String uri = createUri("/datastore/", "simple-nodes:user/name");
137 when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null);
139 Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get();
140 assertEquals(400, response.getStatus());
144 public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException {
145 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
146 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
147 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
148 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
149 TransactionStatus.COMMITED).build();
150 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
151 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
152 .thenReturn(dummyFuture);
153 when(brokerFacade.commitConfigurationDataPost(any(InstanceIdentifier.class), any(CompositeNode.class)))
154 .thenReturn(dummyFuture);
156 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
157 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
158 assertEquals(200, response.getStatus());
160 uri = createUri("/config/", "ietf-interfaces:interfaces");
161 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
162 assertEquals(204, response.getStatus());
164 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
165 response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
166 assertEquals(200, response.getStatus());
168 uri = createUri("/config/", "ietf-interfaces:interfaces");
169 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
170 assertEquals(204, response.getStatus());
172 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
173 entity = Entity.entity(xml, MEDIA_TYPE);
174 response = target(uri).request(MEDIA_TYPE).put(entity);
175 assertEquals(200, response.getStatus());
177 uri = createUri("/datastore/", "ietf-interfaces:interfaces");
178 entity = Entity.entity(xml, MEDIA_TYPE);
179 response = target(uri).request(MEDIA_TYPE).post(entity);
180 assertEquals(204, response.getStatus());
184 public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException {
185 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
186 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
187 Entity<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
188 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
189 TransactionStatus.FAILED).build();
190 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
191 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
192 .thenReturn(dummyFuture);
194 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
195 Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
196 assertEquals(500, response.getStatus());
197 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
198 assertEquals(500, response.getStatus());
200 uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
201 response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity);
202 assertEquals(500, response.getStatus());
203 response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity);
204 assertEquals(500, response.getStatus());
206 uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
207 entity = Entity.entity(xml, MEDIA_TYPE);
208 response = target(uri).request().put(entity);
209 assertEquals(500, response.getStatus());
210 response = target(uri).request().accept(MEDIA_TYPE).post(entity);
211 assertEquals(500, response.getStatus());
214 private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
215 return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
219 protected Application configure() {
220 enable(TestProperties.LOG_TRAFFIC);
221 enable(TestProperties.DUMP_ENTITY);
222 enable(TestProperties.RECORD_LOG_LEVEL);
223 set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
225 ResourceConfig resourceConfig = new ResourceConfig();
226 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
227 XmlToCompositeNodeProvider.INSTANCE);
228 return resourceConfig;