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