2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
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;
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;
44 @RunWith(MockitoJUnitRunner.StrictStubs.class)
45 public class DeleteDataTransactionUtilTest {
47 private DOMTransactionChain transactionChain;
49 private InstanceIdentifierContext<?> context;
51 private DOMDataTreeReadWriteTransaction readWrite;
53 private DOMDataBroker mockDataBroker;
55 private NetconfDataTreeService netconfService;
57 private TransactionChainHandler transactionChainHandler;
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());
66 Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
67 transactionChainHandler = new TransactionChainHandler(mockDataBroker);
71 * Test of successful DELETE operation.
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());
79 delete(new MdsalRestconfStrategy(transactionChainHandler));
80 delete(new NetconfRestconfStrategy(netconfService));
84 * Negative test for DELETE operation when data to delete does not exist. Error DATA_MISSING is expected.
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));
98 Mockito.when(this.netconfService.commit(any())).thenAnswer(invocation -> ret);
100 // test and assert error
101 deleteFail(new MdsalRestconfStrategy(transactionChainHandler));
102 deleteFail(new NetconfRestconfStrategy(netconfService));
105 private void delete(final RestconfStrategy strategy) {
106 final Response response = DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
108 assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
111 private void deleteFail(final RestconfStrategy strategy) {
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());