Search in sources :

Example 1 with Event

use of io.gravitee.am.model.common.event.Event in project gravitee-access-management by gravitee-io.

the class MembershipServiceTest method shouldCreate_primaryOwner.

@Test
public void shouldCreate_primaryOwner() {
    Membership membership = new Membership();
    membership.setReferenceId(DOMAIN_ID);
    membership.setReferenceType(ReferenceType.DOMAIN);
    membership.setMemberId("user-id");
    membership.setMemberType(MemberType.USER);
    membership.setRoleId("role-primary-owner");
    User user = new User();
    user.setReferenceId(ORGANIZATION_ID);
    user.setReferenceType(ReferenceType.ORGANIZATION);
    Role role = new Role();
    role.setId("role-primary-owner");
    role.setName(SystemRole.DOMAIN_PRIMARY_OWNER.name());
    role.setReferenceId(Platform.DEFAULT);
    role.setSystem(true);
    role.setReferenceType(ReferenceType.PLATFORM);
    role.setAssignableType(ReferenceType.DOMAIN);
    when(userService.findById(ReferenceType.ORGANIZATION, ORGANIZATION_ID, membership.getMemberId())).thenReturn(Single.just(user));
    when(roleService.findById(role.getId())).thenReturn(Maybe.just(role));
    when(membershipRepository.findByReferenceAndMember(membership.getReferenceType(), membership.getReferenceId(), membership.getMemberType(), membership.getMemberId())).thenReturn(Maybe.empty());
    when(membershipRepository.create(any())).thenReturn(Single.just(new Membership()));
    when(membershipRepository.findByCriteria(eq(ReferenceType.DOMAIN), eq(DOMAIN_ID), argThat(criteria -> criteria.getRoleId().isPresent()))).thenReturn(Flowable.empty());
    when(eventService.create(any())).thenReturn(Single.just(new Event()));
    TestObserver testObserver = membershipService.addOrUpdate(ORGANIZATION_ID, membership).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
}
Also used : SystemRole(io.gravitee.am.model.permissions.SystemRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) MembershipServiceImpl(io.gravitee.am.service.impl.MembershipServiceImpl) InjectMocks(org.mockito.InjectMocks) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) SystemRole(io.gravitee.am.model.permissions.SystemRole) MemberType(io.gravitee.am.model.membership.MemberType) Mock(org.mockito.Mock) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) io.gravitee.am.model(io.gravitee.am.model) Event(io.gravitee.am.model.common.event.Event) io.gravitee.am.service.exception(io.gravitee.am.service.exception) MembershipRepository(io.gravitee.am.repository.management.api.MembershipRepository) Mockito(org.mockito.Mockito) Flowable(io.reactivex.Flowable) NewMembership(io.gravitee.am.service.model.NewMembership) MembershipCriteria(io.gravitee.am.repository.management.api.search.MembershipCriteria) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) NewMembership(io.gravitee.am.service.model.NewMembership) Event(io.gravitee.am.model.common.event.Event) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 2 with Event

use of io.gravitee.am.model.common.event.Event in project gravitee-access-management by gravitee-io.

the class GroupServiceTest method shouldDelete_technicalException.

@Test
public void shouldDelete_technicalException() {
    when(groupRepository.findById(ReferenceType.DOMAIN, DOMAIN, "my-group")).thenReturn(Maybe.just(new Group()));
    when(groupRepository.delete("my-group")).thenReturn(Completable.error(TechnicalException::new));
    when(eventService.create(any())).thenReturn(Single.just(new Event()));
    TestObserver testObserver = new TestObserver();
    groupService.delete(ReferenceType.DOMAIN, DOMAIN, "my-group").subscribe(testObserver);
    testObserver.assertError(TechnicalManagementException.class);
    testObserver.assertNotComplete();
}
Also used : Group(io.gravitee.am.model.Group) UpdateGroup(io.gravitee.am.service.model.UpdateGroup) NewGroup(io.gravitee.am.service.model.NewGroup) Event(io.gravitee.am.model.common.event.Event) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 3 with Event

use of io.gravitee.am.model.common.event.Event in project gravitee-access-management by gravitee-io.

the class IdentityProviderServiceTest method shouldNotUpdate_notUpgrader_isSystem.

