Search in sources :

Example 6 with PromotionEntity

use of io.gravitee.rest.api.model.promotion.PromotionEntity in project gravitee-management-rest-api by gravitee-io.

the class PromotionTasksServiceImplTest method shouldGetPromotionTasks_withApiUpdate_andApiCreation.

@Test
public void shouldGetPromotionTasks_withApiUpdate_andApiCreation() throws JsonProcessingException {
    PromotionEntity promotionEntity1 = getAPromotionEntity();
    PromotionEntity promotionEntity2 = getAPromotionEntity();
    when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.TO_BE_VALIDATED), any(), any())).thenReturn(new Page<>(List.of(promotionEntity1, promotionEntity2), 0, 0, 0));
    when(environmentService.findByOrganization(any())).thenReturn(singletonList(getAnEnvironmentEntity()));
    PromotionEntity previousPromotionEntity = getAPromotionEntity();
    previousPromotionEntity.setTargetApiId("api#target");
    when(promotionService.search(argThat(query -> query != null && query.getStatuses().get(0) == PromotionEntityStatus.ACCEPTED), any(), any())).thenReturn(new Page<>(singletonList(previousPromotionEntity), 0, 0, 0));
    when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", UPDATE)).thenReturn(true);
    when(permissionService.hasPermission(RolePermission.ENVIRONMENT_API, "env#1", CREATE)).thenReturn(false);
    when(objectMapper.readValue(promotionEntity1.getApiDefinition(), ApiEntity.class)).thenReturn(getAnApiEntity());
    when(objectMapper.readValue(promotionEntity2.getApiDefinition(), ApiEntity.class)).thenReturn(getAnApiEntity());
    when(apiService.exists("api#target")).thenReturn(true);
    final List<TaskEntity> result = cut.getPromotionTasks("org#1");
    assertThat(result).hasSize(2);
}
Also used : java.util(java.util) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Page(io.gravitee.common.data.domain.Page) RunWith(org.junit.runner.RunWith) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) PromotionEntityStatus(io.gravitee.rest.api.model.promotion.PromotionEntityStatus) ApiService(io.gravitee.rest.api.service.ApiService) UPDATE(io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE) Collections.singletonList(java.util.Collections.singletonList) PromotionEntityAuthor(io.gravitee.rest.api.model.promotion.PromotionEntityAuthor) PermissionService(io.gravitee.rest.api.service.PermissionService) Before(org.junit.Before) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TaskEntity(io.gravitee.rest.api.model.TaskEntity) Collections.emptyList(java.util.Collections.emptyList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EnvironmentService(io.gravitee.rest.api.service.EnvironmentService) PromotionService(io.gravitee.rest.api.service.promotion.PromotionService) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) CREATE(io.gravitee.rest.api.model.permissions.RolePermissionAction.CREATE) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) PromotionTasksService(io.gravitee.rest.api.service.promotion.PromotionTasksService) RolePermission(io.gravitee.rest.api.model.permissions.RolePermission) TaskEntity(io.gravitee.rest.api.model.TaskEntity) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) Test(org.junit.Test)

Example 7 with PromotionEntity

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

use of io.gravitee.rest.api.model.promotion.PromotionEntity in project gravitee-management-rest-api by gravitee-io.

the class ProcessPromotionOperationHandler 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 for environment {}", bridgeCommand.getEnvironmentId());
        reply.setCommandStatus(CommandStatus.ERROR);
        reply.setMessage("Problem while deserializing promotion for environment [" + bridgeCommand.getEnvironmentId() + "]");
        return Single.just(reply);
    }
    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 for environment {}", promotion.getId());
        reply.setCommandStatus(CommandStatus.ERROR);
        reply.setMessage("Problem while serializing promotion 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 9 with PromotionEntity

use of io.gravitee.rest.api.model.promotion.PromotionEntity in project gravitee-management-rest-api by gravitee-io.

the class PromotionsResource method searchPromotions.

@POST
@Path("_search")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Search for Promotion")
@ApiResponses({ @ApiResponse(code = 200, message = "List promotions matching request parameters", response = PromotionEntity.class, responseContainer = "List"), @ApiResponse(code = 500, message = "Internal server error") })
public Response searchPromotions(@ApiParam(name = "statuses", required = true) @NotNull @QueryParam("statuses") List<String> statuses, @ApiParam(name = "apiId", required = true) @NotNull @QueryParam("apiId") String apiId) {
    PromotionQuery promotionQuery = new PromotionQuery();
    promotionQuery.setStatuses(statuses.stream().map(PromotionEntityStatus::valueOf).collect(toList()));
    promotionQuery.setApiId(apiId);
    List<PromotionEntity> promotions = promotionService.search(promotionQuery, new SortableImpl("created_at", false), null).getContent();
    return Response.ok().entity(promotions).build();
}
Also used : SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) PromotionEntityStatus(io.gravitee.rest.api.model.promotion.PromotionEntityStatus) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) PromotionQuery(io.gravitee.rest.api.model.promotion.PromotionQuery) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 10 with PromotionEntity

