@Test void acceptEnvironmentNotFound() { doAnswer(i -> null).when(eventSender).sendEvent(baseNamedFlowEvent.capture(), any(Headers.class)); when(environmentService.findEnvironmentById(ENVIRONMENT_ID)).thenReturn(Optional.empty()); underTest.accept(environmentDtoEvent); verify(eventSender).sendEvent(baseNamedFlowEvent.capture(), headersArgumentCaptor.capture()); verify(environmentService, never()).save(any()); verifyEnvCreationEvent(); }
@Override public void accept(Event<EnvironmentDto> environmentDtoEvent) { LOGGER.debug("Accepting PublicKeyCreation event"); EnvironmentDto environmentDto = environmentDtoEvent.getData(); try { environmentService.findEnvironmentById(environmentDto.getId()).ifPresent(environment -> { if (environment.getAuthentication().isManagedKey()) { boolean created = environmentResourceService.createAndUpdateSshKey(environment); if (created) { String publicKeyId = environment.getAuthentication().getPublicKeyId(); LOGGER.info("Update the environment and it's authentication with the created public SSH key id: '{}'", publicKeyId); environmentService.save(environment); } else { LOGGER.info("The public key id could not be created for {}", environmentDto.getName()); } } else { LOGGER.debug("Environment {} requested no managed public key", environment.getName()); } }); EnvCreationEvent envCreationEvent = getEnvCreateEvent(environmentDto); eventSender().sendEvent(envCreationEvent, environmentDtoEvent.getHeaders()); } catch (Exception e) { EnvCreationFailureEvent failedEvent = new EnvCreationFailureEvent(environmentDto.getId(), environmentDto.getName(), e, environmentDto.getResourceCrn()); Event<EnvCreationFailureEvent> ev = new Event<>(environmentDtoEvent.getHeaders(), failedEvent); eventBus.notify(failedEvent.selector(), ev); } }
PublicKeyCreationHandler extends EventSenderAwareHandler<EnvironmentDto> { @Override public void accept(Event<EnvironmentDto> environmentDtoEvent) { LOGGER.debug("Accepting PublicKeyCreation event"); EnvironmentDto environmentDto = environmentDtoEvent.getData(); try { environmentService.findEnvironmentById(environmentDto.getId()).ifPresent(environment -> { if (environment.getAuthentication().isManagedKey()) { boolean created = environmentResourceService.createAndUpdateSshKey(environment); if (created) { String publicKeyId = environment.getAuthentication().getPublicKeyId(); LOGGER.info("Update the environment and it's authentication with the created public SSH key id: '{}'", publicKeyId); environmentService.save(environment); } else { LOGGER.info("The public key id could not be created for {}", environmentDto.getName()); } } else { LOGGER.debug("Environment {} requested no managed public key", environment.getName()); } }); EnvCreationEvent envCreationEvent = getEnvCreateEvent(environmentDto); eventSender().sendEvent(envCreationEvent, environmentDtoEvent.getHeaders()); } catch (Exception e) { EnvCreationFailureEvent failedEvent = new EnvCreationFailureEvent(environmentDto.getId(), environmentDto.getName(), e, environmentDto.getResourceCrn()); Event<EnvCreationFailureEvent> ev = new Event<>(environmentDtoEvent.getHeaders(), failedEvent); eventBus.notify(failedEvent.selector(), ev); } } }
PublicKeyCreationHandler extends EventSenderAwareHandler<EnvironmentDto> { @Override public void accept(Event<EnvironmentDto> environmentDtoEvent) { LOGGER.debug("Accepting PublicKeyCreation event"); EnvironmentDto environmentDto = environmentDtoEvent.getData(); try { environmentService.findEnvironmentById(environmentDto.getId()).ifPresent(environment -> { if (environment.getAuthentication().isManagedKey()) { boolean created = environmentResourceService.createAndUpdateSshKey(environment); if (created) { String publicKeyId = environment.getAuthentication().getPublicKeyId(); LOGGER.info("Update the environment and it's authentication with the created public SSH key id: '{}'", publicKeyId); environmentService.save(environment); } else { LOGGER.info("The public key id could not be created for {}", environmentDto.getName()); } } else { LOGGER.debug("Environment {} requested no managed public key", environment.getName()); } }); EnvCreationEvent envCreationEvent = getEnvCreateEvent(environmentDto); eventSender().sendEvent(envCreationEvent, environmentDtoEvent.getHeaders()); } catch (Exception e) { EnvCreationFailureEvent failedEvent = new EnvCreationFailureEvent(environmentDto.getId(), environmentDto.getName(), e, environmentDto.getResourceCrn()); Event<EnvCreationFailureEvent> ev = new Event<>(environmentDtoEvent.getHeaders(), failedEvent); eventBus.notify(failedEvent.selector(), ev); } } protected  PublicKeyCreationHandler(EventSender eventSender, EnvironmentService environmentService, EnvironmentResourceService environmentResourceService,
            EventBus eventBus); }
