@Test public void handleImageUpdateFailure() { FlowEvent flowEvent = Mockito.mock(FlowEvent.class); when(stateContext.getEvent()).thenReturn(flowEvent); when(flowEvent.name()).thenReturn(EVENT_NAME); StackFailureEvent payload = new StackFailureEvent(StackImageUpdateEvent.STACK_IMAGE_UPDATE_FAILED_EVENT.event(), 1L, new CloudbreakServiceException("test")); when(stateContext.getMessageHeader(HEADERS.DATA.name())).thenReturn(payload); when(state.getId()).thenReturn(StackImageUpdateState.STACK_IMAGE_UPDATE_FAILED_STATE); when(stackService.getViewByIdWithoutAuth(anyLong())).thenReturn(new StackView(1L, null, null, null)); when(runningFlows.get(anyString())).thenReturn(flow); handleImageUpdateFailureAction.execute(stateContext); verify(flowMessageService, times(1)).fireEventAndLog(anyLong(), eq(Status.UPDATE_FAILED.name()), eq(ResourceEvent.STACK_IMAGE_UPDATE_FAILED), eq("test")); verify(eventBus, times(1)).notify(eq(StackImageUpdateEvent.STACK_IMAGE_UPDATE_FAILE_HANDLED_EVENT.event()), any(Event.class)); }
@Bean(name = "STACK_IMAGE_UPDATE_FAILED_STATE") public AbstractStackFailureAction<StackImageUpdateState, StackImageUpdateEvent> handleImageUpdateFailure() { return new AbstractStackFailureAction<>() { @Inject private CloudbreakFlowMessageService flowMessageService; @Inject private StackUpdater stackUpdater; @Inject private ComponentConfigProviderService componentConfigProviderService; @Inject private StackService stackService; @Override protected void doExecute(StackFailureContext context, StackFailureEvent payload, Map<Object, Object> variables) { LOGGER.info("Error during Stack image update flow:", payload.getException()); if (variables.containsKey(AbstractStackImageUpdateAction.ORIGINAL_IMAGE)) { Image originalImage = (Image) variables.get(AbstractStackImageUpdateAction.ORIGINAL_IMAGE); LOGGER.debug("Reset image to the original"); try { Stack stack = stackService.getByIdWithTransaction(context.getStackView().getId()); Component component = new Component(ComponentType.IMAGE, ComponentType.IMAGE.name(), new Json(originalImage), stack); componentConfigProviderService.replaceImageComponentWithNew(component); LOGGER.debug("Image restored"); } catch (IllegalArgumentException e) { LOGGER.info("Could not parse JSON. Image restore failed"); } } flowMessageService.fireEventAndLog(context.getStackView().getId(), Status.UPDATE_FAILED.name(), STACK_IMAGE_UPDATE_FAILED, payload.getException().getMessage()); stackUpdater.updateStackStatus(context.getStackView().getId(), DetailedStackStatus.AVAILABLE); sendEvent(context, new StackEvent(StackImageUpdateEvent.STACK_IMAGE_UPDATE_FAILE_HANDLED_EVENT.event(), context.getStackView().getId())); } }; }
StackImageUpdateActions { @Bean(name = "STACK_IMAGE_UPDATE_FAILED_STATE") public AbstractStackFailureAction<StackImageUpdateState, StackImageUpdateEvent> handleImageUpdateFailure() { return new AbstractStackFailureAction<>() { @Inject private CloudbreakFlowMessageService flowMessageService; @Inject private StackUpdater stackUpdater; @Inject private ComponentConfigProviderService componentConfigProviderService; @Inject private StackService stackService; @Override protected void doExecute(StackFailureContext context, StackFailureEvent payload, Map<Object, Object> variables) { LOGGER.info("Error during Stack image update flow:", payload.getException()); if (variables.containsKey(AbstractStackImageUpdateAction.ORIGINAL_IMAGE)) { Image originalImage = (Image) variables.get(AbstractStackImageUpdateAction.ORIGINAL_IMAGE); LOGGER.debug("Reset image to the original"); try { Stack stack = stackService.getByIdWithTransaction(context.getStackView().getId()); Component component = new Component(ComponentType.IMAGE, ComponentType.IMAGE.name(), new Json(originalImage), stack); componentConfigProviderService.replaceImageComponentWithNew(component); LOGGER.debug("Image restored"); } catch (IllegalArgumentException e) { LOGGER.info("Could not parse JSON. Image restore failed"); } } flowMessageService.fireEventAndLog(context.getStackView().getId(), Status.UPDATE_FAILED.name(), STACK_IMAGE_UPDATE_FAILED, payload.getException().getMessage()); stackUpdater.updateStackStatus(context.getStackView().getId(), DetailedStackStatus.AVAILABLE); sendEvent(context, new StackEvent(StackImageUpdateEvent.STACK_IMAGE_UPDATE_FAILE_HANDLED_EVENT.event(), context.getStackView().getId())); } }; } }
StackImageUpdateActions { @Bean(name = "STACK_IMAGE_UPDATE_FAILED_STATE") public AbstractStackFailureAction<StackImageUpdateState, StackImageUpdateEvent> handleImageUpdateFailure() { return new AbstractStackFailureAction<>() { @Inject private CloudbreakFlowMessageService flowMessageService; @Inject private StackUpdater stackUpdater; @Inject private ComponentConfigProviderService componentConfigProviderService; @Inject private StackService stackService; @Override protected void doExecute(StackFailureContext context, StackFailureEvent payload, Map<Object, Object> variables) { LOGGER.info("Error during Stack image update flow:", payload.getException()); if (variables.containsKey(AbstractStackImageUpdateAction.ORIGINAL_IMAGE)) { Image originalImage = (Image) variables.get(AbstractStackImageUpdateAction.ORIGINAL_IMAGE); LOGGER.debug("Reset image to the original"); try { Stack stack = stackService.getByIdWithTransaction(context.getStackView().getId()); Component component = new Component(ComponentType.IMAGE, ComponentType.IMAGE.name(), new Json(originalImage), stack); componentConfigProviderService.replaceImageComponentWithNew(component); LOGGER.debug("Image restored"); } catch (IllegalArgumentException e) { LOGGER.info("Could not parse JSON. Image restore failed"); } } flowMessageService.fireEventAndLog(context.getStackView().getId(), Status.UPDATE_FAILED.name(), STACK_IMAGE_UPDATE_FAILED, payload.getException().getMessage()); stackUpdater.updateStackStatus(context.getStackView().getId(), DetailedStackStatus.AVAILABLE); sendEvent(context, new StackEvent(StackImageUpdateEvent.STACK_IMAGE_UPDATE_FAILE_HANDLED_EVENT.event(), context.getStackView().getId())); } }; }  }
StackImageUpdateActions { @Bean(name = "STACK_IMAGE_UPDATE_FAILED_STATE") public AbstractStackFailureAction<StackImageUpdateState, StackImageUpdateEvent> handleImageUpdateFailure() { return new AbstractStackFailureAction<>() { @Inject private CloudbreakFlowMessageService flowMessageService; @Inject private StackUpdater stackUpdater; @Inject private ComponentConfigProviderService componentConfigProviderService; @Inject private StackService stackService; @Override protected void doExecute(StackFailureContext context, StackFailureEvent payload, Map<Object, Object> variables) { LOGGER.info("Error during Stack image update flow:", payload.getException()); if (variables.containsKey(AbstractStackImageUpdateAction.ORIGINAL_IMAGE)) { Image originalImage = (Image) variables.get(AbstractStackImageUpdateAction.ORIGINAL_IMAGE); LOGGER.debug("Reset image to the original"); try { Stack stack = stackService.getByIdWithTransaction(context.getStackView().getId()); Component component = new Component(ComponentType.IMAGE, ComponentType.IMAGE.name(), new Json(originalImage), stack); componentConfigProviderService.replaceImageComponentWithNew(component); LOGGER.debug("Image restored"); } catch (IllegalArgumentException e) { LOGGER.info("Could not parse JSON. Image restore failed"); } } flowMessageService.fireEventAndLog(context.getStackView().getId(), Status.UPDATE_FAILED.name(), STACK_IMAGE_UPDATE_FAILED, payload.getException().getMessage()); stackUpdater.updateStackStatus(context.getStackView().getId(), DetailedStackStatus.AVAILABLE); sendEvent(context, new StackEvent(StackImageUpdateEvent.STACK_IMAGE_UPDATE_FAILE_HANDLED_EVENT.event(), context.getStackView().getId())); } }; }  @Bean(name = "CHECK_IMAGE_VERSIONS_STATE") AbstractStackImageUpdateAction<?> checkImageVersion(); @Bean(name = "CHECK_PACKAGE_VERSIONS_STATE") AbstractStackImageUpdateAction<?> checkPackageVersions(); @Bean(name = "UPDATE_IMAGE_STATE") AbstractStackImageUpdateAction<?> updateImage(); @Bean(name = "IMAGE_PREPARE_STATE") AbstractStackImageUpdateAction<?> prepareImageAction(); @Bean(name = "SET_IMAGE_STATE") AbstractStackImageUpdateAction<?> setImageAction(); @Bean(name = "STACK_IMAGE_UPDATE_FINISHED") AbstractStackImageUpdateAction<?> finishAction(); @Bean(name = "STACK_IMAGE_UPDATE_FAILED_STATE") AbstractStackFailureAction<StackImageUpdateState, StackImageUpdateEvent> handleImageUpdateFailure(); }
StackImageUpdateActions { @Bean(name = "STACK_IMAGE_UPDATE_FAILED_STATE") public AbstractStackFailureAction<StackImageUpdateState, StackImageUpdateEvent> handleImageUpdateFailure() { return new AbstractStackFailureAction<>() { @Inject private CloudbreakFlowMessageService flowMessageService; @Inject private StackUpdater stackUpdater; @Inject private ComponentConfigProviderService componentConfigProviderService; @Inject private StackService stackService; @Override protected void doExecute(StackFailureContext context, StackFailureEvent payload, Map<Object, Object> variables) { LOGGER.info("Error during Stack image update flow:", payload.getException()); if (variables.containsKey(AbstractStackImageUpdateAction.ORIGINAL_IMAGE)) { Image originalImage = (Image) variables.get(AbstractStackImageUpdateAction.ORIGINAL_IMAGE); LOGGER.debug("Reset image to the original"); try { Stack stack = stackService.getByIdWithTransaction(context.getStackView().getId()); Component component = new Component(ComponentType.IMAGE, ComponentType.IMAGE.name(), new Json(originalImage), stack); componentConfigProviderService.replaceImageComponentWithNew(component); LOGGER.debug("Image restored"); } catch (IllegalArgumentException e) { LOGGER.info("Could not parse JSON. Image restore failed"); } } flowMessageService.fireEventAndLog(context.getStackView().getId(), Status.UPDATE_FAILED.name(), STACK_IMAGE_UPDATE_FAILED, payload.getException().getMessage()); stackUpdater.updateStackStatus(context.getStackView().getId(), DetailedStackStatus.AVAILABLE); sendEvent(context, new StackEvent(StackImageUpdateEvent.STACK_IMAGE_UPDATE_FAILE_HANDLED_EVENT.event(), context.getStackView().getId())); } }; }  @Bean(name = "CHECK_IMAGE_VERSIONS_STATE") AbstractStackImageUpdateAction<?> checkImageVersion(); @Bean(name = "CHECK_PACKAGE_VERSIONS_STATE") AbstractStackImageUpdateAction<?> checkPackageVersions(); @Bean(name = "UPDATE_IMAGE_STATE") AbstractStackImageUpdateAction<?> updateImage(); @Bean(name = "IMAGE_PREPARE_STATE") AbstractStackImageUpdateAction<?> prepareImageAction(); @Bean(name = "SET_IMAGE_STATE") AbstractStackImageUpdateAction<?> setImageAction(); @Bean(name = "STACK_IMAGE_UPDATE_FINISHED") AbstractStackImageUpdateAction<?> finishAction(); @Bean(name = "STACK_IMAGE_UPDATE_FAILED_STATE") AbstractStackFailureAction<StackImageUpdateState, StackImageUpdateEvent> handleImageUpdateFailure();  }