Search in sources :

Example 1 with NewRole

use of io.gravitee.am.service.model.NewRole in project gravitee-access-management by gravitee-io.

the class RolesResourceTest method shouldCreate.

@Test
public void shouldCreate() {
    final String domainId = "domain-1";
    final Domain mockDomain = new Domain();
    mockDomain.setId(domainId);
    NewRole newRole = new NewRole();
    newRole.setName("role-name");
    Role role = new Role();
    role.setId("role-id");
    role.setName("role-name");
    doReturn(Maybe.just(mockDomain)).when(domainService).findById(domainId);
    doReturn(Single.just(role)).when(roleService).create(eq(domainId), any(), any());
    final Response response = target("domains").path(domainId).path("roles").request().post(Entity.json(newRole));
    assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) Response(javax.ws.rs.core.Response) NewRole(io.gravitee.am.service.model.NewRole) Domain(io.gravitee.am.model.Domain) Test(org.junit.Test) JerseySpringTest(io.gravitee.am.management.handlers.management.api.JerseySpringTest)

Example 2 with NewRole

use of io.gravitee.am.service.model.NewRole in project gravitee-access-management by gravitee-io.

the class RoleServiceTest method shouldCreate_uniquenessException.

@Test
public void shouldCreate_uniquenessException() {
    NewRole newRole = Mockito.mock(NewRole.class);
    when(newRole.getName()).thenReturn("existing-role-name");
    Role role = new Role();
    role.setId("existing-role-id");
    role.setName("existing-role-name");
    role.setReferenceType(ReferenceType.DOMAIN);
    role.setReferenceId("domain#1");
    when(roleRepository.findAll(ReferenceType.DOMAIN, DOMAIN)).thenReturn(Flowable.just(role));
    TestObserver testObserver = new TestObserver();
    roleService.create(DOMAIN, newRole).subscribe(testObserver);
    testObserver.assertError(RoleAlreadyExistsException.class);
    testObserver.assertNotComplete();
    verify(roleRepository, never()).create(any(Role.class));
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) NewRole(io.gravitee.am.service.model.NewRole) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 3 with NewRole

use of io.gravitee.am.service.model.NewRole in project gravitee-access-management by gravitee-io.

the class RoleServiceImpl method create.

@Override
public Single<Role> create(ReferenceType referenceType, String referenceId, NewRole newRole, User principal) {
    LOGGER.debug("Create a new role {} for {} {}", newRole, referenceType, referenceId);
    String roleId = RandomString.generate();
    // check if role name is unique
    return checkRoleUniqueness(newRole.getName(), roleId, referenceType, referenceId).flatMap(__ -> {
        Role role = new Role();
        role.setId(roleId);
        role.setReferenceType(referenceType);
        role.setReferenceId(referenceId);
        role.setName(newRole.getName());
        role.setDescription(newRole.getDescription());
        role.setAssignableType(newRole.getAssignableType());
        role.setPermissionAcls(new HashMap<>());
        role.setOauthScopes(new ArrayList<>());
        role.setCreatedAt(new Date());
        role.setUpdatedAt(role.getCreatedAt());
        return roleRepository.create(role);
    }).flatMap(role -> {
        Event event = new Event(Type.ROLE, new Payload(role.getId(), role.getReferenceType(), role.getReferenceId(), Action.CREATE));
        return eventService.create(event).flatMap(__ -> Single.just(role));
    }).onErrorResumeNext(ex -> {
        if (ex instanceof AbstractManagementException) {
            return Single.error(ex);
        }
        LOGGER.error("An error occurs while trying to create a role", ex);
        return Single.error(new TechnicalManagementException("An error occurs while trying to create a role", ex));
    }).doOnSuccess(role -> auditService.report(AuditBuilder.builder(RoleAuditBuilder.class).principal(principal).type(EventType.ROLE_CREATED).role(role))).doOnError(throwable -> auditService.report(AuditBuilder.builder(RoleAuditBuilder.class).principal(principal).type(EventType.ROLE_CREATED).throwable(throwable)));
}
Also used : SystemRole(io.gravitee.am.model.permissions.SystemRole) Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) java.util(java.util) SystemRole(io.gravitee.am.model.permissions.SystemRole) Role(io.gravitee.am.model.Role) Permission(io.gravitee.am.model.permissions.Permission) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditService(io.gravitee.am.service.AuditService) Platform(io.gravitee.am.model.Platform) Event(io.gravitee.am.model.common.event.Event) Type(io.gravitee.am.common.event.Type) io.gravitee.am.service.exception(io.gravitee.am.service.exception) Acl(io.gravitee.am.model.Acl) User(io.gravitee.am.identityprovider.api.User) AuditBuilder(io.gravitee.am.service.reporter.builder.AuditBuilder) NewRole(io.gravitee.am.service.model.NewRole) ReferenceType(io.gravitee.am.model.ReferenceType) EventService(io.gravitee.am.service.EventService) Observable(io.reactivex.Observable) io.reactivex(io.reactivex) RoleService(io.gravitee.am.service.RoleService) Action(io.gravitee.am.common.event.Action) Page(io.gravitee.am.model.common.Page) Logger(org.slf4j.Logger) RoleAuditBuilder(io.gravitee.am.service.reporter.builder.management.RoleAuditBuilder) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) EventType(io.gravitee.am.common.audit.EventType) Collectors(java.util.stream.Collectors) RandomString(io.gravitee.am.common.utils.RandomString) RoleRepository(io.gravitee.am.repository.management.api.RoleRepository) UpdateRole(io.gravitee.am.service.model.UpdateRole) Component(org.springframework.stereotype.Component) Payload(io.gravitee.am.model.common.event.Payload) Lazy(org.springframework.context.annotation.Lazy) RoleAuditBuilder(io.gravitee.am.service.reporter.builder.management.RoleAuditBuilder) Event(io.gravitee.am.model.common.event.Event) Payload(io.gravitee.am.model.common.event.Payload) RandomString(io.gravitee.am.common.utils.RandomString)

