Search in sources :

Example 1 with NewOrganization

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

the class OrganizationServiceTest method shouldCreate_updateError.

@Test
public void shouldCreate_updateError() {
    Organization existingOrganization = new Organization();
    existingOrganization.setId(ORGANIZATION_ID);
    when(organizationRepository.findById(ORGANIZATION_ID)).thenReturn(Maybe.just(existingOrganization));
    when(organizationRepository.update(argThat(organization -> organization.getId().equals(ORGANIZATION_ID)))).thenReturn(Single.error(new TechnicalManagementException()));
    NewOrganization newOrganization = new NewOrganization();
    newOrganization.setName("TestName");
    newOrganization.setDescription("TestDescription");
    newOrganization.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Organization> obs = cut.createOrUpdate(ORGANIZATION_ID, newOrganization, createdBy).test();
    obs.awaitTerminalEvent();
    obs.assertError(TechnicalManagementException.class);
    verify(auditService, times(1)).report(argThat(builder -> {
        Audit audit = builder.build(new ObjectMapper());
        assertEquals(ReferenceType.ORGANIZATION, audit.getReferenceType());
        assertEquals(ORGANIZATION_ID, audit.getReferenceId());
        assertEquals(createdBy.getId(), audit.getActor().getId());
        assertEquals(EventType.ORGANIZATION_UPDATED, audit.getType());
        assertEquals(Status.FAILURE, audit.getOutcome().getStatus());
        return true;
    }));
}
Also used : OrganizationNotFoundException(io.gravitee.am.service.exception.OrganizationNotFoundException) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) OrganizationServiceImpl(io.gravitee.am.service.impl.OrganizationServiceImpl) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Entrypoint(io.gravitee.am.model.Entrypoint) Flowable(io.reactivex.Flowable) Status(io.gravitee.am.common.audit.Status) ReferenceType(io.gravitee.am.model.ReferenceType) NewOrganization(io.gravitee.am.service.model.NewOrganization) Before(org.junit.Before) OrganizationRepository(io.gravitee.am.repository.management.api.OrganizationRepository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) EventType(io.gravitee.am.common.audit.EventType) PatchOrganization(io.gravitee.am.service.model.PatchOrganization) Mockito(org.mockito.Mockito) Organization(io.gravitee.am.model.Organization) List(java.util.List) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Audit(io.gravitee.am.reporter.api.audit.model.Audit) NewOrganization(io.gravitee.am.service.model.NewOrganization) Audit(io.gravitee.am.reporter.api.audit.model.Audit) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) NewOrganization(io.gravitee.am.service.model.NewOrganization) PatchOrganization(io.gravitee.am.service.model.PatchOrganization) Organization(io.gravitee.am.model.Organization) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with NewOrganization

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

the class OrganizationServiceTest method shouldCreate_update.

@Test
public void shouldCreate_update() {
    Organization existingOrganization = new Organization();
    existingOrganization.setId(ORGANIZATION_ID);
    when(organizationRepository.findById(ORGANIZATION_ID)).thenReturn(Maybe.just(existingOrganization));
    when(organizationRepository.update(argThat(organization -> organization.getId().equals(ORGANIZATION_ID)))).thenAnswer(i -> Single.just(i.getArgument(0)));
    NewOrganization newOrganization = new NewOrganization();
    newOrganization.setName("TestName");
    newOrganization.setDescription("TestDescription");
    newOrganization.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    newOrganization.setHrids(Collections.singletonList("TestHridUpdate"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Organization> obs = cut.createOrUpdate(ORGANIZATION_ID, newOrganization, createdBy).test();
    obs.awaitTerminalEvent();
    obs.assertValue(organization -> {
        assertEquals(ORGANIZATION_ID, organization.getId());
        assertEquals(newOrganization.getName(), organization.getName());
        assertEquals(newOrganization.getDescription(), organization.getDescription());
        assertEquals(newOrganization.getDomainRestrictions(), organization.getDomainRestrictions());
        assertEquals(newOrganization.getHrids(), organization.getHrids());
        return true;
    });
    verify(auditService, times(1)).report(argThat(builder -> {
        Audit audit = builder.build(new ObjectMapper());
        assertEquals(ReferenceType.ORGANIZATION, audit.getReferenceType());
        assertEquals(ORGANIZATION_ID, audit.getReferenceId());
        assertEquals(createdBy.getId(), audit.getActor().getId());
        assertEquals(EventType.ORGANIZATION_UPDATED, audit.getType());
        assertEquals(Status.SUCCESS, audit.getOutcome().getStatus());
        return true;
    }));
}
Also used : OrganizationNotFoundException(io.gravitee.am.service.exception.OrganizationNotFoundException) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) OrganizationServiceImpl(io.gravitee.am.service.impl.OrganizationServiceImpl) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Entrypoint(io.gravitee.am.model.Entrypoint) Flowable(io.reactivex.Flowable) Status(io.gravitee.am.common.audit.Status) ReferenceType(io.gravitee.am.model.ReferenceType) NewOrganization(io.gravitee.am.service.model.NewOrganization) Before(org.junit.Before) OrganizationRepository(io.gravitee.am.repository.management.api.OrganizationRepository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) EventType(io.gravitee.am.common.audit.EventType) PatchOrganization(io.gravitee.am.service.model.PatchOrganization) Mockito(org.mockito.Mockito) Organization(io.gravitee.am.model.Organization) List(java.util.List) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Audit(io.gravitee.am.reporter.api.audit.model.Audit) NewOrganization(io.gravitee.am.service.model.NewOrganization) Audit(io.gravitee.am.reporter.api.audit.model.Audit) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) NewOrganization(io.gravitee.am.service.model.NewOrganization) PatchOrganization(io.gravitee.am.service.model.PatchOrganization) Organization(io.gravitee.am.model.Organization) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with NewOrganization

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

