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());
}
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));
}
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)));
}
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));
}
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));
}
Aggregations