Search in sources :

Example 1 with BridgeMultiReply

use of io.gravitee.cockpit.api.command.bridge.BridgeMultiReply in project gravitee-management-rest-api by gravitee-io.

the class ListEnvironmentOperationHandler method handle.

@Override
public Single<BridgeReply> handle(BridgeCommand bridgeCommand) {
    BridgeMultiReply multiReply = new BridgeMultiReply();
    multiReply.setCommandId(bridgeCommand.getId());
    try {
        final List<EnvironmentEntity> managedEnvironments = this.environmentService.findByOrganization(bridgeCommand.getOrganizationId());
        multiReply.setCommandStatus(CommandStatus.SUCCEEDED);
        multiReply.setReplies(managedEnvironments.stream().map(environmentEntity -> {
            BridgeSimpleReply simpleReply = new BridgeSimpleReply();
            simpleReply.setCommandId(bridgeCommand.getId());
            simpleReply.setCommandStatus(CommandStatus.SUCCEEDED);
            simpleReply.setOrganizationId(environmentEntity.getOrganizationId());
            simpleReply.setEnvironmentId(environmentEntity.getId());
            simpleReply.setInstallationId(installationService.get().getId());
            try {
                simpleReply.setPayload(objectMapper.writeValueAsString(environmentEntity));
            } catch (JsonProcessingException e) {
                logger.warn("Problem while serializing environment {}", environmentEntity.getId());
                simpleReply.setMessage("Problem while serializing environment: " + environmentEntity.getId());
                simpleReply.setCommandStatus(CommandStatus.ERROR);
            }
            return simpleReply;
        }).collect(Collectors.toList()));
    } catch (TechnicalManagementException ex) {
        multiReply.setCommandStatus(CommandStatus.ERROR);
        multiReply.setMessage("No environment available for organization: " + bridgeCommand.getOrganizationId());
    }
    return Single.just(multiReply);
}
Also used : BridgeMultiReply(io.gravitee.cockpit.api.command.bridge.BridgeMultiReply) BridgeSimpleReply(io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 2 with BridgeMultiReply

use of io.gravitee.cockpit.api.command.bridge.BridgeMultiReply in project gravitee-management-rest-api by gravitee-io.

the class CockpitServiceTest method shouldListEnvironmentsFromSuccessfulReplies.

@Test
public void shouldListEnvironmentsFromSuccessfulReplies() throws JsonProcessingException {
    // Given
    EnvironmentEntity envA = new EnvironmentEntity();
    envA.setId("my-env-A");
    envA.setOrganizationId(ORGANIZATION_ID);
    envA.setName("ENV A");
    BridgeSimpleReply envASimpleReply = new BridgeSimpleReply();
    envASimpleReply.setCommandStatus(CommandStatus.SUCCEEDED);
    envASimpleReply.setInstallationId(INSTALLATION_ID);
    envASimpleReply.setOrganizationId(ORGANIZATION_ID);
    envASimpleReply.setEnvironmentId(envA.getId());
    envASimpleReply.setPayload(objectMapper.writeValueAsString(envA));
    EnvironmentEntity envB = new EnvironmentEntity();
    envB.setId("my-env-B");
    envB.setOrganizationId(ORGANIZATION_ID);
    envB.setName("ENV B");
    BridgeSimpleReply envBSimpleReply = new BridgeSimpleReply();
    envBSimpleReply.setCommandStatus(CommandStatus.SUCCEEDED);
    envBSimpleReply.setInstallationId(INSTALLATION_ID);
    envBSimpleReply.setOrganizationId(ORGANIZATION_ID);
    envBSimpleReply.setEnvironmentId(envB.getId());
    envBSimpleReply.setPayload(objectMapper.writeValueAsString(envB));
    EnvironmentEntity envC_ERROR = new EnvironmentEntity();
    envC_ERROR.setId("my-env-C");
    envC_ERROR.setOrganizationId(ORGANIZATION_ID);
    envC_ERROR.setName("ENV C");
    BridgeSimpleReply envCSimpleReply = new BridgeSimpleReply();
    envCSimpleReply.setCommandStatus(CommandStatus.ERROR);
    envCSimpleReply.setInstallationId(INSTALLATION_ID);
    envCSimpleReply.setOrganizationId(ORGANIZATION_ID);
    envCSimpleReply.setEnvironmentId(envC_ERROR.getId());
    envCSimpleReply.setMessage("Problem while serializing environment: " + envC_ERROR.getId());
    BridgeMultiReply environmentsMultiReply = new BridgeMultiReply();
    environmentsMultiReply.setCommandStatus(CommandStatus.SUCCEEDED);
    environmentsMultiReply.setReplies(Arrays.asList(envASimpleReply, envBSimpleReply, envCSimpleReply));
    when(cockpitCommandService.send(any())).thenReturn(environmentsMultiReply);
    // When
    final CockpitReply<List<PromotionTargetEntity>> listCockpitReply = cockpitService.listPromotionTargets(ORGANIZATION_ID);
    // Then
    assertThat(listCockpitReply).isNotNull();
    assertThat(listCockpitReply.getStatus()).isEqualTo(CockpitReplyStatus.SUCCEEDED);
    final List<PromotionTargetEntity> environmentEntities = listCockpitReply.getReply();
    assertThat(environmentEntities).isNotNull();
    assertThat(environmentEntities).isNotEmpty();
    assertThat(environmentEntities.size()).isEqualTo(2);
}
Also used : BridgeMultiReply(io.gravitee.cockpit.api.command.bridge.BridgeMultiReply) BridgeSimpleReply(io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) List(java.util.List) PromotionTargetEntity(io.gravitee.rest.api.model.promotion.PromotionTargetEntity) Test(org.junit.Test)

Example 3 with BridgeMultiReply

use of io.gravitee.cockpit.api.command.bridge.BridgeMultiReply in project gravitee-management-rest-api by gravitee-io.

the class ListEnvironmentOperationHandlerTest method shouldListEnvironments.

@Test
public void shouldListEnvironments() throws JsonProcessingException {
    // Given
    EnvironmentEntity envA = new EnvironmentEntity();
    envA.setId("my-env-A");
    envA.setOrganizationId(ORGANIZATION_ID);
    envA.setName("ENV A");
    EnvironmentEntity envB = new EnvironmentEntity();
    envB.setId("my-env-B");
    envB.setOrganizationId(ORGANIZATION_ID);
    envB.setName("ENV B");
    EnvironmentEntity envC_ERROR = new EnvironmentEntity();
    envC_ERROR.setId("my-env-C");
    envC_ERROR.setOrganizationId(ORGANIZATION_ID);
    envC_ERROR.setName("ENV C");
    when(environmentService.findByOrganization(ORGANIZATION_ID)).thenReturn(Arrays.asList(envA, envB, envC_ERROR));
    when(objectMapper.writeValueAsString(envA)).thenReturn("envA");
    when(objectMapper.writeValueAsString(envB)).thenReturn("envB");
    when(objectMapper.writeValueAsString(envC_ERROR)).thenThrow(new JsonProcessingException("") {
    });
    InstallationEntity installationEntity = new InstallationEntity();
    installationEntity.setId(INSTALLATION_ID);
    when(installationService.get()).thenReturn(installationEntity);
    BridgeCommand command = new BridgeCommand();
    command.setOperation(BridgeOperation.LIST_ENVIRONMENT.name());
    command.setId(COMMAND_ID);
    command.setInstallationId(INSTALLATION_ID);
    command.setOrganizationId(ORGANIZATION_ID);
    command.setEnvironmentId(ENVIRONMENT_ID);
    // When
    TestObserver<BridgeReply> obs = cut.handle(command).test();
    // Then
    obs.awaitTerminalEvent();
    obs.assertValue(reply -> {
        if (reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED) && BridgeMultiReply.class.isInstance(reply)) {
            BridgeMultiReply multiReply = ((BridgeMultiReply) reply);
            if (multiReply.getReplies() != null && multiReply.getReplies().size() == 3) {
                for (BridgeSimpleReply simpleReply : multiReply.getReplies()) {
                    if (simpleReply.getEnvironmentId().equals(envA.getId()) && simpleReply.getCommandStatus() == CommandStatus.ERROR) {
                        return false;
                    }
                    if (simpleReply.getEnvironmentId().equals(envB.getId()) && simpleReply.getCommandStatus() == CommandStatus.ERROR) {
                        return false;
                    }
                    if (simpleReply.getEnvironmentId().equals(envC_ERROR.getId()) && simpleReply.getCommandStatus() == CommandStatus.SUCCEEDED) {
                        return false;
                    }
                }
                return true;
            }
        }
        return false;
    });
}
Also used : BridgeMultiReply(io.gravitee.cockpit.api.command.bridge.BridgeMultiReply) InstallationEntity(io.gravitee.rest.api.model.InstallationEntity) BridgeSimpleReply(io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) BridgeCommand(io.gravitee.cockpit.api.command.bridge.BridgeCommand) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BridgeReply(io.gravitee.cockpit.api.command.bridge.BridgeReply) Test(org.junit.Test)

