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.mock;
13 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
14 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
17 import java.util.Optional;
18 import javax.ws.rs.core.Response;
19 import javax.ws.rs.core.Response.Status;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.mdsal.common.api.CommitInfo;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
29 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
30 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
31 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
32 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
33 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
34 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
35 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
36 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
37 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
38 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 public class DeleteDataTransactionUtilTest {
44 private DOMTransactionChain transactionChain;
46 private InstanceIdentifierContext<?> context;
48 private DOMDataTreeReadWriteTransaction readWrite;
50 private DOMDataBroker mockDataBroker;
52 private NetconfDataTreeService netconfService;
54 private TransactionChainHandler transactionChainHandler;
58 MockitoAnnotations.initMocks(this);
59 Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
60 Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
61 Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(this.netconfService).commit(Mockito.any());
62 Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.empty());
64 Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
65 transactionChainHandler = new TransactionChainHandler(mockDataBroker);
69 * Test of successful DELETE operation.
72 public void deleteData() {
73 // assert that data to delete exists
74 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
75 YangInstanceIdentifier.empty())).thenReturn(immediateTrueFluentFuture());
76 Mockito.when(this.netconfService.getConfig(YangInstanceIdentifier.empty()))
77 .thenReturn(immediateFluentFuture(Optional.of(mock(NormalizedNode.class))));
79 delete(new MdsalRestconfStrategy(this.context, transactionChainHandler));
80 delete(new NetconfRestconfStrategy(netconfService, this.context));
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 Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
90 YangInstanceIdentifier.empty())).thenReturn(immediateFalseFluentFuture());
91 Mockito.when(this.netconfService.getConfig(YangInstanceIdentifier.empty()))
92 .thenReturn(immediateFluentFuture(Optional.empty()));
93 // test and assert error
94 deleteFail(new MdsalRestconfStrategy(this.context, transactionChainHandler));
95 deleteFail(new NetconfRestconfStrategy(netconfService, this.context));
98 private void delete(final RestconfStrategy strategy) {
99 final Response response = DeleteDataTransactionUtil.deleteData(strategy);
101 assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
104 private void deleteFail(final RestconfStrategy strategy) {
106 DeleteDataTransactionUtil.deleteData(strategy);
107 fail("Delete operation should fail due to missing data");
108 } catch (final RestconfDocumentedException e) {
109 assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
110 assertEquals(ErrorTag.DATA_MISSING, e.getErrors().get(0).getErrorTag());