Search in sources :

Example 1 with BridgeSimpleReply

use of io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply 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 BridgeSimpleReply

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

the class PromoteApiOperationHandler method handle.

@Override
public Single<BridgeReply> handle(BridgeCommand bridgeCommand) {
    BridgeSimpleReply reply = new BridgeSimpleReply();
    reply.setCommandId(bridgeCommand.getId());
    final PromotionEntity promotionEntity;
    try {
        promotionEntity = objectMapper.readValue(bridgeCommand.getPayload().getContent(), PromotionEntity.class);
    } catch (JsonProcessingException e) {
        logger.warn("Problem while deserializing promotion request for environment {}", bridgeCommand.getEnvironmentId());
        reply.setCommandStatus(CommandStatus.ERROR);
        reply.setMessage("Problem while deserializing promotion request for environment [" + bridgeCommand.getEnvironmentId() + "]");
        return Single.just(reply);
    }
    promotionEntity.setStatus(PromotionEntityStatus.TO_BE_VALIDATED);
    PromotionEntity promotion = promotionService.createOrUpdate(promotionEntity);
    reply.setCommandStatus(CommandStatus.SUCCEEDED);
    reply.setOrganizationId(bridgeCommand.getOrganizationId());
    reply.setEnvironmentId(bridgeCommand.getTarget().getEnvironmentId());
    reply.setInstallationId(installationService.get().getId());
    try {
        reply.setPayload(objectMapper.writeValueAsString(promotion));
    } catch (JsonProcessingException e) {
        logger.warn("Problem while serializing promotion request for environment {}", promotion.getId());
        reply.setCommandStatus(CommandStatus.ERROR);
        reply.setMessage("Problem while serializing promotion request for environment [" + bridgeCommand.getEnvironmentId() + "]");
        return Single.just(reply);
    }
    return Single.just(reply);
}
Also used : BridgeSimpleReply(io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 3 with BridgeSimpleReply

use of io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply 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 4 with BridgeSimpleReply

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

the class CockpitServiceTest method shouldNotProcessPromotionCommandError.

@Test
public void shouldNotProcessPromotionCommandError() {
    BridgeReply reply = new BridgeSimpleReply();
    reply.setCommandStatus(CommandStatus.ERROR);
    when(cockpitCommandService.send(any())).thenReturn(reply);
    final CockpitReply<PromotionEntity> result = cockpitService.processPromotion(new PromotionEntity());
    assertThat(result).isNotNull();
    assertThat(result.getStatus()).isEqualTo(CockpitReplyStatus.ERROR);
    final PromotionEntity entity = result.getReply();
    assertThat(entity).isNull();
}
Also used : BridgeSimpleReply(io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) BridgeReply(io.gravitee.cockpit.api.command.bridge.BridgeReply) Test(org.junit.Test)

Example 5 with BridgeSimpleReply

use of io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply 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)

Aggregations

BridgeSimpleReply (io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply)9 PromotionEntity (io.gravitee.rest.api.model.promotion.PromotionEntity)6 Test (org.junit.Test)6 BridgeReply (io.gravitee.cockpit.api.command.bridge.BridgeReply)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 BridgeCommand (io.gravitee.cockpit.api.command.bridge.BridgeCommand)3 BridgeMultiReply (io.gravitee.cockpit.api.command.bridge.BridgeMultiReply)3 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)3 InstallationEntity (io.gravitee.rest.api.model.InstallationEntity)3 BridgePayload (io.gravitee.cockpit.api.command.bridge.BridgePayload)2 BridgeTarget (io.gravitee.cockpit.api.command.bridge.BridgeTarget)2 PromotionTargetEntity (io.gravitee.rest.api.model.promotion.PromotionTargetEntity)1 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 List (java.util.List)1