hs-office-coopshares: add validation + RestTest for Controller + improve test data
This commit is contained in:
+2
-1
@@ -16,6 +16,7 @@ import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.validation.ValidationException;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -57,7 +58,7 @@ public class RestResponseEntityExceptionHandler
|
||||
return errorResponse(request, HttpStatus.BAD_REQUEST, message);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Iban4jException.class)
|
||||
@ExceptionHandler({ Iban4jException.class, ValidationException.class })
|
||||
protected ResponseEntity<CustomErrorResponse> handleIbanAndBicExceptions(
|
||||
final Throwable exc, final WebRequest request) {
|
||||
final var message = firstLine(NestedExceptionUtils.getMostSpecificCause(exc).getMessage());
|
||||
|
||||
+51
-26
@@ -7,19 +7,23 @@ import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopSh
|
||||
import net.hostsharing.hsadminng.hs.office.generated.api.v1.model.HsOfficeCoopSharesTransactionResource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.format.annotation.DateTimeFormat.ISO;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.validation.Valid;
|
||||
import javax.validation.ValidationException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.BiConsumer;
|
||||
|
||||
import static java.lang.String.join;
|
||||
import static net.hostsharing.hsadminng.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.SUBSCRIPTION;
|
||||
|
||||
@RestController
|
||||
|
||||
@@ -31,27 +35,22 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
|
||||
@Autowired
|
||||
private HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
|
||||
|
||||
@Autowired
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
@Transactional(readOnly = true)
|
||||
public ResponseEntity<List<HsOfficeCoopSharesTransactionResource>> listCoopShares(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
final UUID membershipUuid,
|
||||
final @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate fromValueDate,
|
||||
final @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate toValueDate) {
|
||||
final @DateTimeFormat(iso = ISO.DATE) LocalDate fromValueDate,
|
||||
final @DateTimeFormat(iso = ISO.DATE) LocalDate toValueDate) {
|
||||
context.define(currentUser, assumedRoles);
|
||||
|
||||
|
||||
final var entities = coopSharesTransactionRepo.findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
|
||||
membershipUuid,
|
||||
fromValueDate,
|
||||
toValueDate);
|
||||
|
||||
final var resources = Mapper.mapList(entities, HsOfficeCoopSharesTransactionResource.class,
|
||||
COOP_SHARES_ENTITY_TO_RESOURCE_POSTMAPPER);
|
||||
final var resources = Mapper.mapList(entities, HsOfficeCoopSharesTransactionResource.class);
|
||||
return ResponseEntity.ok(resources);
|
||||
}
|
||||
|
||||
@@ -60,14 +59,12 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
|
||||
public ResponseEntity<HsOfficeCoopSharesTransactionResource> addCoopSharesTransaction(
|
||||
final String currentUser,
|
||||
final String assumedRoles,
|
||||
@Valid final HsOfficeCoopSharesTransactionInsertResource body) {
|
||||
@Valid final HsOfficeCoopSharesTransactionInsertResource requestBody) {
|
||||
|
||||
context.define(currentUser, assumedRoles);
|
||||
validate(requestBody);
|
||||
|
||||
final var entityToSave = map(
|
||||
body,
|
||||
HsOfficeCoopSharesTransactionEntity.class,
|
||||
COOP_SHARES_RESOURCE_TO_ENTITY_POSTMAPPER);
|
||||
final var entityToSave = map(requestBody, HsOfficeCoopSharesTransactionEntity.class);
|
||||
entityToSave.setUuid(UUID.randomUUID());
|
||||
|
||||
final var saved = coopSharesTransactionRepo.save(entityToSave);
|
||||
@@ -77,19 +74,47 @@ public class HsOfficeCoopSharesTransactionController implements HsOfficeCoopShar
|
||||
.path("/api/hs/office/CoopSharesTransactions/{id}")
|
||||
.buildAndExpand(entityToSave.getUuid())
|
||||
.toUri();
|
||||
final var mapped = map(saved, HsOfficeCoopSharesTransactionResource.class,
|
||||
COOP_SHARES_ENTITY_TO_RESOURCE_POSTMAPPER);
|
||||
final var mapped = map(saved, HsOfficeCoopSharesTransactionResource.class);
|
||||
return ResponseEntity.created(uri).body(mapped);
|
||||
}
|
||||
|
||||
final BiConsumer<HsOfficeCoopSharesTransactionEntity, HsOfficeCoopSharesTransactionResource> COOP_SHARES_ENTITY_TO_RESOURCE_POSTMAPPER = (entity, resource) -> {
|
||||
// resource.setValidFrom(entity.getValidity().lower());
|
||||
// if (entity.getValidity().hasUpperBound()) {
|
||||
// resource.setValidTo(entity.getValidity().upper().minusDays(1));
|
||||
// }
|
||||
};
|
||||
private void validate(final HsOfficeCoopSharesTransactionInsertResource requestBody) {
|
||||
final var violations = new ArrayList<String>();
|
||||
validateSubscriptionTransaction(requestBody, violations);
|
||||
validateCancellationTransaction(requestBody, violations);
|
||||
validateSharesCount(requestBody, violations);
|
||||
if (violations.size() > 0) {
|
||||
throw new ValidationException("[" + join(", ", violations) + "]");
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateSubscriptionTransaction(
|
||||
final HsOfficeCoopSharesTransactionInsertResource requestBody,
|
||||
final ArrayList<String> violations) {
|
||||
if (requestBody.getTransactionType() == SUBSCRIPTION
|
||||
&& requestBody.getSharesCount() < 0) {
|
||||
violations.add("for %s, sharesCount must be positive but is \"%d\"".formatted(
|
||||
requestBody.getTransactionType(), requestBody.getSharesCount()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateCancellationTransaction(
|
||||
final HsOfficeCoopSharesTransactionInsertResource requestBody,
|
||||
final ArrayList<String> violations) {
|
||||
if (requestBody.getTransactionType() == CANCELLATION
|
||||
&& requestBody.getSharesCount() > 0) {
|
||||
violations.add("for %s, sharesCount must be negative but is \"%d\"".formatted(
|
||||
requestBody.getTransactionType(), requestBody.getSharesCount()));
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateSharesCount(
|
||||
final HsOfficeCoopSharesTransactionInsertResource requestBody,
|
||||
final ArrayList<String> violations) {
|
||||
if (requestBody.getSharesCount() == 0) {
|
||||
violations.add("sharesCount must not be 0 but is \"%d\"".formatted(
|
||||
requestBody.getSharesCount()));
|
||||
}
|
||||
}
|
||||
|
||||
final BiConsumer<HsOfficeCoopSharesTransactionInsertResource, HsOfficeCoopSharesTransactionEntity> COOP_SHARES_RESOURCE_TO_ENTITY_POSTMAPPER = (resource, entity) -> {
|
||||
// entity.setValidity(toPostgresDateRange(resource.getValidFrom(), resource.getValidTo()));
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ components:
|
||||
enum:
|
||||
- ADJUSTMENT
|
||||
- SUBSCRIPTION
|
||||
- CANCELLATION;
|
||||
- CANCELLATION
|
||||
|
||||
HsOfficeCoopSharesTransaction:
|
||||
type: object
|
||||
@@ -44,9 +44,12 @@ components:
|
||||
format: date
|
||||
reference:
|
||||
type: string
|
||||
minLength: 6
|
||||
maxLength: 48
|
||||
comment:
|
||||
type: string
|
||||
required:
|
||||
- membershipUuid
|
||||
- transactionType
|
||||
- sharesCount
|
||||
- valueDate
|
||||
|
||||
@@ -24,9 +24,9 @@ begin
|
||||
insert
|
||||
into hs_office_coopsharestransaction(uuid, membershipuuid, transactiontype, valuedate, sharecount, reference, comment)
|
||||
values
|
||||
(uuid_generate_v4(), membership.uuid, 'SUBSCRIPTION', '2010-03-15', 2, 'ref '||givenMembershipNumber||'-1', 'initial subscription'),
|
||||
(uuid_generate_v4(), membership.uuid, 'SUBSCRIPTION', '2021-09-01', 24, 'ref '||givenMembershipNumber||'-2', 'subscibing more'),
|
||||
(uuid_generate_v4(), membership.uuid, 'CANCELLATION', '2022-10-20', 12, 'ref '||givenMembershipNumber||'-3', 'cancelling some');
|
||||
(uuid_generate_v4(), membership.uuid, 'SUBSCRIPTION', '2010-03-15', 4, 'ref '||givenMembershipNumber||'-1', 'initial subscription'),
|
||||
(uuid_generate_v4(), membership.uuid, 'CANCELLATION', '2021-09-01', -2, 'ref '||givenMembershipNumber||'-2', 'cancelling some'),
|
||||
(uuid_generate_v4(), membership.uuid, 'ADJUSTMENT', '2022-10-20', 2, 'ref '||givenMembershipNumber||'-3', 'some adjustment');
|
||||
end; $$;
|
||||
--//
|
||||
|
||||
|
||||
Reference in New Issue
Block a user