introduced MembershipValidator
This commit is contained in:
parent
c6bf856b34
commit
d1d6001842
@ -23,11 +23,16 @@ public class MembershipService {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(MembershipService.class);
|
||||
|
||||
private final MembershipValidator membershipValidator;
|
||||
|
||||
private final MembershipRepository membershipRepository;
|
||||
|
||||
private final MembershipMapper membershipMapper;
|
||||
|
||||
public MembershipService(MembershipRepository membershipRepository, MembershipMapper membershipMapper) {
|
||||
public MembershipService(final MembershipValidator membershipValidator,
|
||||
final MembershipRepository membershipRepository,
|
||||
final MembershipMapper membershipMapper) {
|
||||
this.membershipValidator = membershipValidator;
|
||||
this.membershipRepository = membershipRepository;
|
||||
this.membershipMapper = membershipMapper;
|
||||
}
|
||||
@ -40,6 +45,9 @@ public class MembershipService {
|
||||
*/
|
||||
public MembershipDTO save(MembershipDTO membershipDTO) {
|
||||
log.debug("Request to save Membership : {}", membershipDTO);
|
||||
|
||||
membershipValidator.validate(membershipDTO);
|
||||
|
||||
Membership membership = membershipMapper.toEntity(membershipDTO);
|
||||
membership = membershipRepository.save(membership);
|
||||
return membershipMapper.toDto(membership);
|
||||
|
@ -0,0 +1,15 @@
|
||||
package org.hostsharing.hsadminng.service;
|
||||
|
||||
import org.hostsharing.hsadminng.domain.Membership;
|
||||
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
|
||||
import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MembershipValidator {
|
||||
public void validate(final MembershipDTO membershipDTO) {
|
||||
if (membershipDTO.getUntilDate() != null && !membershipDTO.getUntilDate().isAfter(membershipDTO.getSinceDate())) {
|
||||
throw new BadRequestAlertException("Invalid untilDate", Membership.ENTITY_NAME, "untilDateMustBeAfterSinceDate");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
package org.hostsharing.hsadminng.service.dto;
|
||||
import java.time.LocalDate;
|
||||
import javax.validation.constraints.*;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* A DTO for the Membership entity.
|
||||
@ -16,11 +18,16 @@ public class MembershipDTO implements Serializable {
|
||||
|
||||
private LocalDate untilDate;
|
||||
|
||||
|
||||
private Long customerId;
|
||||
|
||||
private String customerPrefix;
|
||||
|
||||
public MembershipDTO with(
|
||||
Consumer<MembershipDTO> builderFunction) {
|
||||
builderFunction.accept(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
"shareSubscriptionPositivQuantity": "Zeichnungen von Geschäftsanteilen erfordern eine positive Stückzahl",
|
||||
"shareCancellationNegativeQuantity": "Kündigungen von Geschäftsanteilen erfordern eine negative Stückzahl",
|
||||
"shareTransactionImmutable": "Transaktionen mit Geschäftsanteilen sind unveränderlich",
|
||||
"membershipNotDeletable": "Mitgliedschaft kann nicht gelöscht werden, setze stattdessen das 'untilDate'"
|
||||
"membershipNotDeletable": "Mitgliedschaft kann nicht gelöscht werden, setze stattdessen das 'untilDate'",
|
||||
"untilDateMustBeAfterSinceDate": "Mitgliedshafts-Austrittsdatum muss nach dem Beitrittsdatum liegen"
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
"shareSubscriptionPositivQuantity": "Share subscriptions require a positive quantity",
|
||||
"shareCancellationNegativeQuantity": "Share cancellations require a negative quantity",
|
||||
"shareTransactionImmutable": "Share transactions are immutable",
|
||||
"membershipNotDeletable": "Membership cannot be deleted, instead set 'untilDate'"
|
||||
"membershipNotDeletable": "Membership cannot be deleted, instead set 'untilDate'",
|
||||
"untilDateMustBeAfterSinceDate": "Membership until date must be after since date"
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,9 @@
|
||||
package org.hostsharing.hsadminng.service;
|
||||
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.hostsharing.hsadminng.domain.Membership;
|
||||
import org.hostsharing.hsadminng.repository.MembershipRepository;
|
||||
import org.hostsharing.hsadminng.service.mapper.MembershipMapper;
|
||||
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
|
||||
import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@ -13,6 +14,8 @@ import org.mockito.junit.MockitoRule;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowableOfType;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
public class MembershipServiceUnitTest {
|
||||
|
||||
@ -23,7 +26,7 @@ public class MembershipServiceUnitTest {
|
||||
private MembershipRepository membershipRepository;
|
||||
|
||||
@Mock
|
||||
private MembershipMapper membershipMapper;
|
||||
private MembershipValidator membershipValidator;
|
||||
|
||||
@InjectMocks
|
||||
private MembershipService membershipService;
|
||||
@ -37,4 +40,20 @@ public class MembershipServiceUnitTest {
|
||||
assertThat(throwException).isEqualToComparingFieldByField(
|
||||
new BadRequestAlertException("Membership cannot be deleted", "membership", "membershipNotDeletable"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveRejectsInvalidMembershipDTO() {
|
||||
// given
|
||||
final MembershipDTO givenMembershipDTO = new MembershipDTO();
|
||||
final BadRequestAlertException givenBadRequestAlertException = new BadRequestAlertException("Invalid Membership", Membership.ENTITY_NAME, "invalidMembership");
|
||||
doThrow(givenBadRequestAlertException).when(membershipValidator).validate(givenMembershipDTO);
|
||||
|
||||
// when
|
||||
final Throwable throwException = catchThrowableOfType(() -> membershipService.save(givenMembershipDTO), BadRequestAlertException.class);
|
||||
|
||||
// then
|
||||
assertThat(throwException).isSameAs(givenBadRequestAlertException);
|
||||
verify(membershipRepository, never()).save(any(Membership.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,43 @@
|
||||
package org.hostsharing.hsadminng.service;
|
||||
|
||||
import org.hostsharing.hsadminng.repository.MembershipRepository;
|
||||
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
|
||||
import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnit;
|
||||
import org.mockito.junit.MockitoRule;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.catchThrowableOfType;
|
||||
|
||||
public class MembershipValidatorUnitTest {
|
||||
|
||||
@Rule
|
||||
public MockitoRule mockitoRule = MockitoJUnit.rule();
|
||||
|
||||
@Mock
|
||||
private MembershipRepository membershipRepository;
|
||||
|
||||
@InjectMocks
|
||||
private MembershipValidator membershipValidator;
|
||||
|
||||
@Test
|
||||
public void shouldRejectIfUntilDateIsNotAfterSinceDate() {
|
||||
// given
|
||||
final MembershipDTO membershipDTO = new MembershipDTO();
|
||||
membershipDTO.setSinceDate(LocalDate.parse("2019-04-11"));
|
||||
membershipDTO.setUntilDate(LocalDate.parse("2019-04-11"));
|
||||
|
||||
// when
|
||||
final Throwable throwException = catchThrowableOfType(() -> membershipValidator.validate(membershipDTO), BadRequestAlertException.class);
|
||||
|
||||
// then
|
||||
assertThat(throwException).isNotNull();
|
||||
|
||||
}
|
||||
}
|
@ -55,7 +55,7 @@ public class ShareServiceUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void saveNotUpdateAnyExistingShareTransaction() {
|
||||
public void saveShouldNotUpdateAnyExistingShareTransaction() {
|
||||
// given
|
||||
final ShareDTO givenShareDTO = givenShareDTO(anyNonNullId(), ShareAction.SUBSCRIPTION, anyPositiveNumber());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user