Shares and Assets.
This commit is contained in:
		@@ -0,0 +1,205 @@
 | 
			
		||||
package org.hostsharing.hsadminng.service;
 | 
			
		||||
 | 
			
		||||
import org.assertj.core.api.AbstractThrowableAssert;
 | 
			
		||||
import org.hostsharing.hsadminng.domain.enumeration.AssetAction;
 | 
			
		||||
import org.hostsharing.hsadminng.service.dto.AssetDTO;
 | 
			
		||||
import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
import java.math.BigDecimal;
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
public class AssetValidatorUnitTest {
 | 
			
		||||
 | 
			
		||||
    private AssetValidator assetValidator = new AssetValidator();
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldAcceptValidAssetDTO() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.PAYMENT).withAmount("64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isNull();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldAcceptIfDocumentDateEqualsValueDate() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-11")
 | 
			
		||||
            .withAction(AssetAction.PAYMENT).withAmount("64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isNull();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfDocumentDateAfterValueDate() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-13").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.PAYMENT).withAmount("64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Document date may not be after value date", "asset", "documentDateMayNotBeAfterValueDate"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfPaymentWithNegativeAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.PAYMENT).withAmount("-64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset payments require a positive amount", "asset", "assetPaymentsPositiveAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfPaymentWithZeroAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.PAYMENT).withAmount("0.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset payments require a positive amount", "asset", "assetPaymentsPositiveAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfAdoptionWithNegativeAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.ADOPTION).withAmount("-64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset adoptions require a positive amount", "asset", "assetAdoptionsPositiveAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfAdoptionWithZeroAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.ADOPTION).withAmount("0.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset adoptions require a positive amount", "asset", "assetAdoptionsPositiveAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfPaybackWithPositiveAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.PAYBACK).withAmount("64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset paybacks require a negative amount", "asset", "assetPaybacksNegativeAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfPaybackWithZeroAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.PAYBACK).withAmount("0.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset paybacks require a negative amount", "asset", "assetPaybacksNegativeAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfHandoverWithPositiveAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.HANDOVER).withAmount("64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset handovers require a negative amount", "asset", "assetHandoversNegativeAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfHandoverWithZeroAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.HANDOVER).withAmount("0.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset handovers require a negative amount", "asset", "assetHandoversNegativeAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfLossWithPositiveAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.LOSS).withAmount("64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset losses require a negative amount", "asset", "assetLossesNegativeAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfLossWithZeroAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.LOSS).withAmount("0.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset losses require a negative amount", "asset", "assetLossesNegativeAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfClearingWithPositiveAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.CLEARING).withAmount("64.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset clearings require a negative amount", "asset", "assetClearingsNegativeAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfClearingWithZeroAmount() {
 | 
			
		||||
        new GivenAssetValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(AssetAction.CLEARING).withAmount("0.00")
 | 
			
		||||
            .when((AssetDTO assetDto) -> assetValidator.validate(assetDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Asset clearings require a negative amount", "asset", "assetClearingsNegativeAmount"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // -- only test fixture below ---
 | 
			
		||||
 | 
			
		||||
    private class GivenAssetValidationTestCase {
 | 
			
		||||
 | 
			
		||||
        private final AssetDTO assetDto = new AssetDTO();
 | 
			
		||||
        private BadRequestAlertException actualException;
 | 
			
		||||
 | 
			
		||||
        GivenAssetValidationTestCase withDocumentDate(String documentDate) {
 | 
			
		||||
            assetDto.setDocumentDate(LocalDate.parse(documentDate));
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GivenAssetValidationTestCase withValueDate(String valueDate) {
 | 
			
		||||
            assetDto.setValueDate(LocalDate.parse(valueDate));
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GivenAssetValidationTestCase withAction(AssetAction assetAction) {
 | 
			
		||||
            assetDto.setAction(assetAction);
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GivenAssetValidationTestCase withAmount(String amount) {
 | 
			
		||||
            assetDto.setAmount(new BigDecimal(amount));
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GivenAssetValidationTestCase  when(final Consumer<AssetDTO> statement) {
 | 
			
		||||
            actualException = catchThrowableOfType(() -> assetValidator.validate(assetDto), BadRequestAlertException.class);
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public AbstractThrowableAssert<?, ? extends Throwable> thenActualException() {
 | 
			
		||||
            return assertThat(actualException);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,126 @@
 | 
			
		||||
package org.hostsharing.hsadminng.service;
 | 
			
		||||
 | 
			
		||||
import org.assertj.core.api.AbstractThrowableAssert;
 | 
			
		||||
import org.hostsharing.hsadminng.domain.enumeration.ShareAction;
 | 
			
		||||
import org.hostsharing.hsadminng.service.dto.ShareDTO;
 | 
			
		||||
import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
 | 
			
		||||
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;
 | 
			
		||||
 | 
			
		||||
public class ShareValidatorUnitTest {
 | 
			
		||||
 | 
			
		||||
    private ShareValidator shareValidator = new ShareValidator();
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldAcceptValidShareDTO() {
 | 
			
		||||
        new GivenShareValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(ShareAction.SUBSCRIPTION).withQuantity(1)
 | 
			
		||||
            .when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
 | 
			
		||||
            .thenActualException().isNull();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldAcceptIfDocumentDateEqualsValueDate() {
 | 
			
		||||
        new GivenShareValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-11")
 | 
			
		||||
            .withAction(ShareAction.SUBSCRIPTION).withQuantity(1)
 | 
			
		||||
            .when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
 | 
			
		||||
            .thenActualException().isNull();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfDocumentDateAfterValueDate() {
 | 
			
		||||
        new GivenShareValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-13").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(ShareAction.SUBSCRIPTION).withQuantity(1)
 | 
			
		||||
            .when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Document date may not be after value date", "share", "documentDateMayNotBeAfterValueDate"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfSubscriptionWithNegativeQuantity() {
 | 
			
		||||
        new GivenShareValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(ShareAction.SUBSCRIPTION).withQuantity(-1)
 | 
			
		||||
            .when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Share subscriptions require a positive quantity", "share", "shareSubscriptionPositiveQuantity"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfSubscriptionWithZeroQuantity() {
 | 
			
		||||
        new GivenShareValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(ShareAction.SUBSCRIPTION).withQuantity(0)
 | 
			
		||||
            .when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Share subscriptions require a positive quantity", "share", "shareSubscriptionPositiveQuantity"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfCancellationWithPositiveQuantity() {
 | 
			
		||||
        new GivenShareValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(ShareAction.CANCELLATION).withQuantity(1)
 | 
			
		||||
            .when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Share cancellations require a negative quantity", "share", "shareCancellationNegativeQuantity"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void shouldRejectIfCancellationWithZeroQuantity() {
 | 
			
		||||
        new GivenShareValidationTestCase()
 | 
			
		||||
            .withDocumentDate("2019-04-11").withValueDate("2019-04-12")
 | 
			
		||||
            .withAction(ShareAction.CANCELLATION).withQuantity(0)
 | 
			
		||||
            .when((ShareDTO shareDto) -> shareValidator.validate(shareDto))
 | 
			
		||||
            .thenActualException().isEqualToComparingFieldByField(new BadRequestAlertException(
 | 
			
		||||
            "Share cancellations require a negative quantity", "share", "shareCancellationNegativeQuantity"));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // -- only test fixture below ---
 | 
			
		||||
 | 
			
		||||
    private class GivenShareValidationTestCase {
 | 
			
		||||
 | 
			
		||||
        private final ShareDTO shareDto = new ShareDTO();
 | 
			
		||||
        private BadRequestAlertException actualException;
 | 
			
		||||
 | 
			
		||||
        GivenShareValidationTestCase withDocumentDate(String documentDate) {
 | 
			
		||||
            shareDto.setDocumentDate(LocalDate.parse(documentDate));
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GivenShareValidationTestCase withValueDate(String valueDate) {
 | 
			
		||||
            shareDto.setValueDate(LocalDate.parse(valueDate));
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GivenShareValidationTestCase withAction(ShareAction shareAction) {
 | 
			
		||||
            shareDto.setAction(shareAction);
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GivenShareValidationTestCase withQuantity(Integer quantity) {
 | 
			
		||||
            shareDto.setQuantity(quantity);
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GivenShareValidationTestCase  when(final Consumer<ShareDTO> statement) {
 | 
			
		||||
            actualException = catchThrowableOfType(() -> shareValidator.validate(shareDto), BadRequestAlertException.class);
 | 
			
		||||
            return this;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public AbstractThrowableAssert<?, ? extends Throwable> thenActualException() {
 | 
			
		||||
            return assertThat(actualException);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user