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.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
13 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
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;
45 @RunWith(MockitoJUnitRunner.StrictStubs.class)
46 public class DeleteDataTransactionUtilTest {
48 private DOMTransactionChain transactionChain;
50 private InstanceIdentifierContext<?> context;
52 private DOMDataTreeReadWriteTransaction readWrite;
54 private DOMDataBroker mockDataBroker;
56 private NetconfDataTreeService netconfService;
58 private TransactionChainHandler transactionChainHandler;
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());
72 Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
73 transactionChainHandler = new TransactionChainHandler(mockDataBroker);
77 * Test of successful DELETE operation.
80 public void deleteData() {
81 // assert that data to delete exists
82 Mockito.when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
83 .thenReturn(immediateTrueFluentFuture());
85 delete(new MdsalRestconfStrategy(transactionChainHandler));
86 delete(new NetconfRestconfStrategy(netconfService));
90 * Negative test for DELETE operation when data to delete does not exist. Error DATA_MISSING is expected.
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));
104 Mockito.when(this.netconfService.commit()).thenAnswer(invocation -> ret);
106 // test and assert error
107 deleteFail(new MdsalRestconfStrategy(transactionChainHandler));
108 deleteFail(new NetconfRestconfStrategy(netconfService));
111 private void delete(final RestconfStrategy strategy) {
112 final Response response = DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
114 assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
117 private void deleteFail(final RestconfStrategy strategy) {
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());