the class OrganizationCommandHandlerTest method handle.

@Test
public void handle() {
    OrganizationPayload organizationPayload = new OrganizationPayload();
    OrganizationCommand command = new OrganizationCommand(organizationPayload);
    organizationPayload.setId("orga#1");
    organizationPayload.setHrids(Collections.singletonList("orga-1"));
    organizationPayload.setDescription("Organization description");
    organizationPayload.setName("Organization name");
    organizationPayload.setDomainRestrictions(Arrays.asList("domain.restriction1.io", "domain.restriction2.io"));
    when(organizationService.createOrUpdate(eq("orga#1"), argThat(newOrganization -> newOrganization.getHrids().equals(organizationPayload.getHrids()) && newOrganization.getDescription().equals(organizationPayload.getDescription()) && newOrganization.getName().equals(organizationPayload.getName()) && newOrganization.getDomainRestrictions().equals(organizationPayload.getDomainRestrictions())), isNull())).thenReturn(Single.just(new Organization()));
    TestObserver<OrganizationReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.SUCCEEDED));
}
Also used : Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) Command(io.gravitee.cockpit.api.command.Command) OrganizationPayload(io.gravitee.cockpit.api.command.organization.OrganizationPayload) OrganizationReply(io.gravitee.cockpit.api.command.organization.OrganizationReply) Organization(io.gravitee.am.model.Organization) OrganizationService(io.gravitee.am.service.OrganizationService) OrganizationCommand(io.gravitee.cockpit.api.command.organization.OrganizationCommand) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) NewOrganization(io.gravitee.am.service.model.NewOrganization) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) OrganizationPayload(io.gravitee.cockpit.api.command.organization.OrganizationPayload) OrganizationReply(io.gravitee.cockpit.api.command.organization.OrganizationReply) Organization(io.gravitee.am.model.Organization) NewOrganization(io.gravitee.am.service.model.NewOrganization) OrganizationCommand(io.gravitee.cockpit.api.command.organization.OrganizationCommand) Test(org.junit.Test)

Example 4 with NewOrganization

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

the class OrganizationServiceTest method shouldCreate.

@Test
public void shouldCreate() {
    when(organizationRepository.findById(ORGANIZATION_ID)).thenReturn(Maybe.empty());
    when(organizationRepository.create(argThat(organization -> organization.getId().equals(ORGANIZATION_ID)))).thenAnswer(i -> Single.just(i.getArgument(0)));
    when(roleService.createDefaultRoles(ORGANIZATION_ID)).thenReturn(Completable.complete());
    when(entrypointService.createDefaults(any(Organization.class))).thenReturn(Flowable.just(new Entrypoint()));
    NewOrganization newOrganization = new NewOrganization();
    newOrganization.setName("TestName");
    newOrganization.setDescription("TestDescription");
    newOrganization.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    newOrganization.setHrids(Collections.singletonList("testOrgHRID"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Organization> obs = cut.createOrUpdate(ORGANIZATION_ID, newOrganization, createdBy).test();
    obs.awaitTerminalEvent();
    obs.assertValue(organization -> {
        assertEquals(ORGANIZATION_ID, organization.getId());
        assertEquals(newOrganization.getName(), organization.getName());
        assertEquals(newOrganization.getDescription(), organization.getDescription());
        assertEquals(newOrganization.getDomainRestrictions(), organization.getDomainRestrictions());
        assertEquals(newOrganization.getHrids(), organization.getHrids());
        return true;
    });
    verify(auditService, times(1)).report(argThat(builder -> {
        Audit audit = builder.build(new ObjectMapper());
        assertEquals(ReferenceType.ORGANIZATION, audit.getReferenceType());
        assertEquals(ORGANIZATION_ID, audit.getReferenceId());
        assertEquals(createdBy.getId(), audit.getActor().getId());
        assertEquals(EventType.ORGANIZATION_CREATED, audit.getType());
        assertEquals(Status.SUCCESS, audit.getOutcome().getStatus());
        return true;
    }));
}
Also used : OrganizationNotFoundException(io.gravitee.am.service.exception.OrganizationNotFoundException) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) OrganizationServiceImpl(io.gravitee.am.service.impl.OrganizationServiceImpl) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Entrypoint(io.gravitee.am.model.Entrypoint) Flowable(io.reactivex.Flowable) Status(io.gravitee.am.common.audit.Status) ReferenceType(io.gravitee.am.model.ReferenceType) NewOrganization(io.gravitee.am.service.model.NewOrganization) Before(org.junit.Before) OrganizationRepository(io.gravitee.am.repository.management.api.OrganizationRepository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) EventType(io.gravitee.am.common.audit.EventType) PatchOrganization(io.gravitee.am.service.model.PatchOrganization) Mockito(org.mockito.Mockito) Organization(io.gravitee.am.model.Organization) List(java.util.List) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Audit(io.gravitee.am.reporter.api.audit.model.Audit) NewOrganization(io.gravitee.am.service.model.NewOrganization) Audit(io.gravitee.am.reporter.api.audit.model.Audit) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) NewOrganization(io.gravitee.am.service.model.NewOrganization) PatchOrganization(io.gravitee.am.service.model.PatchOrganization) Organization(io.gravitee.am.model.Organization) Entrypoint(io.gravitee.am.model.Entrypoint) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with NewOrganization

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

