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