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.Test;
26 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
27 import org.opendaylight.controller.sal.core.api.mount.MountInstance;
28 import org.opendaylight.controller.sal.core.api.mount.MountService;
29 import org.opendaylight.controller.sal.rest.api.Draft02;
30 import org.opendaylight.controller.sal.rest.impl.JsonToCompositeNodeProvider;
31 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
32 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
33 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
34 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
35 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
36 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
39 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 public class RestPutOperationTest extends JerseyTest {
44 private static String xmlData;
46 private static BrokerFacade brokerFacade;
47 private static RestconfImpl restconfImpl;
48 private static SchemaContext schemaContextYangsIetf;
49 private static SchemaContext schemaContextTestModule;
52 public static void init() throws IOException {
53 schemaContextYangsIetf = TestUtils.loadSchemaContext("/full-versions/yangs");
54 schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
55 ControllerContext controllerContext = ControllerContext.getInstance();
56 controllerContext.setSchemas(schemaContextYangsIetf);
57 brokerFacade = mock(BrokerFacade.class);
58 restconfImpl = RestconfImpl.getInstance();
59 restconfImpl.setBroker(brokerFacade);
60 restconfImpl.setControllerContext(controllerContext);
64 private static void loadData() throws IOException {
65 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml");
66 xmlData = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
70 protected Application configure() {
71 /* enable/disable Jersey logs to console */
72 // enable(TestProperties.LOG_TRAFFIC);
73 // enable(TestProperties.DUMP_ENTITY);
74 // enable(TestProperties.RECORD_LOG_LEVEL);
75 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
76 ResourceConfig resourceConfig = new ResourceConfig();
77 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
78 StructuredDataToJsonProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE,
79 JsonToCompositeNodeProvider.INSTANCE);
80 return resourceConfig;
84 * Tests of status codes for "/config/{identifier}".
87 public void putConfigStatusCodes() throws UnsupportedEncodingException {
88 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0");
89 mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
90 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
92 mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
93 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
98 * Tests of status codes for "/datastore/{identifier}".
101 public void putDatastoreStatusCodes() throws UnsupportedEncodingException {
102 String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0");
103 mockCommitConfigurationDataPutMethod(TransactionStatus.COMMITED);
104 assertEquals(200, put(uri, MediaType.APPLICATION_XML, xmlData));
106 mockCommitConfigurationDataPutMethod(TransactionStatus.FAILED);
107 assertEquals(500, put(uri, MediaType.APPLICATION_XML, xmlData));
111 public void testRpcResultCommitedToStatusCodesWithMountPoint() throws UnsupportedEncodingException,
112 FileNotFoundException, URISyntaxException {
114 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(TransactionStatus.COMMITED)
116 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
117 when(brokerFacade.commitConfigurationDataPutBehindMountPoint(any(MountInstance.class),
118 any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture);
121 InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/full-versions/test-data2/data2.xml");
122 String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream));
123 Entity<String> entity = Entity.entity(xml, Draft02.MediaTypes.DATA + XML);
125 MountInstance mountInstance = mock(MountInstance.class);
126 when(mountInstance.getSchemaContext()).thenReturn(schemaContextTestModule);
127 MountService mockMountService = mock(MountService.class);
128 when(mockMountService.getMountPoint(any(InstanceIdentifier.class))).thenReturn(mountInstance);
130 ControllerContext.getInstance().setMountService(mockMountService);
132 String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/0/yang-ext:mount/test-module:cont");
133 Response response = target(uri).request(Draft02.MediaTypes.DATA + XML).put(entity);
134 assertEquals(200, response.getStatus());
136 uri = createUri("/config/", "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont");
137 response = target(uri).request(Draft02.MediaTypes.DATA + XML).put(entity);
138 assertEquals(200, response.getStatus());
141 private int put(String uri, String mediaType, String data) throws UnsupportedEncodingException {
142 return target(uri).request(mediaType).put(Entity.entity(data, mediaType)).getStatus();
145 private void mockCommitConfigurationDataPutMethod(TransactionStatus statusName) {
146 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName)
148 Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
149 when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class)))
150 .thenReturn(dummyFuture);