ModelMapper as Spring Bean

This commit is contained in:
Michael Hoennig 2022-10-23 09:58:52 +02:00
parent 0b0f57c176
commit 6f3c03e6b6
45 changed files with 329 additions and 280 deletions

View File

@ -16,8 +16,6 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
@RestController @RestController
public class HsOfficeBankAccountController implements HsOfficeBankAccountsApi { public class HsOfficeBankAccountController implements HsOfficeBankAccountsApi {
@ -25,6 +23,9 @@ public class HsOfficeBankAccountController implements HsOfficeBankAccountsApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficeBankAccountRepository bankAccountRepo; private HsOfficeBankAccountRepository bankAccountRepo;
@ -38,7 +39,7 @@ public class HsOfficeBankAccountController implements HsOfficeBankAccountsApi {
final var entities = bankAccountRepo.findByOptionalHolderLike(holder); final var entities = bankAccountRepo.findByOptionalHolderLike(holder);
final var resources = Mapper.mapList(entities, HsOfficeBankAccountResource.class); final var resources = mapper.mapList(entities, HsOfficeBankAccountResource.class);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -54,7 +55,7 @@ public class HsOfficeBankAccountController implements HsOfficeBankAccountsApi {
IbanUtil.validate(body.getIban()); IbanUtil.validate(body.getIban());
BicUtil.validate(body.getBic()); BicUtil.validate(body.getBic());
final var entityToSave = map(body, HsOfficeBankAccountEntity.class); final var entityToSave = mapper.map(body, HsOfficeBankAccountEntity.class);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
final var saved = bankAccountRepo.save(entityToSave); final var saved = bankAccountRepo.save(entityToSave);
@ -64,7 +65,7 @@ public class HsOfficeBankAccountController implements HsOfficeBankAccountsApi {
.path("/api/hs/office/BankAccounts/{id}") .path("/api/hs/office/BankAccounts/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficeBankAccountResource.class); final var mapped = mapper.map(saved, HsOfficeBankAccountResource.class);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -81,7 +82,7 @@ public class HsOfficeBankAccountController implements HsOfficeBankAccountsApi {
if (result.isEmpty()) { if (result.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result.get(), HsOfficeBankAccountResource.class)); return ResponseEntity.ok(mapper.map(result.get(), HsOfficeBankAccountResource.class));
} }
@Override @Override

View File

@ -15,8 +15,6 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
@RestController @RestController
public class HsOfficeContactController implements HsOfficeContactsApi { public class HsOfficeContactController implements HsOfficeContactsApi {
@ -24,6 +22,9 @@ public class HsOfficeContactController implements HsOfficeContactsApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficeContactRepository contactRepo; private HsOfficeContactRepository contactRepo;
@ -37,7 +38,7 @@ public class HsOfficeContactController implements HsOfficeContactsApi {
final var entities = contactRepo.findContactByOptionalLabelLike(label); final var entities = contactRepo.findContactByOptionalLabelLike(label);
final var resources = Mapper.mapList(entities, HsOfficeContactResource.class); final var resources = mapper.mapList(entities, HsOfficeContactResource.class);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -50,7 +51,7 @@ public class HsOfficeContactController implements HsOfficeContactsApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var entityToSave = map(body, HsOfficeContactEntity.class); final var entityToSave = mapper.map(body, HsOfficeContactEntity.class);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
final var saved = contactRepo.save(entityToSave); final var saved = contactRepo.save(entityToSave);
@ -60,7 +61,7 @@ public class HsOfficeContactController implements HsOfficeContactsApi {
.path("/api/hs/office/contacts/{id}") .path("/api/hs/office/contacts/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficeContactResource.class); final var mapped = mapper.map(saved, HsOfficeContactResource.class);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -77,7 +78,7 @@ public class HsOfficeContactController implements HsOfficeContactsApi {
if (result.isEmpty()) { if (result.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result.get(), HsOfficeContactResource.class)); return ResponseEntity.ok(mapper.map(result.get(), HsOfficeContactResource.class));
} }
@Override @Override
@ -111,7 +112,7 @@ public class HsOfficeContactController implements HsOfficeContactsApi {
new HsOfficeContactEntityPatch(current).apply(body); new HsOfficeContactEntityPatch(current).apply(body);
final var saved = contactRepo.save(current); final var saved = contactRepo.save(current);
final var mapped = map(saved, HsOfficeContactResource.class); final var mapped = mapper.map(saved, HsOfficeContactResource.class);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
} }

View File

@ -22,7 +22,6 @@ import java.util.UUID;
import static java.lang.String.join; import static java.lang.String.join;
import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopAssetsTransactionTypeResource.*; import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopAssetsTransactionTypeResource.*;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
@RestController @RestController
public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAssetsApi { public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAssetsApi {
@ -30,49 +29,52 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo; private HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo;
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public ResponseEntity<List<HsOfficeCoopAssetsTransactionResource>> listCoopAssets( public ResponseEntity<List<HsOfficeCoopAssetsTransactionResource>> listCoopAssets(
final String currentUser, final String currentUser,
final String assumedRoles, final String assumedRoles,
final UUID membershipUuid, final UUID membershipUuid,
final @DateTimeFormat(iso = ISO.DATE) LocalDate fromValueDate, final @DateTimeFormat(iso = ISO.DATE) LocalDate fromValueDate,
final @DateTimeFormat(iso = ISO.DATE) LocalDate toValueDate) { final @DateTimeFormat(iso = ISO.DATE) LocalDate toValueDate) {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var entities = coopAssetsTransactionRepo.findCoopAssetsTransactionByOptionalMembershipUuidAndDateRange( final var entities = coopAssetsTransactionRepo.findCoopAssetsTransactionByOptionalMembershipUuidAndDateRange(
membershipUuid, membershipUuid,
fromValueDate, fromValueDate,
toValueDate); toValueDate);
final var resources = Mapper.mapList(entities, HsOfficeCoopAssetsTransactionResource.class); final var resources = mapper.mapList(entities, HsOfficeCoopAssetsTransactionResource.class);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@Override @Override
@Transactional @Transactional
public ResponseEntity<HsOfficeCoopAssetsTransactionResource> addCoopAssetsTransaction( public ResponseEntity<HsOfficeCoopAssetsTransactionResource> addCoopAssetsTransaction(
final String currentUser, final String currentUser,
final String assumedRoles, final String assumedRoles,
@Valid final HsOfficeCoopAssetsTransactionInsertResource requestBody) { @Valid final HsOfficeCoopAssetsTransactionInsertResource requestBody) {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
validate(requestBody); validate(requestBody);
final var entityToSave = map(requestBody, HsOfficeCoopAssetsTransactionEntity.class); final var entityToSave = mapper.map(requestBody, HsOfficeCoopAssetsTransactionEntity.class);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
final var saved = coopAssetsTransactionRepo.save(entityToSave); final var saved = coopAssetsTransactionRepo.save(entityToSave);
final var uri = final var uri =
MvcUriComponentsBuilder.fromController(getClass()) MvcUriComponentsBuilder.fromController(getClass())
.path("/api/hs/office/coopassetstransactions/{id}") .path("/api/hs/office/coopassetstransactions/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficeCoopAssetsTransactionResource.class); final var mapped = mapper.map(saved, HsOfficeCoopAssetsTransactionResource.class);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -87,31 +89,31 @@ public class HsOfficeCoopAssetsTransactionController implements HsOfficeCoopAsse
} }
private static void validateDebitTransaction( private static void validateDebitTransaction(
final HsOfficeCoopAssetsTransactionInsertResource requestBody, final HsOfficeCoopAssetsTransactionInsertResource requestBody,
final ArrayList<String> violations) { final ArrayList<String> violations) {
if (List.of(DEPOSIT, ADOPTION).contains(requestBody.getTransactionType()) if (List.of(DEPOSIT, ADOPTION).contains(requestBody.getTransactionType())
&& requestBody.getAssetValue().signum() < 0) { && requestBody.getAssetValue().signum() < 0) {
violations.add("for %s, assetValue must be positive but is \"%.2f\"".formatted( violations.add("for %s, assetValue must be positive but is \"%.2f\"".formatted(
requestBody.getTransactionType(), requestBody.getAssetValue())); requestBody.getTransactionType(), requestBody.getAssetValue()));
} }
} }
private static void validateCreditTransaction( private static void validateCreditTransaction(
final HsOfficeCoopAssetsTransactionInsertResource requestBody, final HsOfficeCoopAssetsTransactionInsertResource requestBody,
final ArrayList<String> violations) { final ArrayList<String> violations) {
if (List.of(DISBURSAL, TRANSFER, CLEARING, LOSS).contains(requestBody.getTransactionType()) if (List.of(DISBURSAL, TRANSFER, CLEARING, LOSS).contains(requestBody.getTransactionType())
&& requestBody.getAssetValue().signum() > 0) { && requestBody.getAssetValue().signum() > 0) {
violations.add("for %s, assetValue must be negative but is \"%.2f\"".formatted( violations.add("for %s, assetValue must be negative but is \"%.2f\"".formatted(
requestBody.getTransactionType(), requestBody.getAssetValue())); requestBody.getTransactionType(), requestBody.getAssetValue()));
} }
} }
private static void validateAssetValue( private static void validateAssetValue(
final HsOfficeCoopAssetsTransactionInsertResource requestBody, final HsOfficeCoopAssetsTransactionInsertResource requestBody,
final ArrayList<String> violations) { final ArrayList<String> violations) {
if (requestBody.getAssetValue().signum() == 0) { if (requestBody.getAssetValue().signum() == 0) {
violations.add("assetValue must not be 0 but is \"%.2f\"".formatted( violations.add("assetValue must not be 0 but is \"%.2f\"".formatted(
requestBody.getAssetValue())); requestBody.getAssetValue()));
} }
} }

View File

@ -21,7 +21,6 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import static java.lang.String.join; import static java.lang.String.join;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopSharesTransactionTypeResource.CANCELLATION; import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopSharesTransactionTypeResource.CANCELLATION;
import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopSharesTransactionTypeResource.SUBSCRIPTION; import static net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopSharesTransactionTypeResource.SUBSCRIPTION;
@ -31,6 +30,9 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo; private HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
@ -49,7 +51,7 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
fromValueDate, fromValueDate,
toValueDate); toValueDate);
final var resources = Mapper.mapList(entities, HsOfficeCoopSharesTransactionResource.class); final var resources = mapper.mapList(entities, HsOfficeCoopSharesTransactionResource.class);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -63,7 +65,7 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
validate(requestBody); validate(requestBody);
final var entityToSave = map(requestBody, HsOfficeCoopSharesTransactionEntity.class); final var entityToSave = mapper.map(requestBody, HsOfficeCoopSharesTransactionEntity.class);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
final var saved = coopSharesTransactionRepo.save(entityToSave); final var saved = coopSharesTransactionRepo.save(entityToSave);
@ -73,7 +75,7 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
.path("/api/hs/office/coopsharestransactions/{id}") .path("/api/hs/office/coopsharestransactions/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficeCoopSharesTransactionResource.class); final var mapped = mapper.map(saved, HsOfficeCoopSharesTransactionResource.class);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }

View File

@ -1,12 +1,11 @@
package net.hostsharing.hsadminng.hs.office.debitor; package net.hostsharing.hsadminng.hs.office.debitor;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.bankaccount.HsOfficeBankAccountEntity;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeDebitorsApi; import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeDebitorsApi;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.*; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeDebitorInsertResource;
import net.hostsharing.hsadminng.hs.office.partner.HsOfficePartnerEntity; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeDebitorPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeDebitorResource;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -16,9 +15,6 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
@RestController @RestController
@ -27,6 +23,9 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficeDebitorRepository debitorRepo; private HsOfficeDebitorRepository debitorRepo;
@ -46,8 +45,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
? debitorRepo.findDebitorByDebitorNumber(debitorNumber) ? debitorRepo.findDebitorByDebitorNumber(debitorNumber)
: debitorRepo.findDebitorByOptionalNameLike(name); : debitorRepo.findDebitorByOptionalNameLike(name);
final var resources = Mapper.mapList(entities, HsOfficeDebitorResource.class, final var resources = mapper.mapList(entities, HsOfficeDebitorResource.class);
DEBITOR_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -60,7 +58,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var entityToSave = map(body, HsOfficeDebitorEntity.class, DEBITOR_RESOURCE_TO_ENTITY_POSTMAPPER); final var entityToSave = mapper.map(body, HsOfficeDebitorEntity.class);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
final var saved = debitorRepo.save(entityToSave); final var saved = debitorRepo.save(entityToSave);
@ -70,8 +68,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
.path("/api/hs/office/debitors/{id}") .path("/api/hs/office/debitors/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficeDebitorResource.class, final var mapped = mapper.map(saved, HsOfficeDebitorResource.class);
DEBITOR_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -88,7 +85,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
if (result.isEmpty()) { if (result.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result.get(), HsOfficeDebitorResource.class, DEBITOR_ENTITY_TO_RESOURCE_POSTMAPPER)); return ResponseEntity.ok(mapper.map(result.get(), HsOfficeDebitorResource.class));
} }
@Override @Override
@ -122,24 +119,7 @@ public class HsOfficeDebitorController implements HsOfficeDebitorsApi {
new HsOfficeDebitorEntityPatcher(em, current).apply(body); new HsOfficeDebitorEntityPatcher(em, current).apply(body);
final var saved = debitorRepo.save(current); final var saved = debitorRepo.save(current);
final var mapped = map(saved, HsOfficeDebitorResource.class); final var mapped = mapper.map(saved, HsOfficeDebitorResource.class);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
final BiConsumer<HsOfficeDebitorEntity, HsOfficeDebitorResource> DEBITOR_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setPartner(map(entity.getPartner(), HsOfficePartnerResource.class));
resource.setBillingContact(map(entity.getBillingContact(), HsOfficeContactResource.class));
if ( entity.getRefundBankAccount() != null ) {
resource.setRefundBankAccount(map(entity.getRefundBankAccount(), HsOfficeBankAccountResource.class));
}
};
final BiConsumer<HsOfficeDebitorInsertResource, HsOfficeDebitorEntity> DEBITOR_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
entity.setPartner(em.getReference(HsOfficePartnerEntity.class, resource.getPartnerUuid()));
entity.setBillingContact(em.getReference(HsOfficeContactEntity.class, resource.getBillingContactUuid()));
if ( resource.getRefundBankAccountUuid() != null ) {
entity.setRefundBankAccount(em.getReference(HsOfficeBankAccountEntity.class, resource.getRefundBankAccountUuid()));
}
};
} }

View File

@ -5,6 +5,7 @@ import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeMembersh
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipInsertResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipInsertResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipResource;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -17,9 +18,6 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.Mapper.mapList;
@RestController @RestController
public class HsOfficeMembershipController implements HsOfficeMembershipsApi { public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
@ -27,6 +25,9 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficeMembershipRepository membershipRepo; private HsOfficeMembershipRepository membershipRepo;
@ -45,7 +46,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
final var entities = final var entities =
membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(partnerUuid, memberNumber); membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(partnerUuid, memberNumber);
final var resources = mapList(entities, HsOfficeMembershipResource.class, final var resources = mapper.mapList(entities, HsOfficeMembershipResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -59,7 +60,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var entityToSave = map(body, HsOfficeMembershipEntity.class); final var entityToSave = mapper.map(body, HsOfficeMembershipEntity.class);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
final var saved = membershipRepo.save(entityToSave); final var saved = membershipRepo.save(entityToSave);
@ -69,7 +70,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
.path("/api/hs/office/Memberships/{id}") .path("/api/hs/office/Memberships/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficeMembershipResource.class, final var mapped = mapper.map(saved, HsOfficeMembershipResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -87,7 +88,7 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
if (result.isEmpty()) { if (result.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result.get(), HsOfficeMembershipResource.class, return ResponseEntity.ok(mapper.map(result.get(), HsOfficeMembershipResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER)); SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER));
} }
@ -119,14 +120,15 @@ public class HsOfficeMembershipController implements HsOfficeMembershipsApi {
final var current = membershipRepo.findByUuid(membershipUuid).orElseThrow(); final var current = membershipRepo.findByUuid(membershipUuid).orElseThrow();
new HsOfficeMembershipEntityPatcher(em, current).apply(body); new HsOfficeMembershipEntityPatcher(em, mapper, current).apply(body);
final var saved = membershipRepo.save(current); final var saved = membershipRepo.save(current);
final var mapped = map(saved, HsOfficeMembershipResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); final var mapped = mapper.map(saved, HsOfficeMembershipResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
final BiConsumer<HsOfficeMembershipEntity, HsOfficeMembershipResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { final BiConsumer<HsOfficeMembershipEntity, HsOfficeMembershipResource> SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
// TODO.refa: this should be possible via ModelMapper config
resource.setValidFrom(entity.getValidity().lower()); resource.setValidFrom(entity.getValidity().lower());
if (entity.getValidity().hasUpperBound()) { if (entity.getValidity().hasUpperBound()) {
resource.setValidTo(entity.getValidity().upper().minusDays(1)); resource.setValidTo(entity.getValidity().upper().minusDays(1));

View File

@ -3,23 +3,25 @@ package net.hostsharing.hsadminng.hs.office.membership;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource;
import net.hostsharing.hsadminng.mapper.EntityPatcher; import net.hostsharing.hsadminng.mapper.EntityPatcher;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.mapper.OptionalFromJson; import net.hostsharing.hsadminng.mapper.OptionalFromJson;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
public class HsOfficeMembershipEntityPatcher implements EntityPatcher<HsOfficeMembershipPatchResource> { public class HsOfficeMembershipEntityPatcher implements EntityPatcher<HsOfficeMembershipPatchResource> {
private final EntityManager em; private final EntityManager em;
private final Mapper mapper;
private final HsOfficeMembershipEntity entity; private final HsOfficeMembershipEntity entity;
public HsOfficeMembershipEntityPatcher( public HsOfficeMembershipEntityPatcher(
final EntityManager em, final EntityManager em,
final Mapper mapper,
final HsOfficeMembershipEntity entity) { final HsOfficeMembershipEntity entity) {
this.em = em; this.em = em;
this.mapper = mapper;
this.entity = entity; this.entity = entity;
} }
@ -33,7 +35,7 @@ public class HsOfficeMembershipEntityPatcher implements EntityPatcher<HsOfficeMe
OptionalFromJson.of(resource.getValidTo()).ifPresent( OptionalFromJson.of(resource.getValidTo()).ifPresent(
entity::setValidTo); entity::setValidTo);
Optional.ofNullable(resource.getReasonForTermination()) Optional.ofNullable(resource.getReasonForTermination())
.map(v -> map(v, HsOfficeReasonForTermination.class)) .map(v -> mapper.map(v, HsOfficeReasonForTermination.class))
.ifPresent(entity::setReasonForTermination); .ifPresent(entity::setReasonForTermination);
} }

View File

@ -1,11 +1,11 @@
package net.hostsharing.hsadminng.hs.office.partner; package net.hostsharing.hsadminng.hs.office.partner;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficePartnersApi; import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficePartnersApi;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.*; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerInsertResource;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficePartnerResource;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -14,11 +14,7 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
@RestController @RestController
@ -27,15 +23,12 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficePartnerRepository partnerRepo; private HsOfficePartnerRepository partnerRepo;
@Autowired
private HsOfficePersonRepository personRepo;
@Autowired
private HsOfficeContactRepository contactRepo;
@Autowired @Autowired
private EntityManager em; private EntityManager em;
@ -49,8 +42,7 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
final var entities = partnerRepo.findPartnerByOptionalNameLike(name); final var entities = partnerRepo.findPartnerByOptionalNameLike(name);
final var resources = Mapper.mapList(entities, HsOfficePartnerResource.class, final var resources = mapper.mapList(entities, HsOfficePartnerResource.class);
PARTNER_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -63,16 +55,9 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var entityToSave = mapToHsOfficePartnerEntity(body); final var entityToSave = mapper.map(body, HsOfficePartnerEntity.class);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
// TODO.impl: use getReference entityToSave.setDetails(mapper.map(body.getDetails(), HsOfficePartnerDetailsEntity.class));
entityToSave.setContact(contactRepo.findByUuid(body.getContactUuid()).orElseThrow(
() -> new NoSuchElementException("cannot find contact uuid " + body.getContactUuid())
));
entityToSave.setPerson(personRepo.findByUuid(body.getPersonUuid()).orElseThrow(
() -> new NoSuchElementException("cannot find person uuid " + body.getPersonUuid())
));
entityToSave.setDetails(map(body.getDetails(), HsOfficePartnerDetailsEntity.class));
entityToSave.getDetails().setUuid(UUID.randomUUID()); entityToSave.getDetails().setUuid(UUID.randomUUID());
final var saved = partnerRepo.save(entityToSave); final var saved = partnerRepo.save(entityToSave);
@ -82,8 +67,7 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
.path("/api/hs/office/partners/{id}") .path("/api/hs/office/partners/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficePartnerResource.class, final var mapped = mapper.map(saved, HsOfficePartnerResource.class);
PARTNER_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -100,7 +84,7 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
if (result.isEmpty()) { if (result.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result.get(), HsOfficePartnerResource.class, PARTNER_ENTITY_TO_RESOURCE_POSTMAPPER)); return ResponseEntity.ok(mapper.map(result.get(), HsOfficePartnerResource.class));
} }
@Override @Override
@ -134,23 +118,7 @@ public class HsOfficePartnerController implements HsOfficePartnersApi {
new HsOfficePartnerEntityPatcher(em, current).apply(body); new HsOfficePartnerEntityPatcher(em, current).apply(body);
final var saved = partnerRepo.save(current); final var saved = partnerRepo.save(current);
final var mapped = map(saved, HsOfficePartnerResource.class); final var mapped = mapper.map(saved, HsOfficePartnerResource.class);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
final BiConsumer<HsOfficePartnerEntity, HsOfficePartnerResource> PARTNER_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setPerson(map(entity.getPerson(), HsOfficePersonResource.class));
resource.setContact(map(entity.getContact(), HsOfficeContactResource.class));
};
// TODO.impl: user postmapper + getReference
private HsOfficePartnerEntity mapToHsOfficePartnerEntity(final HsOfficePartnerInsertResource resource) {
final var entity = new HsOfficePartnerEntity();
// entity.setBirthday(resource.getBirthday());
// entity.setBirthName(resource.getBirthName());
// entity.setDateOfDeath(resource.getDateOfDeath());
// entity.setRegistrationNumber(resource.getRegistrationNumber());
// entity.setRegistrationOffice(resource.getRegistrationOffice());
return entity;
}
} }

View File

@ -15,8 +15,6 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
@RestController @RestController
public class HsOfficePersonController implements HsOfficePersonsApi { public class HsOfficePersonController implements HsOfficePersonsApi {
@ -24,6 +22,9 @@ public class HsOfficePersonController implements HsOfficePersonsApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficePersonRepository personRepo; private HsOfficePersonRepository personRepo;
@ -37,7 +38,7 @@ public class HsOfficePersonController implements HsOfficePersonsApi {
final var entities = personRepo.findPersonByOptionalNameLike(label); final var entities = personRepo.findPersonByOptionalNameLike(label);
final var resources = Mapper.mapList(entities, HsOfficePersonResource.class); final var resources = mapper.mapList(entities, HsOfficePersonResource.class);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -50,7 +51,7 @@ public class HsOfficePersonController implements HsOfficePersonsApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var entityToSave = map(body, HsOfficePersonEntity.class); final var entityToSave = mapper.map(body, HsOfficePersonEntity.class);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
final var saved = personRepo.save(entityToSave); final var saved = personRepo.save(entityToSave);
@ -60,7 +61,7 @@ public class HsOfficePersonController implements HsOfficePersonsApi {
.path("/api/hs/office/persons/{id}") .path("/api/hs/office/persons/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficePersonResource.class); final var mapped = mapper.map(saved, HsOfficePersonResource.class);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -77,7 +78,7 @@ public class HsOfficePersonController implements HsOfficePersonsApi {
if (result.isEmpty()) { if (result.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result.get(), HsOfficePersonResource.class)); return ResponseEntity.ok(mapper.map(result.get(), HsOfficePersonResource.class));
} }
@Override @Override
@ -111,7 +112,7 @@ public class HsOfficePersonController implements HsOfficePersonsApi {
new HsOfficePersonEntityPatcher(current).apply(body); new HsOfficePersonEntityPatcher(current).apply(body);
final var saved = personRepo.save(current); final var saved = personRepo.save(current);
final var mapped = map(saved, HsOfficePersonResource.class); final var mapped = mapper.map(saved, HsOfficePersonResource.class);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
} }

View File

@ -5,6 +5,7 @@ import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeRelationshipsApi; import net.hostsharing.hsadminng.hs.office.generated.api.v1.api.HsOfficeRelationshipsApi;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.*; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.*;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository; import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -17,8 +18,6 @@ import java.util.NoSuchElementException;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.Mapper.mapList;
@RestController @RestController
@ -27,6 +26,9 @@ public class HsOfficeRelationshipController implements HsOfficeRelationshipsApi
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficeRelationshipRepository relationshipRepo; private HsOfficeRelationshipRepository relationshipRepo;
@ -49,9 +51,9 @@ public class HsOfficeRelationshipController implements HsOfficeRelationshipsApi
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var entities = relationshipRepo.findRelationshipRelatedToPersonUuid(personUuid, final var entities = relationshipRepo.findRelationshipRelatedToPersonUuid(personUuid,
map(relationshipType, HsOfficeRelationshipType.class)); mapper.map(relationshipType, HsOfficeRelationshipType.class));
final var resources = mapList(entities, HsOfficeRelationshipResource.class, final var resources = mapper.mapList(entities, HsOfficeRelationshipResource.class,
RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER); RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -85,7 +87,7 @@ public class HsOfficeRelationshipController implements HsOfficeRelationshipsApi
.path("/api/hs/office/relationships/{id}") .path("/api/hs/office/relationships/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficeRelationshipResource.class, final var mapped = mapper.map(saved, HsOfficeRelationshipResource.class,
RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER); RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -103,7 +105,7 @@ public class HsOfficeRelationshipController implements HsOfficeRelationshipsApi
if (result.isEmpty()) { if (result.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result.get(), HsOfficeRelationshipResource.class, RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER)); return ResponseEntity.ok(mapper.map(result.get(), HsOfficeRelationshipResource.class, RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER));
} }
@Override @Override
@ -137,14 +139,14 @@ public class HsOfficeRelationshipController implements HsOfficeRelationshipsApi
new HsOfficeRelationshipEntityPatcher(em, current).apply(body); new HsOfficeRelationshipEntityPatcher(em, current).apply(body);
final var saved = relationshipRepo.save(current); final var saved = relationshipRepo.save(current);
final var mapped = map(saved, HsOfficeRelationshipResource.class); final var mapped = mapper.map(saved, HsOfficeRelationshipResource.class);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
final BiConsumer<HsOfficeRelationshipEntity, HsOfficeRelationshipResource> RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> { final BiConsumer<HsOfficeRelationshipEntity, HsOfficeRelationshipResource> RELATIONSHIP_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
resource.setRelAnchor(map(entity.getRelAnchor(), HsOfficePersonResource.class)); resource.setRelAnchor(mapper.map(entity.getRelAnchor(), HsOfficePersonResource.class));
resource.setRelHolder(map(entity.getRelHolder(), HsOfficePersonResource.class)); resource.setRelHolder(mapper.map(entity.getRelHolder(), HsOfficePersonResource.class));
resource.setContact(map(entity.getContact(), HsOfficeContactResource.class)); resource.setContact(mapper.map(entity.getContact(), HsOfficeContactResource.class));
}; };
} }

View File

@ -15,12 +15,10 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.validation.Valid; import javax.validation.Valid;
import java.time.LocalDate;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange; import static net.hostsharing.hsadminng.mapper.PostgresDateRange.toPostgresDateRange;
@RestController @RestController
@ -30,6 +28,9 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private HsOfficeSepaMandateRepository SepaMandateRepo; private HsOfficeSepaMandateRepository SepaMandateRepo;
@ -46,7 +47,7 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
final var entities = SepaMandateRepo.findSepaMandateByOptionalIban(iban); final var entities = SepaMandateRepo.findSepaMandateByOptionalIban(iban);
final var resources = Mapper.mapList(entities, HsOfficeSepaMandateResource.class, final var resources = mapper.mapList(entities, HsOfficeSepaMandateResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(resources); return ResponseEntity.ok(resources);
} }
@ -60,7 +61,7 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var entityToSave = map(body, HsOfficeSepaMandateEntity.class, SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER); final var entityToSave = mapper.map(body, HsOfficeSepaMandateEntity.class, SEPA_MANDATE_RESOURCE_TO_ENTITY_POSTMAPPER);
entityToSave.setUuid(UUID.randomUUID()); entityToSave.setUuid(UUID.randomUUID());
final var saved = SepaMandateRepo.save(entityToSave); final var saved = SepaMandateRepo.save(entityToSave);
@ -70,7 +71,7 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
.path("/api/hs/office/SepaMandates/{id}") .path("/api/hs/office/SepaMandates/{id}")
.buildAndExpand(entityToSave.getUuid()) .buildAndExpand(entityToSave.getUuid())
.toUri(); .toUri();
final var mapped = map(saved, HsOfficeSepaMandateResource.class, final var mapped = mapper.map(saved, HsOfficeSepaMandateResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.created(uri).body(mapped); return ResponseEntity.created(uri).body(mapped);
} }
@ -88,7 +89,7 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
if (result.isEmpty()) { if (result.isEmpty()) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result.get(), HsOfficeSepaMandateResource.class, return ResponseEntity.ok(mapper.map(result.get(), HsOfficeSepaMandateResource.class,
SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER)); SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER));
} }
@ -123,7 +124,7 @@ public class HsOfficeSepaMandateController implements HsOfficeSepaMandatesApi {
current.setValidity(toPostgresDateRange(current.getValidity().lower(), body.getValidTo())); current.setValidity(toPostgresDateRange(current.getValidity().lower(), body.getValidTo()));
final var saved = SepaMandateRepo.save(current); final var saved = SepaMandateRepo.save(current);
final var mapped = map(saved, HsOfficeSepaMandateResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER); final var mapped = mapper.map(saved, HsOfficeSepaMandateResource.class, SEPA_MANDATE_ENTITY_TO_RESOURCE_POSTMAPPER);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }

View File

@ -9,15 +9,17 @@ import java.util.stream.Collectors;
/** /**
* A nicer API for ModelMapper. * A nicer API for ModelMapper.
*/ */
public abstract class Mapper { public class Mapper extends ModelMapper {
public final static ModelMapper modelMapper = new ModelMapper(); public Mapper() {
getConfiguration().setAmbiguityIgnored(true);
}
public static <S, T> List<T> mapList(final List<S> source, final Class<T> targetClass) { public <S, T> List<T> mapList(final List<S> source, final Class<T> targetClass) {
return mapList(source, targetClass, null); return mapList(source, targetClass, null);
} }
public static <S, T> List<T> mapList(final List<S> source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) { public <S, T> List<T> mapList(final List<S> source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) {
return source return source
.stream() .stream()
.map(element -> { .map(element -> {
@ -30,18 +32,12 @@ public abstract class Mapper {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
public static <S, T> T map(final S source, final Class<T> targetClass) { public <S, T> T map(final S source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) {
return map(source, targetClass, null);
}
public static <S, T> T map(final S source, final Class<T> targetClass, final BiConsumer<S, T> postMapper) {
if (source == null) { if (source == null) {
return null; return null;
} }
final var target = modelMapper.map(source, targetClass); final var target = map(source, targetClass);
if (postMapper != null) { postMapper.accept(source, target);
postMapper.accept(source, target);
}
return target; return target;
} }
} }

View File

@ -0,0 +1,13 @@
package net.hostsharing.hsadminng.mapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MapperConfiguration {
@Bean
public Mapper modelMapper() {
return new Mapper();
}
}

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.rbac.rbacgrant; package net.hostsharing.hsadminng.rbac.rbacgrant;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.rbac.generated.api.v1.api.RbacGrantsApi; import net.hostsharing.hsadminng.rbac.generated.api.v1.api.RbacGrantsApi;
import net.hostsharing.hsadminng.rbac.generated.api.v1.model.RbacGrantResource; import net.hostsharing.hsadminng.rbac.generated.api.v1.model.RbacGrantResource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -13,16 +14,15 @@ import javax.persistence.EntityManager;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.Mapper.mapList;
@RestController @RestController
public class RbacGrantController implements RbacGrantsApi { public class RbacGrantController implements RbacGrantsApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private RbacGrantRepository rbacGrantRepository; private RbacGrantRepository rbacGrantRepository;
@ -44,7 +44,7 @@ public class RbacGrantController implements RbacGrantsApi {
if (result == null) { if (result == null) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result, RbacGrantResource.class)); return ResponseEntity.ok(mapper.map(result, RbacGrantResource.class));
} }
@Override @Override
@ -55,7 +55,7 @@ public class RbacGrantController implements RbacGrantsApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
return ResponseEntity.ok(mapList(rbacGrantRepository.findAll(), RbacGrantResource.class)); return ResponseEntity.ok(mapper.mapList(rbacGrantRepository.findAll(), RbacGrantResource.class));
} }
@Override @Override
@ -67,7 +67,7 @@ public class RbacGrantController implements RbacGrantsApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var granted = rbacGrantRepository.save(map(body, RbacGrantEntity.class)); final var granted = rbacGrantRepository.save(mapper.map(body, RbacGrantEntity.class));
em.flush(); em.flush();
em.refresh(granted); em.refresh(granted);
@ -76,7 +76,7 @@ public class RbacGrantController implements RbacGrantsApi {
.path("/api/rbac.yaml/grants/{roleUuid}") .path("/api/rbac.yaml/grants/{roleUuid}")
.buildAndExpand(body.getGrantedRoleUuid()) .buildAndExpand(body.getGrantedRoleUuid())
.toUri(); .toUri();
return ResponseEntity.created(uri).body(map(granted, RbacGrantResource.class)); return ResponseEntity.created(uri).body(mapper.map(granted, RbacGrantResource.class));
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.rbac.rbacrole; package net.hostsharing.hsadminng.rbac.rbacrole;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.rbac.generated.api.v1.api.RbacRolesApi; import net.hostsharing.hsadminng.rbac.generated.api.v1.api.RbacRolesApi;
import net.hostsharing.hsadminng.rbac.generated.api.v1.model.RbacRoleResource; import net.hostsharing.hsadminng.rbac.generated.api.v1.model.RbacRoleResource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -10,15 +11,15 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import static net.hostsharing.hsadminng.mapper.Mapper.mapList;
@RestController @RestController
public class RbacRoleController implements RbacRolesApi { public class RbacRoleController implements RbacRolesApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private RbacRoleRepository rbacRoleRepository; private RbacRoleRepository rbacRoleRepository;
@ -30,7 +31,9 @@ public class RbacRoleController implements RbacRolesApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
return ResponseEntity.ok(mapList(rbacRoleRepository.findAll(), RbacRoleResource.class)); final List<RbacRoleEntity> result = rbacRoleRepository.findAll();
return ResponseEntity.ok(mapper.mapList(result, RbacRoleResource.class));
} }
} }

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.rbac.rbacuser; package net.hostsharing.hsadminng.rbac.rbacuser;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.rbac.generated.api.v1.api.RbacUsersApi; import net.hostsharing.hsadminng.rbac.generated.api.v1.api.RbacUsersApi;
import net.hostsharing.hsadminng.rbac.generated.api.v1.model.RbacUserPermissionResource; import net.hostsharing.hsadminng.rbac.generated.api.v1.model.RbacUserPermissionResource;
import net.hostsharing.hsadminng.rbac.generated.api.v1.model.RbacUserResource; import net.hostsharing.hsadminng.rbac.generated.api.v1.model.RbacUserResource;
@ -13,15 +14,15 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.Mapper.mapList;
@RestController @RestController
public class RbacUserController implements RbacUsersApi { public class RbacUserController implements RbacUsersApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private RbacUserRepository rbacUserRepository; private RbacUserRepository rbacUserRepository;
@ -35,14 +36,14 @@ public class RbacUserController implements RbacUsersApi {
if (body.getUuid() == null) { if (body.getUuid() == null) {
body.setUuid(UUID.randomUUID()); body.setUuid(UUID.randomUUID());
} }
final var saved = map(body, RbacUserEntity.class); final var saved = mapper.map(body, RbacUserEntity.class);
rbacUserRepository.create(saved); rbacUserRepository.create(saved);
final var uri = final var uri =
MvcUriComponentsBuilder.fromController(getClass()) MvcUriComponentsBuilder.fromController(getClass())
.path("/api/rbac.yaml/users/{id}") .path("/api/rbac.yaml/users/{id}")
.buildAndExpand(saved.getUuid()) .buildAndExpand(saved.getUuid())
.toUri(); .toUri();
return ResponseEntity.created(uri).body(map(saved, RbacUserResource.class)); return ResponseEntity.created(uri).body(mapper.map(saved, RbacUserResource.class));
} }
@Override @Override
@ -72,7 +73,7 @@ public class RbacUserController implements RbacUsersApi {
if (result == null) { if (result == null) {
return ResponseEntity.notFound().build(); return ResponseEntity.notFound().build();
} }
return ResponseEntity.ok(map(result, RbacUserResource.class)); return ResponseEntity.ok(mapper.map(result, RbacUserResource.class));
} }
@Override @Override
@ -84,7 +85,7 @@ public class RbacUserController implements RbacUsersApi {
) { ) {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
return ResponseEntity.ok(mapList(rbacUserRepository.findByOptionalNameLike(userName), RbacUserResource.class)); return ResponseEntity.ok(mapper.mapList(rbacUserRepository.findByOptionalNameLike(userName), RbacUserResource.class));
} }
@Override @Override
@ -96,7 +97,7 @@ public class RbacUserController implements RbacUsersApi {
) { ) {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
return ResponseEntity.ok(mapList( return ResponseEntity.ok(mapper.mapList(
rbacUserRepository.findPermissionsOfUserByUuid(userUuid), rbacUserRepository.findPermissionsOfUserByUuid(userUuid),
RbacUserPermissionResource.class)); RbacUserPermissionResource.class));
} }

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.test.cust; package net.hostsharing.hsadminng.test.cust;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.test.generated.api.v1.api.TestCustomersApi; import net.hostsharing.hsadminng.test.generated.api.v1.api.TestCustomersApi;
import net.hostsharing.hsadminng.test.generated.api.v1.model.TestCustomerResource; import net.hostsharing.hsadminng.test.generated.api.v1.model.TestCustomerResource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -12,16 +13,15 @@ import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBui
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.Mapper.mapList;
@RestController @RestController
public class TestCustomerController implements TestCustomersApi { public class TestCustomerController implements TestCustomersApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private TestCustomerRepository testCustomerRepository; private TestCustomerRepository testCustomerRepository;
@ -36,7 +36,7 @@ public class TestCustomerController implements TestCustomersApi {
final var result = testCustomerRepository.findCustomerByOptionalPrefixLike(prefix); final var result = testCustomerRepository.findCustomerByOptionalPrefixLike(prefix);
return ResponseEntity.ok(mapList(result, TestCustomerResource.class)); return ResponseEntity.ok(mapper.mapList(result, TestCustomerResource.class));
} }
@Override @Override
@ -52,14 +52,14 @@ public class TestCustomerController implements TestCustomersApi {
customer.setUuid(UUID.randomUUID()); customer.setUuid(UUID.randomUUID());
} }
final var saved = testCustomerRepository.save(map(customer, TestCustomerEntity.class)); final var saved = testCustomerRepository.save(mapper.map(customer, TestCustomerEntity.class));
final var uri = final var uri =
MvcUriComponentsBuilder.fromController(getClass()) MvcUriComponentsBuilder.fromController(getClass())
.path("/api/test/customers/{id}") .path("/api/test/customers/{id}")
.buildAndExpand(customer.getUuid()) .buildAndExpand(customer.getUuid())
.toUri(); .toUri();
return ResponseEntity.created(uri).body(map(saved, TestCustomerResource.class)); return ResponseEntity.created(uri).body(mapper.map(saved, TestCustomerResource.class));
} }
} }

View File

@ -1,5 +1,6 @@
package net.hostsharing.hsadminng.test.pac; package net.hostsharing.hsadminng.test.pac;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.hsadminng.mapper.OptionalFromJson; import net.hostsharing.hsadminng.mapper.OptionalFromJson;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.test.generated.api.v1.api.TestPackagesApi; import net.hostsharing.hsadminng.test.generated.api.v1.api.TestPackagesApi;
@ -13,15 +14,15 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import static net.hostsharing.hsadminng.mapper.Mapper.map;
import static net.hostsharing.hsadminng.mapper.Mapper.mapList;
@RestController @RestController
public class TestPackageController implements TestPackagesApi { public class TestPackageController implements TestPackagesApi {
@Autowired @Autowired
private Context context; private Context context;
@Autowired
private Mapper mapper;
@Autowired @Autowired
private TestPackageRepository testPackageRepository; private TestPackageRepository testPackageRepository;
@ -35,7 +36,7 @@ public class TestPackageController implements TestPackagesApi {
context.define(currentUser, assumedRoles); context.define(currentUser, assumedRoles);
final var result = testPackageRepository.findAllByOptionalNameLike(name); final var result = testPackageRepository.findAllByOptionalNameLike(name);
return ResponseEntity.ok(mapList(result, TestPackageResource.class)); return ResponseEntity.ok(mapper.mapList(result, TestPackageResource.class));
} }
@Override @Override
@ -51,7 +52,7 @@ public class TestPackageController implements TestPackagesApi {
final var current = testPackageRepository.findByUuid(packageUuid); final var current = testPackageRepository.findByUuid(packageUuid);
OptionalFromJson.of(body.getDescription()).ifPresent(current::setDescription); OptionalFromJson.of(body.getDescription()).ifPresent(current::setDescription);
final var saved = testPackageRepository.save(current); final var saved = testPackageRepository.save(current);
final var mapped = map(saved, TestPackageResource.class); final var mapped = mapper.map(saved, TestPackageResource.class);
return ResponseEntity.ok(mapped); return ResponseEntity.ok(mapped);
} }
} }

