coop-shares-transaction-reverse-entry (#40)
Co-authored-by: Marc O. Sandlus <marc.o.sandlus@hostsharing.net> Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/40 Reviewed-by: Timotheus Pokorra <timotheus.pokorra@hostsharing.net>
This commit is contained in:
@ -4,6 +4,8 @@ import io.restassured.RestAssured;
|
||||
import io.restassured.http.ContentType;
|
||||
import net.hostsharing.hsadminng.HsadminNgApplication;
|
||||
import net.hostsharing.hsadminng.context.Context;
|
||||
import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionRawEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.membership.HsOfficeMembershipRepository;
|
||||
import net.hostsharing.hsadminng.hs.office.test.ContextBasedTestWithCleanup;
|
||||
import net.hostsharing.test.Accepts;
|
||||
@ -19,9 +21,12 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.UUID;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionType.DEPOSIT;
|
||||
import static net.hostsharing.test.IsValidUuidMatcher.isUuidValid;
|
||||
import static net.hostsharing.test.JsonMatcher.lenientlyEquals;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@ -69,7 +74,15 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest extends ContextBased
|
||||
void globalAdmin_canViewAllCoopSharesTransactions() {
|
||||
|
||||
RestAssured // @formatter:off
|
||||
.given().header("current-user", "superuser-alex@hostsharing.net").port(port).when().get("http://localhost/api/hs/office/coopsharestransactions").then().log().all().assertThat().statusCode(200).contentType("application/json").body("", hasSize(9)); // @formatter:on
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.port(port)
|
||||
.when()
|
||||
.get("http://localhost/api/hs/office/coopsharestransactions")
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(200)
|
||||
.contentType("application/json")
|
||||
.body("", hasSize(12)); // @formatter:on
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -95,12 +108,33 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest extends ContextBased
|
||||
"reference": "ref 1000202-2",
|
||||
"comment": "cancelling some"
|
||||
},
|
||||
{
|
||||
"transactionType": "SUBSCRIPTION",
|
||||
"shareCount": 2,
|
||||
"valueDate": "2022-10-20",
|
||||
"reference": "ref 1000202-3",
|
||||
"comment": "some subscription",
|
||||
"adjustmentShareTx": {
|
||||
"transactionType": "ADJUSTMENT",
|
||||
"shareCount": -2,
|
||||
"valueDate": "2022-10-21",
|
||||
"reference": "ref 1000202-4",
|
||||
"comment": "some adjustment"
|
||||
}
|
||||
},
|
||||
{
|
||||
"transactionType": "ADJUSTMENT",
|
||||
"shareCount": 2,
|
||||
"valueDate": "2022-10-20",
|
||||
"reference": "ref 1000202-3",
|
||||
"comment": "some adjustment"
|
||||
"shareCount": -2,
|
||||
"valueDate": "2022-10-21",
|
||||
"reference": "ref 1000202-4",
|
||||
"comment": "some adjustment",
|
||||
"adjustedShareTx": {
|
||||
"transactionType": "SUBSCRIPTION",
|
||||
"shareCount": 2,
|
||||
"valueDate": "2022-10-20",
|
||||
"reference": "ref 1000202-3",
|
||||
"comment": "some subscription"
|
||||
}
|
||||
}
|
||||
]
|
||||
""")); // @formatter:on
|
||||
@ -159,8 +193,76 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest extends ContextBased
|
||||
""")).header("Location", startsWith("http://localhost")).extract().header("Location"); // @formatter:on
|
||||
|
||||
// finally, the new coopSharesTransaction can be accessed under the generated UUID
|
||||
final var newUserUuid = UUID.fromString(location.substring(location.lastIndexOf('/') + 1));
|
||||
assertThat(newUserUuid).isNotNull();
|
||||
final var newShareTxUuid = UUID.fromString(location.substring(location.lastIndexOf('/') + 1));
|
||||
assertThat(newShareTxUuid).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void globalAdmin_canAddCoopSharesAdjustmentTransaction() {
|
||||
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenMembership = membershipRepo.findMembershipByMemberNumber(1000101);
|
||||
final var givenTransaction = jpaAttempt.transacted(() -> {
|
||||
// TODO.impl: introduce something like transactedAsSuperuser / transactedAs("...", ...)
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
return coopSharesTransactionRepo.save(HsOfficeCoopSharesTransactionEntity.builder()
|
||||
.transactionType(HsOfficeCoopSharesTransactionType.SUBSCRIPTION)
|
||||
.valueDate(LocalDate.of(2022, 10, 20))
|
||||
.membership(givenMembership)
|
||||
.shareCount(13)
|
||||
.reference("test ref")
|
||||
.build());
|
||||
}).assertSuccessful().assertNotNull().returnedValue();
|
||||
toCleanup(HsOfficeCoopSharesTransactionRawEntity.class, givenTransaction.getUuid());
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
.given()
|
||||
.header("current-user", "superuser-alex@hostsharing.net")
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{
|
||||
"membershipUuid": "%s",
|
||||
"transactionType": "ADJUSTMENT",
|
||||
"shareCount": %s,
|
||||
"valueDate": "2022-10-30",
|
||||
"reference": "test ref adjustment",
|
||||
"comment": "some coop shares adjustment transaction",
|
||||
"adjustedShareTxUuid": "%s"
|
||||
}
|
||||
""".formatted(
|
||||
givenMembership.getUuid(),
|
||||
-givenTransaction.getShareCount(),
|
||||
givenTransaction.getUuid()))
|
||||
.port(port)
|
||||
.when()
|
||||
.post("http://localhost/api/hs/office/coopsharestransactions")
|
||||
.then().log().all().assertThat()
|
||||
.statusCode(201)
|
||||
.contentType(ContentType.JSON)
|
||||
.body("uuid", isUuidValid())
|
||||
.body("", lenientlyEquals("""
|
||||
{
|
||||
"transactionType": "ADJUSTMENT",
|
||||
"shareCount": -13,
|
||||
"valueDate": "2022-10-30",
|
||||
"reference": "test ref adjustment",
|
||||
"comment": "some coop shares adjustment transaction",
|
||||
"adjustedShareTx": {
|
||||
"transactionType": "SUBSCRIPTION",
|
||||
"shareCount": 13,
|
||||
"valueDate": "2022-10-20",
|
||||
"reference": "test ref"
|
||||
}
|
||||
}
|
||||
"""))
|
||||
.header("Location", startsWith("http://localhost"))
|
||||
.extract().header("Location"); // @formatter:on
|
||||
|
||||
// finally, the new coopAssetsTransaction can be accessed under the generated UUID
|
||||
final var newShareTxUuid = UUID.fromString(
|
||||
location.substring(location.lastIndexOf('/') + 1));
|
||||
assertThat(newShareTxUuid).isNotNull();
|
||||
toCleanup(HsOfficeCoopSharesTransactionRawEntity.class, newShareTxUuid);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -169,7 +271,7 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest extends ContextBased
|
||||
context.define("superuser-alex@hostsharing.net");
|
||||
final var givenMembership = membershipRepo.findMembershipByMemberNumber(1000101);
|
||||
|
||||
final var location = RestAssured // @formatter:off
|
||||
RestAssured // @formatter:off
|
||||
.given().header("current-user", "superuser-alex@hostsharing.net").contentType(ContentType.JSON).body("""
|
||||
{
|
||||
"membershipUuid": "%s",
|
||||
|
@ -1,7 +1,10 @@
|
||||
package net.hostsharing.hsadminng.hs.office.coopshares;
|
||||
|
||||
import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionEntity;
|
||||
import net.hostsharing.hsadminng.hs.office.coopassets.HsOfficeCoopAssetsTransactionType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static net.hostsharing.hsadminng.hs.office.membership.TestHsMembership.TEST_MEMBERSHIP;
|
||||
@ -15,34 +18,56 @@ class HsOfficeCoopSharesTransactionEntityUnitTest {
|
||||
.valueDate(LocalDate.parse("2020-01-01"))
|
||||
.transactionType(HsOfficeCoopSharesTransactionType.SUBSCRIPTION)
|
||||
.shareCount(4)
|
||||
.comment("some comment")
|
||||
.build();
|
||||
|
||||
|
||||
final HsOfficeCoopSharesTransactionEntity givenCoopShareAdjustmentTransaction = HsOfficeCoopSharesTransactionEntity.builder()
|
||||
.membership(TEST_MEMBERSHIP)
|
||||
.reference("some-ref")
|
||||
.valueDate(LocalDate.parse("2020-01-15"))
|
||||
.transactionType(HsOfficeCoopSharesTransactionType.ADJUSTMENT)
|
||||
.shareCount(-4)
|
||||
.comment("some comment")
|
||||
.adjustedShareTx(givenCoopSharesTransaction)
|
||||
.build();
|
||||
|
||||
final HsOfficeCoopSharesTransactionEntity givenEmptyCoopSharesTransaction = HsOfficeCoopSharesTransactionEntity.builder().build();
|
||||
|
||||
@Test
|
||||
void toStringContainsAlmostAllPropertiesAccount() {
|
||||
void toStringContainsAllNonNullProperties() {
|
||||
final var result = givenCoopSharesTransaction.toString();
|
||||
|
||||
assertThat(result).isEqualTo("CoopShareTransaction(M-1000101, 2020-01-01, SUBSCRIPTION, 4, some-ref)");
|
||||
assertThat(result).isEqualTo("CoopShareTransaction(M-1000101: 2020-01-01, SUBSCRIPTION, 4, some-ref, some comment)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toShortStringContainsOnlyMemberNumberAndShareCountOnly() {
|
||||
void toStringWithReverseEntryContainsReverseEntry() {
|
||||
givenCoopSharesTransaction.setAdjustedShareTx(givenCoopShareAdjustmentTransaction);
|
||||
|
||||
final var result = givenCoopSharesTransaction.toString();
|
||||
|
||||
assertThat(result).isEqualTo("CoopShareTransaction(M-1000101: 2020-01-01, SUBSCRIPTION, 4, some-ref, some comment, M-1000101:ADJ:-4)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toShortStringContainsOnlyAbbreviatedString() {
|
||||
final var result = givenCoopSharesTransaction.toShortString();
|
||||
|
||||
assertThat(result).isEqualTo("M-1000101+4");
|
||||
assertThat(result).isEqualTo("M-1000101:SUB:+4");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringEmptyTransactionDoesNotThrowException() {
|
||||
final var result = givenEmptyCoopSharesTransaction.toString();
|
||||
|
||||
assertThat(result).isEqualTo("CoopShareTransaction(0)");
|
||||
assertThat(result).isEqualTo("CoopShareTransaction(null: 0)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toShortStringEmptyTransactionDoesNotThrowException() {
|
||||
final var result = givenEmptyCoopSharesTransaction.toShortString();
|
||||
|
||||
assertThat(result).isEqualTo("null+0");
|
||||
assertThat(result).isEqualTo("null:nul:+0");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,18 @@
|
||||
|
||||
package net.hostsharing.hsadminng.hs.office.coopshares;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.Table;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
@Entity
|
||||
@Table(name = "hs_office_coopsharestransaction")
|
||||
@NoArgsConstructor
|
||||
public class HsOfficeCoopSharesTransactionRawEntity {
|
||||
|
||||
@Id
|
||||
private UUID uuid;
|
||||
}
|
@ -126,7 +126,7 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
|
||||
class FindAllCoopSharesTransactions {
|
||||
|
||||
@Test
|
||||
public void globalAdmin_anViewAllCoopSharesTransactions() {
|
||||
public void globalAdmin_canViewAllCoopSharesTransactions() {
|
||||
// given
|
||||
context("superuser-alex@hostsharing.net");
|
||||
|
||||
@ -137,19 +137,22 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
|
||||
null);
|
||||
|
||||
// then
|
||||
allTheseCoopSharesTransactionsAreReturned(
|
||||
exactlyTheseCoopSharesTransactionsAreReturned(
|
||||
result,
|
||||
"CoopShareTransaction(M-1000101, 2010-03-15, SUBSCRIPTION, 4, ref 1000101-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000101, 2021-09-01, CANCELLATION, -2, ref 1000101-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000101, 2022-10-20, ADJUSTMENT, 2, ref 1000101-3, some adjustment)",
|
||||
"CoopShareTransaction(M-1000101: 2010-03-15, SUBSCRIPTION, 4, ref 1000101-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000101: 2021-09-01, CANCELLATION, -2, ref 1000101-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000101: 2022-10-20, SUBSCRIPTION, 2, ref 1000101-3, some subscription, M-1000101:ADJ:-2)",
|
||||
"CoopShareTransaction(M-1000101: 2022-10-21, ADJUSTMENT, -2, ref 1000101-4, some adjustment, M-1000101:SUB:+2)",
|
||||
|
||||
"CoopShareTransaction(M-1000202, 2010-03-15, SUBSCRIPTION, 4, ref 1000202-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000202, 2021-09-01, CANCELLATION, -2, ref 1000202-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000202, 2022-10-20, ADJUSTMENT, 2, ref 1000202-3, some adjustment)",
|
||||
"CoopShareTransaction(M-1000202: 2010-03-15, SUBSCRIPTION, 4, ref 1000202-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000202: 2021-09-01, CANCELLATION, -2, ref 1000202-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000202: 2022-10-20, SUBSCRIPTION, 2, ref 1000202-3, some subscription, M-1000202:ADJ:-2)",
|
||||
"CoopShareTransaction(M-1000202: 2022-10-21, ADJUSTMENT, -2, ref 1000202-4, some adjustment, M-1000202:SUB:+2)",
|
||||
|
||||
"CoopShareTransaction(M-1000303, 2010-03-15, SUBSCRIPTION, 4, ref 1000303-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000303, 2021-09-01, CANCELLATION, -2, ref 1000303-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000303, 2022-10-20, ADJUSTMENT, 2, ref 1000303-3, some adjustment)");
|
||||
"CoopShareTransaction(M-1000303: 2010-03-15, SUBSCRIPTION, 4, ref 1000303-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000303: 2021-09-01, CANCELLATION, -2, ref 1000303-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000303: 2022-10-20, SUBSCRIPTION, 2, ref 1000303-3, some subscription, M-1000303:ADJ:-2)",
|
||||
"CoopShareTransaction(M-1000303: 2022-10-21, ADJUSTMENT, -2, ref 1000303-4, some adjustment, M-1000303:SUB:+2)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -165,11 +168,12 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
|
||||
null);
|
||||
|
||||
// then
|
||||
allTheseCoopSharesTransactionsAreReturned(
|
||||
exactlyTheseCoopSharesTransactionsAreReturned(
|
||||
result,
|
||||
"CoopShareTransaction(M-1000202, 2010-03-15, SUBSCRIPTION, 4, ref 1000202-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000202, 2021-09-01, CANCELLATION, -2, ref 1000202-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000202, 2022-10-20, ADJUSTMENT, 2, ref 1000202-3, some adjustment)");
|
||||
"CoopShareTransaction(M-1000202: 2010-03-15, SUBSCRIPTION, 4, ref 1000202-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000202: 2021-09-01, CANCELLATION, -2, ref 1000202-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000202: 2022-10-20, SUBSCRIPTION, 2, ref 1000202-3, some subscription, M-1000202:ADJ:-2)",
|
||||
"CoopShareTransaction(M-1000202: 2022-10-21, ADJUSTMENT, -2, ref 1000202-4, some adjustment, M-1000202:SUB:+2)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -187,7 +191,7 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
|
||||
// then
|
||||
allTheseCoopSharesTransactionsAreReturned(
|
||||
result,
|
||||
"CoopShareTransaction(M-1000202, 2021-09-01, CANCELLATION, -2, ref 1000202-2, cancelling some)");
|
||||
"CoopShareTransaction(M-1000202: 2021-09-01, CANCELLATION, -2, ref 1000202-2, cancelling some)");
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -204,9 +208,10 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
|
||||
// then:
|
||||
exactlyTheseCoopSharesTransactionsAreReturned(
|
||||
result,
|
||||
"CoopShareTransaction(M-1000101, 2010-03-15, SUBSCRIPTION, 4, ref 1000101-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000101, 2021-09-01, CANCELLATION, -2, ref 1000101-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000101, 2022-10-20, ADJUSTMENT, 2, ref 1000101-3, some adjustment)");
|
||||
"CoopShareTransaction(M-1000101: 2010-03-15, SUBSCRIPTION, 4, ref 1000101-1, initial subscription)",
|
||||
"CoopShareTransaction(M-1000101: 2021-09-01, CANCELLATION, -2, ref 1000101-2, cancelling some)",
|
||||
"CoopShareTransaction(M-1000101: 2022-10-20, SUBSCRIPTION, 2, ref 1000101-3, some subscription, M-1000101:ADJ:-2)",
|
||||
"CoopShareTransaction(M-1000101: 2022-10-21, ADJUSTMENT, -2, ref 1000101-4, some adjustment, M-1000101:SUB:+2)");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -360,10 +360,10 @@ public class ImportOfficeData extends ContextBasedTest {
|
||||
|
||||
assertThat(toFormattedString(coopShares)).isEqualToIgnoringWhitespace("""
|
||||
{
|
||||
33443=CoopShareTransaction(M-1001700, 2000-12-06, SUBSCRIPTION, 20, initial share subscription),
|
||||
33451=CoopShareTransaction(M-1002000, 2000-12-06, SUBSCRIPTION, 2, initial share subscription),
|
||||
33701=CoopShareTransaction(M-1001700, 2005-01-10, SUBSCRIPTION, 40, increase),
|
||||
33810=CoopShareTransaction(M-1002000, 2016-12-31, CANCELLATION, 22, membership ended)
|
||||
33443=CoopShareTransaction(M-1001700, 2000-12-06, SUBSCRIPTION, 20, legacy data import, initial share subscription),
|
||||
33451=CoopShareTransaction(M-1002000, 2000-12-06, SUBSCRIPTION, 2, legacy data import, initial share subscription),
|
||||
33701=CoopShareTransaction(M-1001700, 2005-01-10, SUBSCRIPTION, 40, legacy data import, increase),
|
||||
33810=CoopShareTransaction(M-1002000, 2016-12-31, CANCELLATION, 22, legacy data import, membership ended)
|
||||
}
|
||||
""");
|
||||
}
|
||||
@ -803,8 +803,19 @@ public class ImportOfficeData extends ContextBasedTest {
|
||||
)
|
||||
.shareCount(rec.getInteger("quantity"))
|
||||
.comment( rec.getString("comment"))
|
||||
.reference("legacy data import") // TODO.spec: or use value from comment column?
|
||||
.build();
|
||||
|
||||
if (shareTransaction.getTransactionType() == HsOfficeCoopSharesTransactionType.ADJUSTMENT) {
|
||||
final var negativeValue = -shareTransaction.getShareCount();
|
||||
final var adjustedShareTx = coopShares.values().stream().filter(a ->
|
||||
a.getTransactionType() != HsOfficeCoopSharesTransactionType.ADJUSTMENT &&
|
||||
a.getMembership() == shareTransaction.getMembership() &&
|
||||
a.getShareCount() == negativeValue)
|
||||
.findAny()
|
||||
.orElseThrow(() -> new IllegalStateException("cannot determine share reverse entry for adjustment " + shareTransaction));
|
||||
shareTransaction.setAdjustedShareTx(adjustedShareTx);
|
||||
}
|
||||
coopShares.put(rec.getInteger("member_share_id"), shareTransaction);
|
||||
});
|
||||
}
|
||||
|
Reference in New Issue
Block a user