Merge "Bug 6037 - Check if delete request was successful"
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / restful / utils / DeleteDataTransactionUtilTest.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.restconf.restful.utils;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13
14 import com.google.common.util.concurrent.Futures;
15 import javax.ws.rs.core.Response;
16 import javax.ws.rs.core.Response.Status;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
24 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
26 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
27 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
28 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30
31 public class DeleteDataTransactionUtilTest {
32     @Mock DOMDataReadWriteTransaction transaction;
33     @Mock InstanceIdentifierContext<?> context;
34
35     @Before
36     public void init() throws Exception {
37         MockitoAnnotations.initMocks(this);
38         Mockito.when(this.transaction.submit()).thenReturn(Futures.immediateCheckedFuture(null));
39         Mockito.when(context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.EMPTY);
40     }
41
42     /**
43      * Test of successful DELETE operation.
44      */
45     @Test
46     public void deleteData() throws Exception {
47         // assert that data to delete exists
48         Mockito.when(this.transaction.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY))
49                 .thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
50
51         // test
52         final Response response = DeleteDataTransactionUtil.deleteData(
53                 new TransactionVarsWrapper(this.context, null, this.transaction));
54
55         // assert success
56         assertEquals("Not expected response received", Status.OK.getStatusCode(), response.getStatus());
57     }
58
59     /**
60      * Negative test for DELETE operation when data to delete does not exist. Error 404 is expected.
61      */
62     @Test
63     public void deleteDataNegativeTest() throws Exception {
64         // assert that data to delete does NOT exist
65         Mockito.when(this.transaction.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.EMPTY))
66                 .thenReturn(Futures.immediateCheckedFuture(Boolean.FALSE));
67
68         // test and assert error
69         try {
70             DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null, this.transaction));
71             fail("Delete operation should fail due to missing data");
72         } catch (final RestconfDocumentedException e) {
73             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
74             assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
75             assertEquals(404, e.getErrors().get(0).getErrorTag().getStatusCode());
76         }
77     }
78 }