Search in sources :

Example 1 with UpdateOrganizationEntity

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

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

the class OrganizationCommandHandler method handle.

@Override
public Single<OrganizationReply> handle(OrganizationCommand command) {
    OrganizationPayload organizationPayload = command.getPayload();
    try {
        UpdateOrganizationEntity newOrganization = new UpdateOrganizationEntity();
        newOrganization.setCockpitId(organizationPayload.getCockpitId());
        newOrganization.setHrids(organizationPayload.getHrids());
        newOrganization.setName(organizationPayload.getName());
        newOrganization.setDescription(organizationPayload.getDescription());
        newOrganization.setDomainRestrictions(organizationPayload.getDomainRestrictions());
        final OrganizationEntity organization = organizationService.createOrUpdate(organizationPayload.getId(), newOrganization);
        logger.info("Organization [{}] handled with id [{}].", organization.getName(), organization.getId());
        return Single.just(new OrganizationReply(command.getId(), CommandStatus.SUCCEEDED));
    } catch (Exception e) {
        logger.error("Error occurred when handling organization [{}] with id [{}].", organizationPayload.getName(), organizationPayload.getId(), e);
        return Single.just(new OrganizationReply(command.getId(), CommandStatus.ERROR));
    }
}
Also used : OrganizationPayload(io.gravitee.cockpit.api.command.organization.OrganizationPayload) OrganizationReply(io.gravitee.cockpit.api.command.organization.OrganizationReply) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity)

Example 3 with UpdateOrganizationEntity

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

the class OrganizationServiceImpl method update.

@Override
public OrganizationEntity update(String organizationId, final UpdateOrganizationEntity organizationEntity) {
    try {
        Optional<Organization> organizationOptional = organizationRepository.findById(organizationId);
        if (organizationOptional.isPresent()) {
            flowService.save(FlowReferenceType.ORGANIZATION, organizationId, organizationEntity.getFlows());
            Organization organization = convert(organizationEntity);
            organization.setId(organizationId);
            OrganizationEntity updatedOrganization = convert(organizationRepository.update(organization));
            createPublishOrganizationEvent(updatedOrganization);
            return updatedOrganization;
        } else {
            throw new OrganizationNotFoundException(organizationId);
        }
    } catch (TechnicalException | JsonProcessingException ex) {
        LOGGER.error("An error occurs while trying to update organization {}", organizationEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to update organization " + organizationEntity.getName(), ex);
    }
}
Also used : OrganizationNotFoundException(io.gravitee.rest.api.service.exceptions.OrganizationNotFoundException) Organization(io.gravitee.repository.management.model.Organization) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 4 with UpdateOrganizationEntity

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

the class OrganizationService_CreateTest method shouldUpdateOrganization.

@Test
public void shouldUpdateOrganization() throws TechnicalException {
    when(mockOrganizationRepository.findById(any())).thenReturn(Optional.of(new Organization()));
    UpdateOrganizationEntity org1 = new UpdateOrganizationEntity();
    org1.setHrids(Arrays.asList("orgid"));
    org1.setName("org_name");
    org1.setDescription("org_desc");
    List<String> domainRestrictions = Arrays.asList("domain", "restriction");
    org1.setDomainRestrictions(domainRestrictions);
    org1.setFlows(Arrays.asList(mock(Flow.class)));
    Organization createdOrganization = new Organization();
    createdOrganization.setId("org_id");
    when(mockOrganizationRepository.update(any())).thenReturn(createdOrganization);
    when(mockFlowService.findByReference(FlowReferenceType.ORGANIZATION, "org_id")).thenReturn(org1.getFlows());
    OrganizationEntity organization = organizationService.createOrUpdate("org_id", org1);
    assertNotNull("result is null", organization);
    verify(mockOrganizationRepository, times(1)).update(argThat(arg -> arg != null && arg.getName().equals("org_name") && arg.getDescription().equals("org_desc") && arg.getDomainRestrictions().equals(domainRestrictions)));
    verify(mockOrganizationRepository, never()).create(any());
    verify(mockRoleService, never()).initialize("org_id");
    verify(mockRoleService, never()).createOrUpdateSystemRoles("org_id");
    verify(mockFlowService, times(1)).save(FlowReferenceType.ORGANIZATION, "org_id", org1.getFlows());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) Arrays(java.util.Arrays) Organization(io.gravitee.repository.management.model.Organization) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) Mock(org.mockito.Mock) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Flow(io.gravitee.definition.model.flow.Flow) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) FlowService(io.gravitee.rest.api.service.configuration.flow.FlowService) OrganizationServiceImpl(io.gravitee.rest.api.service.impl.OrganizationServiceImpl) FlowReferenceType(io.gravitee.repository.management.model.flow.FlowReferenceType) Optional(java.util.Optional) OrganizationRepository(io.gravitee.repository.management.api.OrganizationRepository) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Organization(io.gravitee.repository.management.model.Organization) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) Test(org.junit.Test)

