MembershipValidator using membershipRepository.hasUncancelledMembershipForCustomer

This commit is contained in:
Michael Hoennig 2019-04-12 14:43:23 +02:00
parent ca9879db92
commit 4dbbd5ecde
16 changed files with 265 additions and 88 deletions

View File

@ -2,17 +2,16 @@ package org.hostsharing.hsadminng.domain;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
import javax.persistence.*;
import javax.validation.constraints.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Objects;
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
/**
* A Asset.
*/
@ -44,6 +43,7 @@ public class Asset implements Serializable {
@Column(name = "jhi_comment", length = 160)
private String comment;
@NotNull
@ManyToOne
@JsonIgnoreProperties("assets")
private Membership member;

View File

@ -36,8 +36,11 @@ public class Membership implements Serializable {
@OneToMany(mappedBy = "member")
private Set<Share> shares = new HashSet<>();
@OneToMany(mappedBy = "member")
private Set<Asset> assets = new HashSet<>();
@NotNull
@ManyToOne
@JsonIgnoreProperties("memberships")
private Customer customer;

View File

@ -44,6 +44,7 @@ public class Share implements Serializable {
@Column(name = "jhi_comment", length = 160)
private String comment;
@NotNull
@ManyToOne
@JsonIgnoreProperties("shares")
private Membership member;

View File

@ -1,7 +1,10 @@
package org.hostsharing.hsadminng.repository;
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;
@ -12,4 +15,7 @@ import org.springframework.stereotype.Repository;
@Repository
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);
}

View File

@ -1,15 +1,25 @@
package org.hostsharing.hsadminng.service;
import org.hostsharing.hsadminng.domain.Membership;
import org.hostsharing.hsadminng.repository.MembershipRepository;
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MembershipValidator {
@Autowired
private MembershipRepository membershipRepository;
public void validate(final MembershipDTO membershipDTO) {
if (membershipDTO.getUntilDate() != null && !membershipDTO.getUntilDate().isAfter(membershipDTO.getSinceDate())) {
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");
}
}
}

View File

@ -1,10 +1,13 @@
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.math.BigDecimal;
import java.time.LocalDate;
import java.util.Objects;
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
/**
* A DTO for the Asset entity.
@ -25,7 +28,7 @@ public class AssetDTO implements Serializable {
@Size(max = 160)
private String comment;
@NotNull
private Long memberId;
public Long getId() {

View File

@ -18,6 +18,7 @@ public class MembershipDTO implements Serializable {
private LocalDate untilDate;
@NotNull
private Long customerId;
private String customerPrefix;

View File

@ -1,10 +1,13 @@
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 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.
*/
@ -24,7 +27,7 @@ public class ShareDTO implements Serializable {
@Size(max = 160)
private String comment;
@NotNull
private Long memberId;
public Long getId() {

View File

@ -4,6 +4,7 @@
"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'",
"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"
}
}

View File

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

View File

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

View File

@ -1,8 +1,10 @@
package org.hostsharing.hsadminng.service;
import org.assertj.core.api.AbstractThrowableAssert;
import org.hostsharing.hsadminng.repository.MembershipRepository;
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.InjectMocks;
@ -11,9 +13,12 @@ import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import java.time.LocalDate;
import java.util.function.Consumer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowableOfType;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
public class MembershipValidatorUnitTest {
@ -26,41 +31,92 @@ public class MembershipValidatorUnitTest {
@InjectMocks
private MembershipValidator membershipValidator;
@Before
public void initMocks() {
given(membershipRepository.hasUncancelledMembershipForCustomer(anyLong())).willReturn(false);
}
@Test
public void shouldValidateThatUntilDateIsAfterSinceDate() {
// JUnit 4 parameterized tests are quite ugly, that's why I do it this way
shouldAcceptValidUntilDate("2019-04-10", "2019-04-11");
shouldRejectInvalidUntilDate("2019-04-11", "2019-04-11");
shouldRejectInvalidUntilDate("2019-04-12", "2019-04-11");
public void shouldAcceptNewMembershipIfUntilDateAfterSinceDate() {
new GivenMembershipValidationTestCase()
.withNewMembershipForCustomer(1L).since("2019-04-11").until("2019-04-12")
.when((MembershipDTO membershipDto) -> membershipValidator.validate(membershipDto))
.thenActualException().isNull();
}
private void shouldAcceptValidUntilDate(final String sinceDate, final String untilDate) {
// given
final MembershipDTO membershipDTO = new MembershipDTO();
membershipDTO.setSinceDate(LocalDate.parse(sinceDate));
membershipDTO.setUntilDate(LocalDate.parse(untilDate));
// when
final Throwable throwException = catchThrowableOfType(() -> membershipValidator.validate(membershipDTO), Throwable.class);
// then
assertThat(throwException).isNull();
@Test
public void shouldRejectNewMembershipIfUntilDateEqualToSinceDate() {
new GivenMembershipValidationTestCase()
.withNewMembershipForCustomer(1L).since("2019-04-11").until("2019-04-11")
.when((MembershipDTO membershipDto) -> membershipValidator.validate(membershipDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
"Invalid untilDate", "membership", "untilDateMustBeAfterSinceDate"));
}
private void shouldRejectInvalidUntilDate(final String sinceDate, final String untilDate) {
// given
final MembershipDTO membershipDTO = new MembershipDTO();
membershipDTO.setSinceDate(LocalDate.parse(sinceDate));
membershipDTO.setUntilDate(LocalDate.parse(untilDate));
// when
final Throwable throwException = catchThrowableOfType(() -> membershipValidator.validate(membershipDTO), BadRequestAlertException.class);
// then
assertThat(throwException).isNotNull();
@Test
public void shouldRejectNewMembershipIfUntilDateAfterSinceDate() {
new GivenMembershipValidationTestCase()
.withNewMembershipForCustomer(1L).since("2019-04-12").until("2019-04-11")
.when((MembershipDTO membershipDto) -> membershipValidator.validate(membershipDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
"Invalid untilDate", "membership", "untilDateMustBeAfterSinceDate"));
}
@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);
}
}
}

View File

@ -1,17 +1,15 @@
package org.hostsharing.hsadminng.web.rest;
import org.hostsharing.hsadminng.HsadminNgApp;
import org.hostsharing.hsadminng.domain.Asset;
import org.hostsharing.hsadminng.domain.Membership;
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
import org.hostsharing.hsadminng.repository.AssetRepository;
import org.hostsharing.hsadminng.service.AssetQueryService;
import org.hostsharing.hsadminng.service.AssetService;
import org.hostsharing.hsadminng.service.dto.AssetDTO;
import org.hostsharing.hsadminng.service.mapper.AssetMapper;
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.Test;
import org.junit.runner.RunWith;
@ -33,14 +31,11 @@ import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import static org.hostsharing.hsadminng.web.rest.TestUtil.createFormattingConversionService;
import static org.assertj.core.api.Assertions.assertThat;
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.result.MockMvcResultMatchers.*;
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
/**
* 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,
* 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()
.member(membership)
.date(DEFAULT_DATE)
.action(DEFAULT_ACTION)
.amount(DEFAULT_AMOUNT)
@ -122,7 +119,7 @@ public class AssetResourceIntTest {
@Before
public void initTest() {
asset = createEntity(em);
asset = createEntity(em, MembershipResourceIntTest.createEntity(em, CustomerResourceIntTest.createEntity(em)));
}
@Test
@ -445,7 +442,7 @@ public class AssetResourceIntTest {
@Transactional
public void getAllAssetsByMemberIsEqualToSomething() throws Exception {
// Initialize the database
Membership member = MembershipResourceIntTest.createEntity(em);
Membership member = MembershipResourceIntTest.createEntity(em, CustomerResourceIntTest.createAnotherEntity(em));
em.persist(member);
em.flush();
asset.setMember(member);

View File

@ -614,7 +614,7 @@ public class CustomerResourceIntTest {
@Transactional
public void getAllCustomersByMembershipIsEqualToSomething() throws Exception {
// Initialize the database
Membership membership = MembershipResourceIntTest.createEntity(em);
Membership membership = MembershipResourceIntTest.createEntity(em, customer);
em.persist(membership);
em.flush();
customer.addMembership(membership);

View File

@ -1,19 +1,16 @@
package org.hostsharing.hsadminng.web.rest;
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.Customer;
import org.hostsharing.hsadminng.domain.Membership;
import org.hostsharing.hsadminng.domain.Share;
import org.hostsharing.hsadminng.repository.MembershipRepository;
import org.hostsharing.hsadminng.service.MembershipQueryService;
import org.hostsharing.hsadminng.service.MembershipService;
import org.hostsharing.hsadminng.service.dto.MembershipDTO;
import org.hostsharing.hsadminng.service.mapper.MembershipMapper;
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.Test;
import org.junit.runner.RunWith;
@ -34,10 +31,9 @@ import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import static org.hostsharing.hsadminng.web.rest.TestUtil.createFormattingConversionService;
import static org.assertj.core.api.Assertions.assertThat;
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.result.MockMvcResultMatchers.*;
@ -50,11 +46,12 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@SpringBootTest(classes = HsadminNgApp.class)
public class MembershipResourceIntTest {
private static final LocalDate DEFAULT_SINCE_DATE = LocalDate.ofEpochDay(0L);
private static final LocalDate UPDATED_SINCE_DATE = LocalDate.now(ZoneId.systemDefault());
private static final LocalDate DEFAULT_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 UPDATED_UNTIL_DATE = LocalDate.now(ZoneId.systemDefault());
private static final LocalDate DEFAULT_UNTIL_DATE = UPDATED_SINCE_DATE.plusDays(600).withMonth(12).withDayOfMonth(31);
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
private MembershipRepository membershipRepository;
@ -105,8 +102,10 @@ public class MembershipResourceIntTest {
* This is a static method, as tests for other entities might also need it,
* 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()
.customer(customer)
.sinceDate(DEFAULT_SINCE_DATE)
.untilDate(DEFAULT_UNTIL_DATE);
return membership;
@ -114,7 +113,7 @@ public class MembershipResourceIntTest {
@Before
public void initTest() {
membership = createEntity(em);
membership = createEntity(em, CustomerResourceIntTest.createEntity(em));
}
@Test
@ -282,7 +281,7 @@ public class MembershipResourceIntTest {
defaultMembershipShouldBeFound("untilDate.equals=" + DEFAULT_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
@ -292,10 +291,10 @@ public class MembershipResourceIntTest {
membershipRepository.saveAndFlush(membership);
// 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
defaultMembershipShouldNotBeFound("untilDate.in=" + UPDATED_UNTIL_DATE);
defaultMembershipShouldNotBeFound("untilDate.in=" + asString(UPDATED_UNTIL_DATE));
}
@Test
@ -321,7 +320,7 @@ public class MembershipResourceIntTest {
defaultMembershipShouldBeFound("untilDate.greaterOrEqualThan=" + DEFAULT_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
@ -334,7 +333,7 @@ public class MembershipResourceIntTest {
defaultMembershipShouldNotBeFound("untilDate.lessThan=" + DEFAULT_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
public void getAllMembershipsByShareIsEqualToSomething() throws Exception {
// Initialize the database
Share share = ShareResourceIntTest.createEntity(em);
Share share = ShareResourceIntTest.createEntity(em, membership);
em.persist(share);
em.flush();
membership.addShare(share);
membershipRepository.saveAndFlush(membership);
membershipRepository.flush();
Long shareId = share.getId();
// Get all the membershipList where share equals to shareId
@ -361,11 +360,11 @@ public class MembershipResourceIntTest {
@Transactional
public void getAllMembershipsByAssetIsEqualToSomething() throws Exception {
// Initialize the database
Asset asset = AssetResourceIntTest.createEntity(em);
Asset asset = AssetResourceIntTest.createEntity(em, membership);
em.persist(asset);
em.flush();
membership.addAsset(asset);
membershipRepository.saveAndFlush(membership);
membershipRepository.flush();
Long assetId = asset.getId();
// Get all the membershipList where asset equals to assetId
@ -380,7 +379,7 @@ public class MembershipResourceIntTest {
@Transactional
public void getAllMembershipsByCustomerIsEqualToSomething() throws Exception {
// Initialize the database
Customer customer = CustomerResourceIntTest.createEntity(em);
Customer customer = CustomerResourceIntTest.createAnotherEntity(em);
em.persist(customer);
em.flush();
membership.setCustomer(customer);
@ -498,11 +497,11 @@ public class MembershipResourceIntTest {
// Delete the membership
restMembershipMockMvc.perform(delete("/api/memberships/{id}", membership.getId())
.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();
assertThat(membershipList).hasSize(databaseSizeBeforeDelete - 1);
assertThat(membershipList).hasSize(databaseSizeBeforeDelete);
}
@Test
@ -542,4 +541,8 @@ public class MembershipResourceIntTest {
assertThat(membershipMapper.fromId(42L).getId()).isEqualTo(42);
assertThat(membershipMapper.fromId(null)).isNull();
}
private String asString(LocalDate updatedUntilDate) {
return updatedUntilDate == null ? "" : updatedUntilDate.toString();
}
}

View File

@ -106,8 +106,10 @@ public class ShareResourceIntTest {
* This is a static method, as tests for other entities might also need it,
* 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()
.member(membership)
.date(DEFAULT_DATE)
.action(DEFAULT_ACTION)
.quantity(DEFAULT_QUANTITY)
@ -117,7 +119,7 @@ public class ShareResourceIntTest {
@Before
public void initTest() {
share = createEntity(em);
share = createEntity(em, MembershipResourceIntTest.createEntity(em, CustomerResourceIntTest.createEntity(em)));
}
@Test
@ -468,7 +470,7 @@ public class ShareResourceIntTest {
@Transactional
public void getAllSharesByMemberIsEqualToSomething() throws Exception {
// Initialize the database
Membership member = MembershipResourceIntTest.createEntity(em);
Membership member = MembershipResourceIntTest.createEntity(em, CustomerResourceIntTest.createAnotherEntity(em));
em.persist(member);
em.flush();
share.setMember(member);