use of io.gravitee.management.model.UserEntity 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());
}
Aggregations