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.Mockito.doReturn;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.SettableFuture;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.Response.Status;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.common.api.CommitInfo;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
29 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
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;
38 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
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.common.ErrorSeverity;
43 import org.opendaylight.yangtools.yang.common.ErrorType;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
46 @RunWith(MockitoJUnitRunner.StrictStubs.class)
47 public class DeleteDataTransactionUtilTest {
49 private InstanceIdentifierContext<?> context;
51 private DOMDataTreeReadWriteTransaction readWrite;
53 private DOMDataBroker mockDataBroker;
55 private NetconfDataTreeService netconfService;
59 doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
60 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).commit();
61 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).discardChanges();
62 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).unlock();
63 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).lock();
64 doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService)
65 .delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
66 doReturn(YangInstanceIdentifier.empty()).when(this.context).getInstanceIdentifier();
67 doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
71 * Test of successful DELETE operation.
74 public void deleteData() {
75 // assert that data to delete exists
76 when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
77 .thenReturn(immediateTrueFluentFuture());
79 delete(new MdsalRestconfStrategy(mockDataBroker));
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 when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
90 .thenReturn(immediateFalseFluentFuture());
91 final NetconfDocumentedException exception = new NetconfDocumentedException("id",
92 ErrorType.RPC, DocumentedException.ErrorTag.from("data-missing"), ErrorSeverity.ERROR);
93 final SettableFuture<? extends CommitInfo> ret = SettableFuture.create();
94 ret.setException(new TransactionCommitFailedException(
95 String.format("Commit of transaction %s failed", this), exception));
97 doReturn(ret).when(this.netconfService).commit();
99 // test and assert error
100 deleteFail(new MdsalRestconfStrategy(mockDataBroker));
101 deleteFail(new NetconfRestconfStrategy(netconfService));
104 private void delete(final RestconfStrategy strategy) {
105 final Response response = DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
107 assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
110 private void deleteFail(final RestconfStrategy strategy) {
112 DeleteDataTransactionUtil.deleteData(strategy, context.getInstanceIdentifier());
113 fail("Delete operation should fail due to missing data");
114 } catch (final RestconfDocumentedException e) {
115 assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
116 assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());