Added tests for DELETE operation
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / DeleteRestCallTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
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.verify;
8 import static org.mockito.Mockito.when;
9
10 import java.io.FileNotFoundException;
11 import java.io.InputStream;
12 import java.io.UnsupportedEncodingException;
13 import java.net.URI;
14 import java.net.URLEncoder;
15 import java.util.Set;
16 import java.util.concurrent.Future;
17 import java.util.logging.Level;
18
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;
23
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.BeforeClass;
28 import org.junit.Test;
29 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
30 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
31 import org.opendaylight.controller.sal.rest.impl.XmlMapper;
32 import org.opendaylight.controller.sal.rest.impl.XmlToCompositeNodeProvider;
33 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
34 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
35 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
38 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.Module;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41
42 import com.google.common.base.Charsets;
43
44 public class DeleteRestCallTest extends JerseyTest {
45
46     private static ControllerContext controllerContext;
47     private static BrokerFacade brokerFacade;
48     private static RestconfImpl restconfImpl;
49     private static final MediaType MEDIA_TYPE_DRAFT02 = new MediaType("application", "yang.data+xml");
50
51     @BeforeClass
52     public static void init() throws FileNotFoundException {
53         Set<Module> allModules = TestUtils.loadModulesFrom("/test-config-data/yang1");
54         assertNotNull(allModules);
55         SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
56         controllerContext = ControllerContext.getInstance();
57         controllerContext.setSchemas(schemaContext);
58         brokerFacade = mock(BrokerFacade.class);
59         restconfImpl = RestconfImpl.getInstance();
60         restconfImpl.setBroker(brokerFacade);
61         restconfImpl.setControllerContext(controllerContext);
62     }
63
64     @Test
65     public void testDeleteConfigurationData() throws UnsupportedEncodingException, FileNotFoundException {
66         String uri2 = createUri("/config/", "test-interface:interfaces");
67
68         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
69                 TransactionStatus.COMMITED).build();
70         Future<RpcResult<TransactionStatus>> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
71         when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
72
73         Response response = target(uri2).request(MEDIA_TYPE_DRAFT02).delete();
74         assertEquals(200, response.getStatus());
75
76         rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(TransactionStatus.FAILED).build();
77         dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build();
78
79         when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
80
81         response = target(uri2).request(MEDIA_TYPE_DRAFT02).delete();
82         assertEquals(500, response.getStatus());
83     }
84
85     private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException {
86         return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString();
87     }
88
89     @Override
90     protected Application configure() {
91         enable(TestProperties.LOG_TRAFFIC);
92         enable(TestProperties.DUMP_ENTITY);
93         enable(TestProperties.RECORD_LOG_LEVEL);
94         set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
95
96         ResourceConfig resourceConfig = new ResourceConfig();
97         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
98                 XmlToCompositeNodeProvider.INSTANCE);
99         return resourceConfig;
100     }
101 }