use of io.gravitee.rest.api.model.promotion.PromotionEntity in project gravitee-management-rest-api by gravitee-io.

the class PromotionTasksServiceImpl method getPromotionTasksForEnvironments.

@NotNull
private List<TaskEntity> getPromotionTasksForEnvironments(List<EnvironmentEntity> environments, boolean selectUpdatePromotion) {
    if (environments.isEmpty()) {
        return emptyList();
    }
    List<String> envCockpitIds = environments.stream().map(EnvironmentEntity::getCockpitId).filter(Objects::nonNull).collect(toList());
    final PromotionQuery promotionQuery = new PromotionQuery();
    promotionQuery.setStatuses(Collections.singletonList(PromotionEntityStatus.TO_BE_VALIDATED));
    promotionQuery.setTargetEnvCockpitIds(envCockpitIds);
    final Page<PromotionEntity> promotionsPage = promotionService.search(promotionQuery, new SortableImpl("created_at", false), null);
    final PromotionQuery previousPromotionsQuery = new PromotionQuery();
    previousPromotionsQuery.setStatuses(Collections.singletonList(PromotionEntityStatus.ACCEPTED));
    previousPromotionsQuery.setTargetEnvCockpitIds(envCockpitIds);
    previousPromotionsQuery.setTargetApiExists(true);
    List<PromotionEntity> previousPromotions = promotionService.search(previousPromotionsQuery, new SortableImpl("created_at", false), null).getContent();
    final Map<String, List<String>> promotionByApiWithTargetApiId = previousPromotions.stream().collect(groupingBy(PromotionEntity::getApiId, Collectors.mapping(PromotionEntity::getTargetApiId, toList())));
    return promotionsPage.getContent().stream().map(promotionEntity -> {
        Optional<String> foundTargetApiId = promotionByApiWithTargetApiId.getOrDefault(promotionEntity.getApiId(), emptyList()).stream().filter(StringUtils::hasText).findFirst();
        boolean isUpdate = foundTargetApiId.isPresent() && apiService.exists(foundTargetApiId.get());
        return convert(promotionEntity, isUpdate, foundTargetApiId);
    }).filter(taskEntity -> ((Boolean) ((Map<String, Object>) taskEntity.getData()).getOrDefault("isApiUpdate", false) == selectUpdatePromotion)).collect(toList());
}
Also used : java.util(java.util) PromotionQuery(io.gravitee.rest.api.model.promotion.PromotionQuery) Page(io.gravitee.common.data.domain.Page) LoggerFactory(org.slf4j.LoggerFactory) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) PromotionEntityStatus(io.gravitee.rest.api.model.promotion.PromotionEntityStatus) ApiService(io.gravitee.rest.api.service.ApiService) UPDATE(io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) PermissionService(io.gravitee.rest.api.service.PermissionService) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) TaskType(io.gravitee.rest.api.model.TaskType) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) TaskEntity(io.gravitee.rest.api.model.TaskEntity) Logger(org.slf4j.Logger) ENVIRONMENT_API(io.gravitee.rest.api.model.permissions.RolePermission.ENVIRONMENT_API) Collections.emptyList(java.util.Collections.emptyList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) EnvironmentService(io.gravitee.rest.api.service.EnvironmentService) PromotionService(io.gravitee.rest.api.service.promotion.PromotionService) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Collectors(java.util.stream.Collectors) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Component(org.springframework.stereotype.Component) CREATE(io.gravitee.rest.api.model.permissions.RolePermissionAction.CREATE) NotNull(org.jetbrains.annotations.NotNull) PromotionTasksService(io.gravitee.rest.api.service.promotion.PromotionTasksService) StringUtils(org.springframework.util.StringUtils) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) Collections.emptyList(java.util.Collections.emptyList) PromotionEntity(io.gravitee.rest.api.model.promotion.PromotionEntity) PromotionQuery(io.gravitee.rest.api.model.promotion.PromotionQuery) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PromotionEntity (io.gravitee.rest.api.model.promotion.PromotionEntity)18 Test (org.junit.Test)10 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 BridgeSimpleReply (io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply)6 PromotionEntityAuthor (io.gravitee.rest.api.model.promotion.PromotionEntityAuthor)6 PromotionEntityStatus (io.gravitee.rest.api.model.promotion.PromotionEntityStatus)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 Page (io.gravitee.common.data.domain.Page)5 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)5 TaskEntity (io.gravitee.rest.api.model.TaskEntity)5 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)5 CREATE (io.gravitee.rest.api.model.permissions.RolePermissionAction.CREATE)5 UPDATE (io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE)5 ApiService (io.gravitee.rest.api.service.ApiService)5 EnvironmentService (io.gravitee.rest.api.service.EnvironmentService)5 PermissionService (io.gravitee.rest.api.service.PermissionService)5 PromotionService (io.gravitee.rest.api.service.promotion.PromotionService)5 PromotionTasksService (io.gravitee.rest.api.service.promotion.PromotionTasksService)5 java.util (java.util)5 Collections.emptyList (java.util.Collections.emptyList)5