Search in sources :

Example 1 with PromotionTargetEntity

use of io.gravitee.rest.api.model.promotion.PromotionTargetEntity 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 2 with PromotionTargetEntity

use of io.gravitee.rest.api.model.promotion.PromotionTargetEntity 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 3 with PromotionTargetEntity

use of io.gravitee.rest.api.model.promotion.PromotionTargetEntity 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)3 PromotionTargetEntity (io.gravitee.rest.api.model.promotion.PromotionTargetEntity)3 List (java.util.List)3 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CommandStatus (io.gravitee.cockpit.api.command.CommandStatus)1 BridgeCommand (io.gravitee.cockpit.api.command.bridge.BridgeCommand)1 BridgeReply (io.gravitee.cockpit.api.command.bridge.BridgeReply)1 BridgeSimpleReply (io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply)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 Collections (java.util.Collections)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 Component (org.springframework.stereotype.Component)1