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;
8 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
10 import java.io.FileNotFoundException;
11 import java.io.UnsupportedEncodingException;
13 import java.util.concurrent.Future;
15 import javax.ws.rs.core.Application;
16 import javax.ws.rs.core.MediaType;
17 import javax.ws.rs.core.Response;
19 import org.glassfish.jersey.server.ResourceConfig;
20 import org.glassfish.jersey.test.JerseyTest;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
24 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
25 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
26 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
27 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
28 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
29 import org.opendaylight.yangtools.yang.common.RpcResult;
30 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 public class RestDeleteOperationTest extends JerseyTest {
36 private static ControllerContext controllerContext;
37 private static BrokerFacade brokerFacade;
38 private static RestconfImpl restconfImpl;
41 public static void init() throws FileNotFoundException {
42 Set<Module> allModules = TestUtils.loadModulesFrom("/test-config-data/yang1");
43 assertNotNull(allModules);
44 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
45 controllerContext = ControllerContext.getInstance();
46 controllerContext.setSchemas(schemaContext);
47 brokerFacade = mock(BrokerFacade.class);
48 restconfImpl = RestconfImpl.getInstance();
49 restconfImpl.setBroker(brokerFacade);
50 restconfImpl.setControllerContext(controllerContext);
54 protected Application configure() {
55 /* enable/disable Jersey logs to console */
56 // enable(TestProperties.LOG_TRAFFIC);
57 // enable(TestProperties.DUMP_ENTITY);
58 // enable(TestProperties.RECORD_LOG_LEVEL);
59 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
60 ResourceConfig resourceConfig = new ResourceConfig();
61 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
62 XmlToCompositeNodeProvider.INSTANCE);
63 return resourceConfig;
67 public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
68 String uri = createUri("/config/", "test-interface:interfaces");
69 Future<RpcResult<TransactionStatus>> dummyFuture = createFuture(TransactionStatus.COMMITED);
70 when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
71 Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
72 assertEquals(200, response.getStatus());
74 dummyFuture = createFuture(TransactionStatus.FAILED);
75 when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
76 response = target(uri).request(MediaType.APPLICATION_XML).delete();
77 assertEquals(500, response.getStatus());
80 private Future<RpcResult<TransactionStatus>> createFuture(TransactionStatus statusName) {
81 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName).build();
82 return DummyFuture.builder().rpcResult(rpcResult).build();