Rename restconf-nb-rfc8040 to restconf-nb
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / DeleteDataTransactionUtilTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.restconf.nb.rfc8040.rests.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.when;
14 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
16
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.SettableFuture;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.Response.Status;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.common.api.CommitInfo;
27 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
28 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
29 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
31 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
32 import org.opendaylight.netconf.api.NetconfDocumentedException;
33 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
34 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
35 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
36 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
37 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
38 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
39 import org.opendaylight.yangtools.yang.common.ErrorTag;
40 import org.opendaylight.yangtools.yang.common.ErrorType;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42
43 @RunWith(MockitoJUnitRunner.StrictStubs.class)
44 public class DeleteDataTransactionUtilTest {
45     @Mock
46     private DOMDataTreeReadWriteTransaction readWrite;
47     @Mock
48     private DOMDataBroker mockDataBroker;
49     @Mock
50     private NetconfDataTreeService netconfService;
51
52     @Before
53     public void init() {
54         doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
55         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).commit();
56         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).discardChanges();
57         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).unlock();
58         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).lock();
59         doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService)
60             .delete(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty());
61         doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
62     }
63
64     /**
65      * Test of successful DELETE operation.
66      */
67     @Test
68     public void deleteData() {
69         // assert that data to delete exists
70         when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
71             .thenReturn(immediateTrueFluentFuture());
72         // test
73         delete(new MdsalRestconfStrategy(mockDataBroker));
74         delete(new NetconfRestconfStrategy(netconfService));
75     }
76
77     /**
78      * Negative test for DELETE operation when data to delete does not exist. Error DATA_MISSING is expected.
79      */
80     @Test
81     public void deleteDataNegativeTest() {
82         // assert that data to delete does NOT exist
83         when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.empty()))
84             .thenReturn(immediateFalseFluentFuture());
85         final NetconfDocumentedException exception = new NetconfDocumentedException("id",
86             ErrorType.RPC, ErrorTag.DATA_MISSING, ErrorSeverity.ERROR);
87         final SettableFuture<? extends CommitInfo> ret = SettableFuture.create();
88         ret.setException(new TransactionCommitFailedException(
89             String.format("Commit of transaction %s failed", this), exception));
90
91         doReturn(ret).when(netconfService).commit();
92
93         // test and assert error
94         deleteFail(new MdsalRestconfStrategy(mockDataBroker));
95         deleteFail(new NetconfRestconfStrategy(netconfService));
96     }
97
98     private static void delete(final RestconfStrategy strategy) {
99         final Response response = DeleteDataTransactionUtil.deleteData(strategy, YangInstanceIdentifier.empty());
100         // assert success
101         assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
102     }
103
104     private static void deleteFail(final RestconfStrategy strategy) {
105         final var ex = assertThrows(RestconfDocumentedException.class,
106             () -> DeleteDataTransactionUtil.deleteData(strategy, YangInstanceIdentifier.empty()));
107         final var errors = ex.getErrors();
108         assertEquals(1, errors.size());
109         final var error = errors.get(0);
110         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
111         assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
112     }
113 }