|
57 | 57 | import static org.mockito.ArgumentMatchers.any; |
58 | 58 | import static org.mockito.ArgumentMatchers.anyLong; |
59 | 59 | import static org.mockito.Mockito.mock; |
| 60 | +import static org.mockito.Mockito.never; |
60 | 61 | import static org.mockito.Mockito.verify; |
61 | 62 | import static org.mockito.Mockito.when; |
62 | 63 |
|
@@ -198,4 +199,109 @@ void shouldSendNoneStatusWhenNoMarks() { |
198 | 199 | ChangeRequest changeRequest = requestCaptor.getValue().getClientSubmission().getStages().get(0).getChanges().get(0); |
199 | 200 | assertEquals(CloudTargetSystemAuditMarkType.NONE, changeRequest.getOngoingStatus()); |
200 | 201 | } |
| 202 | + |
| 203 | + @Test |
| 204 | + @DisplayName("Should clear marks when response has synchronizedMarks=true") |
| 205 | + void shouldClearMarksWhenResponseHasSynchronizedMarksTrue() { |
| 206 | + TargetSystemAuditMarker marker1 = mock(TargetSystemAuditMarker.class); |
| 207 | + when(marker1.listAll()).thenReturn(new HashSet<>(Collections.singletonList( |
| 208 | + new TargetSystemAuditMark(change1.getId(), TargetSystemAuditMarkType.APPLIED) |
| 209 | + ))); |
| 210 | + |
| 211 | + TargetSystemAuditMarker marker2 = mock(TargetSystemAuditMarker.class); |
| 212 | + when(marker2.listAll()).thenReturn(new HashSet<>(Collections.singletonList( |
| 213 | + new TargetSystemAuditMark(change2.getId(), TargetSystemAuditMarkType.ROLLED_BACK) |
| 214 | + ))); |
| 215 | + |
| 216 | + CloudExecutionPlanner planner = buildPlanner(Arrays.asList(marker1, marker2)); |
| 217 | + |
| 218 | + ExecutionPlanResponse response = buildSyncResponse(CloudExecutionAction.CONTINUE, true); |
| 219 | + when(client.createExecution(any(), any(), anyLong())).thenReturn(response); |
| 220 | + |
| 221 | + List<AbstractLoadedStage> stages = Collections.singletonList( |
| 222 | + new DefaultLoadedStage("stage-1", StageType.DEFAULT, Arrays.asList(change1, change2))); |
| 223 | + |
| 224 | + planner.getNextExecution(stages); |
| 225 | + |
| 226 | + verify(marker1).clearMark(change1.getId()); |
| 227 | + verify(marker2).clearMark(change2.getId()); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + @DisplayName("Should not clear marks when response has synchronizedMarks=false") |
| 232 | + void shouldNotClearMarksWhenResponseHasSynchronizedMarksFalse() { |
| 233 | + TargetSystemAuditMarker marker1 = mock(TargetSystemAuditMarker.class); |
| 234 | + when(marker1.listAll()).thenReturn(new HashSet<>(Collections.singletonList( |
| 235 | + new TargetSystemAuditMark(change1.getId(), TargetSystemAuditMarkType.APPLIED) |
| 236 | + ))); |
| 237 | + |
| 238 | + CloudExecutionPlanner planner = buildPlanner(Collections.singletonList(marker1)); |
| 239 | + |
| 240 | + ExecutionPlanResponse response = buildSyncResponse(CloudExecutionAction.CONTINUE, false); |
| 241 | + when(client.createExecution(any(), any(), anyLong())).thenReturn(response); |
| 242 | + |
| 243 | + List<AbstractLoadedStage> stages = Collections.singletonList( |
| 244 | + new DefaultLoadedStage("stage-1", StageType.DEFAULT, Collections.singletonList(change1))); |
| 245 | + |
| 246 | + planner.getNextExecution(stages); |
| 247 | + |
| 248 | + verify(marker1, never()).clearMark(any()); |
| 249 | + } |
| 250 | + |
| 251 | + @Test |
| 252 | + @DisplayName("Should clear marks regardless of response action (ABORT)") |
| 253 | + void shouldClearMarksRegardlessOfResponseAction() { |
| 254 | + TargetSystemAuditMarker marker1 = mock(TargetSystemAuditMarker.class); |
| 255 | + when(marker1.listAll()).thenReturn(new HashSet<>(Collections.singletonList( |
| 256 | + new TargetSystemAuditMark(change1.getId(), TargetSystemAuditMarkType.APPLIED) |
| 257 | + ))); |
| 258 | + |
| 259 | + CloudExecutionPlanner planner = buildPlanner(Collections.singletonList(marker1)); |
| 260 | + |
| 261 | + ExecutionPlanResponse response = buildSyncResponse(CloudExecutionAction.ABORT, true); |
| 262 | + when(client.createExecution(any(), any(), anyLong())).thenReturn(response); |
| 263 | + |
| 264 | + List<AbstractLoadedStage> stages = Collections.singletonList( |
| 265 | + new DefaultLoadedStage("stage-1", StageType.DEFAULT, Collections.singletonList(change1))); |
| 266 | + |
| 267 | + planner.getNextExecution(stages); |
| 268 | + |
| 269 | + verify(marker1).clearMark(change1.getId()); |
| 270 | + } |
| 271 | + |
| 272 | + @Test |
| 273 | + @DisplayName("Should only clear marks that were captured in the snapshot, not new ones") |
| 274 | + void shouldNotClearNewMarksWrittenAfterRequest() { |
| 275 | + TargetSystemAuditMarker marker1 = mock(TargetSystemAuditMarker.class); |
| 276 | + // At snapshot time, only change1 has a mark |
| 277 | + when(marker1.listAll()).thenReturn(new HashSet<>(Collections.singletonList( |
| 278 | + new TargetSystemAuditMark(change1.getId(), TargetSystemAuditMarkType.APPLIED) |
| 279 | + ))); |
| 280 | + |
| 281 | + CloudExecutionPlanner planner = buildPlanner(Collections.singletonList(marker1)); |
| 282 | + |
| 283 | + ExecutionPlanResponse response = buildSyncResponse(CloudExecutionAction.CONTINUE, true); |
| 284 | + when(client.createExecution(any(), any(), anyLong())).thenReturn(response); |
| 285 | + |
| 286 | + List<AbstractLoadedStage> stages = Collections.singletonList( |
| 287 | + new DefaultLoadedStage("stage-1", StageType.DEFAULT, Arrays.asList(change1, change2))); |
| 288 | + |
| 289 | + planner.getNextExecution(stages); |
| 290 | + |
| 291 | + // Only change1 should be cleared (was in snapshot), not change2 |
| 292 | + verify(marker1).clearMark(change1.getId()); |
| 293 | + verify(marker1, never()).clearMark(change2.getId()); |
| 294 | + } |
| 295 | + |
| 296 | + private ExecutionPlanResponse buildSyncResponse(CloudExecutionAction action, boolean synchronizedMarks) { |
| 297 | + ExecutionPlanResponse response = new ExecutionPlanResponse( |
| 298 | + action, "exec-1", null, |
| 299 | + Collections.singletonList(new StageResponse("stage-1", 0, |
| 300 | + Arrays.asList( |
| 301 | + new ChangeResponse(change1.getId(), CloudChangeAction.SKIP), |
| 302 | + new ChangeResponse(change2.getId(), CloudChangeAction.SKIP)))) |
| 303 | + ); |
| 304 | + response.setSynchronizedMarks(synchronizedMarks); |
| 305 | + return response; |
| 306 | + } |
201 | 307 | } |
0 commit comments