Commit 85c12046 authored by Anastasios Poimenidis's avatar Anastasios Poimenidis
Browse files

fix: simplify service catalog logic according to recent schema changes

parent 8a3ebaa8
Loading
Loading
Loading
Loading
+8 −6
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;

import io.quarkus.security.Authenticated;
import jakarta.annotation.security.PermitAll;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
@@ -42,10 +41,14 @@ public class ServiceCatalogResource {

    private static final Logger logger = Logger.getLogger(ServiceCatalogResource.class);

    @Inject ServiceCatalogService serviceCatalogService;
    private final ServiceCatalogService serviceCatalogService;

    public static final String SERVICE_CATALOG = "serviceCatalog";

    public ServiceCatalogResource(ServiceCatalogService serviceCatalogService) {
        this.serviceCatalogService = serviceCatalogService;
    }

    @POST
    @Path(ApplicationProperties.TMF_URL_VERSION + "/" + SERVICE_CATALOG)
    @Produces(MediaType.APPLICATION_JSON)
@@ -118,8 +121,7 @@ public class ServiceCatalogResource {
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = ErrorMessage.class)))
    public Response getById(@RestPath String id, @RestQuery String fields, @Context UriInfo uriInfo)
            throws Exception {
    public Response getById(@RestPath String id, @RestQuery String fields, @Context UriInfo uriInfo) {
        logger.info("Get service catalog with id : " + id);
        Map<String, String> criteriaParams = UtilService.getRequestedCriteriaForFiltering(uriInfo);
        ServiceCatalog serviceCatalog =
@@ -191,8 +193,8 @@ public class ServiceCatalogResource {
                            schema = @Schema(implementation = ErrorMessage.class)))
    public Response delete(@RestPath String id) {
        logger.info("Delete service catalog with id : " + id);
        ServiceCatalog deletedServiceCatalogue = serviceCatalogService.responseFromDeleteRequest(id);
        serviceCatalogService.responseFromDeleteRequest(id);
        logger.info("Successful delete service catalog with id : " + id);
        return Response.ok(deletedServiceCatalogue).build();
        return Response.noContent().build();
    }
}
+8 −6
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;

