use of io.gravitee.rest.api.model.UpdateRoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleService_UpdateTest method shouldUpdate.
@Test
public void shouldUpdate() throws TechnicalException {
UpdateRoleEntity updateRoleEntityMock = mock(UpdateRoleEntity.class);
when(updateRoleEntityMock.getId()).thenReturn("updated_mock_role");
when(updateRoleEntityMock.getName()).thenReturn("update mock role");
when(updateRoleEntityMock.getScope()).thenReturn(io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT);
when(updateRoleEntityMock.getPermissions()).thenReturn(Collections.singletonMap(DOCUMENTATION.getName(), new char[] { RolePermissionAction.CREATE.getId() }));
Role roleMock = mock(Role.class);
when(roleMock.getId()).thenReturn("updated_mock_role");
when(roleMock.getName()).thenReturn("new mock role");
when(roleMock.getScope()).thenReturn(RoleScope.ENVIRONMENT);
when(roleMock.getPermissions()).thenReturn(new int[] { 3008 });
when(roleMock.getReferenceType()).thenReturn(RoleReferenceType.ORGANIZATION);
when(roleMock.getReferenceId()).thenReturn("orga#1");
when(mockRoleRepository.update(argThat(role -> role.getReferenceId().equals("orga#1") && role.getReferenceType() == RoleReferenceType.ORGANIZATION))).thenReturn(roleMock);
when(mockRoleRepository.findById("updated_mock_role")).thenReturn(Optional.of(roleMock));
RoleEntity entity = roleService.update(updateRoleEntityMock);
assertNotNull("no entoty created", entity);
assertEquals("invalid id", "updated_mock_role", entity.getId());
assertEquals("invalid name", "new mock role", entity.getName());
assertEquals("invalid scope", io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT, entity.getScope());
assertFalse("no permissions found", entity.getPermissions().isEmpty());
assertTrue("invalid Permission name", entity.getPermissions().containsKey(DOCUMENTATION.getName()));
char[] perms = entity.getPermissions().get(DOCUMENTATION.getName());
assertEquals("not enough permissions", 1, perms.length);
assertEquals("not the good permission", RolePermissionAction.CREATE.getId(), perms[0]);
}
use of io.gravitee.rest.api.model.UpdateRoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleServiceImpl method update.
@Override
public RoleEntity update(final UpdateRoleEntity roleEntity) {
if (isReserved(roleEntity.getName())) {
throw new RoleReservedNameException(roleEntity.getName());
}
RoleScope scope = roleEntity.getScope();
try {
Optional<Role> optRole = roleRepository.findById(roleEntity.getId());
if (!optRole.isPresent()) {
throw new RoleNotFoundException(roleEntity.getId());
}
Role role = optRole.get();
Role updatedRole = convert(roleEntity);
updatedRole.setCreatedAt(role.getCreatedAt());
updatedRole.setReferenceId(role.getReferenceId());
updatedRole.setReferenceType(role.getReferenceType());
RoleEntity entity = convert(roleRepository.update(updatedRole));
auditService.createOrganizationAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_UPDATED, updatedRole.getUpdatedAt(), role, updatedRole);
if (entity.isDefaultRole()) {
toggleDefaultRole(scope, entity.getName());
}
return entity;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update role {}", roleEntity.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to update role " + roleEntity.getName(), ex);
}
}
use of io.gravitee.rest.api.model.UpdateRoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleService_UpdateTest method shouldNotUpdateIfNotExists.
@Test(expected = RoleNotFoundException.class)
public void shouldNotUpdateIfNotExists() throws TechnicalException {
UpdateRoleEntity updateRoleEntityMock = mock(UpdateRoleEntity.class);
when(updateRoleEntityMock.getId()).thenReturn("updated_mock_role");
when(updateRoleEntityMock.getName()).thenReturn("update mock role");
when(updateRoleEntityMock.getScope()).thenReturn(io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT);
when(mockRoleRepository.findById("updated_mock_role")).thenReturn(Optional.empty());
roleService.update(updateRoleEntityMock);
fail("should fail because not exists");
}
Aggregations