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 java.io.FileNotFoundException;
19 import java.io.UnsupportedEncodingException;
21 import javax.ws.rs.core.Application;
22 import javax.ws.rs.core.MediaType;
23 import javax.ws.rs.core.Response;
24 import org.glassfish.jersey.server.ResourceConfig;
25 import org.glassfish.jersey.test.JerseyTest;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.opendaylight.controller.sal.rest.impl.JsonNormalizedNodeBodyReader;
29 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter;
30 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeXmlBodyWriter;
31 import org.opendaylight.controller.sal.rest.impl.RestconfDocumentedExceptionMapper;
32 import org.opendaylight.controller.sal.rest.impl.XmlNormalizedNodeBodyReader;
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.RestconfDocumentedException;
36 import org.opendaylight.controller.sal.restconf.impl.RestconfImpl;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.model.api.Module;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
41 public class RestDeleteOperationTest extends JerseyTest {
43 private static ControllerContext controllerContext;
44 private static BrokerFacade brokerFacade;
45 private static RestconfImpl restconfImpl;
48 public static void init() throws FileNotFoundException {
49 final Set<Module> allModules = TestUtils.loadModulesFrom("/test-config-data/yang1");
50 assertNotNull(allModules);
51 final 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);
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, new NormalizedNodeJsonBodyWriter(),
69 new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(), new JsonNormalizedNodeBodyReader());
70 resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
71 return resourceConfig;
74 @SuppressWarnings("unchecked")
76 public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
77 final String uri = "/config/test-interface:interfaces";
78 when(brokerFacade.commitConfigurationDataDelete(any(YangInstanceIdentifier.class))).thenReturn(
79 mock(CheckedFuture.class));
80 Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
81 assertEquals(200, response.getStatus());
83 doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataDelete(
84 any(YangInstanceIdentifier.class));
85 response = target(uri).request(MediaType.APPLICATION_XML).delete();
86 assertEquals(500, response.getStatus());