b0fce39c4853728dc5d666f7a6c514814aef3d57
[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 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.fail;
12 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
13 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
14
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.mdsal.common.api.CommitInfo;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
26 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
27 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
30 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
31 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
32 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34
35 public class DeleteDataTransactionUtilTest {
36     @Mock
37     private DOMTransactionChain transactionChain;
38     @Mock
39     private InstanceIdentifierContext<?> context;
40     @Mock
41     private DOMDataTreeReadWriteTransaction readWrite;
42     @Mock
43     private DOMDataBroker mockDataBroker;
44
45     private TransactionChainHandler transactionChainHandler;
46
47     @Before
48     public void init() throws Exception {
49         MockitoAnnotations.initMocks(this);
50         Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
51         Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
52         Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.empty());
53
54         Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
55         transactionChainHandler = new TransactionChainHandler(mockDataBroker);
56     }
57
58     /**
59      * Test of successful DELETE operation.
60      */
61     @Test
62     public void deleteData() throws Exception {
63         // assert that data to delete exists
64         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
65                 YangInstanceIdentifier.empty())).thenReturn(immediateTrueFluentFuture());
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 DATA_MISSING 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())).thenReturn(immediateFalseFluentFuture());
83
84         // test and assert error
85         try {
86             DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null,
87                     transactionChainHandler));
88             fail("Delete operation should fail due to missing data");
89         } catch (final RestconfDocumentedException e) {
90             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
91             assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
92         }
93     }
94 }