From 4ca422eaa9f680922f166df0af95813ded3057cc Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Thu, 26 Sep 2024 11:34:34 +0200 Subject: [PATCH] fix Relation find by any type --- .../relation/HsOfficeRelationController.java | 2 +- .../HsOfficeRelationRbacRepository.java | 6 +- .../HsOfficeRelationRealRepository.java | 2 +- ...fficeRelationControllerAcceptanceTest.java | 60 ++++++++++++++++++- ...ficeRelationRepositoryIntegrationTest.java | 4 +- 5 files changed, 66 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationController.java index b93537d9..5463803f 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationController.java @@ -52,7 +52,7 @@ public class HsOfficeRelationController implements HsOfficeRelationsApi { context.define(currentSubject, assumedRoles); final var entities = relationRbacRepo.findRelationRelatedToPersonUuidAndRelationType(personUuid, - mapper.map(relationType, HsOfficeRelationType.class)); + relationType == null ? null : HsOfficeRelationType.valueOf(relationType.name())); final var resources = mapper.mapList(entities, HsOfficeRelationResource.class, RELATION_ENTITY_TO_RESOURCE_POSTMAPPER); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRbacRepository.java b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRbacRepository.java index 3c89a0b7..ec9aea59 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRbacRepository.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRbacRepository.java @@ -13,20 +13,20 @@ public interface HsOfficeRelationRbacRepository extends Repository findByUuid(UUID id); default List findRelationRelatedToPersonUuidAndRelationType(@NotNull UUID personUuid, HsOfficeRelationType relationType) { - return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType.toString()); + return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType == null ? null : relationType.toString()); } @Query(value = """ SELECT p.* FROM hs_office.relation_rv AS p WHERE p.anchorUuid = :personUuid OR p.holderUuid = :personUuid - """, nativeQuery = true) + """, nativeQuery = true) List findRelationRelatedToPersonUuid(@NotNull UUID personUuid); @Query(value = """ SELECT p.* FROM hs_office.relation_rv AS p WHERE (:relationType IS NULL OR p.type = cast(:relationType AS hs_office.RelationType)) AND ( p.anchorUuid = :personUuid OR p.holderUuid = :personUuid) - """, nativeQuery = true) + """, nativeQuery = true) List findRelationRelatedToPersonUuidAndRelationTypeString(@NotNull UUID personUuid, String relationType); HsOfficeRelationRbacEntity save(final HsOfficeRelationRbacEntity entity); diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRealRepository.java b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRealRepository.java index 9cf58b86..292f9034 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRealRepository.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRealRepository.java @@ -13,7 +13,7 @@ public interface HsOfficeRelationRealRepository extends Repository findByUuid(UUID id); default List findRelationRelatedToPersonUuidAndRelationType(@NotNull UUID personUuid, HsOfficeRelationType relationType) { - return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType.toString()); + return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType == null ? null : relationType.toString()); } @Query(value = """ diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationControllerAcceptanceTest.java index 25e1629e..23e8410b 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationControllerAcceptanceTest.java @@ -55,7 +55,7 @@ class HsOfficeRelationControllerAcceptanceTest extends ContextBasedTestWithClean class ListRelations { @Test - void globalAdmin_withoutAssumedRoles_canViewAllRelationsOfGivenPersonAndType_ifNoCriteriaGiven() throws JSONException { + void globalAdmin_withoutAssumedRoles_canViewAllRelationsOfGivenPersonAndType() throws JSONException { // given context.define("superuser-alex@hostsharing.net"); @@ -111,6 +111,64 @@ class HsOfficeRelationControllerAcceptanceTest extends ContextBasedTestWithClean """)); // @formatter:on } + + @Test + void personAdmin_canViewAllRelationsOfGivenRelatedPersonAndAnyType() throws JSONException { + + // given + context.define("contact-admin@firstcontact.example.com"); + final var givenPerson = personRepo.findPersonByOptionalNameLike("First GmbH").get(0); + + RestAssured // @formatter:off + .given() + .header("current-subject", "superuser-alex@hostsharing.net") + .port(port) + .when() + .get("http://localhost/api/hs/office/relations?personUuid=%s" + .formatted(givenPerson.getUuid(), HsOfficeRelationTypeResource.PARTNER)) + .then().log().all().assertThat() + .statusCode(200) + .contentType("application/json") + .body("", lenientlyEquals(""" + [ + { + "anchor": { + "tradeName": "First GmbH" + }, + "holder": { + "givenName": "Susan", + "familyName": "Firby" + }, + "type": "REPRESENTATIVE", + "mark": null, + "contact": { "caption": "first contact" } + }, + { + "anchor": { + "tradeName": "Hostsharing eG" + }, + "holder": { + "tradeName": "First GmbH" + }, + "type": "PARTNER", + "mark": null, + "contact": { "caption": "first contact" } + }, + { + "anchor": { + "tradeName": "First GmbH" + }, + "holder": { + "tradeName": "First GmbH" + }, + "type": "DEBITOR", + "mark": null, + "contact": { "caption": "first contact" } + } + ] + """)); + // @formatter:on + } } @Nested diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRepositoryIntegrationTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRepositoryIntegrationTest.java index aa5d54d8..ffba5c42 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRepositoryIntegrationTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/relation/HsOfficeRelationRepositoryIntegrationTest.java @@ -163,7 +163,7 @@ class HsOfficeRelationRepositoryIntegrationTest extends ContextBasedTestWithClea } @Nested - class FindAllRelations { + class FindRelations { @Test public void globalAdmin_withoutAssumedRole_canViewAllRelationsOfArbitraryPerson() { @@ -193,7 +193,7 @@ class HsOfficeRelationRepositoryIntegrationTest extends ContextBasedTestWithClea .findFirst().orElseThrow(); // when: - final var result = relationRbacRepo.findRelationRelatedToPersonUuid(person.getUuid()); + final var result = relationRbacRepo.findRelationRelatedToPersonUuidAndRelationType(person.getUuid(), null); // then: exactlyTheseRelationsAreReturned(