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.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.when;
17 import com.google.common.util.concurrent.CheckedFuture;
18 import com.google.common.util.concurrent.Futures;
19 import java.io.FileNotFoundException;
20 import java.io.UnsupportedEncodingException;
22 import javax.ws.rs.core.Application;
23 import javax.ws.rs.core.MediaType;
24 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.data.TransactionCommitFailedException;
30 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
31 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
32 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
33 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
34 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
35 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
36 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
37 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
38 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.model.api.Module;
41 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
44 public class RestDeleteOperationTest extends JerseyTest {
46 private static ControllerContext controllerContext;
47 private static BrokerFacade brokerFacade;
48 private static RestconfImpl restconfImpl;
51 public static void init() throws FileNotFoundException, ReactorException {
52 final SchemaContext schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
53 final Set<Module> allModules = schemaContext.getModules();
54 assertNotNull(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);
65 protected Application configure() {
66 /* enable/disable Jersey logs to console */
67 // enable(TestProperties.LOG_TRAFFIC);
68 // enable(TestProperties.DUMP_ENTITY);
69 // enable(TestProperties.RECORD_LOG_LEVEL);
70 // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
71 ResourceConfig resourceConfig = new ResourceConfig();
72 resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
73 new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
74 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
75 return resourceConfig;
79 public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
80 final String uri = "/config/test-interface:interfaces";
81 when(brokerFacade.commitConfigurationDataDelete(any(YangInstanceIdentifier.class))).thenReturn(
82 Futures.immediateCheckedFuture(null));
83 Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
84 assertEquals(200, response.getStatus());
86 doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataDelete(
87 any(YangInstanceIdentifier.class));
88 response = target(uri).request(MediaType.APPLICATION_XML).delete();
89 assertEquals(500, response.getStatus());
93 public void deleteFailTest() throws Exception {
94 final String uri = "/config/test-interface:interfaces";
95 final CheckedFuture<Void, TransactionCommitFailedException> future =
96 Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("failed test"));
97 when(brokerFacade.commitConfigurationDataDelete(any(YangInstanceIdentifier.class))).thenReturn(future);
98 final Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
99 assertEquals(500, response.getStatus());