Search in sources :

Example 51 with UserEntity

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

Example 52 with UserEntity

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

the class MembershipService_GetMemberPermissionsTest method shouldGetPermissionsIfMemberOfApiGroup.

@Test
public void shouldGetPermissionsIfMemberOfApiGroup() throws Exception {
    ApiEntity api = mock(ApiEntity.class);
    doReturn(API_ID).when(api).getId();
    doReturn(Collections.singleton(GROUP_ID1)).when(api).getGroups();
    doReturn(api).when(apiService).findById(API_ID);
    doReturn(Collections.emptySet()).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    Membership membership = mock(Membership.class);
    doReturn("API_" + ROLENAME).when(membership).getRoleId();
    doReturn(new HashSet<>(asList(membership))).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
    UserEntity userEntity = mock(UserEntity.class);
    doReturn(userEntity).when(userService).findById(USERNAME);
    RoleEntity roleEntity = mock(RoleEntity.class);
    Map<String, char[]> rolePerms = new HashMap<>();
    rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
    doReturn(rolePerms).when(roleEntity).getPermissions();
    doReturn(RoleScope.API).when(roleEntity).getScope();
    doReturn(roleEntity).when(roleService).findById("API_" + ROLENAME);
    Map<String, char[]> permissions = membershipService.getUserMemberPermissions(api, USERNAME);
    assertNotNull(permissions);
    assertPermissions(rolePerms, permissions);
    verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
    verify(apiService, times(1)).findById(API_ID);
    verify(userService, times(1)).findById(USERNAME);
    verify(roleService, times(1)).findById("API_" + ROLENAME);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 53 with UserEntity

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

the class ApplicationAlertServiceTest method prepareForCreation.

@NotNull
private void prepareForCreation(NewAlertTriggerEntity newAlert) {
    // recipients
    when(membershipService.getMembershipsByReference(MembershipReferenceType.APPLICATION, APPLICATION_ID)).thenReturn(Collections.emptySet());
    UserEntity user1 = new UserEntity();
    user1.setEmail("user1@mail.gio");
    UserEntity user2 = new UserEntity();
    user2.setEmail("user2@mail.gio");
    Set<UserEntity> users = new HashSet<>();
    users.add(user1);
    users.add(user2);
    when(userService.findByIds(any())).thenReturn(users);
    // body
    List<Condition> conditions = new ArrayList<>();
    conditions.add(RateCondition.of(ThresholdRangeCondition.between("response.status", 200D, 299D).build()).duration(10L, TimeUnit.MINUTES).greaterThan(5D).build());
    newAlert.setConditions(conditions);
}
Also used : RateCondition(io.gravitee.alert.api.condition.RateCondition) Condition(io.gravitee.alert.api.condition.Condition) ThresholdRangeCondition(io.gravitee.alert.api.condition.ThresholdRangeCondition) ArrayList(java.util.ArrayList) UserEntity(io.gravitee.rest.api.model.UserEntity) HashSet(java.util.HashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 54 with UserEntity

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

the class ApiService_FindPrimaryOwnerTest method defineUser.

private void defineUser(String username) {
    UserEntity userEntity = new UserEntity();
    userEntity.setId(username);
    when(userService.findById(username)).thenReturn(userEntity);
}
Also used : UserEntity(io.gravitee.rest.api.model.UserEntity)

Example 55 with UserEntity

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

the class MetadataServiceTest method checkMetadataFormat_badEmailFormat_EL.

@Test(expected = TechnicalManagementException.class)
public void checkMetadataFormat_badEmailFormat_EL() throws TemplateException {
    when(this.notificationTemplateService.resolveInlineTemplateWithParam(anyString(), any(Reader.class), any())).thenReturn("test");
    UserEntity userEntity = new UserEntity();
    userEntity.setEmail("test");
    PrimaryOwnerEntity primaryOwnerEntity = new PrimaryOwnerEntity(userEntity);
    ApiEntity apiEntity = new ApiEntity();
    apiEntity.setPrimaryOwner(primaryOwnerEntity);
    metadataService.checkMetadataFormat(MetadataFormat.MAIL, "${api.primaryOwner.email}", API, apiEntity);
}
Also used : PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) Reader(java.io.Reader) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Aggregations

UserEntity (io.gravitee.rest.api.model.UserEntity)57 Test (org.junit.Test)36 User (io.gravitee.rest.api.portal.rest.model.User)14 Response (javax.ws.rs.core.Response)14 RoleEntity (io.gravitee.rest.api.model.RoleEntity)13 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)12 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)12 Date (java.util.Date)10 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)9 UserService (io.gravitee.rest.api.service.UserService)9 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)8 List (java.util.List)8 Instant (java.time.Instant)7 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)6 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)6 MembershipService (io.gravitee.rest.api.service.MembershipService)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)5 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)5