From 3021048c87d0cbe8fb0fe8ed7ccf971000f9ad56 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Tue, 2 Jul 2024 13:05:02 +0200 Subject: [PATCH] fix HsHostingAssetRepository.findAllByCriteriaImpl query --- .../asset/HsHostingAssetRepository.java | 26 ++++++++++++++----- .../hs-hosting/hs-hosting-assets.yaml | 4 +-- ...sHostingAssetControllerAcceptanceTest.java | 14 +--------- ...HostingAssetRepositoryIntegrationTest.java | 5 ++-- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepository.java b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepository.java index cefe79f6..571f484a 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepository.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepository.java @@ -14,12 +14,26 @@ public interface HsHostingAssetRepository extends Repository findByIdentifier(String assetIdentifier); - @Query(""" - SELECT asset FROM HsHostingAssetEntity asset - WHERE (:projectUuid IS NULL OR asset.bookingItem.project.uuid = :projectUuid) - AND (:parentAssetUuid IS NULL OR asset.parentAsset.uuid = :parentAssetUuid) - AND (:type IS NULL OR :type = CAST(asset.type AS String)) - """) + @Query(value = """ + select ha.uuid, + ha.alarmcontactuuid, + ha.assignedtoassetuuid, + ha.bookingitemuuid, + ha.caption, + ha.config, + ha.identifier, + ha.parentassetuuid, + ha.type, + ha.version + from hs_hosting_asset_rv ha + left join hs_booking_item bi on bi.uuid = ha.bookingitemuuid + left join hs_hosting_asset pha on pha.uuid = ha.parentassetuuid + where (:projectUuid is null or bi.projectuuid=:projectUuid) + and (:parentAssetUuid is null or pha.uuid=:parentAssetUuid) + and (:type is null or :type=cast(ha.type as text)) + """, nativeQuery = true) + // The JPQL query did not generate "left join" but just "join". + // I also optimized the query by not using the _rv for hs_booking_item and hs_hosting_asset, only for hs_hosting_asset_rv. List findAllByCriteriaImpl(UUID projectUuid, UUID parentAssetUuid, String type); default List findAllByCriteria(final UUID projectUuid, final UUID parentAssetUuid, final HsHostingAssetType type) { return findAllByCriteriaImpl(projectUuid, parentAssetUuid, HsHostingAssetType.asString(type)); diff --git a/src/main/resources/api-definition/hs-hosting/hs-hosting-assets.yaml b/src/main/resources/api-definition/hs-hosting/hs-hosting-assets.yaml index a08a36a1..8a208c68 100644 --- a/src/main/resources/api-definition/hs-hosting/hs-hosting-assets.yaml +++ b/src/main/resources/api-definition/hs-hosting/hs-hosting-assets.yaml @@ -7,13 +7,13 @@ get: parameters: - $ref: 'auth.yaml#/components/parameters/currentUser' - $ref: 'auth.yaml#/components/parameters/assumedRoles' - - name: debitorUuid + - name: projectUuid in: query required: false schema: type: string format: uuid - description: The UUID of the debitor, whose hosting assets are to be listed. + description: The UUID of the project, whose hosting assets are to be listed. - name: parentAssetUuid in: query required: false diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerAcceptanceTest.java index 021fe02a..89de41bc 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetControllerAcceptanceTest.java @@ -89,22 +89,10 @@ class HsHostingAssetControllerAcceptanceTest extends ContextBasedTestWithCleanup .contentType("application/json") .body("", lenientlyEquals(""" [ - { - "type": "MANAGED_WEBSPACE", - "identifier": "sec01", - "caption": "some Webspace", - "config": {} - }, { "type": "MANAGED_WEBSPACE", "identifier": "fir01", - "caption": "some Webspace", - "config": {} - }, - { - "type": "MANAGED_WEBSPACE", - "identifier": "thi01", - "caption": "some Webspace", + "caption": "some Webspace", "config": {} } ] diff --git a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepositoryIntegrationTest.java index 6c79da67..7275ceb4 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/hosting/asset/HsHostingAssetRepositoryIntegrationTest.java @@ -202,10 +202,12 @@ class HsHostingAssetRepositoryIntegrationTest extends ContextBasedTestWithCleanu public void normalUser_canFilterAssetsRelatedToParentAsset() { // given context("superuser-alex@hostsharing.net"); - final var parentAssetUuid = assetRepo.findAllByCriteria(null, null, MANAGED_SERVER).stream() + final var parentAssetUuid = assetRepo.findByIdentifier("vm1012").stream() + .filter(ha -> ha.getType() == MANAGED_SERVER) .findAny().orElseThrow().getUuid(); // when + context("superuser-alex@hostsharing.net", "hs_hosting_asset#vm1012:AGENT"); final var result = assetRepo.findAllByCriteria(null, parentAssetUuid, null); // then @@ -213,7 +215,6 @@ class HsHostingAssetRepositoryIntegrationTest extends ContextBasedTestWithCleanu result, "HsHostingAssetEntity(MANAGED_WEBSPACE, sec01, some Webspace, MANAGED_SERVER:vm1012, D-1000212:D-1000212 default project:separate ManagedWebspace)"); } - } @Nested