### Question:
ProvisionerService { public void startStackProvisioning(Long id, DetailedEnvironmentResponse environment) { SdxCluster sdxCluster = sdxService.getById(id); LOGGER.info("Call cloudbreak with stackrequest"); try { stackRequestManifester.configureStackForSdxCluster(sdxCluster, environment); StackV4Request stackV4Request = JsonUtil.readValue(sdxCluster.getStackRequestToCloudbreak(), StackV4Request.class); Optional.ofNullable(sdxCluster.getDatabaseCrn()).ifPresent(crn -> { stackV4Request.getCluster().setDatabaseServerCrn(crn); sdxCluster.setStackRequestToCloudbreak(JsonUtil.writeValueAsStringSilent(stackV4Request)); }); StackV4Response stackV4Response; try { stackV4Response = ThreadBasedUserCrnProvider.doAsInternalActor(() -> stackV4Endpoint.getByCrn(0L, sdxCluster.getCrn(), null)); } catch (NotFoundException e) { LOGGER.info("Stack does not exist on cloudbreak side, POST new cluster: {}", sdxCluster.getClusterName(), e); String initiatorUserCrn = ThreadBasedUserCrnProvider.getUserCrn(); stackV4Response = ThreadBasedUserCrnProvider.doAsInternalActor(() -> stackV4Endpoint.postInternal(0L, stackV4Request, initiatorUserCrn)); } sdxCluster.setStackId(stackV4Response.getId()); sdxCluster.setStackCrn(stackV4Response.getCrn()); sdxClusterRepository.save(sdxCluster); cloudbreakFlowService.saveLastCloudbreakFlowChainId(sdxCluster, stackV4Response.getFlowIdentifier()); LOGGER.info("Sdx cluster updated"); } catch (WebApplicationException e) { String errorMessage = webApplicationExceptionMessageExtractor.getErrorMessage(e); LOGGER.info("Cannot start provisioning: {}", errorMessage, e); throw new RuntimeException("Cannot start provisioning, some error happened on Cloudbreak side: " + errorMessage); } catch (IOException e) { LOGGER.info("Cannot parse stackrequest to json", e); throw new RuntimeException("Cannot write stackrequest to json: " + e.getMessage()); } }  void startStackDeletion(Long id, boolean forced); void waitCloudbreakClusterDeletion(Long id, PollingConfig pollingConfig); void startStackProvisioning(Long id, DetailedEnvironmentResponse environment); StackV4Response waitCloudbreakClusterCreation(Long id, PollingConfig pollingConfig); static final int DELETE_FAILED_RETRY_COUNT; }### Answer:
@Test void startProvisioning() { long clusterId = CLUSTER_ID.incrementAndGet(); SdxCluster sdxCluster = generateValidSdxCluster(clusterId); StackV4Response stackV4Response = new StackV4Response(); stackV4Response.setId(1L); ThreadBasedUserCrnProvider.setUserCrn(USER_CRN); when(stackV4Endpoint.getByCrn(anyLong(), nullable(String.class), nullable(Set.class))).thenThrow(new NotFoundException()); when(stackV4Endpoint.postInternal(anyLong(), any(StackV4Request.class), nullable(String.class))).thenReturn(stackV4Response); when(sdxService.getById(clusterId)).thenReturn(sdxCluster); underTest.startStackProvisioning(clusterId, getEnvironmentResponse()); verify(cloudbreakFlowService).saveLastCloudbreakFlowChainId(sdxCluster, stackV4Response.getFlowIdentifier()); verify(sdxClusterRepository, times(1)).save(any(SdxCluster.class)); }
@Test void startProvisioningSdxNotFound() { long clusterId = CLUSTER_ID.incrementAndGet(); when(sdxService.getById(clusterId)).thenThrow(new com.sequenceiq.cloudbreak.exception.NotFoundException("not found")); Assertions.assertThrows(com.sequenceiq.cloudbreak.exception.NotFoundException.class, () -> underTest.startStackProvisioning(clusterId, getEnvironmentResponse())); verifyNoInteractions(cloudbreakFlowService); }