PublicKeyCreationHandler extends EventSenderAwareHandler<EnvironmentDto> { @Override public void accept(Event<EnvironmentDto> environmentDtoEvent) { LOGGER.debug("Accepting PublicKeyCreation event"); EnvironmentDto environmentDto = environmentDtoEvent.getData(); try { environmentService.findEnvironmentById(environmentDto.getId()).ifPresent(environment -> { if (environment.getAuthentication().isManagedKey()) { boolean created = environmentResourceService.createAndUpdateSshKey(environment); if (created) { String publicKeyId = environment.getAuthentication().getPublicKeyId(); LOGGER.info("Update the environment and it's authentication with the created public SSH key id: '{}'", publicKeyId); environmentService.save(environment); } else { LOGGER.info("The public key id could not be created for {}", environmentDto.getName()); } } else { LOGGER.debug("Environment {} requested no managed public key", environment.getName()); } }); EnvCreationEvent envCreationEvent = getEnvCreateEvent(environmentDto); eventSender().sendEvent(envCreationEvent, environmentDtoEvent.getHeaders()); } catch (Exception e) { EnvCreationFailureEvent failedEvent = new EnvCreationFailureEvent(environmentDto.getId(), environmentDto.getName(), e, environmentDto.getResourceCrn()); Event<EnvCreationFailureEvent> ev = new Event<>(environmentDtoEvent.getHeaders(), failedEvent); eventBus.notify(failedEvent.selector(), ev); } } protected  PublicKeyCreationHandler(EventSender eventSender, EnvironmentService environmentService, EnvironmentResourceService environmentResourceService,
            EventBus eventBus); @Override String selector(); @Override void accept(Event<EnvironmentDto> environmentDtoEvent); }
PublicKeyCreationHandler extends EventSenderAwareHandler<EnvironmentDto> { @Override public void accept(Event<EnvironmentDto> environmentDtoEvent) { LOGGER.debug("Accepting PublicKeyCreation event"); EnvironmentDto environmentDto = environmentDtoEvent.getData(); try { environmentService.findEnvironmentById(environmentDto.getId()).ifPresent(environment -> { if (environment.getAuthentication().isManagedKey()) { boolean created = environmentResourceService.createAndUpdateSshKey(environment); if (created) { String publicKeyId = environment.getAuthentication().getPublicKeyId(); LOGGER.info("Update the environment and it's authentication with the created public SSH key id: '{}'", publicKeyId); environmentService.save(environment); } else { LOGGER.info("The public key id could not be created for {}", environmentDto.getName()); } } else { LOGGER.debug("Environment {} requested no managed public key", environment.getName()); } }); EnvCreationEvent envCreationEvent = getEnvCreateEvent(environmentDto); eventSender().sendEvent(envCreationEvent, environmentDtoEvent.getHeaders()); } catch (Exception e) { EnvCreationFailureEvent failedEvent = new EnvCreationFailureEvent(environmentDto.getId(), environmentDto.getName(), e, environmentDto.getResourceCrn()); Event<EnvCreationFailureEvent> ev = new Event<>(environmentDtoEvent.getHeaders(), failedEvent); eventBus.notify(failedEvent.selector(), ev); } } protected  PublicKeyCreationHandler(EventSender eventSender, EnvironmentService environmentService, EnvironmentResourceService environmentResourceService,
            EventBus eventBus); @Override String selector(); @Override void accept(Event<EnvironmentDto> environmentDtoEvent);  }