fix Error code 500 in Relation.find without type (NullPointerException) #109

Merged
hsh-michaelhoennig merged 4 commits from fix-relation-find-by-any-type into master 2024-09-27 11:01:03 +02:00
5 changed files with 66 additions and 8 deletions
Showing only changes of commit 4ca422eaa9 - Show all commits

View File

@ -52,7 +52,7 @@ public class HsOfficeRelationController implements HsOfficeRelationsApi {
context.define(currentSubject, assumedRoles); context.define(currentSubject, assumedRoles);
final var entities = relationRbacRepo.findRelationRelatedToPersonUuidAndRelationType(personUuid, 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, final var resources = mapper.mapList(entities, HsOfficeRelationResource.class,
RELATION_ENTITY_TO_RESOURCE_POSTMAPPER); RELATION_ENTITY_TO_RESOURCE_POSTMAPPER);

View File

@ -13,7 +13,7 @@ public interface HsOfficeRelationRbacRepository extends Repository<HsOfficeRelat
Optional<HsOfficeRelationRbacEntity> findByUuid(UUID id); Optional<HsOfficeRelationRbacEntity> findByUuid(UUID id);
default List<HsOfficeRelationRbacEntity> findRelationRelatedToPersonUuidAndRelationType(@NotNull UUID personUuid, HsOfficeRelationType relationType) { default List<HsOfficeRelationRbacEntity> findRelationRelatedToPersonUuidAndRelationType(@NotNull UUID personUuid, HsOfficeRelationType relationType) {
return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType.toString()); return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType == null ? null : relationType.toString());
} }
@Query(value = """ @Query(value = """

View File

@ -13,7 +13,7 @@ public interface HsOfficeRelationRealRepository extends Repository<HsOfficeRelat
Optional<HsOfficeRelationRealEntity> findByUuid(UUID id); Optional<HsOfficeRelationRealEntity> findByUuid(UUID id);
default List<HsOfficeRelationRealEntity> findRelationRelatedToPersonUuidAndRelationType(@NotNull UUID personUuid, HsOfficeRelationType relationType) { default List<HsOfficeRelationRealEntity> findRelationRelatedToPersonUuidAndRelationType(@NotNull UUID personUuid, HsOfficeRelationType relationType) {
return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType.toString()); return findRelationRelatedToPersonUuidAndRelationTypeString(personUuid, relationType == null ? null : relationType.toString());
} }
@Query(value = """ @Query(value = """

View File

@ -55,7 +55,7 @@ class HsOfficeRelationControllerAcceptanceTest extends ContextBasedTestWithClean
class ListRelations { class ListRelations {
@Test @Test
void globalAdmin_withoutAssumedRoles_canViewAllRelationsOfGivenPersonAndType_ifNoCriteriaGiven() throws JSONException { void globalAdmin_withoutAssumedRoles_canViewAllRelationsOfGivenPersonAndType() throws JSONException {
// given // given
context.define("superuser-alex@hostsharing.net"); context.define("superuser-alex@hostsharing.net");
@ -111,6 +111,64 @@ class HsOfficeRelationControllerAcceptanceTest extends ContextBasedTestWithClean
""")); """));
// @formatter:on // @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 @Nested

View File

@ -163,7 +163,7 @@ class HsOfficeRelationRepositoryIntegrationTest extends ContextBasedTestWithClea
} }
@Nested @Nested
class FindAllRelations { class FindRelations {
@Test @Test
public void globalAdmin_withoutAssumedRole_canViewAllRelationsOfArbitraryPerson() { public void globalAdmin_withoutAssumedRole_canViewAllRelationsOfArbitraryPerson() {
@ -193,7 +193,7 @@ class HsOfficeRelationRepositoryIntegrationTest extends ContextBasedTestWithClea
.findFirst().orElseThrow(); .findFirst().orElseThrow();
// when: // when:
final var result = relationRbacRepo.findRelationRelatedToPersonUuid(person.getUuid()); final var result = relationRbacRepo.findRelationRelatedToPersonUuidAndRelationType(person.getUuid(), null);
// then: // then:
exactlyTheseRelationsAreReturned( exactlyTheseRelationsAreReturned(