use of io.gravitee.rest.api.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class MemberMapperTest method testConvert.
@Test
public void testConvert() {
Instant now = Instant.now();
Date nowDate = Date.from(now);
// init
RoleEntity ownerRoleEntity = new RoleEntity();
ownerRoleEntity.setName("OWNER");
memberEntity = new MemberEntity();
memberEntity.setCreatedAt(nowDate);
memberEntity.setDisplayName(MEMBER_DISPLAYNAME);
memberEntity.setEmail(MEMBER_EMAIL);
memberEntity.setId(MEMBER_ID);
memberEntity.setRoles(Arrays.asList(ownerRoleEntity));
memberEntity.setUpdatedAt(nowDate);
UserEntity userEntity = Mockito.mock(UserEntity.class);
when(userEntity.getDisplayName()).thenReturn(MEMBER_DISPLAYNAME);
when(userEntity.getEmail()).thenReturn(MEMBER_EMAIL);
when(userEntity.getId()).thenReturn(MEMBER_ID);
when(uriInfo.getBaseUriBuilder()).thenReturn(UriBuilder.fromPath(""));
when(userService.findById(MEMBER_ID)).thenReturn(userEntity);
when(userMapper.convert(userEntity)).thenCallRealMethod();
when(userMapper.computeUserLinks(anyString(), any())).thenCallRealMethod();
// Test
Member responseMember = memberMapper.convert(memberEntity, uriInfo);
assertNotNull(responseMember);
assertEquals(now.toEpochMilli(), responseMember.getCreatedAt().toInstant().toEpochMilli());
assertNull(responseMember.getId());
assertEquals("OWNER", responseMember.getRole());
assertEquals(now.toEpochMilli(), responseMember.getUpdatedAt().toInstant().toEpochMilli());
User user = responseMember.getUser();
assertNotNull(user);
assertEquals(MEMBER_DISPLAYNAME, user.getDisplayName());
assertEquals(MEMBER_EMAIL, user.getEmail());
assertEquals(MEMBER_ID, user.getId());
assertEquals("environments/DEFAULT/users/" + MEMBER_ID + "/avatar?", user.getLinks().getAvatar());
}
use of io.gravitee.rest.api.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class ApplicationMembersResource method createApplicationMember.
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.APPLICATION_MEMBER, acls = RolePermissionAction.CREATE) })
public Response createApplicationMember(@PathParam("applicationId") String applicationId, @Valid @NotNull(message = "Input must not be null.") MemberInput memberInput) {
// Does application exist ?
applicationService.findById(applicationId);
// There can be only one
if (SystemRole.PRIMARY_OWNER.name().equals(memberInput.getRole())) {
throw new SinglePrimaryOwnerException(RoleScope.APPLICATION);
}
MemberEntity membership = membershipService.addRoleToMemberOnReference(new MembershipService.MembershipReference(MembershipReferenceType.APPLICATION, applicationId), new MembershipService.MembershipMember(memberInput.getUser(), memberInput.getReference(), MembershipMemberType.USER), new MembershipService.MembershipRole(RoleScope.APPLICATION, memberInput.getRole()));
return Response.created(this.getLocationHeader(membership.getId())).entity(memberMapper.convert(membership, uriInfo)).build();
}
use of io.gravitee.rest.api.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class ApplicationMembersResource method getApplicationMemberByApplicationIdAndMemberId.
@GET
@Path("/{memberId}")
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.APPLICATION_MEMBER, acls = RolePermissionAction.READ) })
public Response getApplicationMemberByApplicationIdAndMemberId(@PathParam("applicationId") String applicationId, @PathParam("memberId") String memberId) {
// Does application exist ?
applicationService.findById(applicationId);
// Does user exist ?
userService.findById(memberId);
MemberEntity memberEntity = membershipService.getUserMember(MembershipReferenceType.APPLICATION, applicationId, memberId);
if (memberEntity != null) {
return Response.ok(memberMapper.convert(memberEntity, uriInfo)).build();
}
throw new NotFoundException();
}
use of io.gravitee.rest.api.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class ApplicationMembersResource method updateApplicationMemberByApplicationIdAndMemberId.
@PUT
@Path("/{memberId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.APPLICATION_MEMBER, acls = RolePermissionAction.UPDATE) })
public Response updateApplicationMemberByApplicationIdAndMemberId(@PathParam("applicationId") String applicationId, @PathParam("memberId") String memberId, @Valid @NotNull(message = "Input must not be null.") MemberInput memberInput) {
// Does application exist ?
applicationService.findById(applicationId);
// Does user exist ?
userService.findById(memberId);
if (memberInput.getUser() != null && !memberId.equals(memberInput.getUser())) {
throw new BadRequestException("'memberInput.user' should the same as 'memberId'");
}
// There can be only one
if (SystemRole.PRIMARY_OWNER.name().equals(memberInput.getRole())) {
throw new SinglePrimaryOwnerException(RoleScope.APPLICATION);
}
MemberEntity membership = membershipService.updateRoleToMemberOnReference(new MembershipService.MembershipReference(MembershipReferenceType.APPLICATION, applicationId), new MembershipService.MembershipMember(memberId, memberInput.getReference(), MembershipMemberType.USER), new MembershipService.MembershipRole(RoleScope.APPLICATION, memberInput.getRole()));
return Response.ok(memberMapper.convert(membership, uriInfo)).build();
}
use of io.gravitee.rest.api.model.MemberEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_GetMembersTest method shouldGetMembersWithoutMembership.
@Test
public void shouldGetMembersWithoutMembership() throws Exception {
Membership membership = new Membership();
membership.setReferenceId(API_ID);
membership.setCreatedAt(new Date());
membership.setUpdatedAt(membership.getCreatedAt());
membership.setReferenceType(MembershipReferenceType.API);
membership.setRoleId("API_PRIMARY_OWNER");
membership.setMemberId("user-id");
membership.setMemberType(MembershipMemberType.USER);
UserEntity userEntity = new UserEntity();
userEntity.setId(membership.getMemberId());
userEntity.setFirstname("John");
userEntity.setLastname("Doe");
RoleEntity po = mock(RoleEntity.class);
po.setName(SystemRole.PRIMARY_OWNER.name());
List<String> memberIds = Collections.singletonList(membership.getMemberId());
Set<UserEntity> userEntities = Collections.singleton(userEntity);
when(roleService.findById("API_PRIMARY_OWNER")).thenReturn(po);
when(membershipRepository.findByReferencesAndRoleId(MembershipReferenceType.API, Collections.singletonList(API_ID), null)).thenReturn(Collections.singleton(membership));
when(userService.findByIds(memberIds, false)).thenReturn(userEntities);
Set<MemberEntity> members = membershipService.getMembersByReferenceAndRole(io.gravitee.rest.api.model.MembershipReferenceType.API, API_ID, null);
Assert.assertNotNull(members);
Assert.assertFalse("members must not be empty", members.isEmpty());
verify(membershipRepository, times(1)).findByReferencesAndRoleId(MembershipReferenceType.API, Collections.singletonList(API_ID), null);
verify(userService, times(1)).findByIds(memberIds, false);
}
Aggregations