Search in sources :

Example 1 with NewEnvironment

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

the class EnvironmentServiceTest method shouldCreate_update.

@Test
public void shouldCreate_update() {
    Environment existingEnvironment = new Environment();
    existingEnvironment.setId(ENVIRONMENT_ID);
    existingEnvironment.setOrganizationId(ORGANIZATION_ID);
    when(environmentRepository.findById(ENVIRONMENT_ID, ORGANIZATION_ID)).thenReturn(Maybe.just(existingEnvironment));
    when(environmentRepository.update(argThat(environment -> environment.getId().equals(ENVIRONMENT_ID)))).thenAnswer(i -> Single.just(i.getArgument(0)));
    NewEnvironment newEnvironment = new NewEnvironment();
    newEnvironment.setName("TestName");
    newEnvironment.setDescription("TestDescription");
    newEnvironment.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    newEnvironment.setHrids(Collections.singletonList("testHRIDUpdated"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Environment> obs = cut.createOrUpdate(ORGANIZATION_ID, ENVIRONMENT_ID, newEnvironment, createdBy).test();
    obs.awaitTerminalEvent();
    obs.assertValue(environment -> {
        assertEquals(ENVIRONMENT_ID, environment.getId());
        assertEquals(newEnvironment.getName(), environment.getName());
        assertEquals(newEnvironment.getDescription(), environment.getDescription());
        assertEquals(newEnvironment.getDomainRestrictions(), environment.getDomainRestrictions());
        assertEquals(newEnvironment.getHrids(), environment.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.ENVIRONMENT_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) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) Environment(io.gravitee.am.model.Environment) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) EnvironmentRepository(io.gravitee.am.repository.management.api.EnvironmentRepository) EnvironmentServiceImpl(io.gravitee.am.service.impl.EnvironmentServiceImpl) Status(io.gravitee.am.common.audit.Status) ReferenceType(io.gravitee.am.model.ReferenceType) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Before(org.junit.Before) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) EventType(io.gravitee.am.common.audit.EventType) Mockito(org.mockito.Mockito) Organization(io.gravitee.am.model.Organization) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) EnvironmentNotFoundException(io.gravitee.am.service.exception.EnvironmentNotFoundException) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Audit(io.gravitee.am.reporter.api.audit.model.Audit) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) Audit(io.gravitee.am.reporter.api.audit.model.Audit) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Environment(io.gravitee.am.model.Environment) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 2 with NewEnvironment

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

the class EnvironmentServiceTest method shouldCreate_organizationNotFound.

@Test
public void shouldCreate_organizationNotFound() {
    when(environmentRepository.findById(ENVIRONMENT_ID, ORGANIZATION_ID)).thenReturn(Maybe.empty());
    when(organizationService.findById(ORGANIZATION_ID)).thenReturn(Single.error(new OrganizationNotFoundException(ORGANIZATION_ID)));
    NewEnvironment newEnvironment = new NewEnvironment();
    newEnvironment.setName("TestName");
    newEnvironment.setDescription("TestDescription");
    newEnvironment.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Environment> obs = cut.createOrUpdate(ORGANIZATION_ID, ENVIRONMENT_ID, newEnvironment, createdBy).test();
    obs.awaitTerminalEvent();
    obs.assertError(OrganizationNotFoundException.class);
    verifyZeroInteractions(auditService);
}
Also used : NewEnvironment(io.gravitee.am.service.model.NewEnvironment) OrganizationNotFoundException(io.gravitee.am.service.exception.OrganizationNotFoundException) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Environment(io.gravitee.am.model.Environment) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) Test(org.junit.Test)

Example 3 with NewEnvironment

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

the class EnvironmentServiceTest method shouldCreate_error.

@Test
public void shouldCreate_error() {
    Organization organization = new Organization();
    organization.setId(ORGANIZATION_ID);
    when(environmentRepository.findById(ENVIRONMENT_ID, ORGANIZATION_ID)).thenReturn(Maybe.empty());
    when(organizationService.findById(ORGANIZATION_ID)).thenReturn(Single.just(organization));
    when(environmentRepository.create(argThat(environment -> environment.getId().equals(ENVIRONMENT_ID)))).thenReturn(Single.error(new TechnicalManagementException()));
    NewEnvironment newEnvironment = new NewEnvironment();
    newEnvironment.setName("TestName");
    newEnvironment.setDescription("TestDescription");
    newEnvironment.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Environment> obs = cut.createOrUpdate(ORGANIZATION_ID, ENVIRONMENT_ID, newEnvironment, 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.ENVIRONMENT_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) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) Environment(io.gravitee.am.model.Environment) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) EnvironmentRepository(io.gravitee.am.repository.management.api.EnvironmentRepository) EnvironmentServiceImpl(io.gravitee.am.service.impl.EnvironmentServiceImpl) Status(io.gravitee.am.common.audit.Status) ReferenceType(io.gravitee.am.model.ReferenceType) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Before(org.junit.Before) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) EventType(io.gravitee.am.common.audit.EventType) Mockito(org.mockito.Mockito) Organization(io.gravitee.am.model.Organization) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) EnvironmentNotFoundException(io.gravitee.am.service.exception.EnvironmentNotFoundException) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Audit(io.gravitee.am.reporter.api.audit.model.Audit) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) Audit(io.gravitee.am.reporter.api.audit.model.Audit) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Organization(io.gravitee.am.model.Organization) Environment(io.gravitee.am.model.Environment) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with NewEnvironment

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

the class EnvironmentServiceTest method shouldCreate_updateError.

