Migrate restconf to MD-SAL APIs
[netconf.git] / restconf / restconf-nb-bierman02 / 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.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.doThrow;
15 import static org.mockito.Mockito.mock;
16 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
17
18 import java.io.FileNotFoundException;
19 import java.io.UnsupportedEncodingException;
20 import java.util.Set;
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.md.sal.common.api.data.TransactionCommitFailedException;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.mdsal.common.api.CommitInfo;
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;
44
45 public class RestDeleteOperationTest extends JerseyTest {
46     private static SchemaContext schemaContext;
47
48     private ControllerContext controllerContext;
49     private BrokerFacade brokerFacade;
50     private RestconfImpl restconfImpl;
51
52     @BeforeClass
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);
57     }
58
59     @Override
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);
70
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;
77     }
78
79     @Test
80     public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
81         final String uri = "/config/test-interface:interfaces";
82         doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade)
83             .commitConfigurationDataDelete(any(YangInstanceIdentifier.class));
84         Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
85         assertEquals(200, response.getStatus());
86
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());
91     }
92
93     @Test
94     public void deleteFailTest() throws Exception {
95         final String uri = "/config/test-interface:interfaces";
96         doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("failed test"))).when(brokerFacade)
97             .commitConfigurationDataDelete(any(YangInstanceIdentifier.class));
98         final Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
99         assertEquals(500, response.getStatus());
100     }
101 }