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.restful.utils;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
13 import com.google.common.util.concurrent.Futures;
14 import javax.ws.rs.core.Response;
15 import javax.ws.rs.core.Response.Status;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.mockito.Mock;
19 import org.mockito.Mockito;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
23 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
24 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
25 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
26 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
27 import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
28 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 public class DeleteDataTransactionUtilTest {
33 private DOMTransactionChain transactionChain;
35 private InstanceIdentifierContext<?> context;
37 private DOMDataReadWriteTransaction readWrite;
40 public void init() throws Exception {
41 MockitoAnnotations.initMocks(this);
42 Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
43 Mockito.when(this.readWrite.submit()).thenReturn(Futures.immediateCheckedFuture(null));
44 Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.EMPTY);
48 * Test of successful DELETE operation.
51 public void deleteData() throws Exception {
52 // assert that data to delete exists
53 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
54 YangInstanceIdentifier.EMPTY))
55 .thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
58 final Response response = DeleteDataTransactionUtil.deleteData(
59 new TransactionVarsWrapper(this.context, null, this.transactionChain));
62 assertEquals("Not expected response received", Status.OK.getStatusCode(), response.getStatus());
66 * Negative test for DELETE operation when data to delete does not exist. Error 404 is expected.
69 public void deleteDataNegativeTest() throws Exception {
70 // assert that data to delete does NOT exist
71 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
72 YangInstanceIdentifier.EMPTY))
73 .thenReturn(Futures.immediateCheckedFuture(Boolean.FALSE));
75 // test and assert error
77 DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null, this.transactionChain));
78 fail("Delete operation should fail due to missing data");
79 } catch (final RestconfDocumentedException e) {
80 assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
81 assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
82 assertEquals(404, e.getErrors().get(0).getErrorTag().getStatusCode());