Search in sources :

Example 1 with Environment

use of io.gravitee.repository.management.model.Environment in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentService_FindByUserAndIdOrHridTest method getEnvironments.

public Set<Environment> getEnvironments() {
    Environment env1 = new Environment();
    env1.setId("envId");
    env1.setHrids(Arrays.asList("env1", "1env"));
    Environment env2 = new Environment();
    env2.setId("env2Id");
    env2.setHrids(Arrays.asList("env2", "2env"));
    HashSet<Environment> envSet = new HashSet<>();
    envSet.add(env1);
    envSet.add(env2);
    return envSet;
}
Also used : Environment(io.gravitee.repository.management.model.Environment) HashSet(java.util.HashSet)

Example 2 with Environment

use of io.gravitee.repository.management.model.Environment in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentServiceImpl method createOrUpdate.

@Override
public EnvironmentEntity createOrUpdate(String organizationId, String environmentId, final UpdateEnvironmentEntity environmentEntity) {
    try {
        // First we check that organization exists
        this.organizationService.findById(organizationId);
        Optional<Environment> environmentOptional = environmentRepository.findById(environmentId);
        Environment environment = convert(environmentEntity);
        environment.setId(environmentId);
        environment.setOrganizationId(organizationId);
        if (environmentOptional.isPresent()) {
            return convert(environmentRepository.update(environment));
        } else {
            EnvironmentEntity createdEnvironment = convert(environmentRepository.create(environment));
            // create Default items for environment
            apiHeaderService.initialize(createdEnvironment.getId());
            pageService.initialize(createdEnvironment.getId());
            return createdEnvironment;
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update environment {}", environmentEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to update environment " + environmentEntity.getName(), ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Environment(io.gravitee.repository.management.model.Environment) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 3 with Environment

use of io.gravitee.repository.management.model.Environment in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentServiceImpl method initialize.

@Override
public void initialize() {
    Environment defaultEnvironment = new Environment();
    defaultEnvironment.setId(GraviteeContext.getDefaultEnvironment());
    defaultEnvironment.setName("Default environment");
    defaultEnvironment.setDescription("Default environment");
    defaultEnvironment.setOrganizationId(GraviteeContext.getDefaultOrganization());
    try {
        environmentRepository.create(defaultEnvironment);
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create default environment", ex);
        throw new TechnicalManagementException("An error occurs while trying to create default environment", ex);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Environment(io.gravitee.repository.management.model.Environment) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with Environment

use of io.gravitee.repository.management.model.Environment in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentService_CreateTest method shouldCreateEnvironment.

@Test
public void shouldCreateEnvironment() throws TechnicalException {
    when(mockOrganizationService.findById(any())).thenReturn(null);
    when(mockEnvironmentRepository.findById(any())).thenReturn(Optional.empty());
    UpdateEnvironmentEntity env1 = new UpdateEnvironmentEntity();
    env1.setHrids(Arrays.asList("envhrid"));
    env1.setName("env_name");
    env1.setDescription("env_desc");
    List<String> domainRestrictions = Arrays.asList("domain", "restriction");
    env1.setDomainRestrictions(domainRestrictions);
    Environment createdEnv = new Environment();
    createdEnv.setId("env_id");
    when(mockEnvironmentRepository.create(any())).thenReturn(createdEnv);
    EnvironmentEntity environment = environmentService.createOrUpdate("DEFAULT", "env_id", env1);
    assertNotNull("result is null", environment);
    verify(mockEnvironmentRepository, times(1)).create(argThat(arg -> arg != null && arg.getHrids().equals(Arrays.asList("envhrid")) && arg.getName().equals("env_name") && arg.getDescription().equals("env_desc") && arg.getDomainRestrictions().equals(domainRestrictions) && arg.getOrganizationId().equals("DEFAULT")));
    verify(mockEnvironmentRepository, never()).update(any());
    verify(mockAPIHeaderService, times(1)).initialize("env_id");
    verify(mockPageService, times(1)).initialize("env_id");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EnvironmentRepository(io.gravitee.repository.management.api.EnvironmentRepository) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Mockito(org.mockito.Mockito) List(java.util.List) OrganizationNotFoundException(io.gravitee.rest.api.service.exceptions.OrganizationNotFoundException) BadOrganizationException(io.gravitee.rest.api.service.exceptions.BadOrganizationException) Environment(io.gravitee.repository.management.model.Environment) Optional(java.util.Optional) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) EnvironmentServiceImpl(io.gravitee.rest.api.service.impl.EnvironmentServiceImpl) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Environment(io.gravitee.repository.management.model.Environment) Test(org.junit.Test)

Example 5 with Environment

use of io.gravitee.repository.management.model.Environment in project gravitee-management-rest-api by gravitee-io.

the class EnvironmentService_CreateTest method shouldUpdateEnvironment.

@Test
public void shouldUpdateEnvironment() throws TechnicalException {
    when(mockOrganizationService.findById(any())).thenReturn(null);
    when(mockEnvironmentRepository.findById(any())).thenReturn(Optional.of(new Environment()));
    UpdateEnvironmentEntity env1 = new UpdateEnvironmentEntity();
    env1.setHrids(Arrays.asList("envhrid"));
    env1.setName("env_name");
    env1.setDescription("env_desc");
    List<String> domainRestrictions = Arrays.asList("domain", "restriction");
    env1.setDomainRestrictions(domainRestrictions);
    Environment updatedEnv = new Environment();
    when(mockEnvironmentRepository.update(any())).thenReturn(updatedEnv);
    EnvironmentEntity environment = environmentService.createOrUpdate("DEFAULT", "env_id", env1);
    assertNotNull("result is null", environment);
    verify(mockEnvironmentRepository, times(1)).update(argThat(arg -> arg != null && arg.getHrids().equals(Arrays.asList("envhrid")) && arg.getName().equals("env_name") && arg.getDescription().equals("env_desc") && arg.getDomainRestrictions().equals(domainRestrictions) && arg.getOrganizationId().equals("DEFAULT")));
    verify(mockEnvironmentRepository, never()).create(any());
    verify(mockAPIHeaderService, never()).initialize("env_id");
    verify(mockPageService, never()).initialize("env_id");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) Arrays(java.util.Arrays) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) Mock(org.mockito.Mock) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) EnvironmentRepository(io.gravitee.repository.management.api.EnvironmentRepository) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Mockito(org.mockito.Mockito) List(java.util.List) OrganizationNotFoundException(io.gravitee.rest.api.service.exceptions.OrganizationNotFoundException) BadOrganizationException(io.gravitee.rest.api.service.exceptions.BadOrganizationException) Environment(io.gravitee.repository.management.model.Environment) Optional(java.util.Optional) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) EnvironmentServiceImpl(io.gravitee.rest.api.service.impl.EnvironmentServiceImpl) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) Environment(io.gravitee.repository.management.model.Environment) Test(org.junit.Test)

Aggregations

Environment (io.gravitee.repository.management.model.Environment)8 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)6 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)4 EnvironmentRepository (io.gravitee.repository.management.api.EnvironmentRepository)3 List (java.util.List)3 Optional (java.util.Optional)3 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)2 UpdateEnvironmentEntity (io.gravitee.rest.api.model.UpdateEnvironmentEntity)2 BadOrganizationException (io.gravitee.rest.api.service.exceptions.BadOrganizationException)2 EnvironmentNotFoundException (io.gravitee.rest.api.service.exceptions.EnvironmentNotFoundException)2 OrganizationNotFoundException (io.gravitee.rest.api.service.exceptions.OrganizationNotFoundException)2 EnvironmentServiceImpl (io.gravitee.rest.api.service.impl.EnvironmentServiceImpl)2 Arrays (java.util.Arrays)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)2 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)2 InjectMocks (org.mockito.InjectMocks)2 Mock (org.mockito.Mock)2