Search in sources :

Example 26 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class GoodbyeCommandHandlerTest method handleWithException.

@Test
public void handleWithException() {
    final InstallationEntity installation = new InstallationEntity();
    installation.setId(INSTALLATION_ID);
    installation.getAdditionalInformation().put(CUSTOM_KEY, CUSTOM_VALUE);
    GoodbyeCommand command = new GoodbyeCommand();
    when(installationService.getOrInitialize()).thenReturn(installation);
    when(installationService.setAdditionalInformation(anyMap())).thenThrow(new TechnicalManagementException());
    when(promotionService.search(any(), any(), any())).thenReturn(new Page<>(emptyList(), 0, 0, 0));
    TestObserver<GoodbyeReply> obs = cut.handle(command).test();
    obs.awaitTerminalEvent();
    obs.assertValue(reply -> reply.getCommandId().equals(command.getId()) && reply.getCommandStatus().equals(CommandStatus.ERROR));
}
Also used : GoodbyeCommand(io.gravitee.cockpit.api.command.goodbye.GoodbyeCommand) InstallationEntity(io.gravitee.rest.api.model.InstallationEntity) GoodbyeReply(io.gravitee.cockpit.api.command.goodbye.GoodbyeReply) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) Test(org.junit.Test)

Example 27 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class CategoryServiceImpl method update.

@Override
public CategoryEntity update(String categoryId, UpdateCategoryEntity categoryEntity) {
    try {
        LOGGER.debug("Update Category {}", categoryId);
        Optional<Category> optCategoryToUpdate = categoryRepository.findById(categoryId);
        if (!optCategoryToUpdate.isPresent()) {
            throw new CategoryNotFoundException(categoryId);
        }
        final Category categoryToUpdate = optCategoryToUpdate.get();
        Category category = convert(categoryEntity, categoryToUpdate.getEnvironmentId());
        // If no new picture and the current picture url is not the default one, keep the current picture
        if (categoryEntity.getPicture() == null && categoryEntity.getPictureUrl() != null && categoryEntity.getPictureUrl().indexOf("?hash") > 0) {
            category.setPicture(categoryToUpdate.getPicture());
        }
        final Date updatedAt = new Date();
        category.setCreatedAt(categoryToUpdate.getCreatedAt());
        category.setUpdatedAt(updatedAt);
        CategoryEntity updatedCategory = convert(categoryRepository.update(category));
        auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_UPDATED, updatedAt, categoryToUpdate, category);
        return updatedCategory;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to update category {}", categoryEntity.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to update category " + categoryEntity.getName(), ex);
    }
}
Also used : Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 28 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class CategoryServiceImpl method create.

@Override
public CategoryEntity create(NewCategoryEntity newCategory) {
    // First we prevent the duplicate category name
    final List<CategoryEntity> categories = findAll();
    final Optional<CategoryEntity> optionalCategory = categories.stream().filter(v -> v.getName().equals((newCategory.getName()))).findAny();
    if (optionalCategory.isPresent()) {
        throw new DuplicateCategoryNameException(optionalCategory.get().getName());
    }
    try {
        // check if environment exists
        String environment = GraviteeContext.getCurrentEnvironment();
        this.environmentService.findById(environment);
        Category category = convert(newCategory);
        final Date createdAt = new Date();
        category.setCreatedAt(createdAt);
        category.setUpdatedAt(createdAt);
        category.setEnvironmentId(environment);
        category.setOrder(categories.size());
        CategoryEntity createdCategory = convert(categoryRepository.create(category));
        auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_CREATED, createdAt, null, category);
        return createdCategory;
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to create category {}", newCategory.getName(), ex);
        throw new TechnicalManagementException("An error occurs while trying to create category " + newCategory.getName(), ex);
    }
}
Also used : java.util(java.util) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) LoggerFactory(org.slf4j.LoggerFactory) CategoryNotFoundException(io.gravitee.rest.api.service.exceptions.CategoryNotFoundException) Autowired(org.springframework.beans.factory.annotation.Autowired) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) ApiService(io.gravitee.rest.api.service.ApiService) IdGenerator(io.gravitee.common.utils.IdGenerator) Category(io.gravitee.repository.management.model.Category) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) AuditEvent(io.gravitee.repository.management.model.Category.AuditEvent) DuplicateCategoryNameException(io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) UuidString(io.gravitee.rest.api.service.common.UuidString) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Logger(org.slf4j.Logger) EnvironmentService(io.gravitee.rest.api.service.EnvironmentService) AuditService(io.gravitee.rest.api.service.AuditService) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity) CategoryService(io.gravitee.rest.api.service.CategoryService) Collectors(java.util.stream.Collectors) CATEGORY(io.gravitee.repository.management.model.Audit.AuditProperties.CATEGORY) Component(org.springframework.stereotype.Component) CategoryRepository(io.gravitee.repository.management.api.CategoryRepository) DatatypeConverter(javax.xml.bind.DatatypeConverter) Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) DuplicateCategoryNameException(io.gravitee.rest.api.service.exceptions.DuplicateCategoryNameException) UuidString(io.gravitee.rest.api.service.common.UuidString) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 29 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException in project gravitee-management-rest-api by gravitee-io.

