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;
7 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.XML;
8 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
10 import java.io.FileNotFoundException;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.UnsupportedEncodingException;
14 import java.net.URISyntaxException;
15 import java.util.concurrent.Future;
17 import javax.ws.rs.client.Entity;
18 import javax.ws.rs.core.Application;
19 import javax.ws.rs.core.MediaType;
20 import javax.ws.rs.core.Response;
22 import org.glassfish.jersey.server.ResourceConfig;
23 import org.glassfish.jersey.test.JerseyTest;
24 import org.junit.BeforeClass;
25 import org.junit.Ignore;
26 import org.junit.Test;
27 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
28 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
29 import org.opendaylight.controller.sal.core.api.mount.MountService;
30 import org.opendaylight.controller.sal.rest.api.Draft02;
31 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
32 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
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.RestconfImpl;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
40 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 public class RestPutOperationTest extends JerseyTest {
45 private static String xmlData;
46 private static String xmlData2;
47 private static String xmlData3;
49 private static BrokerFacade brokerFacade;
50 private static RestconfImpl restconfImpl;
51 private static SchemaContext schemaContextYangsIetf;
52 private static SchemaContext schemaContextTestModule;
55 public static void init() throws IOException {
56 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
57 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
58 ControllerContext controllerContext = ControllerContext.getInstance();
59 controllerContext.setSchemas(schemaContextYangsIetf);
60 brokerFacade = mock(BrokerFacade.class);
61 restconfImpl = RestconfImpl.getInstance();
62 restconfImpl.setBroker(brokerFacade);
63 restconfImpl.setControllerContext(controllerContext);
67 private static void loadData() throws IOException {
68 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
69 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
70 InputStream xmlStream2 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
71 xmlData2 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream2));
72 InputStream xmlStream3 = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data7.xml");
73 xmlData3 = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream3));
77 protected Application configure() {
78 /* enable/disable Jersey logs to console */
79 // enable(TestProperties.LOG_TRAFFIC);
80 // enable(TestProperties.DUMP_ENTITY);
81 // enable(TestProperties.RECORD_LOG_LEVEL);
82 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
83 ResourceConfig resourceConfig = new ResourceConfig();
84 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
85 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
86 JsonToCompositeNodeProvider.INSTANCE);
87 return resourceConfig;
91 * Tests of status codes for "/config/{identifier}".
94 public void putConfigStatusCodes() throws UnsupportedEncodingException {
95 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
96 mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
97 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
99 mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
100 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
104 * Tests of status codes for "/datastore/{identifier}".
107 public void putDatastoreStatusCodes() throws UnsupportedEncodingException {
108 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
109 mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
110 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
112 mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
113 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
117 public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
118 FileNotFoundException, URISyntaxException {
120 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
121 TransactionStatus.COMMITED).build();
122 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
124 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
125 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
127 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
128 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
129 Entity<String> entity = Entity.entity(xml, Draft02.MediaTypes.DATA + XML);
131 MountInstance mountInstance = mock(MountInstance.class);
132 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
133 MountService mockMountService = mock(MountService.class);
134 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
136 ControllerContext.getInstance().setMountService(mockMountService);
138 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
139 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
141 uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont");
142 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData2));
146 public void putDataMountPointIntoHighestElement() throws UnsupportedEncodingException, URISyntaxException {
147 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
148 TransactionStatus.COMMITED).build();
149 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
151 brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
152 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
154 MountInstance mountInstance = mock(MountInstance.class);
155 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
156 MountService mockMountService = mock(MountService.class);
157 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
159 ControllerContext.getInstance().setMountService(mockMountService);
161 String uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount");
162 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData3));
165 private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
166 return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
169 private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
170 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
172 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
173 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
174 .thenReturn(dummyFuture);