Bump upstreams
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / RestconfDataDeleteTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.jaxrs;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
16 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
17
18 import java.util.Optional;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.jupiter.MockitoExtension;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMActionService;
27 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
29 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
30 import org.opendaylight.mdsal.dom.api.DOMRpcService;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
33 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
34 import org.opendaylight.yangtools.yang.common.ErrorTag;
35 import org.opendaylight.yangtools.yang.common.ErrorType;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37
38 @ExtendWith(MockitoExtension.class)
39 class RestconfDataDeleteTest extends AbstractRestconfTest {
40     @Mock
41     private DOMDataTreeReadWriteTransaction tx;
42
43     @BeforeEach
44     void beforeEach() {
45         doReturn(tx).when(dataBroker).newReadWriteTransaction();
46     }
47
48     @Test
49     public void testDeleteData() {
50         doNothing().when(tx).delete(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
51         doReturn(immediateTrueFluentFuture())
52                 .when(tx).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
53         doReturn(CommitInfo.emptyFluentFuture()).when(tx).commit();
54
55         assertNull(assertEntity(204, ar -> restconf.dataDELETE(JUKEBOX_API_PATH, ar)));
56     }
57
58     @Test
59     public void testDeleteDataNotExisting() {
60         doReturn(immediateFalseFluentFuture())
61                 .when(tx).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
62         doReturn(true).when(tx).cancel();
63
64         final var error = assertError(ar -> restconf.dataDELETE(JUKEBOX_API_PATH, ar));
65         assertEquals(ErrorType.PROTOCOL, error.getErrorType());
66         assertEquals(ErrorTag.DATA_MISSING, error.getErrorTag());
67     }
68
69     /**
70      * Test of deleting data on mount point.
71      */
72     @Test
73     public void testDeleteDataMountPoint() {
74         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
75         doReturn(Optional.of(new FixedDOMSchemaService(JUKEBOX_SCHEMA))).when(mountPoint)
76             .getService(DOMSchemaService.class);
77         doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
78         doReturn(Optional.of(rpcService)).when(mountPoint).getService(DOMRpcService.class);
79         doReturn(Optional.empty()).when(mountPoint).getService(DOMActionService.class);
80         doReturn(Optional.empty()).when(mountPoint).getService(DOMMountPointService.class);
81         doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
82
83         doNothing().when(tx).delete(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
84         doReturn(immediateTrueFluentFuture())
85                 .when(tx).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
86         doReturn(CommitInfo.emptyFluentFuture()).when(tx).commit();
87
88         assertNull(assertEntity(204, ar -> restconf.dataDELETE(
89             apiPath("example-jukebox:jukebox/yang-ext:mount/example-jukebox:jukebox"), ar)));
90     }
91 }