BrokerStatusListener implements EventSubscriber<BrokerEvent> { @Override public void onEvent(BrokerEvent event) { if (event.getRuntimeId() == null || !workspaceId.equals(event.getRuntimeId().getWorkspaceId())) { return; } switch (event.getStatus()) { case DONE: List<ChePlugin> tooling = event.getTooling(); if (tooling != null) { try { pluginsValidator.validatePluginNames(tooling); } catch (ValidationException e) { brokersResult.error(e); return; } try { brokersResult.setResult(tooling); } catch (InfrastructureException e) { LOG.error(e.getLocalizedMessage(), e); } } else { brokersResult.error( new InternalInfrastructureException( format( "Plugin brokering process for workspace `%s` is finished but plugins list is missing", workspaceId))); } break; case FAILED: brokersResult.error( new InfrastructureException( format( "Plugin brokering process for workspace %s failed with error: %s", workspaceId, event.getError()))); break; case STARTED: default: } } BrokerStatusListener(
      String workspaceId,
      KubernetesPluginsToolingValidator pluginsValidator,
      BrokersResult brokersResult); @Override void onEvent(BrokerEvent event);  }
@Test public void shouldDoNothingIfEventWithForeignWorkspaceIdIsReceived() { BrokerEvent event = new BrokerEvent() .withRuntimeId(new RuntimeIdentityImpl("foreignWorkspace", null, null, null)); brokerStatusListener.onEvent(event); verifyNoMoreInteractions(brokersResult); }
@Test public void shouldDoNothingIfEventWithoutRuntimeIdentityIsReceived() { BrokerEvent event = new BrokerEvent().withRuntimeId(null); brokerStatusListener.onEvent(event); verifyNoMoreInteractions(brokersResult); }
@Test public void shouldAddResultWhenDoneEventIsReceivedAndToolingIsNotNull() throws Exception { BrokerEvent event = new BrokerEvent() .withRuntimeId(new RuntimeIdentityImpl(WORKSPACE_ID, null, null, null)) .withStatus(BrokerStatus.DONE) .withTooling(emptyList()); brokerStatusListener.onEvent(event); verify(brokersResult).setResult(emptyList()); }
@Test public void shouldSubmitErrorWhenDoneEventIsReceivedButToolingIsValidationFails() throws Exception { doThrow(new ValidationException("test")).when(validator).validatePluginNames(anyList()); BrokerEvent event = new BrokerEvent() .withRuntimeId(new RuntimeIdentityImpl(WORKSPACE_ID, null, null, null)) .withStatus(BrokerStatus.DONE) .withTooling(emptyList()); brokerStatusListener.onEvent(event); verify(brokersResult).error(any(ValidationException.class)); }
@Test public void shouldNotCallAddResultWhenValidationFails() throws Exception { doThrow(new ValidationException("test")).when(validator).validatePluginNames(anyList()); BrokerEvent event = new BrokerEvent() .withRuntimeId(new RuntimeIdentityImpl(WORKSPACE_ID, null, null, null)) .withStatus(BrokerStatus.DONE) .withTooling(emptyList()); brokerStatusListener.onEvent(event); verify(brokersResult, never()).setResult(anyList()); }
@Test public void shouldSubmitErrorWhenDoneEventIsReceivedButToolingIsNull() { BrokerEvent event = new BrokerEvent() .withRuntimeId(new RuntimeIdentityImpl(WORKSPACE_ID, null, null, null)) .withStatus(BrokerStatus.DONE) .withTooling(null); brokerStatusListener.onEvent(event); verify(brokersResult).error(any(InternalInfrastructureException.class)); }
@Test public void shouldSubmitErrorWhenFailedEventIsReceived() { BrokerEvent event = new BrokerEvent() .withRuntimeId(new RuntimeIdentityImpl(WORKSPACE_ID, null, null, null)) .withStatus(BrokerStatus.FAILED) .withError("error"); brokerStatusListener.onEvent(event); verify(brokersResult).error(any(InfrastructureException.class)); }