Remove static TransactionChainHandler instance
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / 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.nb.rfc8040.rests.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.DOMDataBroker;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
26 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
29 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
30 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
31 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33
34 public class DeleteDataTransactionUtilTest {
35     @Mock
36     private DOMTransactionChain transactionChain;
37     @Mock
38     private InstanceIdentifierContext<?> context;
39     @Mock
40     private DOMDataReadWriteTransaction readWrite;
41     @Mock
42     private DOMDataBroker mockDataBroker;
43
44     private TransactionChainHandler transactionChainHandler;
45
46     @Before
47     public void init() throws Exception {
48         MockitoAnnotations.initMocks(this);
49         Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
50         Mockito.when(this.readWrite.submit()).thenReturn(Futures.immediateCheckedFuture(null));
51         Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.EMPTY);
52
53         Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
54         transactionChainHandler = new TransactionChainHandler(mockDataBroker);
55     }
56
57     /**
58      * Test of successful DELETE operation.
59      */
60     @Test
61     public void deleteData() throws Exception {
62         // assert that data to delete exists
63         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
64                 YangInstanceIdentifier.EMPTY))
65                 .thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
66
67         // test
68         final Response response = DeleteDataTransactionUtil.deleteData(
69                 new TransactionVarsWrapper(this.context, null, transactionChainHandler));
70
71         // assert success
72         assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
73     }
74
75     /**
76      * Negative test for DELETE operation when data to delete does not exist. Error 404 is expected.
77      */
78     @Test
79     public void deleteDataNegativeTest() throws Exception {
80         // assert that data to delete does NOT exist
81         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
82                 YangInstanceIdentifier.EMPTY))
83                 .thenReturn(Futures.immediateCheckedFuture(Boolean.FALSE));
84
85         // test and assert error
86         try {
87             DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null,
88                     transactionChainHandler));
89             fail("Delete operation should fail due to missing data");
90         } catch (final RestconfDocumentedException e) {
91             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
92             assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
93             assertEquals(404, e.getErrors().get(0).getErrorTag().getStatusCode());
94         }
95     }
96 }