@Test
public void shouldCreate_updateError() {
    Environment existingEnvironment = new Environment();
    existingEnvironment.setId(ENVIRONMENT_ID);
    existingEnvironment.setOrganizationId(ORGANIZATION_ID);
    when(environmentRepository.findById(ENVIRONMENT_ID, ORGANIZATION_ID)).thenReturn(Maybe.just(existingEnvironment));
    when(environmentRepository.update(argThat(environment -> environment.getId().equals(ENVIRONMENT_ID)))).thenReturn(Single.error(new TechnicalManagementException()));
    NewEnvironment newEnvironment = new NewEnvironment();
    newEnvironment.setName("TestName");
    newEnvironment.setDescription("TestDescription");
    newEnvironment.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Environment> obs = cut.createOrUpdate(ORGANIZATION_ID, ENVIRONMENT_ID, newEnvironment, 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.ENVIRONMENT_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) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) Environment(io.gravitee.am.model.Environment) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) EnvironmentRepository(io.gravitee.am.repository.management.api.EnvironmentRepository) EnvironmentServiceImpl(io.gravitee.am.service.impl.EnvironmentServiceImpl) Status(io.gravitee.am.common.audit.Status) ReferenceType(io.gravitee.am.model.ReferenceType) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Before(org.junit.Before) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) EventType(io.gravitee.am.common.audit.EventType) Mockito(org.mockito.Mockito) Organization(io.gravitee.am.model.Organization) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) EnvironmentNotFoundException(io.gravitee.am.service.exception.EnvironmentNotFoundException) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Audit(io.gravitee.am.reporter.api.audit.model.Audit) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) Audit(io.gravitee.am.reporter.api.audit.model.Audit) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Environment(io.gravitee.am.model.Environment) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 5 with NewEnvironment

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

the class EnvironmentServiceTest method shouldCreate.

@Test
public void shouldCreate() {
    Organization organization = new Organization();
    organization.setId(ORGANIZATION_ID);
    when(environmentRepository.findById(ENVIRONMENT_ID, ORGANIZATION_ID)).thenReturn(Maybe.empty());
    when(organizationService.findById(ORGANIZATION_ID)).thenReturn(Single.just(organization));
    when(environmentRepository.create(argThat(environment -> environment.getId().equals(ENVIRONMENT_ID)))).thenAnswer(i -> Single.just(i.getArgument(0)));
    NewEnvironment newEnvironment = new NewEnvironment();
    newEnvironment.setName("TestName");
    newEnvironment.setDescription("TestDescription");
    newEnvironment.setDomainRestrictions(Collections.singletonList("TestDomainRestriction"));
    newEnvironment.setHrids(Collections.singletonList("testEnvHRID"));
    DefaultUser createdBy = new DefaultUser("test");
    createdBy.setId(USER_ID);
    TestObserver<Environment> obs = cut.createOrUpdate(ORGANIZATION_ID, ENVIRONMENT_ID, newEnvironment, createdBy).test();
    obs.awaitTerminalEvent();
    obs.assertValue(environment -> {
        assertEquals(ORGANIZATION_ID, environment.getOrganizationId());
        assertEquals(newEnvironment.getName(), environment.getName());
        assertEquals(newEnvironment.getDescription(), environment.getDescription());
        assertEquals(newEnvironment.getDomainRestrictions(), environment.getDomainRestrictions());
        assertEquals(newEnvironment.getHrids(), environment.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.ENVIRONMENT_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) Maybe(io.reactivex.Maybe) RunWith(org.junit.runner.RunWith) Environment(io.gravitee.am.model.Environment) TechnicalException(io.gravitee.am.repository.exceptions.TechnicalException) Single(io.reactivex.Single) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) TechnicalManagementException(io.gravitee.am.service.exception.TechnicalManagementException) Flowable(io.reactivex.Flowable) EnvironmentRepository(io.gravitee.am.repository.management.api.EnvironmentRepository) EnvironmentServiceImpl(io.gravitee.am.service.impl.EnvironmentServiceImpl) Status(io.gravitee.am.common.audit.Status) ReferenceType(io.gravitee.am.model.ReferenceType) TestSubscriber(io.reactivex.subscribers.TestSubscriber) Before(org.junit.Before) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TestObserver(io.reactivex.observers.TestObserver) Test(org.junit.Test) EventType(io.gravitee.am.common.audit.EventType) Mockito(org.mockito.Mockito) Organization(io.gravitee.am.model.Organization) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) EnvironmentNotFoundException(io.gravitee.am.service.exception.EnvironmentNotFoundException) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Audit(io.gravitee.am.reporter.api.audit.model.Audit) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) Audit(io.gravitee.am.reporter.api.audit.model.Audit) DefaultUser(io.gravitee.am.identityprovider.api.DefaultUser) Organization(io.gravitee.am.model.Organization) Environment(io.gravitee.am.model.Environment) NewEnvironment(io.gravitee.am.service.model.NewEnvironment) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

NewEnvironment (io.gravitee.am.service.model.NewEnvironment)7 Environment (io.gravitee.am.model.Environment)6 Single (io.reactivex.Single)6 Test (org.junit.Test)6 DefaultUser (io.gravitee.am.identityprovider.api.DefaultUser)5 TechnicalException (io.gravitee.am.repository.exceptions.TechnicalException)5 OrganizationNotFoundException (io.gravitee.am.service.exception.OrganizationNotFoundException)5 TestObserver (io.reactivex.observers.TestObserver)5 Collections (java.util.Collections)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Before (org.junit.Before)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 Organization (io.gravitee.am.model.Organization)4 ReferenceType (io.gravitee.am.model.ReferenceType)4 Audit (io.gravitee.am.reporter.api.audit.model.Audit)4