View File

@ -18,6 +18,7 @@ components:
HsOfficePartnerDetails: HsOfficePartnerDetails:
type: object type: object
nullable: false
properties: properties:
uuid: uuid:
type: string type: string
@ -56,7 +57,7 @@ components:
HsOfficePartnerDetailsPatch: HsOfficePartnerDetailsPatch:
type: object type: object
nullable: true nullable: false
properties: properties:
registrationOffice: registrationOffice:
type: string type: string
@ -90,9 +91,11 @@ components:
required: required:
- personUuid - personUuid
- contactUuid - contactUuid
- details
HsOfficePartnerDetailsInsert: HsOfficePartnerDetailsInsert:
type: object type: object
nullable: false
properties: properties:
registrationOffice: registrationOffice:
type: string type: string

View File

@ -2,6 +2,7 @@ package net.hostsharing.hsadminng.hs.office.bankaccount;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.restassured.http.ContentType; import io.restassured.http.ContentType;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.test.Accepts; import net.hostsharing.test.Accepts;
import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.HsadminNgApplication;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
@ -36,9 +37,6 @@ class HsOfficeBankAccountControllerAcceptanceTest {
@Autowired @Autowired
Context context; Context context;
@Autowired
Context contextMock;
@Autowired @Autowired
HsOfficeBankAccountRepository bankAccountRepo; HsOfficeBankAccountRepository bankAccountRepo;

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.hs.office.bankaccount; package net.hostsharing.hsadminng.hs.office.bankaccount;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -23,6 +24,9 @@ class HsOfficeBankAccountControllerRestTest {
@MockBean @MockBean
Context contextMock; Context contextMock;
@MockBean
Mapper mapper;
@MockBean @MockBean
HsOfficeBankAccountRepository bankAccountRepo; HsOfficeBankAccountRepository bankAccountRepo;

View File

@ -6,16 +6,19 @@ import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository;
import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository;
import net.hostsharing.test.Array; import net.hostsharing.test.Array;
import net.hostsharing.test.JpaAttempt; import net.hostsharing.test.JpaAttempt;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.modelmapper.internal.bytebuddy.utility.RandomString; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Container;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
@ -31,8 +34,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeBankAccountRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTest { class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired
@ -311,7 +313,7 @@ class HsOfficeBankAccountRepositoryIntegrationTest extends ContextBasedTest {
} }
private HsOfficeBankAccountEntity givenSomeTemporaryBankAccount(final String createdByUser) { private HsOfficeBankAccountEntity givenSomeTemporaryBankAccount(final String createdByUser) {
final var random = RandomString.make(3); final var random = RandomStringUtils.randomAlphabetic(3);
return givenSomeTemporaryBankAccount(createdByUser, () -> return givenSomeTemporaryBankAccount(createdByUser, () ->
hsOfficeBankAccount( hsOfficeBankAccount(
"some temp acc #" + random, "some temp acc #" + random,

View File

@ -6,15 +6,16 @@ import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository;
import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository;
import net.hostsharing.test.Array; import net.hostsharing.test.Array;
import net.hostsharing.test.JpaAttempt; import net.hostsharing.test.JpaAttempt;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.modelmapper.internal.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Container;
@ -32,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeContactRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficeContactRepositoryIntegrationTest extends ContextBasedTest { class HsOfficeContactRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired
@ -305,7 +305,7 @@ class HsOfficeContactRepositoryIntegrationTest extends ContextBasedTest {
} }
private HsOfficeContactEntity givenSomeTemporaryContact(final String createdByUser) { private HsOfficeContactEntity givenSomeTemporaryContact(final String createdByUser) {
final var random = RandomString.make(12); final var random = RandomStringUtils.randomAlphabetic(12);
return givenSomeTemporaryContact(createdByUser, () -> return givenSomeTemporaryContact(createdByUser, () ->
hsOfficeContact( hsOfficeContact(
"some temporary contact #" + random, "some temporary contact #" + random,

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.hs.office.coopassets; package net.hostsharing.hsadminng.hs.office.coopassets;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.test.JsonBuilder; import net.hostsharing.test.JsonBuilder;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.EnumSource;
@ -28,6 +29,9 @@ class HsOfficeCoopAssetsTransactionControllerRestTest {
@MockBean @MockBean
Context contextMock; Context contextMock;
@MockBean
Mapper mapper;
@MockBean @MockBean
HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo; HsOfficeCoopAssetsTransactionRepository coopAssetsTransactionRepo;

View File

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -32,8 +33,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeCoopAssetsTransactionRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBasedTest { class HsOfficeCoopAssetsTransactionRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -1,6 +1,7 @@
package net.hostsharing.hsadminng.hs.office.coopshares; package net.hostsharing.hsadminng.hs.office.coopshares;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.test.JsonBuilder; import net.hostsharing.test.JsonBuilder;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.EnumSource;
@ -28,6 +29,9 @@ class HsOfficeCoopSharesTransactionControllerRestTest {
@MockBean @MockBean
Context contextMock; Context contextMock;
@MockBean
Mapper mapper;
@MockBean @MockBean
HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo; HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;

View File

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
@ -30,8 +31,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeCoopSharesTransactionRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBasedTest { class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -43,9 +43,6 @@ class HsOfficeDebitorControllerAcceptanceTest {
@Autowired @Autowired
Context context; Context context;
@Autowired
Context contextMock;
@Autowired @Autowired
HsOfficeDebitorRepository debitorRepo; HsOfficeDebitorRepository debitorRepo;

View File

@ -19,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -32,8 +33,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeDebitorRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTest { class HsOfficeDebitorRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -2,12 +2,17 @@ package net.hostsharing.hsadminng.hs.office.membership;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionRepository; import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionRepository;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.orm.jpa.JpaObjectRetrievalFailureException; import org.springframework.orm.jpa.JpaObjectRetrievalFailureException;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@ -22,6 +27,8 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(HsOfficeMembershipController.class) @WebMvcTest(HsOfficeMembershipController.class)
@Import(Mapper.class)
@RunWith(SpringRunner.class)
public class HsOfficeMembershipControllerRestTest { public class HsOfficeMembershipControllerRestTest {
@Autowired @Autowired

View File

@ -4,12 +4,14 @@ import com.vladmihalcea.hibernate.type.range.Range;
import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity; import net.hostsharing.hsadminng.hs.office.debitor.HsOfficeDebitorEntity;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeMembershipPatchResource;
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeReasonForTerminationResource; import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeReasonForTerminationResource;
import net.hostsharing.hsadminng.mapper.Mapper;
import net.hostsharing.test.PatchUnitTestBase; import net.hostsharing.test.PatchUnitTestBase;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import java.time.LocalDate; import java.time.LocalDate;
@ -31,7 +33,6 @@ class HsOfficeMembershipEntityPatcherUnitTest extends PatchUnitTestBase<
> { > {
private static final UUID INITIAL_MEMBERSHIP_UUID = UUID.randomUUID(); private static final UUID INITIAL_MEMBERSHIP_UUID = UUID.randomUUID();
private static final UUID INITIAL_MAIN_DEBITOR_UUID = UUID.randomUUID();
private static final UUID PATCHED_MAIN_DEBITOR_UUID = UUID.randomUUID(); private static final UUID PATCHED_MAIN_DEBITOR_UUID = UUID.randomUUID();
private static final LocalDate GIVEN_VALID_FROM = LocalDate.parse("2020-04-15"); private static final LocalDate GIVEN_VALID_FROM = LocalDate.parse("2020-04-15");
private static final LocalDate PATCHED_VALID_TO = LocalDate.parse("2022-12-31"); private static final LocalDate PATCHED_VALID_TO = LocalDate.parse("2022-12-31");
@ -39,6 +40,8 @@ class HsOfficeMembershipEntityPatcherUnitTest extends PatchUnitTestBase<
@Mock @Mock
private EntityManager em; private EntityManager em;
private Mapper mapper = new Mapper();
@BeforeEach @BeforeEach
void initMocks() { void initMocks() {
lenient().when(em.getReference(eq(HsOfficeDebitorEntity.class), any())).thenAnswer(invocation -> lenient().when(em.getReference(eq(HsOfficeDebitorEntity.class), any())).thenAnswer(invocation ->
@ -63,8 +66,8 @@ class HsOfficeMembershipEntityPatcherUnitTest extends PatchUnitTestBase<
} }
@Override @Override
protected HsOfficeMembershipEntityPatcher createPatcher(final HsOfficeMembershipEntity Membership) { protected HsOfficeMembershipEntityPatcher createPatcher(final HsOfficeMembershipEntity membership) {
return new HsOfficeMembershipEntityPatcher(em, Membership); return new HsOfficeMembershipEntityPatcher(em, mapper, membership);
} }
@Override @Override

View File

@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -32,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeMembershipRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficeMembershipRepositoryIntegrationTest extends ContextBasedTest { class HsOfficeMembershipRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -2,11 +2,11 @@ package net.hostsharing.hsadminng.hs.office.partner;
import io.restassured.RestAssured; import io.restassured.RestAssured;
import io.restassured.http.ContentType; import io.restassured.http.ContentType;
import net.hostsharing.test.Accepts;
import net.hostsharing.hsadminng.HsadminNgApplication; import net.hostsharing.hsadminng.HsadminNgApplication;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository; import net.hostsharing.hsadminng.hs.office.person.HsOfficePersonRepository;
import net.hostsharing.test.Accepts;
import net.hostsharing.test.JpaAttempt; import net.hostsharing.test.JpaAttempt;
import org.json.JSONException; import org.json.JSONException;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
@ -166,15 +166,16 @@ class HsOfficePartnerControllerAcceptanceTest {
.body(""" .body("""
{ {
"contactUuid": "%s", "contactUuid": "%s",
"personUuid": "%s" "personUuid": "%s",
"details": {}
} }
""".formatted(givenContactUuid, givenPerson.getUuid())) """.formatted(givenContactUuid, givenPerson.getUuid()))
.port(port) .port(port)
.when() .when()
.post("http://localhost/api/hs/office/partners") .post("http://localhost/api/hs/office/partners")
.then().log().all().assertThat() .then().log().all().assertThat()
.statusCode(404) .statusCode(400)
.body("message", is("cannot find contact uuid 3fa85f64-5717-4562-b3fc-2c963f66afa6")); .body("message", is("Unable to find Contact with uuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
// @formatter:on // @formatter:on
} }
@ -193,19 +194,15 @@ class HsOfficePartnerControllerAcceptanceTest {
{ {
"contactUuid": "%s", "contactUuid": "%s",
"personUuid": "%s", "personUuid": "%s",
"registrationOffice": "Registergericht Hamburg", "details": {}
"registrationNumber": "123456",
"birthName": null,
"birthday": null,
"dateOfDeath": null
} }
""".formatted(givenContact.getUuid(), givenPersonUuid)) """.formatted(givenContact.getUuid(), givenPersonUuid))
.port(port) .port(port)
.when() .when()
.post("http://localhost/api/hs/office/partners") .post("http://localhost/api/hs/office/partners")
.then().log().all().assertThat() .then().log().all().assertThat()
.statusCode(404) .statusCode(400)
.body("message", is("cannot find person uuid 3fa85f64-5717-4562-b3fc-2c963f66afa6")); .body("message", is("Unable to find Person with uuid 3fa85f64-5717-4562-b3fc-2c963f66afa6"));
// @formatter:on // @formatter:on
} }
} }

View File

@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -29,8 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficePartnerRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficePartnerRepositoryIntegrationTest extends ContextBasedTest { class HsOfficePartnerRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -6,14 +6,15 @@ import net.hostsharing.hsadminng.rbac.rbacgrant.RawRbacGrantRepository;
import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository; import net.hostsharing.hsadminng.rbac.rbacrole.RawRbacRoleRepository;
import net.hostsharing.test.Array; import net.hostsharing.test.Array;
import net.hostsharing.test.JpaAttempt; import net.hostsharing.test.JpaAttempt;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.modelmapper.internal.bytebuddy.utility.RandomString;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
@ -30,8 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficePersonRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTest { class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired
@ -304,7 +304,7 @@ class HsOfficePersonRepositoryIntegrationTest extends ContextBasedTest {
private HsOfficePersonEntity givenSomeTemporaryPerson(final String createdByUser) { private HsOfficePersonEntity givenSomeTemporaryPerson(final String createdByUser) {
return givenSomeTemporaryPerson(createdByUser, () -> return givenSomeTemporaryPerson(createdByUser, () ->
hsOfficePerson("some temporary person #" + RandomString.make(12))); hsOfficePerson("some temporary person #" + RandomStringUtils.random(12)));
} }
void exactlyThesePersonsAreReturned(final List<HsOfficePersonEntity> actualResult, final String... personLabels) { void exactlyThesePersonsAreReturned(final List<HsOfficePersonEntity> actualResult, final String... personLabels) {

View File

@ -1,5 +1,6 @@
package net.hostsharing.hsadminng.hs.office.relationship; package net.hostsharing.hsadminng.hs.office.relationship;
import net.hostsharing.hsadminng.HsadminNgApplication;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.context.ContextBasedTest; import net.hostsharing.hsadminng.context.ContextBasedTest;
import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository; import net.hostsharing.hsadminng.hs.office.contact.HsOfficeContactRepository;
@ -15,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -28,8 +30,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeRelationshipRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficeRelationshipRepositoryIntegrationTest extends ContextBasedTest { class HsOfficeRelationshipRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -17,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -32,8 +33,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { HsOfficeSepaMandateRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class HsOfficeSepaMandateRepositoryIntegrationTest extends ContextBasedTest { class HsOfficeSepaMandateRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
@ -28,8 +29,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assumptions.assumeThat; import static org.assertj.core.api.Assumptions.assumeThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { RbacGrantRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class RbacGrantRepositoryIntegrationTest extends ContextBasedTest { class RbacGrantRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -1,11 +1,15 @@
package net.hostsharing.hsadminng.rbac.rbacrole; package net.hostsharing.hsadminng.rbac.rbacrole;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@ -18,12 +22,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(RbacRoleController.class) @WebMvcTest(RbacRoleController.class)
@Import(Mapper.class)
@RunWith(SpringRunner.class)
class RbacRoleControllerRestTest { class RbacRoleControllerRestTest {
@Autowired @Autowired
MockMvc mockMvc; MockMvc mockMvc;
@MockBean @MockBean
Context contextMock; Context contextMock;
@MockBean @MockBean
RbacRoleRepository rbacRoleRepository; RbacRoleRepository rbacRoleRepository;
@ -32,23 +40,23 @@ class RbacRoleControllerRestTest {
// given // given
when(rbacRoleRepository.findAll()).thenReturn( when(rbacRoleRepository.findAll()).thenReturn(
asList(hostmasterRole, customerXxxOwner, customerXxxAdmin)); asList(hostmasterRole, customerXxxOwner, customerXxxAdmin));
// when // when
mockMvc.perform(MockMvcRequestBuilders mockMvc.perform(MockMvcRequestBuilders
.get("/api/rbac/roles") .get("/api/rbac/roles")
.header("current-user", "superuser-alex@hostsharing.net") .header("current-user", "superuser-alex@hostsharing.net")
.accept(MediaType.APPLICATION_JSON)) .accept(MediaType.APPLICATION_JSON))
// then // then
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3))) .andExpect(jsonPath("$", hasSize(3)))
.andExpect(jsonPath("$[0].roleName", is("global#global.admin"))) .andExpect(jsonPath("$[0].roleName", is("global#global.admin")))
.andExpect(jsonPath("$[1].roleName", is("test_customer#xxx.owner"))) .andExpect(jsonPath("$[1].roleName", is("test_customer#xxx.owner")))
.andExpect(jsonPath("$[2].roleName", is("test_customer#xxx.admin"))) .andExpect(jsonPath("$[2].roleName", is("test_customer#xxx.admin")))
.andExpect(jsonPath("$[2].uuid", is(customerXxxAdmin.getUuid().toString()))) .andExpect(jsonPath("$[2].uuid", is(customerXxxAdmin.getUuid().toString())))
.andExpect(jsonPath("$[2].objectUuid", is(customerXxxAdmin.getObjectUuid().toString()))) .andExpect(jsonPath("$[2].objectUuid", is(customerXxxAdmin.getObjectUuid().toString())))
.andExpect(jsonPath("$[2].objectTable", is(customerXxxAdmin.getObjectTable().toString()))) .andExpect(jsonPath("$[2].objectTable", is(customerXxxAdmin.getObjectTable().toString())))
.andExpect(jsonPath("$[2].objectIdName", is(customerXxxAdmin.getObjectIdName().toString()))); .andExpect(jsonPath("$[2].objectIdName", is(customerXxxAdmin.getObjectIdName().toString())));
} }
} }

View File

@ -2,12 +2,14 @@ package net.hostsharing.hsadminng.rbac.rbacrole;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.test.Array; import net.hostsharing.test.Array;
import net.hostsharing.test.JpaAttempt;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -19,8 +21,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { Context.class, RbacRoleRepository.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class RbacRoleRepositoryIntegrationTest { class RbacRoleRepositoryIntegrationTest {
@Autowired @Autowired

View File

@ -1,11 +1,15 @@
package net.hostsharing.hsadminng.rbac.rbacuser; package net.hostsharing.hsadminng.rbac.rbacuser;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@ -19,12 +23,16 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(RbacUserController.class) @WebMvcTest(RbacUserController.class)
@Import(Mapper.class)
@RunWith(SpringRunner.class)
class RbacUserControllerRestTest { class RbacUserControllerRestTest {
@Autowired @Autowired
MockMvc mockMvc; MockMvc mockMvc;
@MockBean @MockBean
Context contextMock; Context contextMock;
@MockBean @MockBean
RbacUserRepository rbacUserRepository; RbacUserRepository rbacUserRepository;

View File

@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.jpa.JpaSystemException; import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
@ -24,8 +25,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { RbacUserRepository.class, Context.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class RbacUserRepositoryIntegrationTest extends ContextBasedTest { class RbacUserRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -2,12 +2,14 @@ package net.hostsharing.hsadminng.test.cust;
import net.hostsharing.hsadminng.context.Context; import net.hostsharing.hsadminng.context.Context;
import net.hostsharing.hsadminng.context.ContextBasedTest; import net.hostsharing.hsadminng.context.ContextBasedTest;
import net.hostsharing.test.JpaAttempt;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
@ -20,8 +22,7 @@ import static net.hostsharing.test.JpaAttempt.attempt;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { Context.class, TestCustomerRepository.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class TestCustomerRepositoryIntegrationTest extends ContextBasedTest { class TestCustomerRepositoryIntegrationTest extends ContextBasedTest {
@Autowired @Autowired

View File

@ -9,6 +9,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.orm.ObjectOptimisticLockingFailureException; import org.springframework.orm.ObjectOptimisticLockingFailureException;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
@ -19,8 +20,7 @@ import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@DataJpaTest @DataJpaTest
@ComponentScan(basePackageClasses = { Context.class, TestCustomerRepository.class, JpaAttempt.class }) @Import( { Context.class, JpaAttempt.class })
@DirtiesContext
class TestPackageRepositoryIntegrationTest { class TestPackageRepositoryIntegrationTest {
@Autowired @Autowired

View File

@ -7,16 +7,21 @@ import lombok.Setter;
import net.hostsharing.hsadminng.mapper.Mapper; import net.hostsharing.hsadminng.mapper.Mapper;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.fail;
class MapperUnitTest { class MapperUnitTest {
private Mapper mapper = new Mapper();
@Getter @Getter
@Setter @Setter
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public static class SourceBean { public static class SourceBean {
private String a; private String a;
private String b; private String b;
} }
@ -26,6 +31,7 @@ class MapperUnitTest {
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public static class TargetBean { public static class TargetBean {
private String a; private String a;
private String b; private String b;
private String c; private String c;
@ -34,14 +40,14 @@ class MapperUnitTest {
@Test @Test
void mapsNullBeanToNull() { void mapsNullBeanToNull() {
final SourceBean givenSource = null; final SourceBean givenSource = null;
final var result = Mapper.map(givenSource, TargetBean.class, (s, t) -> { fail("should not have been called"); }); final var result = mapper.map(givenSource, TargetBean.class, (s, t) -> {fail("should not have been called");});
assertThat(result).isNull(); assertThat(result).isNull();
} }
@Test @Test
void mapsBean() { void mapsBean() {
final SourceBean givenSource = new SourceBean("1234", "Text"); final SourceBean givenSource = new SourceBean("1234", "Text");
final var result = Mapper.map(givenSource, TargetBean.class, null); final var result = mapper.map(givenSource, TargetBean.class);
assertThat(result).usingRecursiveComparison().isEqualTo( assertThat(result).usingRecursiveComparison().isEqualTo(
new TargetBean("1234", "Text", null) new TargetBean("1234", "Text", null)
); );
@ -50,9 +56,38 @@ class MapperUnitTest {
@Test @Test
void mapsBeanWithPostmapper() { void mapsBeanWithPostmapper() {
final SourceBean givenSource = new SourceBean("1234", "Text"); final SourceBean givenSource = new SourceBean("1234", "Text");
final var result = Mapper.map(givenSource, TargetBean.class, (s, t) -> { t.setC("Extra"); }); final var result = mapper.map(givenSource, TargetBean.class, (s, t) -> {t.setC("Extra");});
assertThat(result).usingRecursiveComparison().isEqualTo( assertThat(result).usingRecursiveComparison().isEqualTo(
new TargetBean("1234", "Text", "Extra") new TargetBean("1234", "Text", "Extra")
); );
} }
@Test
void mapsList() {
final var givenSource = List.of(
new SourceBean("111", "Text A"),
new SourceBean("222", "Text B"),
new SourceBean("333", "Text C"));
final var result = mapper.mapList(givenSource, TargetBean.class);
assertThat(result).usingRecursiveComparison().isEqualTo(
List.of(
new TargetBean("111", "Text A", null),
new TargetBean("222", "Text B", null),
new TargetBean("333", "Text C", null)));
}
@Test
void mapsListWithPostMapper() {
final var givenSource = List.of(
new SourceBean("111", "Text A"),
new SourceBean("222", "Text B"),
new SourceBean("333", "Text C"));
final var result = mapper.mapList(givenSource, TargetBean.class, (s, t) -> {t.setC("Extra");});
assertThat(result).usingRecursiveComparison().isEqualTo(
List.of(
new TargetBean("111", "Text A", "Extra"),
new TargetBean("222", "Text B", "Extra"),
new TargetBean("333", "Text C", "Extra")));
}
} }