Search in sources :

Example 1 with UpdateEnvironmentEntity

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

the class EnvironmentCommandHandler method handle.

@Override
public Single<EnvironmentReply> handle(EnvironmentCommand command) {
    EnvironmentPayload environmentPayload = command.getPayload();
    try {
        UpdateEnvironmentEntity newEnvironment = new UpdateEnvironmentEntity();
        newEnvironment.setCockpitId(environmentPayload.getCockpitId());
        newEnvironment.setHrids(environmentPayload.getHrids());
        newEnvironment.setName(environmentPayload.getName());
        newEnvironment.setDescription(environmentPayload.getDescription());
        newEnvironment.setDomainRestrictions(environmentPayload.getDomainRestrictions());
        final EnvironmentEntity environment = environmentService.createOrUpdate(environmentPayload.getOrganizationId(), environmentPayload.getId(), newEnvironment);
        logger.info("Environment [{}] handled with id [{}].", environment.getName(), environment.getId());
        return Single.just(new EnvironmentReply(command.getId(), CommandStatus.SUCCEEDED));
    } catch (Exception e) {
        logger.error("Error occurred when handling environment [{}] with id [{}].", environmentPayload.getName(), environmentPayload.getId(), e);
        return Single.just(new EnvironmentReply(command.getId(), CommandStatus.ERROR));
    }
}
Also used : UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentReply(io.gravitee.cockpit.api.command.environment.EnvironmentReply) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) EnvironmentEntity(io.gravitee.rest.api.model.EnvironmentEntity) EnvironmentPayload(io.gravitee.cockpit.api.command.environment.EnvironmentPayload)

Example 2 with UpdateEnvironmentEntity

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

the class CockpitIdUpgrader method upgrade.

@Override
public boolean upgrade() {
    Collection<OrganizationEntity> organizations = organizationService.findAll();
    organizations.stream().filter(org -> !org.getId().equals(GraviteeContext.getDefaultOrganization()) && org.getCockpitId() == null).forEach(org -> {
        UpdateOrganizationEntity newOrganization = new UpdateOrganizationEntity(org);
        newOrganization.setCockpitId(org.getId());
        organizationService.update(org.getId(), newOrganization);
    });
    organizations.forEach(org -> environmentService.findByOrganization(org.getId()).stream().filter(env -> !env.getId().equals(GraviteeContext.getDefaultEnvironment()) && env.getCockpitId() == null).forEach(env -> {
        UpdateEnvironmentEntity updateEnv = new UpdateEnvironmentEntity(env);
        updateEnv.setCockpitId(env.getId());
        environmentService.createOrUpdate(org.getId(), env.getId(), updateEnv);
    }));
    return true;
}
Also used : UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) Upgrader(io.gravitee.rest.api.service.Upgrader) Ordered(org.springframework.core.Ordered) Component(org.springframework.stereotype.Component) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) Collection(java.util.Collection) EnvironmentService(io.gravitee.rest.api.service.EnvironmentService) Autowired(org.springframework.beans.factory.annotation.Autowired) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) OrganizationService(io.gravitee.rest.api.service.OrganizationService) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity)

Example 3 with UpdateEnvironmentEntity

use of io.gravitee.rest.api.model.UpdateEnvironmentEntity 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 4 with UpdateEnvironmentEntity

use of io.gravitee.rest.api.model.UpdateEnvironmentEntity 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)

Example 5 with UpdateEnvironmentEntity

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

the class EnvironmentService_CreateTest method shouldHaveBadOrganizationExceptionWhenNoOrganizationInEntity.

@Test(expected = OrganizationNotFoundException.class)
public void shouldHaveBadOrganizationExceptionWhenNoOrganizationInEntity() {
    when(mockOrganizationService.findById("UNKNOWN")).thenThrow(new OrganizationNotFoundException("UNKNOWN"));
    environmentService.createOrUpdate("UNKNOWN", "env_id", new UpdateEnvironmentEntity());
}
Also used : OrganizationNotFoundException(io.gravitee.rest.api.service.exceptions.OrganizationNotFoundException) UpdateEnvironmentEntity(io.gravitee.rest.api.model.UpdateEnvironmentEntity) Test(org.junit.Test)

Aggregations

UpdateEnvironmentEntity (io.gravitee.rest.api.model.UpdateEnvironmentEntity)5 EnvironmentEntity (io.gravitee.rest.api.model.EnvironmentEntity)3 OrganizationNotFoundException (io.gravitee.rest.api.service.exceptions.OrganizationNotFoundException)3 Test (org.junit.Test)3 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)2 EnvironmentRepository (io.gravitee.repository.management.api.EnvironmentRepository)2 Environment (io.gravitee.repository.management.model.Environment)2 BadOrganizationException (io.gravitee.rest.api.service.exceptions.BadOrganizationException)2 EnvironmentServiceImpl (io.gravitee.rest.api.service.impl.EnvironmentServiceImpl)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)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 Mockito (org.mockito.Mockito)2 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)2