Search in sources :

Example 26 with EnvironmentEntity

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

the class PromotionServiceTest method shouldPromote.

@Test
public void shouldPromote() throws TechnicalException {
    when(userService.findById(any())).thenReturn(getAUserEntity());
    EnvironmentEntity environmentEntity = new EnvironmentEntity();
    environmentEntity.setCockpitId("env#cockpit-1");
    environmentEntity.setName("Env 1");
    when(environmentService.findById(any())).thenReturn(environmentEntity);
    Page<Promotion> promotionPage = new Page<>(emptyList(), 0, 0, 0);
    when(promotionRepository.search(any(), any(), any())).thenReturn(promotionPage);
    when(promotionRepository.create(any())).thenReturn(getAPromotion());
    when(cockpitService.requestPromotion(any())).thenReturn(new CockpitReply<>(getAPromotionEntity(), CockpitReplyStatus.SUCCEEDED));
    when(promotionRepository.update(any())).thenReturn(mock(Promotion.class));
    final PromotionEntity promotionEntity = promotionService.promote("api#1", getAPromotionRequestEntity(), "user#1");
    assertThat(promotionEntity).isNotNull();
    verify(auditService).createApiAuditLog(eq("api#1"), any(), eq(PROMOTION_CREATED), any(), isNull(), any());
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Page(io.gravitee.common.data.domain.Page) Promotion(io.gravitee.repository.management.model.Promotion) Test(org.junit.Test)

Example 27 with EnvironmentEntity

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

the class PromotionServiceTest method shouldNotProcessPromotionIfNoPermissionForTargetEnvironment.

@Test(expected = ForbiddenAccessException.class)
public void shouldNotProcessPromotionIfNoPermissionForTargetEnvironment() throws Exception {
    when(promotionRepository.findById(any())).thenReturn(Optional.of(getAPromotion()));
    when(environmentService.findByCockpitId(any())).thenReturn(new EnvironmentEntity());
    when(apiService.exists(any())).thenReturn(true);
    Page<Promotion> promotionPage = new Page<>(singletonList(getAPromotion()), 0, 1, 1);
    when(promotionRepository.search(any(), any(), any())).thenReturn(promotionPage);
    when(permissionService.hasPermission(any(), any(), any())).thenReturn(false);
    promotionService.processPromotion(PROMOTION_ID, true, USER_ID);
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Page(io.gravitee.common.data.domain.Page) Promotion(io.gravitee.repository.management.model.Promotion) Test(org.junit.Test)

Example 28 with EnvironmentEntity

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

the class PromotionServiceTest method shouldNotPromoteIfThereIsAlreadyAnInProgressPromotionForTheSameEnv.

@Test(expected = PromotionAlreadyInProgressException.class)
public void shouldNotPromoteIfThereIsAlreadyAnInProgressPromotionForTheSameEnv() throws TechnicalException {
    when(userService.findById(any())).thenReturn(getAUserEntity());
    EnvironmentEntity environmentEntity = new EnvironmentEntity();
    environmentEntity.setCockpitId("env#cockpit-1");
    environmentEntity.setName("Env 1");
    when(environmentService.findById(any())).thenReturn(environmentEntity);
    String targetEnvCockpitId = "env#cockpit-target";
    Promotion promotion = getAPromotion();
    promotion.setApiId("api#1");
    promotion.setStatus(PromotionStatus.TO_BE_VALIDATED);
    promotion.setTargetEnvCockpitId(targetEnvCockpitId);
    Promotion promotion2 = getAPromotion();
    promotion2.setApiId("api#1");
    promotion2.setStatus(PromotionStatus.TO_BE_VALIDATED);
    promotion2.setTargetEnvCockpitId("env#another-env");
    Page<Promotion> promotionPage = new Page<>(List.of(promotion, promotion2), 0, 2, 2);
    when(promotionRepository.search(any(), any(), any())).thenReturn(promotionPage);
    PromotionRequestEntity promotionRequestEntity = getAPromotionRequestEntity();
    promotionRequestEntity.setTargetEnvCockpitId(targetEnvCockpitId);
    promotionService.promote("api#1", promotionRequestEntity, "user#1");
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Page(io.gravitee.common.data.domain.Page) Promotion(io.gravitee.repository.management.model.Promotion) Test(org.junit.Test)

Example 29 with EnvironmentEntity

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

the class PromotionServiceImpl method processPromotion.

@Override
public PromotionEntity processPromotion(String promotion, boolean accepted, String user) {
    try {
        final Promotion existing = promotionRepository.findById(promotion).orElseThrow(() -> new PromotionNotFoundException(promotion));
        EnvironmentEntity environment = environmentService.findByCockpitId(existing.getTargetEnvCockpitId());
        existing.setStatus(accepted ? PromotionStatus.ACCEPTED : PromotionStatus.REJECTED);
        final PromotionQuery promotionQuery = new PromotionQuery();
        promotionQuery.setStatuses(Collections.singletonList(PromotionEntityStatus.ACCEPTED));
        promotionQuery.setTargetEnvCockpitIds(singletonList(existing.getTargetEnvCockpitId()));
        promotionQuery.setTargetApiExists(true);
        promotionQuery.setApiId(existing.getApiId());
        List<PromotionEntity> previousPromotions = search(promotionQuery, new SortableImpl("created_at", false), null).getContent();
        // Should create a new API if there is no previous promotion for this API or if the API existed once (after a promotion) but has been deleted since
        boolean shouldCreate = CollectionUtils.isEmpty(previousPromotions) || !apiService.exists(previousPromotions.get(0).getTargetApiId());
        if (PromotionStatus.ACCEPTED.equals(existing.getStatus())) {
            ApiEntity promoted = null;
            // FIXME: All the methods should take then env id as input instead of relying on GraviteeContext.getCurrentEnv
            GraviteeContext.setCurrentEnvironment(environment.getId());
            if (shouldCreate) {
                if (!permissionService.hasPermission(ENVIRONMENT_API, environment.getId(), CREATE)) {
                    throw new ForbiddenAccessException();
                }
                promoted = apiDuplicatorService.createWithImportedDefinition(existing.getApiDefinition(), user, GraviteeContext.getCurrentOrganization(), GraviteeContext.getCurrentEnvironment());
            } else {
                if (!permissionService.hasPermission(ENVIRONMENT_API, environment.getId(), UPDATE)) {
                    throw new ForbiddenAccessException();
                }
                PromotionEntity lastAcceptedPromotion = previousPromotions.get(0);
                final ApiEntity existingApi = apiService.findById(lastAcceptedPromotion.getTargetApiId());
                promoted = apiDuplicatorService.updateWithImportedDefinition(existingApi.getId(), existing.getApiDefinition(), user, GraviteeContext.getCurrentOrganization(), GraviteeContext.getCurrentEnvironment());
            }
            existing.setTargetApiId(promoted.getId());
        }
        final PromotionEntity promotionEntity = convert(existing);
        final CockpitReply<PromotionEntity> cockpitReply = cockpitService.processPromotion(promotionEntity);
        if (cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED) {
            throw new BridgeOperationException(BridgeOperation.PROMOTE_API);
        }
        final Promotion updated = promotionRepository.update(existing);
        return convert(updated);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to process promotion", ex);
        throw new TechnicalManagementException("An error occurs while trying to process promotion", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Promotion(io.gravitee.repository.management.model.Promotion) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl)

Example 30 with EnvironmentEntity

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

the class PromotionServiceImpl method promote.

@Override
public PromotionEntity promote(String apiId, PromotionRequestEntity promotionRequest, String userId) {
    // TODO: do we have to use filteredFields like for duplicate (i think no need members and groups)
    // FIXME: can we get the version from target environment
    String apiDefinition = apiDuplicatorService.exportAsJson(apiId, ApiSerializer.Version.DEFAULT.getVersion(), "id", "members", "groups");
    EnvironmentEntity currentEnvironmentEntity = environmentService.findById(GraviteeContext.getCurrentEnvironment());
    UserEntity author = userService.findById(userId);
    PromotionQuery promotionQuery = new PromotionQuery();
    promotionQuery.setStatuses(List.of(PromotionEntityStatus.CREATED, PromotionEntityStatus.TO_BE_VALIDATED));
    promotionQuery.setApiId(apiId);
    List<PromotionEntity> inProgressPromotions = search(promotionQuery, null, null).getContent().stream().filter(promotionEntity -> promotionEntity.getTargetEnvCockpitId().equals(promotionRequest.getTargetEnvCockpitId())).collect(Collectors.toList());
    if (!inProgressPromotions.isEmpty()) {
        throw new PromotionAlreadyInProgressException(inProgressPromotions.get(0).getId());
    }
    Promotion promotionToSave = convert(apiId, apiDefinition, currentEnvironmentEntity, promotionRequest, author);
    promotionToSave.setId(UuidString.generateRandom());
    Promotion createdPromotion = null;
    try {
        createdPromotion = promotionRepository.create(promotionToSave);
        auditService.createApiAuditLog(createdPromotion.getApiId(), emptyMap(), PROMOTION_CREATED, createdPromotion.getCreatedAt(), null, createdPromotion);
    } catch (TechnicalException exception) {
        throw new TechnicalManagementException(String.format("An error occurs while trying to create a promotion request for API %s", apiId), exception);
    }
    PromotionEntity promotionEntity = convert(createdPromotion);
    CockpitReply<PromotionEntity> cockpitReply = cockpitService.requestPromotion(promotionEntity);
    promotionEntity.setStatus(cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED ? PromotionEntityStatus.ERROR : PromotionEntityStatus.TO_BE_VALIDATED);
    try {
        promotionRepository.update(convert(promotionEntity));
    } catch (TechnicalException exception) {
        throw new TechnicalManagementException(String.format("An error occurs while trying to update promotion %s", promotionEntity.getId()), exception);
    }
    if (cockpitReply.getStatus() != CockpitReplyStatus.SUCCEEDED) {
        throw new BridgeOperationException(BridgeOperation.PROMOTE_API);
    }
    return promotionEntity;
}
Also used : PROMOTION_CREATED(io.gravitee.repository.management.model.Promotion.AuditEvent.PROMOTION_CREATED) io.gravitee.rest.api.service(io.gravitee.rest.api.service) CockpitService(io.gravitee.rest.api.service.cockpit.services.CockpitService) Pageable(io.gravitee.rest.api.model.common.Pageable) Date(java.util.Date) Page(io.gravitee.common.data.domain.Page) Sortable(io.gravitee.rest.api.model.common.Sortable) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Order(io.gravitee.repository.management.api.search.Order) CockpitReplyStatus(io.gravitee.rest.api.service.cockpit.services.CockpitReplyStatus) LoggerFactory(org.slf4j.LoggerFactory) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) UPDATE(io.gravitee.rest.api.model.permissions.RolePermissionAction.UPDATE) Collections.singletonList(java.util.Collections.singletonList) SortableImpl(io.gravitee.rest.api.model.common.SortableImpl) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) AbstractService(io.gravitee.rest.api.service.impl.AbstractService) PromotionCriteria(io.gravitee.repository.management.api.search.PromotionCriteria) Collections.emptyMap(java.util.Collections.emptyMap) UuidString(io.gravitee.rest.api.service.common.UuidString) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Logger(org.slf4j.Logger) ENVIRONMENT_API(io.gravitee.rest.api.model.permissions.RolePermission.ENVIRONMENT_API) Promotion(io.gravitee.repository.management.model.Promotion) BridgeOperation(io.gravitee.rest.api.service.cockpit.command.bridge.operation.BridgeOperation) CockpitReply(io.gravitee.rest.api.service.cockpit.services.CockpitReply) PromotionService(io.gravitee.rest.api.service.promotion.PromotionService) Collectors(java.util.stream.Collectors) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) io.gravitee.rest.api.model.promotion(io.gravitee.rest.api.model.promotion) List(java.util.List) Component(org.springframework.stereotype.Component) PromotionStatus(io.gravitee.repository.management.model.PromotionStatus) SortableBuilder(io.gravitee.repository.management.api.search.builder.SortableBuilder) io.gravitee.rest.api.service.exceptions(io.gravitee.rest.api.service.exceptions) CollectionUtils(org.springframework.util.CollectionUtils) CREATE(io.gravitee.rest.api.model.permissions.RolePermissionAction.CREATE) PromotionRepository(io.gravitee.repository.management.api.PromotionRepository) Optional(java.util.Optional) PromotionAuthor(io.gravitee.repository.management.model.PromotionAuthor) ApiSerializer(io.gravitee.rest.api.service.jackson.ser.api.ApiSerializer) Collections(java.util.Collections) UserEntity(io.gravitee.rest.api.model.UserEntity) StringUtils(org.springframework.util.StringUtils) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) Promotion(io.gravitee.repository.management.model.Promotion) UserEntity(io.gravitee.rest.api.model.UserEntity)

Aggregations

EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)33 Test (org.junit.Test)21 Page (io.gravitee.common.data.domain.Page)10 Promotion (io.gravitee.repository.management.model.Promotion)10 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)8 VirtualHost (io.gravitee.definition.model.VirtualHost)6 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)6 CockpitReply (io.gravitee.rest.api.service.cockpit.services.CockpitReply)6 List (java.util.List)6 Collections (java.util.Collections)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 BridgeMultiReply (io.gravitee.cockpit.api.command.bridge.BridgeMultiReply)4 UpdateEnvironmentEntity (io.gravitee.rest.api.model.UpdateEnvironmentEntity)4 RunWith (org.junit.runner.RunWith)4 Mock (org.mockito.Mock)4 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)4 CommandStatus (io.gravitee.cockpit.api.command.CommandStatus)3 BridgeSimpleReply (io.gravitee.cockpit.api.command.bridge.BridgeSimpleReply)3 SortableImpl (io.gravitee.rest.api.model.common.SortableImpl)3 Arrays (java.util.Arrays)3