the class CategoryServiceImpl method update.

@Override
public List<CategoryEntity> update(final List<UpdateCategoryEntity> categoriesEntities) {
    final List<CategoryEntity> savedCategories = new ArrayList<>(categoriesEntities.size());
    categoriesEntities.forEach(categoryEntity -> {
        try {
            Optional<Category> optCategoryToUpdate = categoryRepository.findById(categoryEntity.getId());
            if (optCategoryToUpdate.isPresent()) {
                final Category categoryToUpdate = optCategoryToUpdate.get();
                Category category = convert(categoryEntity, categoryToUpdate.getEnvironmentId());
                // check if picture has been set
                if (category.getPicture() == null) {
                    // Picture can not be updated when re-ordering categories
                    category.setPicture(categoryToUpdate.getPicture());
                }
                // check if background has been set
                if (category.getBackground() == null) {
                    // Background can not be updated when re-ordering categories
                    category.setBackground(categoryToUpdate.getBackground());
                }
                final Date updatedAt = new Date();
                category.setCreatedAt(categoryToUpdate.getCreatedAt());
                category.setUpdatedAt(updatedAt);
                savedCategories.add(convert(categoryRepository.update(category)));
                auditService.createEnvironmentAuditLog(Collections.singletonMap(CATEGORY, category.getId()), CATEGORY_UPDATED, updatedAt, categoryToUpdate, category);
            }
        } catch (TechnicalException ex) {
            LOGGER.error("An error occurs while trying to update category {}", categoryEntity.getName(), ex);
            throw new TechnicalManagementException("An error occurs while trying to update category " + categoryEntity.getName(), ex);
        }
    });
    return savedCategories;
}
Also used : Category(io.gravitee.repository.management.model.Category) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException) NewCategoryEntity(io.gravitee.rest.api.model.NewCategoryEntity) CategoryEntity(io.gravitee.rest.api.model.CategoryEntity) UpdateCategoryEntity(io.gravitee.rest.api.model.UpdateCategoryEntity)

Example 30 with TechnicalManagementException

use of io.gravitee.rest.api.service.exceptions.TechnicalManagementException 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)

Aggregations

TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)149 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)120 UuidString (io.gravitee.rest.api.service.common.UuidString)26 Date (java.util.Date)23 Component (org.springframework.stereotype.Component)18 Logger (org.slf4j.Logger)17 LoggerFactory (org.slf4j.LoggerFactory)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)16 Collectors (java.util.stream.Collectors)13 Autowired (org.springframework.beans.factory.annotation.Autowired)13 IOException (java.io.IOException)12 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 Rating (io.gravitee.repository.management.model.Rating)9 ApiRatingUnavailableException (io.gravitee.rest.api.service.exceptions.ApiRatingUnavailableException)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 Dictionary (io.gravitee.repository.management.model.Dictionary)8 AuditService (io.gravitee.rest.api.service.AuditService)8 java.util (java.util)8 Theme (io.gravitee.repository.management.model.Theme)6 List (java.util.List)6