import io.quarkus.security.Authenticated;
import jakarta.annotation.security.PermitAll;
import jakarta.inject.Inject;
import jakarta.validation.Valid;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
@@ -42,10 +41,14 @@ public class ServiceCategoryResource {

    private static final Logger logger = Logger.getLogger(ServiceCategoryResource.class);

    @Inject ServiceCategoryService serviceCategoryService;
    private final ServiceCategoryService serviceCategoryService;

    public static final String SERVICE_CATEGORY = "serviceCategory";

    public ServiceCategoryResource(ServiceCategoryService serviceCategoryService) {
        this.serviceCategoryService = serviceCategoryService;
    }

    @POST
    @Path(ApplicationProperties.TMF_URL_VERSION + "/" + SERVICE_CATEGORY)
    @Produces(MediaType.APPLICATION_JSON)
@@ -118,8 +121,7 @@ public class ServiceCategoryResource {
                    @Content(
                            mediaType = APPLICATION_JSON,
                            schema = @Schema(implementation = ErrorMessage.class)))
    public Response getById(@RestPath String id, @RestQuery String fields, @Context UriInfo uriInfo)
            throws Exception {
    public Response getById(@RestPath String id, @RestQuery String fields, @Context UriInfo uriInfo) {
        logger.info("Get service category with id : " + id);
        Map<String, String> criteriaParams = UtilService.getRequestedCriteriaForFiltering(uriInfo);
        ServiceCategory category =
@@ -191,8 +193,8 @@ public class ServiceCategoryResource {
                            schema = @Schema(implementation = ErrorMessage.class)))
    public Response delete(@RestPath String id) {
        logger.info("Delete service category with id : " + id);
        ServiceCategory deletedServiceCategory = serviceCategoryService.responseFromDeleteRequest(id);
        serviceCategoryService.responseFromDeleteRequest(id);
        logger.info("Successful delete service category with id : " + id);
        return Response.ok(deletedServiceCategory).build();
        return Response.noContent().build();
    }
}
+66 −49
Original line number Diff line number Diff line
@@ -136,10 +136,8 @@ public class ServiceCatalogService {
        return serviceCatalogApiEntityToResponseMapper.serviceCatalogEntityToResponseDTO(updatedEntity);
    }

    public ServiceCatalog responseFromDeleteRequest(String serviceCatalogueId) {
        ServiceCatalogEntity serviceCatalogEntity = deleteServiceCatalog(serviceCatalogueId);
        return serviceCatalogApiEntityToResponseMapper.serviceCatalogEntityToResponseDTO(
                serviceCatalogEntity);
    public void responseFromDeleteRequest(String serviceCatalogueId) {
        deleteServiceCatalog(serviceCatalogueId);
    }

    @Transactional
@@ -311,56 +309,24 @@ public class ServiceCatalogService {
     *
     * @param relatedPartyId: the organization/individual/party id related to the catalogs
     */
    @Transactional
    public void cleanUpServiceCatalogsByPartyID(String relatedPartyId) {
        // find serviceCatalogs by party Id
        List<ServiceCatalogEntity> serviceCatalogsByPartyId =
                serviceCatalogRepository.findCatalogsByRelatedPartyId(relatedPartyId);

        List<String> serviceCatalogIdsForDeletion = new ArrayList<>();

        List<String> serviceCategoryIdsForDeletion = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(serviceCatalogsByPartyId)) {
            serviceCatalogsByPartyId.forEach(
                    serviceCatalogEntity -> {
                        int relatedPartySize = serviceCatalogEntity.getRelatedParties().size();
                        // this boolean condition is critical for shared catalogs among many organizations
                        boolean hasOnlyOneRelatedOrganization = relatedPartySize == 1;
                        // this boolean condition checks if all the categories are empty ( without any service
                        // candidates)
                        boolean allCategoriesAreEmpty = checkCatalogEmptyCategories(serviceCatalogEntity);

                        // if catalog has only one party relationship and all its categories are empty, proceed
                        // to removal
                        if (hasOnlyOneRelatedOrganization && allCategoriesAreEmpty) {
                            serviceCatalogIdsForDeletion.add(serviceCatalogEntity.getId());
                        } else {
                            removeRelatedPartyRelationShip(serviceCatalogEntity, relatedPartyId);
        CatalogAndCategoryDeletionIds catalogAndCategoryDeletionIds =
                extractCatalogsAndCategoryIdsForDeletions(relatedPartyId);
        // remove catalogs
        removeCatalogs(catalogAndCategoryDeletionIds.catalogIds());
        // remove categories
        removeCategories(catalogAndCategoryDeletionIds.categoryIds());
    }

                        Set<ServiceCategoryEntity> serviceCategories =
                                serviceCatalogEntity.getServiceCategories();

                        if (CollectionUtils.isNotEmpty(serviceCategories)) {
                            // collect category ids without any candidates
                            List<String> serviceCategoryIDs =
                                    serviceCategories.stream()
                                            .filter(getValidCategoryForDeletion())
                                            .map(BaseEntity::getId)
                                            .toList();

                            // add them on the list to be removed
                            serviceCategoryIdsForDeletion.addAll(serviceCategoryIDs);
                        }
                    });
    @Transactional
    public void removeCategories(List<String> serviceCategoryIdsForDeletion) {
        serviceCategoryIdsForDeletion.forEach(serviceCategoryService::deleteServiceCategory);
    }

        // remove catalogs
    @Transactional
    public void removeCatalogs(List<String> serviceCatalogIdsForDeletion) {
        serviceCatalogIdsForDeletion.forEach(this::deleteServiceCatalog);

        // remove categories
        serviceCategoryIdsForDeletion.forEach(serviceCategoryService::deleteServiceCategory);
    }

    private static Predicate<ServiceCategoryEntity>
@@ -456,4 +422,55 @@ public class ServiceCatalogService {
    private boolean serviceCatalogExistsByName(String name) {
        return serviceCatalogRepository.findByName(name).isPresent();
    }

    @Transactional
    public CatalogAndCategoryDeletionIds extractCatalogsAndCategoryIdsForDeletions(
            String relatedPartyId) {

        List<ServiceCatalogEntity> serviceCatalogsByPartyId =
                serviceCatalogRepository.findCatalogsByRelatedPartyId(relatedPartyId);

        List<String> serviceCatalogIdsForDeletion = new ArrayList<>();
        List<String> serviceCategoryIdsForDeletion = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(serviceCatalogsByPartyId)) {
            serviceCatalogsByPartyId.forEach(
                    serviceCatalogEntity -> {
                        int relatedPartySize = CollectionUtils.size(serviceCatalogEntity.getRelatedParties());

                        // Critical condition for shared catalogs
                        boolean hasOnlyOneRelatedOrganization = relatedPartySize == 1;

                        // Check if all categories in the catalog are empty
                        boolean allCategoriesAreEmpty = checkCatalogEmptyCategories(serviceCatalogEntity);

                        // If catalog has only one party relationship and all categories are empty, flag for
                        // removal
                        if (hasOnlyOneRelatedOrganization && allCategoriesAreEmpty) {
                            serviceCatalogIdsForDeletion.add(serviceCatalogEntity.getId());
                        } else {
                            removeRelatedPartyRelationShip(serviceCatalogEntity, relatedPartyId);
                        }

                        Set<ServiceCategoryEntity> serviceCategories =
                                serviceCatalogEntity.getServiceCategories();

                        if (CollectionUtils.isNotEmpty(serviceCategories)) {
                            // Collect category IDs without candidates
                            List<String> emptyCategoryIds =
                                    serviceCategories.stream()
                                            .filter(getValidCategoryForDeletion())
                                            .map(BaseEntity::getId)
                                            .toList();

                            serviceCategoryIdsForDeletion.addAll(emptyCategoryIds);
                        }
                    });
        }

        return new CatalogAndCategoryDeletionIds(
                serviceCatalogIdsForDeletion, serviceCategoryIdsForDeletion);
    }

    public record CatalogAndCategoryDeletionIds(List<String> catalogIds, List<String> categoryIds) {}
}
+2 −8
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ import org.etsi.osl.hypo.api.tmf.services.catalog.api.model.service.category.Ser
import org.etsi.osl.hypo.api.tmf.services.catalog.api.model.service.response.ServiceCandidateRef;
import org.etsi.osl.hypo.api.tmf.services.catalog.api.model.service.response.ServiceCategory;
import org.etsi.osl.hypo.api.tmf.services.catalog.api.repository.ServiceCandidateRepository;
import org.etsi.osl.hypo.api.tmf.services.catalog.api.repository.ServiceCatalogRepository;
import org.etsi.osl.hypo.api.tmf.services.catalog.api.repository.ServiceCategoryRepository;
import org.etsi.osl.hypo.api.tmf.services.catalog.schema.ServiceCandidateEntity;
import org.etsi.osl.hypo.api.tmf.services.catalog.schema.ServiceCategoryEntity;
@@ -36,7 +35,6 @@ public class ServiceCategoryService {
    private final ServiceCatalogApiRequestToEntityMapper serviceCatalogueApiRequestToEntityMapper;
    private final ServiceCatalogUpdateEntityMapper serviceCatalogUpdateEntityMapper;
    private final ServiceCategoryRepository serviceCategoryRepository;
    private final ServiceCatalogRepository serviceCatalogueRepository;
    private final ServiceCandidateRepository serviceCandidateRepository;
    private final ServiceCandidateService serviceCandidateService;

@@ -49,14 +47,12 @@ public class ServiceCategoryService {
            ServiceCatalogApiRequestToEntityMapper serviceCatalogueApiRequestToEntityMapper,
            ServiceCatalogUpdateEntityMapper serviceCatalogUpdateEntityMapper,
            ServiceCategoryRepository serviceCategoryRepository,
            ServiceCatalogRepository serviceCatalogueRepository,
            ServiceCandidateRepository serviceCandidateRepository,
            ServiceCandidateService serviceCandidateService) {
        this.serviceCatalogueApiEntityToResponseMapper = serviceCatalogueApiEntityToResponseMapper;
        this.serviceCatalogueApiRequestToEntityMapper = serviceCatalogueApiRequestToEntityMapper;
        this.serviceCatalogUpdateEntityMapper = serviceCatalogUpdateEntityMapper;
        this.serviceCategoryRepository = serviceCategoryRepository;
        this.serviceCatalogueRepository = serviceCatalogueRepository;
        this.serviceCandidateRepository = serviceCandidateRepository;
        this.serviceCandidateService = serviceCandidateService;
    }
@@ -114,10 +110,8 @@ public class ServiceCategoryService {
                updatedCategoryEntity);
    }

    public ServiceCategory responseFromDeleteRequest(String serviceCategoryId) {
        ServiceCategoryEntity deletedCategoryEntity = deleteServiceCategory(serviceCategoryId);
        return serviceCatalogueApiEntityToResponseMapper.serviceCategoryEntityToResponseDTO(
                deletedCategoryEntity);
    public void responseFromDeleteRequest(String serviceCategoryId) {
        deleteServiceCategory(serviceCategoryId);
    }

    public List<ServiceCategory> responseFromGetAllRequest(
+12 −15
Original line number Diff line number Diff line
@@ -456,23 +456,20 @@ class ServiceCatalogServiceTest {
                objectMapper.readValue(jsonObject.toString(), ServiceCatalogCreateRequest.class);
        ServiceCatalog serviceCatalog =
                serviceCatalogService.responseFromCreateRequest(serviceCatalogCreateRequest);
        String catalogId = serviceCatalog.getId();

        ServiceCatalog serviceCatalogFromDeleteRequest =
        serviceCatalogService.responseFromDeleteRequest(serviceCatalog.getId());
        // assert
        Optional<ServiceCatalogEntity> optionalServiceCatalogEntity =
                serviceCatalogService.findByCatalogId(catalogId);
        assertThat(optionalServiceCatalogEntity).isEmpty();

        assertThat(serviceCatalogFromDeleteRequest.getId()).isEqualTo(serviceCatalog.getId());
        assertThat(serviceCatalogFromDeleteRequest.getName())
                .isEqualTo(serviceCatalogCreateRequest.getName());
        assertThat(serviceCatalogFromDeleteRequest.getHref())
                .isEqualTo(serviceCatalogCreateRequest.getHref());
        assertThat(serviceCatalogFromDeleteRequest.getDescription())
                .isEqualTo(serviceCatalogCreateRequest.getDescription());
        assertThat(serviceCatalogFromDeleteRequest.getType())
                .isEqualTo(serviceCatalogCreateRequest.getType());
        assertThat(serviceCatalogFromDeleteRequest.getBaseType())
                .isEqualTo(serviceCatalogCreateRequest.getBaseType());
        assertThat(serviceCatalogFromDeleteRequest.getVersion())
                .isEqualTo(serviceCatalogCreateRequest.getVersion());
        List<ServiceCategoryEntity> serviceCategoryEntities =
                serviceCategoryService.findAllCategories();
        assertThat(serviceCategoryEntities).isNotNull().isNotEmpty().hasSize(1);

        List<RelatedPartyEntity> relatedPartyEntities = relatedPartyService.findAllRelatedParties();
        assertThat(relatedPartyEntities).isNotNull().isNotEmpty().hasSize(2);
    }

    @Test
Loading