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
9 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
14 import com.google.common.util.concurrent.Futures;
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.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
26 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
27 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
28 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
29 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
30 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
31 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 public class DeleteDataTransactionUtilTest {
36 private DOMTransactionChain transactionChain;
38 private InstanceIdentifierContext<?> context;
40 private DOMDataReadWriteTransaction readWrite;
42 private DOMDataBroker mockDataBroker;
44 private TransactionChainHandler transactionChainHandler;
47 public void init() throws Exception {
48 MockitoAnnotations.initMocks(this);
49 Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
50 Mockito.when(this.readWrite.submit()).thenReturn(Futures.immediateCheckedFuture(null));
51 Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.EMPTY);
53 Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
54 transactionChainHandler = new TransactionChainHandler(mockDataBroker);
58 * Test of successful DELETE operation.
61 public void deleteData() throws Exception {
62 // assert that data to delete exists
63 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
64 YangInstanceIdentifier.EMPTY))
65 .thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
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 404 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))
83 .thenReturn(Futures.immediateCheckedFuture(Boolean.FALSE));
85 // test and assert error
87 DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null,
88 transactionChainHandler));
89 fail("Delete operation should fail due to missing data");
90 } catch (final RestconfDocumentedException e) {
91 assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
92 assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
93 assertEquals(404, e.getErrors().get(0).getErrorTag().getStatusCode());