Search in sources :

Example 1 with GroupEntity

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

the class MembershipService_AddOrUpdateMemberTest method shouldUpdateApiGroupMembership.

@Test
public void shouldUpdateApiGroupMembership() throws Exception {
    UserEntity userEntity = new UserEntity();
    userEntity.setId("my name");
    userEntity.setUsername("my name");
    userEntity.setEmail("me@mail.com");
    Membership membership = new Membership();
    membership.setUserId(userEntity.getUsername());
    membership.setReferenceType(MembershipReferenceType.GROUP);
    membership.setReferenceId(GROUP_ID);
    Map<Integer, String> roles = new HashMap<>();
    roles.put(RoleScope.API.getId(), "USER");
    membership.setRoles(roles);
    Membership newMembership = new Membership();
    newMembership.setUserId(userEntity.getUsername());
    newMembership.setReferenceType(MembershipReferenceType.GROUP);
    newMembership.setReferenceId(GROUP_ID);
    GroupEntity groupEntityMock = mock(GroupEntity.class);
    when(groupEntityMock.getName()).thenReturn("foo");
    RoleEntity role = mock(RoleEntity.class);
    when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.API);
    when(roleService.findById(any(), any())).thenReturn(role);
    when(userService.findById(userEntity.getId())).thenReturn(userEntity);
    when(groupService.findById(GROUP_ID)).thenReturn(groupEntityMock);
    when(membershipRepository.findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID)).thenReturn(of(membership));
    when(membershipRepository.update(any())).thenReturn(newMembership);
    MemberEntity updateMember = membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(userEntity.getUsername(), null), new MembershipService.MembershipRole(RoleScope.API, "OWNER"));
    verify(userService, times(2)).findById(userEntity.getId());
    verify(membershipRepository, times(2)).findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID);
    verify(membershipRepository, never()).create(any());
    verify(membershipRepository, times(1)).update(any());
    verify(emailService, never()).sendAsyncEmailNotification(any());
}
Also used : HashMap(java.util.HashMap) UserEntity(io.gravitee.management.model.UserEntity) RoleEntity(io.gravitee.management.model.RoleEntity) GroupEntity(io.gravitee.management.model.GroupEntity) Membership(io.gravitee.repository.management.model.Membership) MemberEntity(io.gravitee.management.model.MemberEntity) Test(org.junit.Test)

Example 2 with GroupEntity

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

the class MembershipService_AddOrUpdateMemberTest method shouldAddApiGroupMembership.

@Test
public void shouldAddApiGroupMembership() throws Exception {
    UserEntity userEntity = new UserEntity();
    userEntity.setId("my name");
    userEntity.setUsername("my name");
    userEntity.setEmail("me@mail.com");
    RoleEntity role = mock(RoleEntity.class);
    Membership newMembership = new Membership();
    newMembership.setReferenceType(MembershipReferenceType.GROUP);
    newMembership.setRoles(Collections.singletonMap(RoleScope.API.getId(), "OWNER"));
    newMembership.setReferenceId(GROUP_ID);
    newMembership.setUserId(userEntity.getUsername());
    GroupEntity groupEntityMock = mock(GroupEntity.class);
    when(groupEntityMock.getName()).thenReturn("foo");
    when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.API);
    when(roleService.findById(any(), any())).thenReturn(role);
    when(userService.findById(userEntity.getId())).thenReturn(userEntity);
    when(groupService.findById(GROUP_ID)).thenReturn(groupEntityMock);
    when(membershipRepository.findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID)).thenReturn(empty(), of(newMembership));
    when(membershipRepository.create(any())).thenReturn(newMembership);
    membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(userEntity.getId(), null), new MembershipService.MembershipRole(RoleScope.API, "OWNER"));
    verify(userService, times(2)).findById(userEntity.getId());
    verify(membershipRepository, times(2)).findById(userEntity.getUsername(), MembershipReferenceType.GROUP, GROUP_ID);
    verify(membershipRepository, times(1)).create(any());
    verify(membershipRepository, never()).update(any());
    verify(emailService, times(1)).sendAsyncEmailNotification(any());
}
Also used : RoleEntity(io.gravitee.management.model.RoleEntity) GroupEntity(io.gravitee.management.model.GroupEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Aggregations

GroupEntity (io.gravitee.management.model.GroupEntity)2 RoleEntity (io.gravitee.management.model.RoleEntity)2 UserEntity (io.gravitee.management.model.UserEntity)2 Membership (io.gravitee.repository.management.model.Membership)2 Test (org.junit.Test)2 MemberEntity (io.gravitee.management.model.MemberEntity)1 HashMap (java.util.HashMap)1