use of io.gravitee.rest.api.model.NewRoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleService_CreateTest method shouldNotCreateBecauseOfReservedRoleName.
@Test(expected = RoleReservedNameException.class)
public void shouldNotCreateBecauseOfReservedRoleName() throws TechnicalException {
NewRoleEntity newRoleEntityMock = mock(NewRoleEntity.class);
when(newRoleEntityMock.getName()).thenReturn("admin");
roleService.create(newRoleEntityMock);
fail("should fail earlier");
}
use of io.gravitee.rest.api.model.NewRoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleService_CreateTest method shouldCreate.
@Test
public void shouldCreate() throws TechnicalException {
NewRoleEntity newRoleEntityMock = mock(NewRoleEntity.class);
when(newRoleEntityMock.getName()).thenReturn("new mock role");
when(newRoleEntityMock.getScope()).thenReturn(io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT);
when(newRoleEntityMock.getPermissions()).thenReturn(Collections.singletonMap(DOCUMENTATION.getName(), new char[] { RolePermissionAction.CREATE.getId() }));
Role roleMock = mock(Role.class);
when(roleMock.getId()).thenReturn("new_mock_role");
when(roleMock.getName()).thenReturn("new mock role");
when(roleMock.getScope()).thenReturn(RoleScope.ENVIRONMENT);
when(roleMock.getPermissions()).thenReturn(new int[] { 3008 });
when(mockRoleRepository.create(any())).thenReturn(roleMock);
RoleEntity entity = roleService.create(newRoleEntityMock);
assertNotNull("no entoty created", entity);
assertEquals("invalid id", "new_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.NewRoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleServiceImpl method create.
private RoleEntity create(final NewRoleEntity roleEntity, String organizationId) {
try {
Role role = convert(roleEntity);
if (roleRepository.findByScopeAndNameAndReferenceIdAndReferenceType(role.getScope(), role.getName(), organizationId, RoleReferenceType.ORGANIZATION).isPresent()) {
throw new RoleAlreadyExistsException(role.getScope(), role.getName());
}
role.setId(UuidString.generateRandom());
role.setCreatedAt(new Date());
role.setUpdatedAt(role.getCreatedAt());
role.setReferenceId(organizationId);
role.setReferenceType(RoleReferenceType.ORGANIZATION);
RoleEntity entity = convert(roleRepository.create(role));
auditService.createOrganizationAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_CREATED, role.getCreatedAt(), null, role);
if (entity.isDefaultRole()) {
toggleDefaultRole(roleEntity.getScope(), entity.getName());
}
return entity;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to create role {}", roleEntity.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to create role " + roleEntity.getName(), ex);
}
}
use of io.gravitee.rest.api.model.NewRoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleService_CreateTest method shouldNotCreateBecauseOfInvalidPermissionAction.
@Test(expected = IllegalArgumentException.class)
public void shouldNotCreateBecauseOfInvalidPermissionAction() throws TechnicalException {
NewRoleEntity newRoleEntityMock = mock(NewRoleEntity.class);
when(newRoleEntityMock.getName()).thenReturn("new mock role");
when(newRoleEntityMock.getScope()).thenReturn(io.gravitee.rest.api.model.permissions.RoleScope.ENVIRONMENT);
when(newRoleEntityMock.getPermissions()).thenReturn(Collections.singletonMap(DOCUMENTATION.getName(), new char[] { 'X' }));
roleService.create(newRoleEntityMock);
fail("should fail earlier");
}
Aggregations