improving branch coverage for AssetValidator+ShareValidator

This commit is contained in:
Michael Hoennig 2019-04-20 14:47:45 +02:00
parent 7ba20b3687
commit 1ac8df5ed9
2 changed files with 86 additions and 24 deletions

View File

@ -1,5 +1,7 @@
package org.hostsharing.hsadminng.service;
import com.google.common.collect.ImmutableList;
import org.apache.commons.lang3.RandomUtils;
import org.assertj.core.api.AbstractThrowableAssert;
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
import org.hostsharing.hsadminng.service.dto.AssetDTO;
@ -18,13 +20,26 @@ public class AssetValidatorUnitTest {
private AssetValidator assetValidator = new AssetValidator();
@Test
public void shouldAcceptValidAssetDTO() {
public void shouldAcceptValidIncreasingTransaction() {
for (AssetAction action : ImmutableList.of(AssetAction.PAYMENT, AssetAction.ADOPTION)) {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAction(AssetAction.PAYMENT).withAmount("64.00")
.withAnyValidDateValues()
.withAction(action).withAmount("64.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isNull();
}
}
@Test
public void shouldAcceptValidDecreasingTransaction() {
for (AssetAction action : ImmutableList.of(AssetAction.PAYBACK, AssetAction.HANDOVER, AssetAction.CLEARING, AssetAction.LOSS)) {
new GivenAssetValidationTestCase()
.withAnyValidDateValues()
.withAction(action).withAmount("-64.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isNull();
}
}
@Test
public void shouldAcceptIfDocumentDateEqualsValueDate() {
@ -35,6 +50,15 @@ public class AssetValidatorUnitTest {
.thenActualException().isNull();
}
@Test
public void shouldRejectUpdates() {
new GivenAssetValidationTestCase()
.withId(RandomUtils.nextLong())
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
"Asset transactions are immutable", "asset", "assetTransactionImmutable"));
}
@Test
public void shouldRejectIfDocumentDateAfterValueDate() {
new GivenAssetValidationTestCase()
@ -48,7 +72,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfPaymentWithNegativeAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.PAYMENT).withAmount("-64.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -58,7 +82,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfPaymentWithZeroAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.PAYMENT).withAmount("0.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -68,7 +92,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfAdoptionWithNegativeAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.ADOPTION).withAmount("-64.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -78,7 +102,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfAdoptionWithZeroAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.ADOPTION).withAmount("0.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -88,7 +112,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfPaybackWithPositiveAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.PAYBACK).withAmount("64.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -98,7 +122,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfPaybackWithZeroAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.PAYBACK).withAmount("0.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -108,7 +132,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfHandoverWithPositiveAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.HANDOVER).withAmount("64.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -118,7 +142,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfHandoverWithZeroAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.HANDOVER).withAmount("0.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -128,7 +152,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfLossWithPositiveAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.LOSS).withAmount("64.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -138,7 +162,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfLossWithZeroAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.LOSS).withAmount("0.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -148,7 +172,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfClearingWithPositiveAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.CLEARING).withAmount("64.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -158,7 +182,7 @@ public class AssetValidatorUnitTest {
@Test
public void shouldRejectIfClearingWithZeroAmount() {
new GivenAssetValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(AssetAction.CLEARING).withAmount("0.00")
.when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -173,6 +197,11 @@ public class AssetValidatorUnitTest {
private final AssetDTO assetDto = new AssetDTO();
private BadRequestAlertException actualException;
public GivenAssetValidationTestCase withId(long id) {
assetDto.setId(id);
return this;
}
GivenAssetValidationTestCase withDocumentDate(String documentDate) {
assetDto.setDocumentDate(LocalDate.parse(documentDate));
return this;
@ -183,6 +212,10 @@ public class AssetValidatorUnitTest {
return this;
}
public GivenAssetValidationTestCase withAnyValidDateValues() {
return withDocumentDate("2019-04-11").withValueDate("2019-04-12");
}
GivenAssetValidationTestCase withAction(AssetAction assetAction) {
assetDto.setAction(assetAction);
return this;

View File

@ -1,7 +1,9 @@
package org.hostsharing.hsadminng.service;
import org.apache.commons.lang3.RandomUtils;
import org.assertj.core.api.AbstractThrowableAssert;
import org.hostsharing.hsadminng.domain.enumeration.ShareAction;
import org.hostsharing.hsadminng.service.dto.AssetDTO;
import org.hostsharing.hsadminng.service.dto.ShareDTO;
import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
import org.junit.Test;
@ -17,14 +19,23 @@ public class ShareValidatorUnitTest {
private ShareValidator shareValidator = new ShareValidator();
@Test
public void shouldAcceptValidShareDTO() {
public void shouldAcceptValidSubscription() {
new GivenShareValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(ShareAction.SUBSCRIPTION).withQuantity(1)
.when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
.thenActualException().isNull();
}
@Test
public void shouldAcceptValidCancellation() {
new GivenShareValidationTestCase()
.withAnyValidDateValues()
.withAction(ShareAction.CANCELLATION).withQuantity(-1)
.when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
.thenActualException().isNull();
}
@Test
public void shouldAcceptIfDocumentDateEqualsValueDate() {
new GivenShareValidationTestCase()
@ -34,6 +45,15 @@ public class ShareValidatorUnitTest {
.thenActualException().isNull();
}
@Test
public void shouldRejectUpdates() {
new GivenShareValidationTestCase()
.withId(RandomUtils.nextLong())
.when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
"Share transactions are immutable", "share", "shareTransactionImmutable"));
}
@Test
public void shouldRejectIfDocumentDateAfterValueDate() {
new GivenShareValidationTestCase()
@ -47,7 +67,7 @@ public class ShareValidatorUnitTest {
@Test
public void shouldRejectIfSubscriptionWithNegativeQuantity() {
new GivenShareValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(ShareAction.SUBSCRIPTION).withQuantity(-1)
.when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -57,7 +77,7 @@ public class ShareValidatorUnitTest {
@Test
public void shouldRejectIfSubscriptionWithZeroQuantity() {
new GivenShareValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(ShareAction.SUBSCRIPTION).withQuantity(0)
.when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -67,7 +87,7 @@ public class ShareValidatorUnitTest {
@Test
public void shouldRejectIfCancellationWithPositiveQuantity() {
new GivenShareValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(ShareAction.CANCELLATION).withQuantity(1)
.when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -77,7 +97,7 @@ public class ShareValidatorUnitTest {
@Test
public void shouldRejectIfCancellationWithZeroQuantity() {
new GivenShareValidationTestCase()
.withDocumentDate("2019-04-11").withValueDate("2019-04-12")
.withAnyValidDateValues()
.withAction(ShareAction.CANCELLATION).withQuantity(0)
.when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
.thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
@ -92,6 +112,11 @@ public class ShareValidatorUnitTest {
private final ShareDTO shareDto = new ShareDTO();
private BadRequestAlertException actualException;
public GivenShareValidationTestCase withId(long id) {
shareDto.setId(id);
return this;
}
GivenShareValidationTestCase withDocumentDate(String documentDate) {
shareDto.setDocumentDate(LocalDate.parse(documentDate));
return this;
@ -102,6 +127,10 @@ public class ShareValidatorUnitTest {
return this;
}
public GivenShareValidationTestCase withAnyValidDateValues() {
return withDocumentDate("2019-04-11").withValueDate("2019-04-12");
}
GivenShareValidationTestCase withAction(ShareAction shareAction) {
shareDto.setAction(shareAction);
return this;