From f5153cc2a175f39aa6f838e59704f7a53fa99485 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Mon, 18 Nov 2024 09:55:31 +0100 Subject: [PATCH] fix related tests and rename Controller methods --- .../debitor/HsOfficeDebitorController.java | 19 +++++++++++------- .../office/debitor/HsOfficeDebitorEntity.java | 11 +++------- .../debitor/HsOfficeDebitorRepository.java | 2 -- .../HsOfficeMembershipController.java | 15 +++++++------- .../HsOfficeSepaMandateController.java | 2 +- .../hs-office-debitors-with-uuid.yaml | 2 +- .../hs-office/hs-office-debitors.yaml | 4 ++-- .../hs-office-memberships-with-uuid.yaml | 2 +- .../hs-office/hs-office-memberships.yaml | 4 ++-- ...OfficeDebitorControllerAcceptanceTest.java | 20 +++++++++---------- .../HsOfficeDebitorEntityUnitTest.java | 10 +++++----- ...iceMembershipControllerAcceptanceTest.java | 2 +- .../HsOfficeMembershipControllerRestTest.java | 6 +++--- 13 files changed, 49 insertions(+), 50 deletions(-) diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java index 86548373..011db6a2 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorController.java @@ -22,6 +22,7 @@ import jakarta.persistence.PersistenceContext; import jakarta.validation.ValidationException; import java.util.List; import java.util.UUID; +import java.util.function.BiConsumer; import static net.hostsharing.hsadminng.hs.office.relation.HsOfficeRelationType.DEBITOR; import static net.hostsharing.hsadminng.repr.TaggedNumber.cropTag; @@ -50,7 +51,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { @Override @Transactional(readOnly = true) - public ResponseEntity> listDebitors( + public ResponseEntity> getListOfDebitors( final String currentSubject, final String assumedRoles, final String name, @@ -61,13 +62,13 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { ? debitorRepo.findDebitorByDebitorNumber(cropTag("D-", debitorNumber)) : debitorRepo.findDebitorByOptionalNameLike(name); - final var resources = mapper.mapList(entities, HsOfficeDebitorResource.class); + final var resources = mapper.mapList(entities, HsOfficeDebitorResource.class, ENTITY_TO_RESOURCE_POSTMAPPER); return ResponseEntity.ok(resources); } @Override @Transactional - public ResponseEntity addDebitor( + public ResponseEntity postNewDebitor( String currentSubject, String assumedRoles, HsOfficeDebitorInsertResource body) { @@ -108,13 +109,13 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { .path("/api/hs/office/debitors/{id}") .buildAndExpand(savedEntity.getUuid()) .toUri(); - final var mapped = mapper.map(savedEntity, HsOfficeDebitorResource.class); + final var mapped = mapper.map(savedEntity, HsOfficeDebitorResource.class, ENTITY_TO_RESOURCE_POSTMAPPER); return ResponseEntity.created(uri).body(mapped); } @Override @Transactional(readOnly = true) - public ResponseEntity getDebitorByUuid( + public ResponseEntity getSingleDebitorByUuid( final String currentSubject, final String assumedRoles, final UUID debitorUuid) { @@ -125,7 +126,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { if (result.isEmpty()) { return ResponseEntity.notFound().build(); } - return ResponseEntity.ok(mapper.map(result.get(), HsOfficeDebitorResource.class)); + return ResponseEntity.ok(mapper.map(result.get(), HsOfficeDebitorResource.class, ENTITY_TO_RESOURCE_POSTMAPPER)); } @Override @@ -160,7 +161,11 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi { final var saved = debitorRepo.save(current); Hibernate.initialize(saved); - final var mapped = mapper.map(saved, HsOfficeDebitorResource.class); + final var mapped = mapper.map(saved, HsOfficeDebitorResource.class, ENTITY_TO_RESOURCE_POSTMAPPER); return ResponseEntity.ok(mapped); } + + final BiConsumer ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { + resource.setDebitorNumber(entity.getTaggedDebitorNumber()); + }; } diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntity.java b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntity.java index e7e25ea9..623fa2cf 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntity.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntity.java @@ -142,19 +142,14 @@ public class HsOfficeDebitorEntity implements BaseEntity, return this; } - private String getDebitorNumberString() { + public String getTaggedDebitorNumber() { return ofNullable(partner) .filter(partner -> debitorNumberSuffix != null) .map(HsOfficePartnerEntity::getPartnerNumber) - .map(Object::toString) - .map(partnerNumber -> partnerNumber + debitorNumberSuffix) + .map(partnerNumber -> DEBITOR_NUMBER_TAG + partnerNumber + debitorNumberSuffix) .orElse(null); } - public Integer getDebitorNumber() { - return ofNullable(getDebitorNumberString()).map(Integer::parseInt).orElse(null); - } - @Override public String toString() { return stringify.apply(this); @@ -162,7 +157,7 @@ public class HsOfficeDebitorEntity implements BaseEntity, @Override public String toShortString() { - return DEBITOR_NUMBER_TAG + getDebitorNumberString(); + return getTaggedDebitorNumber(); } public static RbacView rbac() { diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepository.java b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepository.java index b2b51df6..30dabb59 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepository.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorRepository.java @@ -1,10 +1,8 @@ package net.hostsharing.hsadminng.hs.office.debitor; -import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipEntity; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; -import jakarta.validation.constraints.NotNull; import java.util.List; import java.util.Optional; import java.util.UUID; diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java index a8add39a..f41bd5c2 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipController.java @@ -20,7 +20,6 @@ import static java.util.Optional.ofNullable; import static net.hostsharing.hsadminng.repr.TaggedNumber.cropTag; @RestController - public class HsOfficeMembershipController implements HsOfficeMembershipsApi { @Autowired @@ -34,16 +33,18 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi { @Override @Transactional(readOnly = true) - public ResponseEntity> listMemberships( + public ResponseEntity> getListOfMemberships( final String currentSubject, final String assumedRoles, final UUID partnerUuid, final String memberNumber) { context.define(currentSubject, assumedRoles); - final var entities = ( memberNumber != null) - ? ofNullable(membershipRepo.findMembershipByMemberNumber(cropTag("M-", memberNumber))).stream().toList() - : membershipRepo.findMembershipsByOptionalPartnerUuid(partnerUuid); + final var entities = (memberNumber != null) + ? ofNullable(membershipRepo.findMembershipByMemberNumber( + cropTag(HsOfficeMembershipEntity.MEMBER_NUMBER_TAG, memberNumber))).stream() + .toList() + : membershipRepo.findMembershipsByOptionalPartnerUuid(partnerUuid); final var resources = mapper.mapList(entities, HsOfficeMembershipResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); @@ -52,7 +53,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi { @Override @Transactional - public ResponseEntity addMembership( + public ResponseEntity postNewMembership( final String currentSubject, final String assumedRoles, final HsOfficeMembershipInsertResource body) { @@ -75,7 +76,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi { @Override @Transactional(readOnly = true) - public ResponseEntity getMembershipByUuid( + public ResponseEntity getSingleMembershipByUuid( final String currentSubject, final String assumedRoles, final UUID membershipUuid) { diff --git a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java index 976aad83..0a72e425 100644 --- a/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java +++ b/src/main/java/net/hostsharing/hsadminng/hs/office/sepamandate/HsOfficeSepaMandateController.java @@ -131,7 +131,7 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi { if (entity.getValidity().hasUpperBound()) { resource.setValidTo(entity.getValidity().upper().minusDays(1)); } - resource.getDebitor().setDebitorNumber("D-" + entity.getDebitor().getDebitorNumber()); + resource.getDebitor().setDebitorNumber(entity.getDebitor().getTaggedDebitorNumber()); }; final BiConsumer SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> { diff --git a/src/main/resources/api-definition/hs-office/hs-office-debitors-with-uuid.yaml b/src/main/resources/api-definition/hs-office/hs-office-debitors-with-uuid.yaml index feb8e473..22548852 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-debitors-with-uuid.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-debitors-with-uuid.yaml @@ -2,7 +2,7 @@ get: tags: - hs-office-debitors description: 'Fetch a single debitor by its uuid, if visible for the current subject.' - operationId: getDebitorByUuid + operationId: getSingleDebitorByUuid parameters: - $ref: 'auth.yaml#/components/parameters/currentSubject' - $ref: 'auth.yaml#/components/parameters/assumedRoles' diff --git a/src/main/resources/api-definition/hs-office/hs-office-debitors.yaml b/src/main/resources/api-definition/hs-office/hs-office-debitors.yaml index 2e5c3383..c825817f 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-debitors.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-debitors.yaml @@ -3,7 +3,7 @@ get: description: Returns the list of (optionally filtered) debitors which are visible to the current subject or any of it's assumed roles. tags: - hs-office-debitors - operationId: listDebitors + operationId: getListOfDebitors parameters: - $ref: 'auth.yaml#/components/parameters/currentSubject' - $ref: 'auth.yaml#/components/parameters/assumedRoles' @@ -40,7 +40,7 @@ post: summary: Adds a new debitor. tags: - hs-office-debitors - operationId: addDebitor + operationId: postNewDebitor parameters: - $ref: 'auth.yaml#/components/parameters/currentSubject' - $ref: 'auth.yaml#/components/parameters/assumedRoles' diff --git a/src/main/resources/api-definition/hs-office/hs-office-memberships-with-uuid.yaml b/src/main/resources/api-definition/hs-office/hs-office-memberships-with-uuid.yaml index 1511e09f..820fc0bc 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-memberships-with-uuid.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-memberships-with-uuid.yaml @@ -2,7 +2,7 @@ get: tags: - hs-office-memberships description: 'Fetch a single membership by its uuid, if visible for the current subject.' - operationId: getMembershipByUuid + operationId: getSingleMembershipByUuid parameters: - $ref: 'auth.yaml#/components/parameters/currentSubject' - $ref: 'auth.yaml#/components/parameters/assumedRoles' diff --git a/src/main/resources/api-definition/hs-office/hs-office-memberships.yaml b/src/main/resources/api-definition/hs-office/hs-office-memberships.yaml index 74093839..9b2c27b4 100644 --- a/src/main/resources/api-definition/hs-office/hs-office-memberships.yaml +++ b/src/main/resources/api-definition/hs-office/hs-office-memberships.yaml @@ -4,7 +4,7 @@ get: The list can optionally be filtered by either the `partnerUuid` or the `memberNumber` - not both at the same time. tags: - hs-office-memberships - operationId: listMemberships + operationId: getListOfMemberships parameters: - $ref: 'auth.yaml#/components/parameters/currentSubject' - $ref: 'auth.yaml#/components/parameters/assumedRoles' @@ -42,7 +42,7 @@ post: summary: Adds a new membership. tags: - hs-office-memberships - operationId: addMembership + operationId: postNewMembership parameters: - $ref: 'auth.yaml#/components/parameters/currentSubject' - $ref: 'auth.yaml#/components/parameters/assumedRoles' diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java index 82f6cdf0..53f32e80 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorControllerAcceptanceTest.java @@ -72,7 +72,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu EntityManager em; @Nested - class ListDebitors { + class GetListOfDebitors { @Test void globalAdmin_withoutAssumedRoles_canViewAllDebitors_ifNoCriteriaGiven() { @@ -110,7 +110,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu "phoneNumbers": { "phone_office": "+49 123 1234567" } } }, - "debitorNumber": 1000111, + "debitorNumber": "D-1000111", "debitorNumberSuffix": "11", "partner": { "partnerNumber": "P-10001", @@ -165,7 +165,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu "emailAddresses": { "main": "contact-admin@secondcontact.example.com" } } }, - "debitorNumber": 1000212, + "debitorNumber": "D-1000212", "debitorNumberSuffix": "12", "partner": { "partnerNumber": "P-10002", @@ -199,7 +199,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu "emailAddresses": { "main": "contact-admin@thirdcontact.example.com" } } }, - "debitorNumber": 1000313, + "debitorNumber": "D-1000313", "debitorNumberSuffix": "13", "partner": { "partnerNumber": "P-10003", @@ -237,14 +237,14 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu .header("current-subject", "superuser-alex@hostsharing.net") .port(port) .when() - .get("http://localhost/api/hs/office/debitors?debitorNumber=1000212") + .get("http://localhost/api/hs/office/debitors?debitorNumber=D-1000212") .then().log().all().assertThat() .statusCode(200) .contentType("application/json") .body("", lenientlyEquals(""" [ { - "debitorNumber": 1000212, + "debitorNumber": "D-1000212", "partner": { "partnerNumber": "P-10002" }, "debitorRel": { "contact": { "caption": "second contact" } @@ -260,7 +260,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu } @Nested - class AddDebitor { + class PostNewDebitor { @Test void globalAdmin_withoutAssumedRole_canAddDebitorWithBankAccount() { @@ -436,7 +436,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu } @Nested - class GetDebitor { + class GetSingleDebitorByUuid { @Test void globalAdmin_withoutAssumedRole_canGetArbitraryDebitor() { @@ -534,7 +534,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu .contentType("application/json") .body("", lenientlyEquals(""" { - "debitorNumber": 1000111, + "debitorNumber": "D-1000111", "partner": { "partnerNumber": "P-10001" }, "debitorRel": { "contact": { "caption": "first contact" } }, "refundBankAccount": null @@ -581,7 +581,7 @@ class HsOfficeDebitorControllerAcceptanceTest extends ContextBasedTestWithCleanu "mark": null, "contact": { "caption": "fourth contact" } }, - "debitorNumber": 10004${debitorNumberSuffix}, + "debitorNumber": "D-10004${debitorNumberSuffix}", "debitorNumberSuffix": "${debitorNumberSuffix}", "partner": { "partnerNumber": "P-10004", diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntityUnitTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntityUnitTest.java index 951ff536..a1c5ea0e 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntityUnitTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/debitor/HsOfficeDebitorEntityUnitTest.java @@ -65,9 +65,9 @@ class HsOfficeDebitorEntityUnitTest { .build()) .build(); - final var result = given.getDebitorNumber(); + final var result = given.getTaggedDebitorNumber(); - assertThat(result).isEqualTo(1234567); + assertThat(result).isEqualTo("D-1234567"); } @Test @@ -78,7 +78,7 @@ class HsOfficeDebitorEntityUnitTest { .partner(null) .build(); - final var result = given.getDebitorNumber(); + final var result = given.getTaggedDebitorNumber(); assertThat(result).isNull(); } @@ -91,7 +91,7 @@ class HsOfficeDebitorEntityUnitTest { .partner(HsOfficePartnerEntity.builder().build()) .build(); - final var result = given.getDebitorNumber(); + final var result = given.getTaggedDebitorNumber(); assertThat(result).isNull(); } @@ -106,7 +106,7 @@ class HsOfficeDebitorEntityUnitTest { .build()) .build(); - final var result = given.getDebitorNumber(); + final var result = given.getTaggedDebitorNumber(); assertThat(result).isNull(); } diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java index 68320f21..2a8beba2 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerAcceptanceTest.java @@ -144,7 +144,7 @@ class HsOfficeMembershipControllerAcceptanceTest extends ContextBasedTestWithCle .header("current-subject", "superuser-alex@hostsharing.net") .port(port) .when() - .queryParam("memberNumber", 1000202 ) + .queryParam("memberNumber", "M-1000202" ) .get("http://localhost/api/hs/office/memberships") .then().log().all().assertThat() .statusCode(200) diff --git a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java index 2306fcca..cc4b7d0d 100644 --- a/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java +++ b/src/test/java/net/hostsharing/hsadminng/hs/office/membership/HsOfficeMembershipControllerRestTest.java @@ -46,14 +46,14 @@ public class HsOfficeMembershipControllerRestTest { EntityManagerWrapper em; @Nested - class GetMemberships { + class GetListOfMemberships { @Test void findMembershipByNonExistingMemberNumberReturnsEmptyList() throws Exception { // when mockMvc.perform(MockMvcRequestBuilders - .get("/api/hs/office/memberships?memberNumber=12345") + .get("/api/hs/office/memberships?memberNumber=M-1234501") .header("current-subject", "superuser-alex@hostsharing.net") .contentType(MediaType.APPLICATION_JSON) .content(""" @@ -73,7 +73,7 @@ public class HsOfficeMembershipControllerRestTest { } @Nested - class AddMembership { + class PostNewMembership { @Test void respondBadRequest_ifPartnerUuidIsMissing() throws Exception {