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