2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.transactions;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.doNothing;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
16 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
17 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.mdsal.common.api.CommitInfo;
25 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
26 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
29 import org.opendaylight.restconf.common.patch.PatchStatusContext;
30 import org.opendaylight.restconf.nb.rfc8040.WriteDataParams;
31 import org.opendaylight.restconf.nb.rfc8040.rests.utils.PutDataTransactionUtil;
32 import org.opendaylight.yangtools.yang.common.ErrorTag;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
36 import org.w3c.dom.DOMException;
38 @RunWith(MockitoJUnitRunner.StrictStubs.class)
39 public final class MdsalRestconfStrategyTest extends AbstractRestconfStrategyTest {
41 private DOMDataTreeReadWriteTransaction readWrite;
43 private DOMDataBroker mockDataBroker;
45 private DOMDataTreeReadTransaction read;
48 public void before() {
49 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
50 doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
54 RestconfStrategy testDeleteDataStrategy() {
55 // assert that data to delete exists
56 when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of()))
57 .thenReturn(immediateTrueFluentFuture());
58 return new MdsalRestconfStrategy(mockDataBroker);
62 RestconfStrategy testNegativeDeleteDataStrategy() {
63 // assert that data to delete does NOT exist
64 when(readWrite.exists(LogicalDatastoreType.CONFIGURATION, YangInstanceIdentifier.of()))
65 .thenReturn(immediateFalseFluentFuture());
66 return new MdsalRestconfStrategy(mockDataBroker);
70 RestconfStrategy testPostListDataStrategy(final MapEntryNode entryNode, final YangInstanceIdentifier node) {
71 doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
72 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
73 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
74 return new MdsalRestconfStrategy(mockDataBroker);
78 RestconfStrategy testPostDataFailStrategy(final DOMException domException) {
79 doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
80 doReturn(immediateFailedFluentFuture(domException)).when(readWrite).commit();
81 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX);
82 return new MdsalRestconfStrategy(mockDataBroker);
86 public void testPutContainerData() {
87 doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
88 doReturn(read).when(mockDataBroker).newReadOnlyTransaction();
89 doReturn(immediateFalseFluentFuture()).when(read).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
90 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX);
91 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
93 PutDataTransactionUtil.putData(JUKEBOX_IID, EMPTY_JUKEBOX, JUKEBOX_SCHEMA,
94 new MdsalRestconfStrategy(mockDataBroker), WriteDataParams.empty());
95 verify(read).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
96 verify(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX);
100 public void testPutLeafData() {
101 doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
102 doReturn(read).when(mockDataBroker).newReadOnlyTransaction();
103 doReturn(immediateFalseFluentFuture()).when(read).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
104 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, GAP_IID, GAP_LEAF);
105 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
107 PutDataTransactionUtil.putData(GAP_IID, GAP_LEAF, JUKEBOX_SCHEMA, new MdsalRestconfStrategy(mockDataBroker),
108 WriteDataParams.empty());
109 verify(read).exists(LogicalDatastoreType.CONFIGURATION, GAP_IID);
110 verify(readWrite).put(LogicalDatastoreType.CONFIGURATION, GAP_IID, GAP_LEAF);
115 public void testPutListData() {
116 doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
117 doReturn(read).when(mockDataBroker).newReadOnlyTransaction();
118 doReturn(immediateFalseFluentFuture())
119 .when(read).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
120 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, JUKEBOX_WITH_BANDS);
121 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
122 PutDataTransactionUtil.putData(JUKEBOX_IID, JUKEBOX_WITH_BANDS, JUKEBOX_SCHEMA,
123 new MdsalRestconfStrategy(mockDataBroker), WriteDataParams.empty());
124 verify(read).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
125 verify(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, JUKEBOX_WITH_BANDS);
129 RestconfStrategy testPostContainerDataStrategy() {
130 doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID);
131 doNothing().when(readWrite).put(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX);
132 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
133 return new MdsalRestconfStrategy(mockDataBroker);
137 RestconfStrategy testPatchContainerDataStrategy() {
138 doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
139 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
140 return new MdsalRestconfStrategy(mockDataBroker);
144 RestconfStrategy testPatchLeafDataStrategy() {
145 doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
146 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
147 return new MdsalRestconfStrategy(mockDataBroker);
151 RestconfStrategy testPatchListDataStrategy() {
152 doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction();
153 doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit();
154 return new MdsalRestconfStrategy(mockDataBroker);
158 RestconfStrategy testPatchDataReplaceMergeAndRemoveStrategy() {
159 return new MdsalRestconfStrategy(mockDataBroker);
163 RestconfStrategy testPatchDataCreateAndDeleteStrategy() {
164 doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION, PLAYER_IID);
165 doReturn(immediateTrueFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION,
166 CREATE_AND_DELETE_TARGET);
167 return new MdsalRestconfStrategy(mockDataBroker);
171 RestconfStrategy testPatchMergePutContainerStrategy() {
172 return new MdsalRestconfStrategy(mockDataBroker);
176 RestconfStrategy deleteNonexistentDataTestStrategy() {
177 doReturn(immediateFalseFluentFuture()).when(readWrite).exists(LogicalDatastoreType.CONFIGURATION,
178 CREATE_AND_DELETE_TARGET);
179 return new MdsalRestconfStrategy(mockDataBroker);
183 void assertTestDeleteNonexistentData(final PatchStatusContext status) {
184 final var editCollection = status.getEditCollection();
185 assertEquals(1, editCollection.size());
186 final var editErrors = editCollection.get(0).getEditErrors();
187 assertEquals(1, editErrors.size());
188 final var editError = editErrors.get(0);
189 assertEquals(ErrorType.PROTOCOL, editError.getErrorType());
190 assertEquals(ErrorTag.DATA_MISSING, editError.getErrorTag());