Search in sources :

Example 21 with EnvironmentEntity

use of io.gravitee.rest.api.model.EnvironmentEntity 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)

Example 22 with EnvironmentEntity

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

the class VirtualHostServiceTest method validate_nullIsNotASubDomain.

@Test
public void validate_nullIsNotASubDomain() {
    VirtualHost vhost = new VirtualHost();
    vhost.setHost(null);
    vhost.setPath("/validVhostPath");
    EnvironmentEntity environmentEntity = new EnvironmentEntity();
    environmentEntity.setDomainRestrictions(Arrays.asList("test.gravitee.io", "other.gravitee.io"));
    when(environmentService.findById(any())).thenReturn(environmentEntity);
    final Collection<VirtualHost> virtualHosts = virtualHostService.sanitizeAndValidate(Collections.singletonList(vhost));
    Assert.assertEquals(1, virtualHosts.size());
    Assert.assertEquals("test.gravitee.io", virtualHosts.iterator().next().getHost());
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) VirtualHost(io.gravitee.definition.model.VirtualHost) Test(org.junit.Test)

Example 23 with EnvironmentEntity

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

the class VirtualHostServiceTest method validate_hostSubDomainOfDomainConstraint.

@Test
public void validate_hostSubDomainOfDomainConstraint() {
    VirtualHost vhost = getValidVirtualHost();
    String domainConstraint = vhost.getHost();
    vhost.setHost("level2.level1." + domainConstraint);
    EnvironmentEntity environmentEntity = new EnvironmentEntity();
    environmentEntity.setDomainRestrictions(Collections.singletonList(domainConstraint));
    when(environmentService.findById(any())).thenReturn(environmentEntity);
    virtualHostService.sanitizeAndValidate(Collections.singletonList(vhost));
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) VirtualHost(io.gravitee.definition.model.VirtualHost) Test(org.junit.Test)

Example 24 with EnvironmentEntity

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

the class VirtualHostServiceTest method validate_notASubDomain.

@Test(expected = InvalidVirtualHostException.class)
public void validate_notASubDomain() {
    VirtualHost vhost = getValidVirtualHost();
    EnvironmentEntity environmentEntity = new EnvironmentEntity();
    environmentEntity.setDomainRestrictions(Arrays.asList("test.gravitee.io", "other.gravitee.io"));
    when(environmentService.findById(any())).thenReturn(environmentEntity);
    virtualHostService.sanitizeAndValidate(Collections.singletonList(vhost));
}
Also used : EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) VirtualHost(io.gravitee.definition.model.VirtualHost) Test(org.junit.Test)

Example 25 with EnvironmentEntity

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

the class PromotionServiceTest method shouldNotProcessPromotionIfCockpitReplyError.

@Test(expected = BridgeOperationException.class)
public void shouldNotProcessPromotionIfCockpitReplyError() throws Exception {
    when(promotionRepository.findById(any())).thenReturn(Optional.of(getAPromotion()));
    when(environmentService.findByCockpitId(any())).thenReturn(new EnvironmentEntity());
    when(permissionService.hasPermission(any(), any(), any())).thenReturn(true);
    Page<Promotion> promotionPage = new Page<>(singletonList(getAPromotion()), 0, 1, 1);
    when(promotionRepository.search(any(), any(), any())).thenReturn(promotionPage);
    when(apiService.exists(any())).thenReturn(true);
    when(apiDuplicatorService.updateWithImportedDefinition(any(), any(), any(), any(), any())).thenReturn(new ApiEntity());
    ApiEntity existingApi = new ApiEntity();
    existingApi.setId("api#existing");
    when(apiService.findById(any())).thenReturn(existingApi);
    CockpitReply<PromotionEntity> cockpitReply = new CockpitReply<>(null, CockpitReplyStatus.ERROR);
    when(cockpitService.processPromotion(any())).thenReturn(cockpitReply);
    promotionService.processPromotion(PROMOTION_ID, true, USER_ID);
    verify(apiDuplicatorService, times(1)).updateWithImportedDefinition(any(), any(), eq(USER_ID), any(), any());
    verify(promotionRepository, times(0)).update(any());
}
Also used : CockpitReply(io.gravitee.rest.api.service.cockpit.services.CockpitReply) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Page(io.gravitee.common.data.domain.Page) Promotion(io.gravitee.repository.management.model.Promotion) Test(org.junit.Test)

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