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);
}
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);
}
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;
});
}
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);
}
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();
}
Aggregations