3ebbf2868fee3deb15af493b4542ff38cde71cd7
[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.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
16
17 import java.io.FileNotFoundException;
18 import java.io.UnsupportedEncodingException;
19 import javax.ws.rs.core.Application;
20 import javax.ws.rs.core.MediaType;
21 import javax.ws.rs.core.Response;
22 import org.glassfish.jersey.server.ResourceConfig;
23 import org.glassfish.jersey.test.JerseyTest;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
27 import org.opendaylight.mdsal.common.api.CommitInfo;
28 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
29 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
30 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
31 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
32 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
33 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
34 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
35 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
36 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
37 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
40 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
41
42 public class RestDeleteOperationTest extends JerseyTest {
43     private static EffectiveModelContext schemaContext;
44
45     private ControllerContext controllerContext;
46     private BrokerFacade brokerFacade;
47     private RestconfImpl restconfImpl;
48
49     @BeforeClass
50     public static void init() throws FileNotFoundException, ReactorException {
51         schemaContext = TestUtils.loadSchemaContext("/test-config-data/yang1");
52     }
53
54     @Override
55     protected Application configure() {
56         /* enable/disable Jersey logs to console */
57         // enable(TestProperties.LOG_TRAFFIC);
58         // enable(TestProperties.DUMP_ENTITY);
59         // enable(TestProperties.RECORD_LOG_LEVEL);
60         // set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue());
61         controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
62         controllerContext.setSchemas(schemaContext);
63         brokerFacade = mock(BrokerFacade.class);
64         restconfImpl = RestconfImpl.newInstance(brokerFacade, controllerContext);
65
66         ResourceConfig resourceConfig = new ResourceConfig();
67         resourceConfig = resourceConfig.registerInstances(restconfImpl, new NormalizedNodeJsonBodyWriter(),
68             new NormalizedNodeXmlBodyWriter(), new XmlNormalizedNodeBodyReader(controllerContext),
69             new JsonNormalizedNodeBodyReader(controllerContext),
70             new RestconfDocumentedExceptionMapper(controllerContext));
71         return resourceConfig;
72     }
73
74     @Test
75     public void deleteConfigStatusCodes() throws UnsupportedEncodingException {
76         final String uri = "/config/test-interface:interfaces";
77         doReturn(CommitInfo.emptyFluentFuture()).when(brokerFacade)
78             .commitConfigurationDataDelete(any(YangInstanceIdentifier.class));
79         Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
80         assertEquals(200, response.getStatus());
81
82         doThrow(RestconfDocumentedException.class).when(brokerFacade).commitConfigurationDataDelete(
83                 any(YangInstanceIdentifier.class));
84         response = target(uri).request(MediaType.APPLICATION_XML).delete();
85         assertEquals(500, response.getStatus());
86     }
87
88     @Test
89     public void deleteFailTest() throws Exception {
90         final String uri = "/config/test-interface:interfaces";
91         doReturn(immediateFailedFluentFuture(new TransactionCommitFailedException("failed test"))).when(brokerFacade)
92             .commitConfigurationDataDelete(any(YangInstanceIdentifier.class));
93         final Response response = target(uri).request(MediaType.APPLICATION_XML).delete();
94         assertEquals(500, response.getStatus());
95     }
96 }