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.ArgumentMatchers.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.controller.md.sal.rest.common.TestRestconfUtils;
31 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
32 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
33 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
34 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
35 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
36 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
37 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
38 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
39 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 import org.opendaylight.yangtools.yang.model.api.Module;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
45 public class RestDeleteOperationTest extends JerseyTest {
46 private static SchemaContext schemaContext;
48 private ControllerContext controllerContext;
49 private BrokerFacade brokerFacade;
50 private RestconfImpl restconfImpl;
53 public static void init() throws FileNotFoundException, ReactorException {
54 schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
55 final Set<Module> allModules = schemaContext.getModules();
56 assertNotNull(allModules);
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 controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
67 controllerContext.setSchemas(schemaContext);
68 brokerFacade = mock(BrokerFacade.class);
69 restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
71 ResourceConfig resourceConfig = new ResourceConfig();
72 resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
73 new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext),
74 new JsonNormalizedNodeBodyReader(controllerContext),
75 new RestconfDocumentedExceptionMapper(controllerContext));
76 return resourceConfig;
80 public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
81 final String uri = "/config/test-interface:interfaces";
82 when(brokerFacade.commitConfigurationDataDelete(any(YangInstanceIdentifier.class))).thenReturn(
83 Futures.immediateCheckedFuture(null));
84 Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
85 assertEquals(200, response.getStatus());
87 doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataDelete(
88 any(YangInstanceIdentifier.class));
89 response = target(uri).request(MediaType.APPLICATION_XML).delete();
90 assertEquals(500, response.getStatus());
94 public void deleteFailTest() throws Exception {
95 final String uri = "/config/test-interface:interfaces";
96 final CheckedFuture<Void, TransactionCommitFailedException> future =
97 Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("failed test"));
98 when(brokerFacade.commitConfigurationDataDelete(any(YangInstanceIdentifier.class))).thenReturn(future);
99 final Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
100 assertEquals(500, response.getStatus());