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