Example 4 with BridgeMultiReply

use of io.gravitee.cockpit.api.command.bridge.BridgeMultiReply in project gravitee-management-rest-api by gravitee-io.

the class CockpitServiceImpl method listPromotionTargets.

@Override
public CockpitReply<List<PromotionTargetEntity>> listPromotionTargets(String organizationId) {
    final BridgeCommand listEnvironmentCommand = this.bridgeCommandFactory.createListEnvironmentCommand();
    BridgeReply bridgeReply = cockpitCommandService.send(listEnvironmentCommand);
    if (bridgeReply.getCommandStatus() != CommandStatus.SUCCEEDED) {
        logger.warn("Problem while listing promotion targets through cockpit. \n {}", bridgeReply.getMessage());
        return new CockpitReply<>(Collections.emptyList(), CockpitReplyStatus.ERROR);
    }
    final List<PromotionTargetEntity> environmentEntities = ((BridgeMultiReply) bridgeReply).getReplies().stream().filter(simpleReply -> CommandStatus.SUCCEEDED == simpleReply.getCommandStatus()).map(simpleReply -> {
        try {
            final EnvironmentEntity environmentEntity = this.objectMapper.readValue(simpleReply.getPayload(), EnvironmentEntity.class);
            // because cockpit has updated them to handle the case were id is "DEFAULT"
            return new PromotionTargetEntity(environmentEntity, simpleReply.getOrganizationId(), simpleReply.getEnvironmentId(), simpleReply.getInstallationId());
        } catch (JsonProcessingException e) {
            logger.warn("Problem while deserializing environment {} with payload {}", simpleReply.getEnvironmentId(), simpleReply.getPayload());
            return null;
        }
    }).filter(Objects::nonNull).collect(Collectors.toList());
    return new CockpitReply<>(environmentEntities, CockpitReplyStatus.SUCCEEDED);
}
Also used : Logger(org.slf4j.Logger) BridgeCommandFactory(io.gravitee.rest.api.service.cockpit.command.bridge.BridgeCommandFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) LoggerFactory(org.slf4j.LoggerFactory) BridgeMultiReply(io.gravitee.cockpit.api.command.bridge.BridgeMultiReply) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Objects(java.util.Objects) BridgeCommand(io.gravitee.cockpit.api.command.bridge.BridgeCommand) List(java.util.List) Component(org.springframework.stereotype.Component) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) BridgeReply(io.gravitee.cockpit.api.command.bridge.BridgeReply) CockpitCommandService(io.gravitee.rest.api.service.cockpit.command.CockpitCommandService) Collections(java.util.Collections) PromotionTargetEntity(io.gravitee.rest.api.model.promotion.PromotionTargetEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) PromotionTargetEntity(io.gravitee.rest.api.model.promotion.PromotionTargetEntity) BridgeCommand(io.gravitee.cockpit.api.command.bridge.BridgeCommand) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) BridgeReply(io.gravitee.cockpit.api.command.bridge.BridgeReply)

