Search in sources :

Example 1 with MemberEntity

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());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) User(io.gravitee.rest.api.portal.rest.model.User) Instant(java.time.Instant) MemberEntity(io.gravitee.rest.api.model.MemberEntity) Member(io.gravitee.rest.api.portal.rest.model.Member) Date(java.util.Date) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 2 with MemberEntity

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();
}
Also used : SinglePrimaryOwnerException(io.gravitee.rest.api.service.exceptions.SinglePrimaryOwnerException) MembershipService(io.gravitee.rest.api.service.MembershipService) MemberEntity(io.gravitee.rest.api.model.MemberEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 3 with MemberEntity

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();
}
Also used : MemberEntity(io.gravitee.rest.api.model.MemberEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 4 with MemberEntity

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();
}
Also used : SinglePrimaryOwnerException(io.gravitee.rest.api.service.exceptions.SinglePrimaryOwnerException) MembershipService(io.gravitee.rest.api.service.MembershipService) MemberEntity(io.gravitee.rest.api.model.MemberEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 5 with MemberEntity

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);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.rest.api.model.MemberEntity) Date(java.util.Date) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Aggregations

MemberEntity (io.gravitee.rest.api.model.MemberEntity)8 Test (org.junit.Test)4 RoleEntity (io.gravitee.rest.api.model.RoleEntity)3 UserEntity (io.gravitee.rest.api.model.UserEntity)3 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)3 MembershipService (io.gravitee.rest.api.service.MembershipService)3 Date (java.util.Date)3 Membership (io.gravitee.repository.management.model.Membership)2 SinglePrimaryOwnerException (io.gravitee.rest.api.service.exceptions.SinglePrimaryOwnerException)2 Member (io.gravitee.rest.api.portal.rest.model.Member)1 User (io.gravitee.rest.api.portal.rest.model.User)1 Instant (java.time.Instant)1 Response (javax.ws.rs.core.Response)1 Before (org.junit.Before)1