73b4b3a2af4d8092b2076d7b79c6c78aabc004f6
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestDeleteOperationTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15 import static org.opendaylight.controller.sal.restconf.impl.test.RestOperationUtils.createUri;
16
17 import java.io.FileNotFoundException;
18 import java.io.UnsupportedEncodingException;
19 import java.util.Set;
20 import java.util.concurrent.Future;
21
22 import javax.ws.rs.core.Application;
23 import javax.ws.rs.core.MediaType;
24 import javax.ws.rs.core.Response;
25
26 import org.glassfish.jersey.server.ResourceConfig;
27 import org.glassfish.jersey.test.JerseyTest;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
31 import org.opendaylight.controller.sal.rest.impl.StructuredDataToXmlProvider;
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.InstanceIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40
41 public class RestDeleteOperationTest extends JerseyTest {
42
43     private static ControllerContext controllerContext;
44     private static BrokerFacade brokerFacade;
45     private static RestconfImpl restconfImpl;
46
47     @BeforeClass
48     public static void init() throws FileNotFoundException {
49         Set<Module> allModules = TestUtils.loadModulesFrom("/test-config-data/yang1");
50         assertNotNull(allModules);
51         SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
52         controllerContext = ControllerContext.getInstance();
53         controllerContext.setSchemas(schemaContext);
54         brokerFacade = mock(BrokerFacade.class);
55         restconfImpl = RestconfImpl.getInstance();
56         restconfImpl.setBroker(brokerFacade);
57         restconfImpl.setControllerContext(controllerContext);
58     }
59
60     @Override
61     protected Application configure() {
62         /* enable/disable Jersey logs to console */
63 //        enable(TestProperties.LOG_TRAFFIC);
64 //        enable(TestProperties.DUMP_ENTITY);
65 //        enable(TestProperties.RECORD_LOG_LEVEL);
66 //        set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
67         ResourceConfig resourceConfig = new ResourceConfig();
68         resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
69                 XmlToCompositeNodeProvider.INSTANCE);
70         return resourceConfig;
71     }
72
73     @Test
74     public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
75         String uri = createUri("/config/", "test-interface:interfaces");
76         Future<RpcResult<TransactionStatus>> dummyFuture = createFuture(TransactionStatus.COMMITED);
77         when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
78         Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
79         assertEquals(200, response.getStatus());
80         
81         dummyFuture = createFuture(TransactionStatus.FAILED);
82         when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
83         response = target(uri).request(MediaType.APPLICATION_XML).delete();
84         assertEquals(500, response.getStatus());
85     }
86     
87     private Future<RpcResult<TransactionStatus>> createFuture(TransactionStatus statusName) {
88         RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName).build();
89         return DummyFuture.builder().rpcResult(rpcResult).build();
90     }
91
92 }