Eliminate unnecessary blocking checks
[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.mockito.ArgumentMatchers.any;
13 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
14 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
15
16 import com.google.common.util.concurrent.SettableFuture;
17 import javax.ws.rs.core.Response;
18 import javax.ws.rs.core.Response.Status;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.common.api.CommitInfo;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
28 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
30 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
31 import org.opendaylight.netconf.api.DocumentedException;
32 import org.opendaylight.netconf.api.NetconfDocumentedException;
33 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
34 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
37 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
38 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
39 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
40 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
41 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class DeleteDataTransactionUtilTest {
46     @Mock
47     private DOMTransactionChain transactionChain;
48     @Mock
49     private InstanceIdentifierContext<?> context;
50     @Mock
51     private DOMDataTreeReadWriteTransaction readWrite;
52     @Mock
53     private DOMDataBroker mockDataBroker;
54     @Mock
55     private NetconfDataTreeService netconfService;
56
57     private TransactionChainHandler transactionChainHandler;
58
59     @Before
60     public void init() {
61         Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
62         Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
63         Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(this.netconfService).commit(Mockito.any());
64         Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.empty());
65
66         Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
67         transactionChainHandler = new TransactionChainHandler(mockDataBroker);
68     }
69
70     /**
71      * Test of successful DELETE operation.
72      */
73     @Test
74     public void deleteData() {
75         // assert that data to delete exists
76         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
77                 YangInstanceIdentifier.empty())).thenReturn(immediateTrueFluentFuture());
78         // test
79         delete(new MdsalRestconfStrategy(transactionChainHandler));
80         delete(new NetconfRestconfStrategy(netconfService));
81     }
82
83     /**
84      * Negative test for DELETE operation when data to delete does not exist. Error DATA_MISSING is expected.
85      */
86     @Test
87     public void deleteDataNegativeTest() {
88         // assert that data to delete does NOT exist
89         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
90                 YangInstanceIdentifier.empty())).thenReturn(immediateFalseFluentFuture());
91         final NetconfDocumentedException exception = new NetconfDocumentedException("id",
92             DocumentedException.ErrorType.RPC, DocumentedException.ErrorTag.from("data-missing"),
93             DocumentedException.ErrorSeverity.ERROR);
94         final SettableFuture<? extends CommitInfo> ret = SettableFuture.create();
95         ret.setException(new TransactionCommitFailedException(
96             String.format("Commit of transaction %s failed", this), exception));
97
98         Mockito.when(this.netconfService.commit(any())).thenAnswer(invocation -> ret);
99
100         // test and assert error
101         deleteFail(new MdsalRestconfStrategy(transactionChainHandler));
102         deleteFail(new NetconfRestconfStrategy(netconfService));
103     }
104
105     private void delete(final RestconfStrategy strategy) {
106         final Response response = DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
107         // assert success
108         assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
109     }
110
111     private void deleteFail(final RestconfStrategy strategy) {
112         try {
113             DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
114             fail("Delete operation should fail due to missing data");
115         } catch (final RestconfDocumentedException e) {
116             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
117             assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
118         }
119     }
120 }