MembershipValidator using membershipRepository.hasUncancelledMembershipForCustomer
This commit is contained in:
parent
ca9879db92
commit
4dbbd5ecde
@ -2,17 +2,16 @@ package org.hostsharing.hsadminng.domain;
|
|||||||
|
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||||
|
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
import javax.validation.constraints.*;
|
import javax.validation.constraints.NotNull;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Asset.
|
* A Asset.
|
||||||
*/
|
*/
|
||||||
@ -44,6 +43,7 @@ public class Asset implements Serializable {
|
|||||||
@Column(name = "jhi_comment", length = 160)
|
@Column(name = "jhi_comment", length = 160)
|
||||||
private String comment;
|
private String comment;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JsonIgnoreProperties("assets")
|
@JsonIgnoreProperties("assets")
|
||||||
private Membership member;
|
private Membership member;
|
||||||
|
@ -36,8 +36,11 @@ public class Membership implements Serializable {
|
|||||||
|
|
||||||
@OneToMany(mappedBy = "member")
|
@OneToMany(mappedBy = "member")
|
||||||
private Set<Share> shares = new HashSet<>();
|
private Set<Share> shares = new HashSet<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "member")
|
@OneToMany(mappedBy = "member")
|
||||||
private Set<Asset> assets = new HashSet<>();
|
private Set<Asset> assets = new HashSet<>();
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JsonIgnoreProperties("memberships")
|
@JsonIgnoreProperties("memberships")
|
||||||
private Customer customer;
|
private Customer customer;
|
||||||
|
@ -44,6 +44,7 @@ public class Share implements Serializable {
|
|||||||
@Column(name = "jhi_comment", length = 160)
|
@Column(name = "jhi_comment", length = 160)
|
||||||
private String comment;
|
private String comment;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@JsonIgnoreProperties("shares")
|
@JsonIgnoreProperties("shares")
|
||||||
private Membership member;
|
private Membership member;
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package org.hostsharing.hsadminng.repository;
|
package org.hostsharing.hsadminng.repository;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.domain.Membership;
|
import org.hostsharing.hsadminng.domain.Membership;
|
||||||
import org.springframework.data.jpa.repository.*;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
|
||||||
@ -12,4 +15,7 @@ import org.springframework.stereotype.Repository;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface MembershipRepository extends JpaRepository<Membership, Long>, JpaSpecificationExecutor<Membership> {
|
public interface MembershipRepository extends JpaRepository<Membership, Long>, JpaSpecificationExecutor<Membership> {
|
||||||
|
|
||||||
|
@Query("SELECT CASE WHEN COUNT(m)> 0 THEN TRUE ELSE FALSE END " +
|
||||||
|
" FROM Membership m WHERE m.customer.id=:customerId AND m.untilDate IS NULL")
|
||||||
|
boolean hasUncancelledMembershipForCustomer(@Param("customerId") final long customerId);
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,25 @@
|
|||||||
package org.hostsharing.hsadminng.service;
|
package org.hostsharing.hsadminng.service;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.domain.Membership;
|
import org.hostsharing.hsadminng.domain.Membership;
|
||||||
|
import org.hostsharing.hsadminng.repository.MembershipRepository;
|
||||||
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
|
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.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class MembershipValidator {
|
public class MembershipValidator {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MembershipRepository membershipRepository;
|
||||||
|
|
||||||
public void validate(final MembershipDTO membershipDTO) {
|
public void validate(final MembershipDTO membershipDTO) {
|
||||||
if (membershipDTO.getUntilDate() != null && !membershipDTO.getUntilDate().isAfter(membershipDTO.getSinceDate())) {
|
if (membershipDTO.getUntilDate() != null && !membershipDTO.getUntilDate().isAfter(membershipDTO.getSinceDate())) {
|
||||||
throw new BadRequestAlertException("Invalid untilDate", Membership.ENTITY_NAME, "untilDateMustBeAfterSinceDate");
|
throw new BadRequestAlertException("Invalid untilDate", Membership.ENTITY_NAME, "untilDateMustBeAfterSinceDate");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (membershipRepository.hasUncancelledMembershipForCustomer(membershipDTO.getCustomerId())) {
|
||||||
|
throw new BadRequestAlertException("Another uncancelled membership exists", Membership.ENTITY_NAME, "anotherUncancelledMembershipExists");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package org.hostsharing.hsadminng.service.dto;
|
package org.hostsharing.hsadminng.service.dto;
|
||||||
import java.time.LocalDate;
|
|
||||||
import javax.validation.constraints.*;
|
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A DTO for the Asset entity.
|
* A DTO for the Asset entity.
|
||||||
@ -25,7 +28,7 @@ public class AssetDTO implements Serializable {
|
|||||||
@Size(max = 160)
|
@Size(max = 160)
|
||||||
private String comment;
|
private String comment;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private Long memberId;
|
private Long memberId;
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
|
@ -18,6 +18,7 @@ public class MembershipDTO implements Serializable {
|
|||||||
|
|
||||||
private LocalDate untilDate;
|
private LocalDate untilDate;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private Long customerId;
|
private Long customerId;
|
||||||
|
|
||||||
private String customerPrefix;
|
private String customerPrefix;
|
||||||
|
@ -1,10 +1,13 @@
|
|||||||
package org.hostsharing.hsadminng.service.dto;
|
package org.hostsharing.hsadminng.service.dto;
|
||||||
import java.time.LocalDate;
|
|
||||||
import javax.validation.constraints.*;
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.Objects;
|
|
||||||
import org.hostsharing.hsadminng.domain.enumeration.ShareAction;
|
import org.hostsharing.hsadminng.domain.enumeration.ShareAction;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import javax.validation.constraints.Size;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A DTO for the Share entity.
|
* A DTO for the Share entity.
|
||||||
*/
|
*/
|
||||||
@ -24,7 +27,7 @@ public class ShareDTO implements Serializable {
|
|||||||
@Size(max = 160)
|
@Size(max = 160)
|
||||||
private String comment;
|
private String comment;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private Long memberId;
|
private Long memberId;
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
"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"
|
"untilDateMustBeAfterSinceDate": "Mitgliedshafts-Austrittsdatum muss nach dem Beitrittsdatum liegen",
|
||||||
|
"anotherUncancelledMembershipExists": "Nur eine einzige ungekündigte Mitgliedschaft pro Kunde ist zulässig"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
"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"
|
"untilDateMustBeAfterSinceDate": "Membership until date must be after since date",
|
||||||
|
"anotherUncancelledMembershipExists": "Only a single uncancelled membership allowed per customer"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,90 @@
|
|||||||
|
package org.hostsharing.hsadminng.repository;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
|
import org.apache.commons.lang3.RandomUtils;
|
||||||
|
import org.hostsharing.hsadminng.HsadminNgApp;
|
||||||
|
import org.hostsharing.hsadminng.domain.Customer;
|
||||||
|
import org.hostsharing.hsadminng.domain.Membership;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = HsadminNgApp.class)
|
||||||
|
@Transactional
|
||||||
|
public class MembershipRepositoryIntTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CustomerRepository customerRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MembershipRepository membershipRepository;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasUncancelledMembershipForCustomerIsTrueForCustomerWithUncancelledMembership() {
|
||||||
|
// given
|
||||||
|
final Customer givenCustomerWithUncancelledMembership = createCustomerWithMembership("2011-08-18", null);
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean actual = membershipRepository.hasUncancelledMembershipForCustomer(givenCustomerWithUncancelledMembership.getId());
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(actual).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasUncancelledMembershipForCustomerIsFalseForCustomerWithoutMembership() {
|
||||||
|
// given
|
||||||
|
final Customer givenCustomerWithoutMembership = createCustomer();
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean actual = membershipRepository.hasUncancelledMembershipForCustomer(givenCustomerWithoutMembership.getId());
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(actual).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void hasUncancelledMembershipForCustomerIsFalseForCustomerWithCancelledMembership() {
|
||||||
|
// given
|
||||||
|
final Customer givenCustomerWithCancelledMembership = createCustomerWithMembership("2011-08-18", "2017-12-31");
|
||||||
|
|
||||||
|
// when
|
||||||
|
boolean actual = membershipRepository.hasUncancelledMembershipForCustomer(givenCustomerWithCancelledMembership.getId());
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(actual).isFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- only test fixture below ---
|
||||||
|
|
||||||
|
private Customer createCustomer() {
|
||||||
|
final Customer customer = new Customer();
|
||||||
|
customer.setPrefix(RandomStringUtils.randomAlphabetic(3).toLowerCase());
|
||||||
|
customer.setNumber(RandomUtils.nextInt(10001, 19999));
|
||||||
|
customer.setName(RandomStringUtils.randomAlphabetic(10));
|
||||||
|
customer.setContractualAddress(RandomStringUtils.randomAlphabetic(10));
|
||||||
|
customerRepository.save(customer);
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
|
||||||
|
private Customer createCustomerWithMembership(final String sinceDate, final String untilDate) {
|
||||||
|
final Customer customer = createCustomer();
|
||||||
|
final Membership membership = new Membership();
|
||||||
|
membership.setCustomer(customer);
|
||||||
|
membership.setSinceDate(LocalDate.parse(sinceDate));
|
||||||
|
if (untilDate != null) {
|
||||||
|
membership.setUntilDate(LocalDate.parse(untilDate));
|
||||||
|
}
|
||||||
|
membershipRepository.save(membership);
|
||||||
|
return customer;
|
||||||
|
}
|
||||||
|
}
|
@ -1,8 +1,10 @@
|
|||||||
package org.hostsharing.hsadminng.service;
|
package org.hostsharing.hsadminng.service;
|
||||||
|
|
||||||
|
import org.assertj.core.api.AbstractThrowableAssert;
|
||||||
import org.hostsharing.hsadminng.repository.MembershipRepository;
|
import org.hostsharing.hsadminng.repository.MembershipRepository;
|
||||||
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
|
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.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.InjectMocks;
|
import org.mockito.InjectMocks;
|
||||||
@ -11,9 +13,12 @@ import org.mockito.junit.MockitoJUnit;
|
|||||||
import org.mockito.junit.MockitoRule;
|
import org.mockito.junit.MockitoRule;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
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.anyLong;
|
||||||
|
import static org.mockito.BDDMockito.given;
|
||||||
|
|
||||||
public class MembershipValidatorUnitTest {
|
public class MembershipValidatorUnitTest {
|
||||||
|
|
||||||
@ -26,41 +31,92 @@ public class MembershipValidatorUnitTest {
|
|||||||
@InjectMocks
|
@InjectMocks
|
||||||
private MembershipValidator membershipValidator;
|
private MembershipValidator membershipValidator;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void initMocks() {
|
||||||
|
given(membershipRepository.hasUncancelledMembershipForCustomer(anyLong())).willReturn(false);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldValidateThatUntilDateIsAfterSinceDate() {
|
public void shouldAcceptNewMembershipIfUntilDateAfterSinceDate() {
|
||||||
|
new GivenMembershipValidationTestCase()
|
||||||
// JUnit 4 parameterized tests are quite ugly, that's why I do it this way
|
.withNewMembershipForCustomer(1L).since("2019-04-11").until("2019-04-12")
|
||||||
shouldAcceptValidUntilDate("2019-04-10", "2019-04-11");
|
.when((MembershipDTO membershipDto) -> membershipValidator.validate(membershipDto))
|
||||||
shouldRejectInvalidUntilDate("2019-04-11", "2019-04-11");
|
.thenActualException().isNull();
|
||||||
shouldRejectInvalidUntilDate("2019-04-12", "2019-04-11");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void shouldAcceptValidUntilDate(final String sinceDate, final String untilDate) {
|
@Test
|
||||||
|
public void shouldRejectNewMembershipIfUntilDateEqualToSinceDate() {
|
||||||
// given
|
new GivenMembershipValidationTestCase()
|
||||||
final MembershipDTO membershipDTO = new MembershipDTO();
|
.withNewMembershipForCustomer(1L).since("2019-04-11").until("2019-04-11")
|
||||||
membershipDTO.setSinceDate(LocalDate.parse(sinceDate));
|
.when((MembershipDTO membershipDto) -> membershipValidator.validate(membershipDto))
|
||||||
membershipDTO.setUntilDate(LocalDate.parse(untilDate));
|
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
|
||||||
|
"Invalid untilDate", "membership", "untilDateMustBeAfterSinceDate"));
|
||||||
// when
|
|
||||||
final Throwable throwException = catchThrowableOfType(() -> membershipValidator.validate(membershipDTO), Throwable.class);
|
|
||||||
|
|
||||||
// then
|
|
||||||
assertThat(throwException).isNull();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void shouldRejectInvalidUntilDate(final String sinceDate, final String untilDate) {
|
@Test
|
||||||
|
public void shouldRejectNewMembershipIfUntilDateAfterSinceDate() {
|
||||||
// given
|
new GivenMembershipValidationTestCase()
|
||||||
final MembershipDTO membershipDTO = new MembershipDTO();
|
.withNewMembershipForCustomer(1L).since("2019-04-12").until("2019-04-11")
|
||||||
membershipDTO.setSinceDate(LocalDate.parse(sinceDate));
|
.when((MembershipDTO membershipDto) -> membershipValidator.validate(membershipDto))
|
||||||
membershipDTO.setUntilDate(LocalDate.parse(untilDate));
|
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
|
||||||
|
"Invalid untilDate", "membership", "untilDateMustBeAfterSinceDate"));
|
||||||
// when
|
|
||||||
final Throwable throwException = catchThrowableOfType(() -> membershipValidator.validate(membershipDTO), BadRequestAlertException.class);
|
|
||||||
|
|
||||||
// then
|
|
||||||
assertThat(throwException).isNotNull();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldAcceptNewUncancelledMembershipIfNoUncancelledMembershipExistsForSameCustomer() {
|
||||||
|
new GivenMembershipValidationTestCase()
|
||||||
|
.withUncancelledMembershipForCustomer(1L, false)
|
||||||
|
.withNewMembershipForCustomer(1L).since("2019-04-12")
|
||||||
|
.when((MembershipDTO membershipDto) -> membershipValidator.validate(membershipDto))
|
||||||
|
.thenActualException().isNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldRejectNewMembershipIfAnyUncancelledMembershipExistsForSameCustomer() {
|
||||||
|
|
||||||
|
new GivenMembershipValidationTestCase()
|
||||||
|
.withUncancelledMembershipForCustomer(1L, true)
|
||||||
|
.withNewMembershipForCustomer(1L).since("2019-04-12")
|
||||||
|
.when((MembershipDTO membershipDto) -> membershipValidator.validate(membershipDto))
|
||||||
|
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
|
||||||
|
"Another uncancelled membership exists", "membership", "anotherUncancelledMembershipExists"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// -- only test fixture below ---
|
||||||
|
|
||||||
|
private class GivenMembershipValidationTestCase {
|
||||||
|
|
||||||
|
private final MembershipDTO membershipDto = new MembershipDTO();
|
||||||
|
private BadRequestAlertException actualException;
|
||||||
|
|
||||||
|
GivenMembershipValidationTestCase withUncancelledMembershipForCustomer(final long customerId, final boolean hasUncancelledMembership) {
|
||||||
|
given(membershipRepository.hasUncancelledMembershipForCustomer(customerId)).willReturn(hasUncancelledMembership);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
GivenMembershipValidationTestCase withNewMembershipForCustomer(long customerId) {
|
||||||
|
membershipDto.setCustomerId(1L);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
GivenMembershipValidationTestCase since(final String sinceDate) {
|
||||||
|
membershipDto.setSinceDate(LocalDate.parse(sinceDate));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public GivenMembershipValidationTestCase until(final String untilDate) {
|
||||||
|
membershipDto.setUntilDate(LocalDate.parse(untilDate));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
GivenMembershipValidationTestCase when(final Consumer<MembershipDTO> statement) {
|
||||||
|
actualException = catchThrowableOfType(() -> membershipValidator.validate(membershipDto), BadRequestAlertException.class);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public AbstractThrowableAssert<?, ? extends Throwable> thenActualException() {
|
||||||
|
return assertThat(actualException);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,15 @@
|
|||||||
package org.hostsharing.hsadminng.web.rest;
|
package org.hostsharing.hsadminng.web.rest;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.HsadminNgApp;
|
import org.hostsharing.hsadminng.HsadminNgApp;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.domain.Asset;
|
import org.hostsharing.hsadminng.domain.Asset;
|
||||||
import org.hostsharing.hsadminng.domain.Membership;
|
import org.hostsharing.hsadminng.domain.Membership;
|
||||||
|
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
|
||||||
import org.hostsharing.hsadminng.repository.AssetRepository;
|
import org.hostsharing.hsadminng.repository.AssetRepository;
|
||||||
|
import org.hostsharing.hsadminng.service.AssetQueryService;
|
||||||
import org.hostsharing.hsadminng.service.AssetService;
|
import org.hostsharing.hsadminng.service.AssetService;
|
||||||
import org.hostsharing.hsadminng.service.dto.AssetDTO;
|
import org.hostsharing.hsadminng.service.dto.AssetDTO;
|
||||||
import org.hostsharing.hsadminng.service.mapper.AssetMapper;
|
import org.hostsharing.hsadminng.service.mapper.AssetMapper;
|
||||||
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
|
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
|
||||||
import org.hostsharing.hsadminng.service.dto.AssetCriteria;
|
|
||||||
import org.hostsharing.hsadminng.service.AssetQueryService;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -33,14 +31,11 @@ import java.time.LocalDate;
|
|||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
import static org.hostsharing.hsadminng.web.rest.TestUtil.createFormattingConversionService;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.hamcrest.Matchers.hasItem;
|
import static org.hamcrest.Matchers.hasItem;
|
||||||
|
import static org.hostsharing.hsadminng.web.rest.TestUtil.createFormattingConversionService;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
|
|
||||||
/**
|
/**
|
||||||
* Test class for the AssetResource REST controller.
|
* Test class for the AssetResource REST controller.
|
||||||
*
|
*
|
||||||
@ -111,8 +106,10 @@ public class AssetResourceIntTest {
|
|||||||
* This is a static method, as tests for other entities might also need it,
|
* This is a static method, as tests for other entities might also need it,
|
||||||
* if they test an entity which requires the current entity.
|
* if they test an entity which requires the current entity.
|
||||||
*/
|
*/
|
||||||
public static Asset createEntity(EntityManager em) {
|
public static Asset createEntity(EntityManager em, Membership membership) {
|
||||||
|
em.persist(membership);
|
||||||
Asset asset = new Asset()
|
Asset asset = new Asset()
|
||||||
|
.member(membership)
|
||||||
.date(DEFAULT_DATE)
|
.date(DEFAULT_DATE)
|
||||||
.action(DEFAULT_ACTION)
|
.action(DEFAULT_ACTION)
|
||||||
.amount(DEFAULT_AMOUNT)
|
.amount(DEFAULT_AMOUNT)
|
||||||
@ -122,7 +119,7 @@ public class AssetResourceIntTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initTest() {
|
public void initTest() {
|
||||||
asset = createEntity(em);
|
asset = createEntity(em, MembershipResourceIntTest.createEntity(em, CustomerResourceIntTest.createEntity(em)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -445,7 +442,7 @@ public class AssetResourceIntTest {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void getAllAssetsByMemberIsEqualToSomething() throws Exception {
|
public void getAllAssetsByMemberIsEqualToSomething() throws Exception {
|
||||||
// Initialize the database
|
// Initialize the database
|
||||||
Membership member = MembershipResourceIntTest.createEntity(em);
|
Membership member = MembershipResourceIntTest.createEntity(em, CustomerResourceIntTest.createAnotherEntity(em));
|
||||||
em.persist(member);
|
em.persist(member);
|
||||||
em.flush();
|
em.flush();
|
||||||
asset.setMember(member);
|
asset.setMember(member);
|
||||||
|
@ -614,7 +614,7 @@ public class CustomerResourceIntTest {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void getAllCustomersByMembershipIsEqualToSomething() throws Exception {
|
public void getAllCustomersByMembershipIsEqualToSomething() throws Exception {
|
||||||
// Initialize the database
|
// Initialize the database
|
||||||
Membership membership = MembershipResourceIntTest.createEntity(em);
|
Membership membership = MembershipResourceIntTest.createEntity(em, customer);
|
||||||
em.persist(membership);
|
em.persist(membership);
|
||||||
em.flush();
|
em.flush();
|
||||||
customer.addMembership(membership);
|
customer.addMembership(membership);
|
||||||
|
@ -1,19 +1,16 @@
|
|||||||
package org.hostsharing.hsadminng.web.rest;
|
package org.hostsharing.hsadminng.web.rest;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.HsadminNgApp;
|
import org.hostsharing.hsadminng.HsadminNgApp;
|
||||||
|
|
||||||
import org.hostsharing.hsadminng.domain.Membership;
|
|
||||||
import org.hostsharing.hsadminng.domain.Share;
|
|
||||||
import org.hostsharing.hsadminng.domain.Asset;
|
import org.hostsharing.hsadminng.domain.Asset;
|
||||||
import org.hostsharing.hsadminng.domain.Customer;
|
import org.hostsharing.hsadminng.domain.Customer;
|
||||||
|
import org.hostsharing.hsadminng.domain.Membership;
|
||||||
|
import org.hostsharing.hsadminng.domain.Share;
|
||||||
import org.hostsharing.hsadminng.repository.MembershipRepository;
|
import org.hostsharing.hsadminng.repository.MembershipRepository;
|
||||||
|
import org.hostsharing.hsadminng.service.MembershipQueryService;
|
||||||
import org.hostsharing.hsadminng.service.MembershipService;
|
import org.hostsharing.hsadminng.service.MembershipService;
|
||||||
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
|
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
|
||||||
import org.hostsharing.hsadminng.service.mapper.MembershipMapper;
|
import org.hostsharing.hsadminng.service.mapper.MembershipMapper;
|
||||||
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
|
import org.hostsharing.hsadminng.web.rest.errors.ExceptionTranslator;
|
||||||
import org.hostsharing.hsadminng.service.dto.MembershipCriteria;
|
|
||||||
import org.hostsharing.hsadminng.service.MembershipQueryService;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@ -34,10 +31,9 @@ import java.time.LocalDate;
|
|||||||
import java.time.ZoneId;
|
import java.time.ZoneId;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
import static org.hostsharing.hsadminng.web.rest.TestUtil.createFormattingConversionService;
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.hamcrest.Matchers.hasItem;
|
import static org.hamcrest.Matchers.hasItem;
|
||||||
|
import static org.hostsharing.hsadminng.web.rest.TestUtil.createFormattingConversionService;
|
||||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||||
|
|
||||||
@ -50,11 +46,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
|
|||||||
@SpringBootTest(classes = HsadminNgApp.class)
|
@SpringBootTest(classes = HsadminNgApp.class)
|
||||||
public class MembershipResourceIntTest {
|
public class MembershipResourceIntTest {
|
||||||
|
|
||||||
private static final LocalDate DEFAULT_SINCE_DATE = LocalDate.ofEpochDay(0L);
|
private static final LocalDate DEFAULT_SINCE_DATE = LocalDate.now(ZoneId.systemDefault());
|
||||||
private static final LocalDate UPDATED_SINCE_DATE = LocalDate.now(ZoneId.systemDefault());
|
private static final LocalDate UPDATED_SINCE_DATE = DEFAULT_SINCE_DATE.plusMonths(1);
|
||||||
|
|
||||||
private static final LocalDate DEFAULT_UNTIL_DATE = LocalDate.ofEpochDay(0L);
|
private static final LocalDate DEFAULT_UNTIL_DATE = UPDATED_SINCE_DATE.plusDays(600).withMonth(12).withDayOfMonth(31);
|
||||||
private static final LocalDate UPDATED_UNTIL_DATE = LocalDate.now(ZoneId.systemDefault());
|
private static final LocalDate UPDATED_UNTIL_DATE = DEFAULT_UNTIL_DATE.plusYears(1);
|
||||||
|
private static final LocalDate ANOTHER_QUERY_DATE = DEFAULT_UNTIL_DATE.plusMonths(2);
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MembershipRepository membershipRepository;
|
private MembershipRepository membershipRepository;
|
||||||
@ -105,8 +102,10 @@ public class MembershipResourceIntTest {
|
|||||||
* This is a static method, as tests for other entities might also need it,
|
* This is a static method, as tests for other entities might also need it,
|
||||||
* if they test an entity which requires the current entity.
|
* if they test an entity which requires the current entity.
|
||||||
*/
|
*/
|
||||||
public static Membership createEntity(EntityManager em) {
|
public static Membership createEntity(final EntityManager em, final Customer customer) {
|
||||||
|
em.persist(customer);
|
||||||
Membership membership = new Membership()
|
Membership membership = new Membership()
|
||||||
|
.customer(customer)
|
||||||
.sinceDate(DEFAULT_SINCE_DATE)
|
.sinceDate(DEFAULT_SINCE_DATE)
|
||||||
.untilDate(DEFAULT_UNTIL_DATE);
|
.untilDate(DEFAULT_UNTIL_DATE);
|
||||||
return membership;
|
return membership;
|
||||||
@ -114,7 +113,7 @@ public class MembershipResourceIntTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initTest() {
|
public void initTest() {
|
||||||
membership = createEntity(em);
|
membership = createEntity(em, CustomerResourceIntTest.createEntity(em));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -282,7 +281,7 @@ public class MembershipResourceIntTest {
|
|||||||
defaultMembershipShouldBeFound("untilDate.equals=" + DEFAULT_UNTIL_DATE);
|
defaultMembershipShouldBeFound("untilDate.equals=" + DEFAULT_UNTIL_DATE);
|
||||||
|
|
||||||
// Get all the membershipList where untilDate equals to UPDATED_UNTIL_DATE
|
// Get all the membershipList where untilDate equals to UPDATED_UNTIL_DATE
|
||||||
defaultMembershipShouldNotBeFound("untilDate.equals=" + UPDATED_UNTIL_DATE);
|
defaultMembershipShouldNotBeFound("untilDate.equals=" + asString(UPDATED_UNTIL_DATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -292,10 +291,10 @@ public class MembershipResourceIntTest {
|
|||||||
membershipRepository.saveAndFlush(membership);
|
membershipRepository.saveAndFlush(membership);
|
||||||
|
|
||||||
// Get all the membershipList where untilDate in DEFAULT_UNTIL_DATE or UPDATED_UNTIL_DATE
|
// Get all the membershipList where untilDate in DEFAULT_UNTIL_DATE or UPDATED_UNTIL_DATE
|
||||||
defaultMembershipShouldBeFound("untilDate.in=" + DEFAULT_UNTIL_DATE + "," + UPDATED_UNTIL_DATE);
|
defaultMembershipShouldBeFound("untilDate.in=" + DEFAULT_UNTIL_DATE + "," + ANOTHER_QUERY_DATE);
|
||||||
|
|
||||||
// Get all the membershipList where untilDate equals to UPDATED_UNTIL_DATE
|
// Get all the membershipList where untilDate equals to UPDATED_UNTIL_DATE
|
||||||
defaultMembershipShouldNotBeFound("untilDate.in=" + UPDATED_UNTIL_DATE);
|
defaultMembershipShouldNotBeFound("untilDate.in=" + asString(UPDATED_UNTIL_DATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -321,7 +320,7 @@ public class MembershipResourceIntTest {
|
|||||||
defaultMembershipShouldBeFound("untilDate.greaterOrEqualThan=" + DEFAULT_UNTIL_DATE);
|
defaultMembershipShouldBeFound("untilDate.greaterOrEqualThan=" + DEFAULT_UNTIL_DATE);
|
||||||
|
|
||||||
// Get all the membershipList where untilDate greater than or equals to UPDATED_UNTIL_DATE
|
// Get all the membershipList where untilDate greater than or equals to UPDATED_UNTIL_DATE
|
||||||
defaultMembershipShouldNotBeFound("untilDate.greaterOrEqualThan=" + UPDATED_UNTIL_DATE);
|
defaultMembershipShouldNotBeFound("untilDate.greaterOrEqualThan=" + asString(UPDATED_UNTIL_DATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -334,7 +333,7 @@ public class MembershipResourceIntTest {
|
|||||||
defaultMembershipShouldNotBeFound("untilDate.lessThan=" + DEFAULT_UNTIL_DATE);
|
defaultMembershipShouldNotBeFound("untilDate.lessThan=" + DEFAULT_UNTIL_DATE);
|
||||||
|
|
||||||
// Get all the membershipList where untilDate less than or equals to UPDATED_UNTIL_DATE
|
// Get all the membershipList where untilDate less than or equals to UPDATED_UNTIL_DATE
|
||||||
defaultMembershipShouldBeFound("untilDate.lessThan=" + UPDATED_UNTIL_DATE);
|
defaultMembershipShouldBeFound("untilDate.lessThan=" + asString(UPDATED_UNTIL_DATE));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -342,11 +341,11 @@ public class MembershipResourceIntTest {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void getAllMembershipsByShareIsEqualToSomething() throws Exception {
|
public void getAllMembershipsByShareIsEqualToSomething() throws Exception {
|
||||||
// Initialize the database
|
// Initialize the database
|
||||||
Share share = ShareResourceIntTest.createEntity(em);
|
Share share = ShareResourceIntTest.createEntity(em, membership);
|
||||||
em.persist(share);
|
em.persist(share);
|
||||||
em.flush();
|
em.flush();
|
||||||
membership.addShare(share);
|
membership.addShare(share);
|
||||||
membershipRepository.saveAndFlush(membership);
|
membershipRepository.flush();
|
||||||
Long shareId = share.getId();
|
Long shareId = share.getId();
|
||||||
|
|
||||||
// Get all the membershipList where share equals to shareId
|
// Get all the membershipList where share equals to shareId
|
||||||
@ -361,11 +360,11 @@ public class MembershipResourceIntTest {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void getAllMembershipsByAssetIsEqualToSomething() throws Exception {
|
public void getAllMembershipsByAssetIsEqualToSomething() throws Exception {
|
||||||
// Initialize the database
|
// Initialize the database
|
||||||
Asset asset = AssetResourceIntTest.createEntity(em);
|
Asset asset = AssetResourceIntTest.createEntity(em, membership);
|
||||||
em.persist(asset);
|
em.persist(asset);
|
||||||
em.flush();
|
em.flush();
|
||||||
membership.addAsset(asset);
|
membership.addAsset(asset);
|
||||||
membershipRepository.saveAndFlush(membership);
|
membershipRepository.flush();
|
||||||
Long assetId = asset.getId();
|
Long assetId = asset.getId();
|
||||||
|
|
||||||
// Get all the membershipList where asset equals to assetId
|
// Get all the membershipList where asset equals to assetId
|
||||||
@ -380,7 +379,7 @@ public class MembershipResourceIntTest {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void getAllMembershipsByCustomerIsEqualToSomething() throws Exception {
|
public void getAllMembershipsByCustomerIsEqualToSomething() throws Exception {
|
||||||
// Initialize the database
|
// Initialize the database
|
||||||
Customer customer = CustomerResourceIntTest.createEntity(em);
|
Customer customer = CustomerResourceIntTest.createAnotherEntity(em);
|
||||||
em.persist(customer);
|
em.persist(customer);
|
||||||
em.flush();
|
em.flush();
|
||||||
membership.setCustomer(customer);
|
membership.setCustomer(customer);
|
||||||
@ -498,11 +497,11 @@ public class MembershipResourceIntTest {
|
|||||||
// Delete the membership
|
// Delete the membership
|
||||||
restMembershipMockMvc.perform(delete("/api/memberships/{id}", membership.getId())
|
restMembershipMockMvc.perform(delete("/api/memberships/{id}", membership.getId())
|
||||||
.accept(TestUtil.APPLICATION_JSON_UTF8))
|
.accept(TestUtil.APPLICATION_JSON_UTF8))
|
||||||
.andExpect(status().isOk());
|
.andExpect(status().isBadRequest());
|
||||||
|
|
||||||
// Validate the database is empty
|
// Validate the database is unchanged empty
|
||||||
List<Membership> membershipList = membershipRepository.findAll();
|
List<Membership> membershipList = membershipRepository.findAll();
|
||||||
assertThat(membershipList).hasSize(databaseSizeBeforeDelete - 1);
|
assertThat(membershipList).hasSize(databaseSizeBeforeDelete);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -542,4 +541,8 @@ public class MembershipResourceIntTest {
|
|||||||
assertThat(membershipMapper.fromId(42L).getId()).isEqualTo(42);
|
assertThat(membershipMapper.fromId(42L).getId()).isEqualTo(42);
|
||||||
assertThat(membershipMapper.fromId(null)).isNull();
|
assertThat(membershipMapper.fromId(null)).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String asString(LocalDate updatedUntilDate) {
|
||||||
|
return updatedUntilDate == null ? "" : updatedUntilDate.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,8 +106,10 @@ public class ShareResourceIntTest {
|
|||||||
* This is a static method, as tests for other entities might also need it,
|
* This is a static method, as tests for other entities might also need it,
|
||||||
* if they test an entity which requires the current entity.
|
* if they test an entity which requires the current entity.
|
||||||
*/
|
*/
|
||||||
public static Share createEntity(EntityManager em) {
|
public static Share createEntity(final EntityManager em, final Membership membership) {
|
||||||
|
em.persist(membership);
|
||||||
Share share = new Share()
|
Share share = new Share()
|
||||||
|
.member(membership)
|
||||||
.date(DEFAULT_DATE)
|
.date(DEFAULT_DATE)
|
||||||
.action(DEFAULT_ACTION)
|
.action(DEFAULT_ACTION)
|
||||||
.quantity(DEFAULT_QUANTITY)
|
.quantity(DEFAULT_QUANTITY)
|
||||||
@ -117,7 +119,7 @@ public class ShareResourceIntTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void initTest() {
|
public void initTest() {
|
||||||
share = createEntity(em);
|
share = createEntity(em, MembershipResourceIntTest.createEntity(em, CustomerResourceIntTest.createEntity(em)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -468,7 +470,7 @@ public class ShareResourceIntTest {
|
|||||||
@Transactional
|
@Transactional
|
||||||
public void getAllSharesByMemberIsEqualToSomething() throws Exception {
|
public void getAllSharesByMemberIsEqualToSomething() throws Exception {
|
||||||
// Initialize the database
|
// Initialize the database
|
||||||
Membership member = MembershipResourceIntTest.createEntity(em);
|
Membership member = MembershipResourceIntTest.createEntity(em, CustomerResourceIntTest.createAnotherEntity(em));
|
||||||
em.persist(member);
|
em.persist(member);
|
||||||
em.flush();
|
em.flush();
|
||||||
share.setMember(member);
|
share.setMember(member);
|
||||||
|
Loading…
Reference in New Issue
Block a user