Search in sources :

Example 1 with UpdateRoleEntity

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]);
}
Also used : Role(io.gravitee.repository.management.model.Role) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) RolePermissionAction(io.gravitee.rest.api.model.permissions.RolePermissionAction) RoleScope(io.gravitee.repository.management.model.RoleScope) RoleRepository(io.gravitee.repository.management.api.RoleRepository) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) RunWith(org.junit.runner.RunWith) AuditService(io.gravitee.rest.api.service.AuditService) RoleNotFoundException(io.gravitee.rest.api.service.exceptions.RoleNotFoundException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DOCUMENTATION(io.gravitee.rest.api.model.permissions.EnvironmentPermission.DOCUMENTATION) RoleEntity(io.gravitee.rest.api.model.RoleEntity) RoleServiceImpl(io.gravitee.rest.api.service.impl.RoleServiceImpl) Optional(java.util.Optional) Assert(org.junit.Assert) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Role(io.gravitee.repository.management.model.Role) RoleReferenceType(io.gravitee.repository.management.model.RoleReferenceType) Mockito.mock(org.mockito.Mockito.mock) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) Test(org.junit.Test)

Example 2 with UpdateRoleEntity

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);
    }
}
Also used : Role(io.gravitee.repository.management.model.Role) UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) NewRoleEntity(io.gravitee.rest.api.model.NewRoleEntity) RoleEntity(io.gravitee.rest.api.model.RoleEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException)

Example 3 with UpdateRoleEntity

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");
}
Also used : UpdateRoleEntity(io.gravitee.rest.api.model.UpdateRoleEntity) Test(org.junit.Test)

Aggregations

UpdateRoleEntity (io.gravitee.rest.api.model.UpdateRoleEntity)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 Role (io.gravitee.repository.management.model.Role)2 RoleEntity (io.gravitee.rest.api.model.RoleEntity)2 Test (org.junit.Test)2 RoleRepository (io.gravitee.repository.management.api.RoleRepository)1 RoleReferenceType (io.gravitee.repository.management.model.RoleReferenceType)1 RoleScope (io.gravitee.repository.management.model.RoleScope)1 NewRoleEntity (io.gravitee.rest.api.model.NewRoleEntity)1 DOCUMENTATION (io.gravitee.rest.api.model.permissions.EnvironmentPermission.DOCUMENTATION)1 RolePermissionAction (io.gravitee.rest.api.model.permissions.RolePermissionAction)1 AuditService (io.gravitee.rest.api.service.AuditService)1 RoleNotFoundException (io.gravitee.rest.api.service.exceptions.RoleNotFoundException)1 RoleServiceImpl (io.gravitee.rest.api.service.impl.RoleServiceImpl)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 Assert (org.junit.Assert)1 RunWith (org.junit.runner.RunWith)1 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)1 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)1