### Question:
StackApiViewService { public boolean canChangeCredential(StackApiView stackApiView) { if (stackApiView.getStatus() != null) { if (stackApiView.getStatus() == Status.AVAILABLE) { return !flowLogService.isOtherNonTerminationFlowRunning(stackApiView.getId()); } } return false; } StackApiViewService(); boolean canChangeCredential(StackApiView stackApiView); StackApiView save(StackApiView stackApiView); Set<StackApiView> retrieveStackViewsByWorkspaceIdAndEnvironmentName(Long workspaceId, String environmentName, List<StackType> stackTypes); Set<StackApiView> retrieveStackViewsByWorkspaceIdAndEnvironmentCrn(Long workspaceId, String environmentCrn, List<StackType> stackTypes); @PreAuthorize("hasRole('INTERNAL')") StackApiView retrieveStackByCrnAndType(String crn, StackType stackType);  }### Answer:
@Test public void testCanChangeCredentialWithNullStatus() { StackApiView stackApiView = new StackApiView(); stackApiView.setStackStatus(null); boolean result = stackApiViewService.canChangeCredential(stackApiView); assertFalse(result); }
@Test public void testCanChangeCredentialWithStatusIsNotAvailable() { StackApiView stackApiView = new StackApiView(); StackStatusView stackStatusView = Mockito.mock(StackStatusView.class); when(stackStatusView.getStatus()).thenReturn(Status.CREATE_IN_PROGRESS); stackApiView.setStackStatus(stackStatusView); boolean result = stackApiViewService.canChangeCredential(stackApiView); assertFalse(result); }
@Test public void testCanChangeCredentialWithOngoingFlowOperation() { Long stackId = 1L; StackApiView stackApiView = new StackApiView(); stackApiView.setId(stackId); StackStatusView stackStatusView = Mockito.mock(StackStatusView.class); when(stackStatusView.getStatus()).thenReturn(Status.AVAILABLE); stackApiView.setStackStatus(stackStatusView); when(flowLogService.isOtherNonTerminationFlowRunning(stackId)).thenReturn(true); boolean result = stackApiViewService.canChangeCredential(stackApiView); assertFalse(result); }
@Test public void testCanChangeCredentialHappyPath() { Long stackId = 1L; StackApiView stackApiView = new StackApiView(); stackApiView.setId(stackId); StackStatusView stackStatusView = Mockito.mock(StackStatusView.class); when(stackStatusView.getStatus()).thenReturn(Status.AVAILABLE); stackApiView.setStackStatus(stackStatusView); when(flowLogService.isOtherNonTerminationFlowRunning(stackId)).thenReturn(false); boolean result = stackApiViewService.canChangeCredential(stackApiView); assertTrue(result); }