2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
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;
16 import java.io.FileNotFoundException;
17 import java.io.UnsupportedEncodingException;
19 import java.util.concurrent.Future;
21 import javax.ws.rs.core.Application;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
25 import org.glassfish.jersey.server.ResourceConfig;
26 import org.glassfish.jersey.test.JerseyTest;
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.XmlToCompositeNodeProvider;
32 import org.opendaylight.controller.sal.restconf.impl.BrokerFacade;
33 import org.opendaylight.controller.sal.restconf.impl.ControllerContext;
34 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.model.api.Module;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 public class RestDeleteOperationTest extends JerseyTest {
42 private static ControllerContext controllerContext;
43 private static BrokerFacade brokerFacade;
44 private static RestconfImpl restconfImpl;
47 public static void init() throws FileNotFoundException {
48 Set<Module> allModules = TestUtils.loadModulesFrom("/test-config-data/yang1");
49 assertNotNull(allModules);
50 SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules);
51 controllerContext = ControllerContext.getInstance();
52 controllerContext.setSchemas(schemaContext);
53 brokerFacade = mock(BrokerFacade.class);
54 restconfImpl = RestconfImpl.getInstance();
55 restconfImpl.setBroker(brokerFacade);
56 restconfImpl.setControllerContext(controllerContext);
60 protected Application configure() {
61 /* enable/disable Jersey logs to console */
62 // enable(TestProperties.LOG_TRAFFIC);
63 // enable(TestProperties.DUMP_ENTITY);
64 // enable(TestProperties.RECORD_LOG_LEVEL);
65 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
66 ResourceConfig resourceConfig = new ResourceConfig();
67 resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE,
68 XmlToCompositeNodeProvider.INSTANCE);
69 return resourceConfig;
73 public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
74 String uri = "/config/test-interface:interfaces";
75 Future<RpcResult<TransactionStatus>> dummyFuture = createFuture(TransactionStatus.COMMITED);
76 when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
77 Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
78 assertEquals(200, response.getStatus());
80 dummyFuture = createFuture(TransactionStatus.FAILED);
81 when(brokerFacade.commitConfigurationDataDelete(any(InstanceIdentifier.class))).thenReturn(dummyFuture);
82 response = target(uri).request(MediaType.APPLICATION_XML).delete();
83 assertEquals(500, response.getStatus());
86 private Future<RpcResult<TransactionStatus>> createFuture(TransactionStatus statusName) {
87 RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(statusName).build();
88 return DummyFuture.builder().rpcResult(rpcResult).build();