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 javax.ws.rs.core.Response;
16 import javax.ws.rs.core.Response.Status;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.Mockito;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.mdsal.common.api.CommitInfo;
23 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
24 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
25 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
26 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
27 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
30 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
31 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
32 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 public class DeleteDataTransactionUtilTest {
37 private DOMTransactionChain transactionChain;
39 private InstanceIdentifierContext<?> context;
41 private DOMDataTreeReadWriteTransaction readWrite;
43 private DOMDataBroker mockDataBroker;
45 private TransactionChainHandler transactionChainHandler;
48 public void init() throws Exception {
49 MockitoAnnotations.initMocks(this);
50 Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
51 Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
52 Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.empty());
54 Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
55 transactionChainHandler = new TransactionChainHandler(mockDataBroker);
59 * Test of successful DELETE operation.
62 public void deleteData() throws Exception {
63 // assert that data to delete exists
64 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
65 YangInstanceIdentifier.empty())).thenReturn(immediateTrueFluentFuture());
68 final Response response = DeleteDataTransactionUtil.deleteData(
69 new TransactionVarsWrapper(this.context, null, transactionChainHandler));
72 assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
76 * Negative test for DELETE operation when data to delete does not exist. Error DATA_MISSING is expected.
79 public void deleteDataNegativeTest() throws Exception {
80 // assert that data to delete does NOT exist
81 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
82 YangInstanceIdentifier.empty())).thenReturn(immediateFalseFluentFuture());
84 // test and assert error
86 DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null,
87 transactionChainHandler));
88 fail("Delete operation should fail due to missing data");
89 } catch (final RestconfDocumentedException e) {
90 assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
91 assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());