@Test
public void shouldNotUpdate_notUpgrader_isSystem() {
    UpdateIdentityProvider updateIdentityProvider = (UpdateIdentityProvider) createIdentityProviders(true, false);
    IdentityProvider identityProvider = (IdentityProvider) createIdentityProviders(false, true);
    when(identityProviderRepository.findById(eq(ReferenceType.DOMAIN), eq(DOMAIN), eq("my-identity-provider"))).thenReturn(Maybe.just(identityProvider));
    when(identityProviderRepository.update(any(IdentityProvider.class))).thenReturn(Single.just(identityProvider));
    when(eventService.create(any())).thenReturn(Single.just(new Event()));
    TestObserver testObserver = identityProviderService.update(DOMAIN, "my-identity-provider", updateIdentityProvider, false).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
    verify(identityProviderRepository, times(1)).update(argThat(upIdp -> upIdp.getConfiguration().equals("initial-config")));
    verify(identityProviderRepository, times(1)).update(any(IdentityProvider.class));
    verify(eventService, times(1)).create(any());
}
Also used : Mock(org.mockito.Mock) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) Application(io.gravitee.am.model.Application) IdentityProvider(io.gravitee.am.model.IdentityProvider) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) Event(io.gravitee.am.model.common.event.Event) Matchers.anyString(org.mockito.Matchers.anyString) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) NewIdentityProvider(io.gravitee.am.service.model.NewIdentityProvider) ReferenceType(io.gravitee.am.model.ReferenceType) TestSubscriber(io.reactivex.subscribers.TestSubscriber) IdentityProviderRepository(io.gravitee.am.repository.management.api.IdentityProviderRepository) IdentityProviderNotFoundException(io.gravitee.am.service.exception.IdentityProviderNotFoundException) InjectMocks(org.mockito.InjectMocks) IdentityProviderWithApplicationsException(io.gravitee.am.service.exception.IdentityProviderWithApplicationsException) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) UpdateIdentityProvider(io.gravitee.am.service.model.UpdateIdentityProvider) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) IdentityProviderServiceImpl(io.gravitee.am.service.impl.IdentityProviderServiceImpl) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) UpdateIdentityProvider(io.gravitee.am.service.model.UpdateIdentityProvider) Event(io.gravitee.am.model.common.event.Event) IdentityProvider(io.gravitee.am.model.IdentityProvider) NewIdentityProvider(io.gravitee.am.service.model.NewIdentityProvider) UpdateIdentityProvider(io.gravitee.am.service.model.UpdateIdentityProvider) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 4 with Event

use of io.gravitee.am.model.common.event.Event in project gravitee-access-management by gravitee-io.

the class MembershipServiceTest method shouldCreate_groupMembership.

@Test
public void shouldCreate_groupMembership() {
    Membership membership = new Membership();
    membership.setReferenceId(DOMAIN_ID);
    membership.setReferenceType(ReferenceType.DOMAIN);
    membership.setMemberId("group-id");
    membership.setMemberType(MemberType.GROUP);
    membership.setRoleId("role-id");
    Role role = new Role();
    role.setId("role-id");
    role.setReferenceId(DOMAIN_ID);
    role.setReferenceType(ReferenceType.DOMAIN);
    role.setAssignableType(ReferenceType.DOMAIN);
    Group group = new Group();
    group.setReferenceId(DOMAIN_ID);
    group.setReferenceType(ReferenceType.DOMAIN);
    when(groupService.findById(ReferenceType.ORGANIZATION, ORGANIZATION_ID, membership.getMemberId())).thenReturn(Single.just(group));
    when(roleService.findById(role.getId())).thenReturn(Maybe.just(role));
    when(membershipRepository.findByReferenceAndMember(membership.getReferenceType(), membership.getReferenceId(), membership.getMemberType(), membership.getMemberId())).thenReturn(Maybe.empty());
    when(membershipRepository.create(any())).thenReturn(Single.just(new Membership()));
    when(eventService.create(any())).thenReturn(Single.just(new Event()));
    TestObserver testObserver = membershipService.addOrUpdate(ORGANIZATION_ID, membership).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertComplete();
    testObserver.assertNoErrors();
}
Also used : SystemRole(io.gravitee.am.model.permissions.SystemRole) DefaultRole(io.gravitee.am.model.permissions.DefaultRole) NewMembership(io.gravitee.am.service.model.NewMembership) Event(io.gravitee.am.model.common.event.Event) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Example 5 with Event

use of io.gravitee.am.model.common.event.Event in project gravitee-access-management by gravitee-io.

the class EventServiceTest method shouldNotCreate_technicalException.

@Test
public void shouldNotCreate_technicalException() {
    Event newEvent = Mockito.mock(Event.class);
    when(eventRepository.create(any(Event.class))).thenReturn(Single.error(TechnicalException::new));
    TestObserver testObserver = eventService.create(newEvent).test();
    testObserver.awaitTerminalEvent();
    testObserver.assertError(TechnicalManagementException.class);
    testObserver.assertNotComplete();
}
Also used : Event(io.gravitee.am.model.common.event.Event) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test)

Aggregations

Event (io.gravitee.am.model.common.event.Event)134 Test (org.junit.Test)103 TestObserver (io.reactivex.observers.TestObserver)91 Payload (io.gravitee.am.model.common.event.Payload)35 ReferenceType (io.gravitee.am.model.ReferenceType)33 Action (io.gravitee.am.common.event.Action)28 Maybe (io.reactivex.Maybe)28 Single (io.reactivex.Single)28 Type (io.gravitee.am.common.event.Type)27 Autowired (org.springframework.beans.factory.annotation.Autowired)27 Lazy (org.springframework.context.annotation.Lazy)27 Component (org.springframework.stereotype.Component)27 EventType (io.gravitee.am.common.audit.EventType)26 RandomString (io.gravitee.am.common.utils.RandomString)26 AuditBuilder (io.gravitee.am.service.reporter.builder.AuditBuilder)26 Completable (io.reactivex.Completable)26 Logger (org.slf4j.Logger)26 LoggerFactory (org.slf4j.LoggerFactory)26 User (io.gravitee.am.identityprovider.api.User)24 Flowable (io.reactivex.Flowable)23