f38923d9522b00e73f1c27cc0f6385cb511d3dd0
[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 import com.google.common.util.concurrent.Futures;
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.Response.Status;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
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
33     private DOMTransactionChain transactionChain;
34     @Mock
35     private InstanceIdentifierContext<?> context;
36     @Mock
37     private DOMDataReadWriteTransaction readWrite;
38
39     @Before
40     public void init() throws Exception {
41         MockitoAnnotations.initMocks(this);
42         Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
43         Mockito.when(this.readWrite.submit()).thenReturn(Futures.immediateCheckedFuture(null));
44         Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.EMPTY);
45     }
46
47     /**
48      * Test of successful DELETE operation.
49      */
50     @Test
51     public void deleteData() throws Exception {
52         // assert that data to delete exists
53         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
54                 YangInstanceIdentifier.EMPTY))
55                 .thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
56
57         // test
58         final Response response = DeleteDataTransactionUtil.deleteData(
59                 new TransactionVarsWrapper(this.context, null, this.transactionChain));
60
61         // assert success
62         assertEquals("Not expected response received", Status.OK.getStatusCode(), response.getStatus());
63     }
64
65     /**
66      * Negative test for DELETE operation when data to delete does not exist. Error 404 is expected.
67      */
68     @Test
69     public void deleteDataNegativeTest() throws Exception {
70         // assert that data to delete does NOT exist
71         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
72                 YangInstanceIdentifier.EMPTY))
73                 .thenReturn(Futures.immediateCheckedFuture(Boolean.FALSE));
74
75         // test and assert error
76         try {
77             DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null, this.transactionChain));
78             fail("Delete operation should fail due to missing data");
79         } catch (final RestconfDocumentedException e) {
80             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
81             assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
82             assertEquals(404, e.getErrors().get(0).getErrorTag().getStatusCode());
83         }
84     }
85 }