the class OrganizationServiceTest method shouldCreate_error.

@Test
public void shouldCreate_error() {
    when(organizationRepository.findById(ORGANIZATION_ID)).thenReturn(Maybe.empty());
    when(organizationRepository.create(argThat(organization -> organization.getId().equals(ORGANIZATION_ID)))).thenReturn(Single.error(new TechnicalManagementException()));
    NewOrganization newOrganization = new NewOrganization();
    newOrganization.setName("TestName");
    newOrganization.setDescription("TestDescription");
    newOrganization.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Organization> obs = cut.createOrUpdate(ORGANIZATION_ID, newOrganization, createdBy).test();
    obs.awaitTerminalEvent();
    obs.assertError(TechnicalManagementException.class);
    verify(auditService, times(1)).report(argThat(builder -> {
        Audit audit = builder.build(new ObjectMapper());
        assertEquals(ReferenceType.ORGANIZATION, audit.getReferenceType());
        assertEquals(ORGANIZATION_ID, audit.getReferenceId());
        assertEquals(createdBy.getId(), audit.getActor().getId());
        assertEquals(EventType.ORGANIZATION_CREATED, audit.getType());
        assertEquals(Status.FAILURE, audit.getOutcome().getStatus());
        return true;
    }));
}
Also used : OrganizationNotFoundException(io.gravitee.am.service.exception.OrganizationNotFoundException) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) Completable(io.reactivex.Completable) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) OrganizationServiceImpl(io.gravitee.am.service.impl.OrganizationServiceImpl) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Entrypoint(io.gravitee.am.model.Entrypoint) Flowable(io.reactivex.Flowable) Status(io.gravitee.am.common.audit.Status) ReferenceType(io.gravitee.am.model.ReferenceType) NewOrganization(io.gravitee.am.service.model.NewOrganization) Before(org.junit.Before) OrganizationRepository(io.gravitee.am.repository.management.api.OrganizationRepository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) EventType(io.gravitee.am.common.audit.EventType) PatchOrganization(io.gravitee.am.service.model.PatchOrganization) Mockito(org.mockito.Mockito) Organization(io.gravitee.am.model.Organization) List(java.util.List) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Audit(io.gravitee.am.reporter.api.audit.model.Audit) NewOrganization(io.gravitee.am.service.model.NewOrganization) Audit(io.gravitee.am.reporter.api.audit.model.Audit) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) NewOrganization(io.gravitee.am.service.model.NewOrganization) PatchOrganization(io.gravitee.am.service.model.PatchOrganization) Organization(io.gravitee.am.model.Organization) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

NewOrganization (io.gravitee.am.service.model.NewOrganization)6 Single (io.reactivex.Single)6 Organization (io.gravitee.am.model.Organization)5 TechnicalException (io.gravitee.am.repository.exceptions.TechnicalException)5 TestObserver (io.reactivex.observers.TestObserver)5 Collections (java.util.Collections)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Before (org.junit.Before)5 Test (org.junit.Test)5 RunWith (org.junit.runner.RunWith)5 Mock (org.mockito.Mock)5 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 EventType (io.gravitee.am.common.audit.EventType)4 Status (io.gravitee.am.common.audit.Status)4 DefaultUser (io.gravitee.am.identityprovider.api.DefaultUser)4 Entrypoint (io.gravitee.am.model.Entrypoint)4 ReferenceType (io.gravitee.am.model.ReferenceType)4 Audit (io.gravitee.am.reporter.api.audit.model.Audit)4 OrganizationRepository (io.gravitee.am.repository.management.api.OrganizationRepository)4