Example 5 with UpdateOrganizationEntity

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

the class OrganizationService_CreateTest method shouldCreateOrganization.

@Test
public void shouldCreateOrganization() throws TechnicalException {
    when(mockOrganizationRepository.findById(any())).thenReturn(Optional.empty());
    UpdateOrganizationEntity org1 = new UpdateOrganizationEntity();
    org1.setHrids(Arrays.asList("orgid"));
    org1.setName("org_name");
    org1.setDescription("org_desc");
    List<String> domainRestrictions = Arrays.asList("domain", "restriction");
    org1.setDomainRestrictions(domainRestrictions);
    org1.setFlows(Arrays.asList());
    Organization createdOrganization = new Organization();
    createdOrganization.setId("org_id");
    when(mockOrganizationRepository.create(any())).thenReturn(createdOrganization);
    when(mockFlowService.findByReference(FlowReferenceType.ORGANIZATION, "org_id")).thenReturn(new ArrayList<>());
    OrganizationEntity organization = organizationService.createOrUpdate("org_id", org1);
    assertNotNull("result is null", organization);
    verify(mockOrganizationRepository, times(1)).create(argThat(arg -> arg != null && arg.getName().equals("org_name") && arg.getDescription().equals("org_desc") && arg.getDomainRestrictions().equals(domainRestrictions)));
    verify(mockOrganizationRepository, never()).update(any());
    verify(mockRoleService, times(1)).initialize("org_id");
    verify(mockRoleService, times(1)).createOrUpdateSystemRoles("org_id");
    verify(mockFlowService, times(1)).save(FlowReferenceType.ORGANIZATION, "org_id", Arrays.asList());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) InjectMocks(org.mockito.InjectMocks) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) Arrays(java.util.Arrays) Organization(io.gravitee.repository.management.model.Organization) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) Assert.assertNotNull(org.junit.Assert.assertNotNull) Mock(org.mockito.Mock) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Flow(io.gravitee.definition.model.flow.Flow) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) ArrayList(java.util.ArrayList) Mockito(org.mockito.Mockito) List(java.util.List) FlowService(io.gravitee.rest.api.service.configuration.flow.FlowService) OrganizationServiceImpl(io.gravitee.rest.api.service.impl.OrganizationServiceImpl) FlowReferenceType(io.gravitee.repository.management.model.flow.FlowReferenceType) Optional(java.util.Optional) OrganizationRepository(io.gravitee.repository.management.api.OrganizationRepository) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Organization(io.gravitee.repository.management.model.Organization) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) UpdateOrganizationEntity(io.gravitee.rest.api.model.UpdateOrganizationEntity) OrganizationEntity(io.gravitee.rest.api.model.OrganizationEntity) Test(org.junit.Test)

Aggregations

OrganizationEntity (io.gravitee.rest.api.model.OrganizationEntity)5 UpdateOrganizationEntity (io.gravitee.rest.api.model.UpdateOrganizationEntity)5 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)3 Organization (io.gravitee.repository.management.model.Organization)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Flow (io.gravitee.definition.model.flow.Flow)2 OrganizationRepository (io.gravitee.repository.management.api.OrganizationRepository)2 FlowReferenceType (io.gravitee.repository.management.model.flow.FlowReferenceType)2 FlowService (io.gravitee.rest.api.service.configuration.flow.FlowService)2 OrganizationServiceImpl (io.gravitee.rest.api.service.impl.OrganizationServiceImpl)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Optional (java.util.Optional)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