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;
13 import static org.mockito.Mockito.mock;
15 import com.google.common.util.concurrent.Futures;
16 import java.lang.reflect.Field;
17 import javax.ws.rs.core.Response;
18 import javax.ws.rs.core.Response.Status;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
29 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
30 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
31 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
32 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
33 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
34 import org.opendaylight.restconf.nb.rfc8040.RestConnectorProvider;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
36 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 public class DeleteDataTransactionUtilTest {
41 private DOMTransactionChain transactionChain;
43 private InstanceIdentifierContext<?> context;
45 private DOMDataReadWriteTransaction readWrite;
47 // Fields used when delete operation fails to reset transaction chain
48 private static Field handler;
49 private static Field broker;
52 public static void setup() throws Exception {
53 DeleteDataTransactionUtilTest.handler = RestConnectorProvider.class.getDeclaredField("transactionChainHandler");
54 DeleteDataTransactionUtilTest.broker = RestConnectorProvider.class.getDeclaredField("dataBroker");
56 DeleteDataTransactionUtilTest.handler.setAccessible(true);
57 DeleteDataTransactionUtilTest.handler.set(RestConnectorProvider.class, mock(TransactionChainHandler.class));
59 DeleteDataTransactionUtilTest.broker.setAccessible(true);
60 DeleteDataTransactionUtilTest.broker.set(RestConnectorProvider.class, mock(DOMDataBroker.class));
64 public static void clean() throws Exception {
65 DeleteDataTransactionUtilTest.handler.set(RestConnectorProvider.class, null);
66 DeleteDataTransactionUtilTest.handler.setAccessible(false);
68 DeleteDataTransactionUtilTest.broker.set(RestConnectorProvider.class, null);
69 DeleteDataTransactionUtilTest.broker.setAccessible(false);
73 public void init() throws Exception {
74 MockitoAnnotations.initMocks(this);
75 Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
76 Mockito.when(this.readWrite.submit()).thenReturn(Futures.immediateCheckedFuture(null));
77 Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.EMPTY);
81 * Test of successful DELETE operation.
84 public void deleteData() throws Exception {
85 // assert that data to delete exists
86 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
87 YangInstanceIdentifier.EMPTY))
88 .thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
91 final Response response = DeleteDataTransactionUtil.deleteData(
92 new TransactionVarsWrapper(this.context, null, this.transactionChain));
95 assertEquals("Not expected response received", Status.OK.getStatusCode(), response.getStatus());
99 * Negative test for DELETE operation when data to delete does not exist. Error 404 is expected.
102 public void deleteDataNegativeTest() throws Exception {
103 // assert that data to delete does NOT exist
104 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
105 YangInstanceIdentifier.EMPTY))
106 .thenReturn(Futures.immediateCheckedFuture(Boolean.FALSE));
108 // test and assert error
110 DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null, this.transactionChain));
111 fail("Delete operation should fail due to missing data");
112 } catch (final RestconfDocumentedException e) {
113 assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
114 assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());
115 assertEquals(404, e.getErrors().get(0).getErrorTag().getStatusCode());