introduced MembershipValidator

This commit is contained in:
Michael Hoennig 2019-04-11 18:06:12 +02:00
parent c6bf856b34
commit d1d6001842
8 changed files with 103 additions and 9 deletions

View File

@ -23,11 +23,16 @@ public class MembershipService {
private final Logger log = LoggerFactory.getLogger(MembershipService.class); private final Logger log = LoggerFactory.getLogger(MembershipService.class);
private final MembershipValidator membershipValidator;
private final MembershipRepository membershipRepository; private final MembershipRepository membershipRepository;
private final MembershipMapper membershipMapper; 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.membershipRepository = membershipRepository;
this.membershipMapper = membershipMapper; this.membershipMapper = membershipMapper;
} }
@ -40,6 +45,9 @@ public class MembershipService {
*/ */
public MembershipDTO save(MembershipDTO membershipDTO) { public MembershipDTO save(MembershipDTO membershipDTO) {
log.debug("Request to save Membership : {}", membershipDTO); log.debug("Request to save Membership : {}", membershipDTO);
membershipValidator.validate(membershipDTO);
Membership membership = membershipMapper.toEntity(membershipDTO); Membership membership = membershipMapper.toEntity(membershipDTO);
membership = membershipRepository.save(membership); membership = membershipRepository.save(membership);
return membershipMapper.toDto(membership); return membershipMapper.toDto(membership);

View File

@ -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");
}
}
}

View File

@ -1,8 +1,10 @@
package org.hostsharing.hsadminng.service.dto; 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.io.Serializable;
import java.time.LocalDate;
import java.util.Objects; import java.util.Objects;
import java.util.function.Consumer;
/** /**
* A DTO for the Membership entity. * A DTO for the Membership entity.
@ -16,11 +18,16 @@ public class MembershipDTO implements Serializable {
private LocalDate untilDate; private LocalDate untilDate;
private Long customerId; private Long customerId;
private String customerPrefix; private String customerPrefix;
public MembershipDTO with(
Consumer<MembershipDTO> builderFunction) {
builderFunction.accept(this);
return this;
}
public Long getId() { public Long getId() {
return id; return id;
} }

View File

@ -3,6 +3,7 @@
"shareSubscriptionPositivQuantity": "Zeichnungen von Geschäftsanteilen erfordern eine positive Stückzahl", "shareSubscriptionPositivQuantity": "Zeichnungen von Geschäftsanteilen erfordern eine positive Stückzahl",
"shareCancellationNegativeQuantity": "Kündigungen von Geschäftsanteilen erfordern eine negative Stückzahl", "shareCancellationNegativeQuantity": "Kündigungen von Geschäftsanteilen erfordern eine negative Stückzahl",
"shareTransactionImmutable": "Transaktionen mit Geschäftsanteilen sind unveränderlich", "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"
} }
} }

View File

@ -3,6 +3,7 @@
"shareSubscriptionPositivQuantity": "Share subscriptions require a positive quantity", "shareSubscriptionPositivQuantity": "Share subscriptions require a positive quantity",
"shareCancellationNegativeQuantity": "Share cancellations require a negative quantity", "shareCancellationNegativeQuantity": "Share cancellations require a negative quantity",
"shareTransactionImmutable": "Share transactions are immutable", "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"
} }
} }

View File

@ -1,8 +1,9 @@
package org.hostsharing.hsadminng.service; package org.hostsharing.hsadminng.service;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import org.hostsharing.hsadminng.domain.Membership;
import org.hostsharing.hsadminng.repository.MembershipRepository; 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.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; 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.assertThat;
import static org.assertj.core.api.Assertions.catchThrowableOfType; import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;
public class MembershipServiceUnitTest { public class MembershipServiceUnitTest {
@ -23,7 +26,7 @@ public class MembershipServiceUnitTest {
private MembershipRepository membershipRepository; private MembershipRepository membershipRepository;
@Mock @Mock
private MembershipMapper membershipMapper; private MembershipValidator membershipValidator;
@InjectMocks @InjectMocks
private MembershipService membershipService; private MembershipService membershipService;
@ -37,4 +40,20 @@ public class MembershipServiceUnitTest {
assertThat(throwException).isEqualToComparingFieldByField( assertThat(throwException).isEqualToComparingFieldByField(
new BadRequestAlertException("Membership cannot be deleted", "membership", "membershipNotDeletable")); 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));
}
} }

View File

@ -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();
}
}

View File

@ -55,7 +55,7 @@ public class ShareServiceUnitTest {
} }
@Test @Test
public void saveNotUpdateAnyExistingShareTransaction() { public void saveShouldNotUpdateAnyExistingShareTransaction() {
// given // given
final ShareDTO givenShareDTO = givenShareDTO(anyNonNullId(), ShareAction.SUBSCRIPTION, anyPositiveNumber()); final ShareDTO givenShareDTO = givenShareDTO(anyNonNullId(), ShareAction.SUBSCRIPTION, anyPositiveNumber());