Example 4 with NewRole

use of io.gravitee.am.service.model.NewRole in project gravitee-access-management by gravitee-io.

the class RoleServiceTest method shouldCreate.

@Test
public void shouldCreate() {
    NewRole newRole = Mockito.mock(NewRole.class);
    when(roleRepository.findAll(ReferenceType.DOMAIN, DOMAIN)).thenReturn(Flowable.empty());
    Role role = new Role();
    role.setReferenceType(ReferenceType.DOMAIN);
    role.setReferenceId("domain#1");
    when(roleRepository.create(any(Role.class))).thenReturn(Single.just(role));
    when(eventService.create(any())).thenReturn(Single.just(new Event()));
    TestObserver testObserver = roleService.create(DOMAIN, newRole).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(roleRepository, times(1)).findAll(ReferenceType.DOMAIN, DOMAIN);
    verify(roleRepository, times(1)).create(any(Role.class));
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) NewRole(io.gravitee.am.service.model.NewRole) Event(io.gravitee.am.model.common.event.Event) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 5 with NewRole

use of io.gravitee.am.service.model.NewRole in project gravitee-access-management by gravitee-io.

the class RoleServiceTest method shouldCreate_technicalException.

@Test
public void shouldCreate_technicalException() {
    NewRole newRole = Mockito.mock(NewRole.class);
    when(roleRepository.findAll(ReferenceType.DOMAIN, DOMAIN)).thenReturn(Flowable.error(TechnicalException::new));
    TestObserver testObserver = new TestObserver();
    roleService.create(DOMAIN, newRole).subscribe(testObserver);
    testObserver.assertError(TechnicalManagementException.class);
    testObserver.assertNotComplete();
    verify(roleRepository, never()).create(any(Role.class));
}
Also used : Role(io.gravitee.am.model.Role) NewRole(io.gravitee.am.service.model.NewRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) UpdateRole(io.gravitee.am.service.model.UpdateRole) NewRole(io.gravitee.am.service.model.NewRole) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

Role (io.gravitee.am.model.Role)6 NewRole (io.gravitee.am.service.model.NewRole)6 DefaultRole (io.gravitee.am.model.permissions.DefaultRole)4 UpdateRole (io.gravitee.am.service.model.UpdateRole)4 Test (org.junit.Test)4 TestObserver (io.reactivex.observers.TestObserver)3 User (io.gravitee.am.identityprovider.api.User)2 Acl (io.gravitee.am.model.Acl)2 Page (io.gravitee.am.model.common.Page)2 Event (io.gravitee.am.model.common.event.Event)2 Permission (io.gravitee.am.model.permissions.Permission)2 RoleService (io.gravitee.am.service.RoleService)2 Collectors (java.util.stream.Collectors)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 EventType (io.gravitee.am.common.audit.EventType)1 Action (io.gravitee.am.common.event.Action)1 Type (io.gravitee.am.common.event.Type)1 RandomString (io.gravitee.am.common.utils.RandomString)1 JerseySpringTest (io.gravitee.am.management.handlers.management.api.JerseySpringTest)1 AbstractResource (io.gravitee.am.management.handlers.management.api.resources.AbstractResource)1