Example 5 with BridgeMultiReply

use of io.gravitee.cockpit.api.command.bridge.BridgeMultiReply in project gravitee-management-rest-api by gravitee-io.

the class CockpitServiceTest method shouldNotListEnvironments.

@Test
public void shouldNotListEnvironments() {
    // Given
    BridgeMultiReply environmentsMultiReply = new BridgeMultiReply();
    environmentsMultiReply.setCommandStatus(CommandStatus.ERROR);
    when(cockpitCommandService.send(any())).thenReturn(environmentsMultiReply);
    // When
    final CockpitReply<List<PromotionTargetEntity>> listCockpitReply = cockpitService.listPromotionTargets(ORGANIZATION_ID);
    // Then
    assertThat(listCockpitReply).isNotNull();
    assertThat(listCockpitReply.getStatus()).isEqualTo(CockpitReplyStatus.ERROR);
    final List<PromotionTargetEntity> environmentEntities = listCockpitReply.getReply();
    assertThat(environmentEntities).isNotNull();
    assertThat(environmentEntities).isEmpty();
}
Also used : BridgeMultiReply(io.gravitee.cockpit.api.command.bridge.BridgeMultiReply) List(java.util.List) PromotionTargetEntity(io.gravitee.rest.api.model.promotion.PromotionTargetEntity) Test(org.junit.Test)

Aggregations

BridgeMultiReply (io.gravitee.cockpit.api.command.bridge.BridgeMultiReply)5 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 BridgeSimpleReply (io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply)3 PromotionTargetEntity (io.gravitee.rest.api.model.promotion.PromotionTargetEntity)3 List (java.util.List)3 Test (org.junit.Test)3 BridgeCommand (io.gravitee.cockpit.api.command.bridge.BridgeCommand)2 BridgeReply (io.gravitee.cockpit.api.command.bridge.BridgeReply)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CommandStatus (io.gravitee.cockpit.api.command.CommandStatus)1 InstallationEntity (io.gravitee.rest.api.model.InstallationEntity)1 PromotionEntity (io.gravitee.rest.api.model.promotion.PromotionEntity)1 CockpitCommandService (io.gravitee.rest.api.service.cockpit.command.CockpitCommandService)1 BridgeCommandFactory (io.gravitee.rest.api.service.cockpit.command.bridge.BridgeCommandFactory)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 Collections (java.util.Collections)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1