@Test void addUserToNonExistingRole() { Attribute roleNameAttr = mock(Attribute.class); when(roleNameAttr.getName()).thenReturn("name"); when(roleMetadata.getAttribute(RoleMetadata.NAME)).thenReturn(roleNameAttr); when(roleMetadata.getId()).thenReturn("sys_sec_Role"); String username = "henk"; User user = mock(User.class); when(userService.getUser(username)).thenReturn(user); String rolename = "GCC_DELETER"; @SuppressWarnings("unchecked") Query<Role> query = mock(Query.class, RETURNS_SELF); when(dataService.query(RoleMetadata.ROLE, Role.class)).thenReturn(query); when(query.eq(RoleMetadata.NAME, rolename).findOne()).thenReturn(null); Exception exception = assertThrows( UnknownEntityException.class, () -> roleMembershipService.addUserToRole(username, rolename)); assertThat(exception.getMessage()) .containsPattern("type:sys_sec_Role id:GCC_DELETER attribute:name"); }
@RunAsSystem @Override public void addUserToRole(String username, String roleName) { User user = userService.getUser(username); if (user == null) { throw new UnknownEntityException(userMetadata, userMetadata.getAttribute(USERNAME), username); } Role role = dataService.query(RoleMetadata.ROLE, Role.class).eq(NAME, roleName).findOne(); if (role == null) { throw new UnknownEntityException(roleMetadata, roleMetadata.getAttribute(NAME), roleName); } this.addUserToRole(user, role); }
RoleMembershipServiceImpl implements RoleMembershipService { @RunAsSystem @Override public void addUserToRole(String username, String roleName) { User user = userService.getUser(username); if (user == null) { throw new UnknownEntityException(userMetadata, userMetadata.getAttribute(USERNAME), username); } Role role = dataService.query(RoleMetadata.ROLE, Role.class).eq(NAME, roleName).findOne(); if (role == null) { throw new UnknownEntityException(roleMetadata, roleMetadata.getAttribute(NAME), roleName); } this.addUserToRole(user, role); } }
RoleMembershipServiceImpl implements RoleMembershipService { @RunAsSystem @Override public void addUserToRole(String username, String roleName) { User user = userService.getUser(username); if (user == null) { throw new UnknownEntityException(userMetadata, userMetadata.getAttribute(USERNAME), username); } Role role = dataService.query(RoleMetadata.ROLE, Role.class).eq(NAME, roleName).findOne(); if (role == null) { throw new UnknownEntityException(roleMetadata, roleMetadata.getAttribute(NAME), roleName); } this.addUserToRole(user, role); } RoleMembershipServiceImpl(
      UserService userService,
      RoleMembershipFactory roleMembershipFactory,
      DataService dataService,
      UserMetadata userMetadata,
      RoleMetadata roleMetadata); }
RoleMembershipServiceImpl implements RoleMembershipService { @RunAsSystem @Override public void addUserToRole(String username, String roleName) { User user = userService.getUser(username); if (user == null) { throw new UnknownEntityException(userMetadata, userMetadata.getAttribute(USERNAME), username); } Role role = dataService.query(RoleMetadata.ROLE, Role.class).eq(NAME, roleName).findOne(); if (role == null) { throw new UnknownEntityException(roleMetadata, roleMetadata.getAttribute(NAME), roleName); } this.addUserToRole(user, role); } RoleMembershipServiceImpl(
      UserService userService,
      RoleMembershipFactory roleMembershipFactory,
      DataService dataService,
      UserMetadata userMetadata,
      RoleMetadata roleMetadata); @RunAsSystem @Override void addUserToRole(String username, String roleName); @RunAsSystem @Override void addUserToRole(final User user, final Role role); @RunAsSystem @Override void removeMembership(final RoleMembership roleMembership); @RunAsSystem @Override void updateMembership(RoleMembership roleMembership, Role newRole); @RunAsSystem @Override Collection<RoleMembership> getMemberships(Collection<Role> roles); }
RoleMembershipServiceImpl implements RoleMembershipService { @RunAsSystem @Override public void addUserToRole(String username, String roleName) { User user = userService.getUser(username); if (user == null) { throw new UnknownEntityException(userMetadata, userMetadata.getAttribute(USERNAME), username); } Role role = dataService.query(RoleMetadata.ROLE, Role.class).eq(NAME, roleName).findOne(); if (role == null) { throw new UnknownEntityException(roleMetadata, roleMetadata.getAttribute(NAME), roleName); } this.addUserToRole(user, role); } RoleMembershipServiceImpl(
      UserService userService,
      RoleMembershipFactory roleMembershipFactory,
      DataService dataService,
      UserMetadata userMetadata,
      RoleMetadata roleMetadata); @RunAsSystem @Override void addUserToRole(String username, String roleName); @RunAsSystem @Override void addUserToRole(final User user, final Role role); @RunAsSystem @Override void removeMembership(final RoleMembership roleMembership); @RunAsSystem @Override void updateMembership(RoleMembership roleMembership, Role newRole); @RunAsSystem @Override Collection<RoleMembership> getMemberships(Collection<Role> roles);  }