### Question:
FreeIpaDeletionService { public void delete(String environmentCrn, String accountId, boolean forced) { List<Stack> stacks = stackService.findAllByEnvironmentCrnAndAccountId(environmentCrn, accountId); if (stacks.isEmpty()) { throw new NotFoundException("No FreeIpa found in environment"); } stacks.forEach(stack -> validateDeletion(stack, accountId)); stacks.forEach(stack -> unscheduleAndTriggerTerminate(stack, forced)); }  void delete(String environmentCrn, String accountId, boolean forced);  }### Answer:
@Test void delete() { when(stackService.findAllByEnvironmentCrnAndAccountId(eq(ENVIRONMENT_CRN), eq(ACCOUNT_ID))).thenReturn(Collections.singletonList(stack)); when(flowLogService.findAllByResourceIdAndFinalizedIsFalseOrderByCreatedDesc(STACK_ID)).thenReturn(List.of()); underTest.delete(ENVIRONMENT_CRN, ACCOUNT_ID, false); verify(stackService, times(1)).findAllByEnvironmentCrnAndAccountId(eq(ENVIRONMENT_CRN), eq(ACCOUNT_ID)); ArgumentCaptor<TerminationEvent> terminationEventArgumentCaptor = ArgumentCaptor.forClass(TerminationEvent.class); verify(flowManager, times(1)).notify(eq(TERMINATION_EVENT.event()), terminationEventArgumentCaptor.capture()); verify(flowCancelService).cancelTooOldTerminationFlowForResource(stack.getId(), stack.getName()); verify(freeipaJobService).unschedule(stack); assertAll( () -> assertEquals(TERMINATION_EVENT.event(), terminationEventArgumentCaptor.getValue().selector()), () -> assertEquals(STACK_ID, terminationEventArgumentCaptor.getValue().getResourceId()), () -> assertFalse(terminationEventArgumentCaptor.getValue().getForced()) ); }
@Test public void testAlreadyDeleted() { stack.getStackStatus().setStatus(Status.DELETE_COMPLETED); when(stackService.findAllByEnvironmentCrnAndAccountId(eq(ENVIRONMENT_CRN), eq(ACCOUNT_ID))).thenReturn(Collections.singletonList(stack)); underTest.delete(ENVIRONMENT_CRN, ACCOUNT_ID, false); verify(flowManager, never()).notify(anyString(), any(Acceptable.class)); verify(flowCancelService, never()).cancelRunningFlows(stack.getId()); }
@Test public void testTerminationFlowExists() { when(stackService.findAllByEnvironmentCrnAndAccountId(eq(ENVIRONMENT_CRN), eq(ACCOUNT_ID))).thenReturn(Collections.singletonList(stack)); when(applicationFlowInformation.getTerminationFlow()).thenReturn(List.of(StackTerminationFlowConfig.class)); FlowLog flowLog = new FlowLog(); flowLog.setFlowType(StackTerminationFlowConfig.class); flowLog.setCurrentState(StackTerminationState.INIT_STATE.name()); when(flowLogService.findAllByResourceIdAndFinalizedIsFalseOrderByCreatedDesc(stack.getId())).thenReturn(List.of(flowLog)); underTest.delete(ENVIRONMENT_CRN, ACCOUNT_ID, false); verify(flowManager, never()).notify(anyString(), any(Acceptable.class)); verify(flowCancelService, never()).cancelRunningFlows(stack.getId()); }
@Test void deleteInvalid() { when(stackService.findAllByEnvironmentCrnAndAccountId(eq(ENVIRONMENT_CRN), eq(ACCOUNT_ID))).thenReturn(Collections.singletonList(stack)); when(childEnvironmentService.findChildEnvironments(stack, ACCOUNT_ID)).thenReturn(Collections.singletonList(new ChildEnvironment())); assertThrows(BadRequestException.class, () -> underTest.delete(ENVIRONMENT_CRN, ACCOUNT_ID, false)); verify(stackService, times(1)).findAllByEnvironmentCrnAndAccountId(eq(ENVIRONMENT_CRN), eq(ACCOUNT_ID)); verify(childEnvironmentService, times(1)).findChildEnvironments(stack, ACCOUNT_ID); }