1
0

hs-office-coopshares: add validation + RestTest for Controller + improve test data

This commit is contained in:
Michael Hoennig
2022-10-20 07:41:48 +02:00
parent ca0589d084
commit 5f4f50a325
7 changed files with 254 additions and 66 deletions

View File

@@ -92,24 +92,24 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest {
[
{
"transactionType": "SUBSCRIPTION",
"sharesCount": 2,
"sharesCount": 4,
"valueDate": "2010-03-15",
"reference": "ref 10002-1",
"comment": "initial subscription"
},
{
"transactionType": "SUBSCRIPTION",
"sharesCount": 24,
"transactionType": "CANCELLATION",
"sharesCount": -2,
"valueDate": "2021-09-01",
"reference": "ref 10002-2",
"comment": "subscibing more"
"comment": "cancelling some"
},
{
"transactionType": "CANCELLATION;",
"sharesCount": 12,
"transactionType": "ADJUSTMENT",
"sharesCount": 2,
"valueDate": "2022-10-20",
"reference": "ref 10002-3",
"comment": "cancelling some"
"comment": "some adjustment"
}
]
""")); // @formatter:on
@@ -135,11 +135,11 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest {
.body("", lenientlyEquals("""
[
{
"transactionType": "SUBSCRIPTION",
"sharesCount": 24,
"transactionType": "CANCELLATION",
"sharesCount": -2,
"valueDate": "2021-09-01",
"reference": "ref 10002-2",
"comment": "subscibing more"
"comment": "cancelling some"
}
]
""")); // @formatter:on
@@ -195,13 +195,6 @@ class HsOfficeCoopSharesTransactionControllerAcceptanceTest {
location.substring(location.lastIndexOf('/') + 1));
assertThat(newUserUuid).isNotNull();
}
// TODO.test: move validation tests to a ...WebMvcTest
@Test
void globalAdmin_canNotAddCoopSharesTransactionWhenMembershipUuidIsMissing() {
}
}
@BeforeEach

View File

@@ -0,0 +1,164 @@
package net.hostsharing.hsadminng.hs.office.coopshares;
import net.hostsharing.hsadminng.context.Context;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.util.UUID;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(HsOfficeCoopSharesTransactionController.class)
class HsOfficeCoopSharesTransactionControllerRestTest {
@Autowired
MockMvc mockMvc;
@MockBean
Context contextMock;
@MockBean
HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
enum BadRequestTestCases {
MEMBERSHIP_UUID_MISSING(
"""
{
"transactionType": "SUBSCRIPTION",
"sharesCount": 8,
"valueDate": "2022-10-13",
"reference": "temp ref A"
}
""",
"[membershipUuid must not be null but is \"null\"]"),
TRANSACTION_TYPE_MISSING(
"""
{
"membershipUuid": "%s",
"sharesCount": 8,
"valueDate": "2022-10-13",
"reference": "temp ref A"
}
""".formatted(UUID.randomUUID()),
"[transactionType must not be null but is \"null\"]"),
VALUE_DATE_MISSING(
"""
{
"membershipUuid": "%s",
"transactionType": "SUBSCRIPTION",
"sharesCount": 8,
"reference": "temp ref A"
}
""".formatted(UUID.randomUUID()),
"[valueDate must not be null but is \"null\"]"),
SHARES_COUNT_FOR_SUBSCRIPTION_MUST_BE_POSITIVE(
"""
{
"membershipUuid": "%s",
"transactionType": "SUBSCRIPTION",
"sharesCount": -1,
"valueDate": "2022-10-13",
"reference": "temp ref A"
}
""".formatted(UUID.randomUUID()),
"[for SUBSCRIPTION, sharesCount must be positive but is \"-1\"]"),
SHARES_COUNT_FOR_CANCELLATION_MUST_BE_NEGATIVE(
"""
{
"membershipUuid": "%s",
"transactionType": "CANCELLATION",
"sharesCount": 1,
"valueDate": "2022-10-13",
"reference": "temp ref A"
}
""".formatted(UUID.randomUUID()),
"[for CANCELLATION, sharesCount must be negative but is \"1\"]"),
SHARES_COUNT_MUST_NOT_BE_NULL(
"""
{
"membershipUuid": "%s",
"transactionType": "ADJUSTMENT",
"sharesCount": 0,
"valueDate": "2022-10-13",
"reference": "temp ref A"
}
""".formatted(UUID.randomUUID()),
"[sharesCount must not be 0 but is \"0\"]"),
REFERENCE_MISSING(
"""
{
"membershipUuid": "%s",
"transactionType": "SUBSCRIPTION",
"sharesCount": 8,
"valueDate": "2022-10-13"
}
""".formatted(UUID.randomUUID()),
"[reference must not be null but is \"null\"]"),
REFERENCE_TOO_SHORT(
"""
{
"membershipUuid": "%s",
"transactionType": "SUBSCRIPTION",
"sharesCount": 8,
"valueDate": "2022-10-13",
"reference": "12345"
}
""".formatted(UUID.randomUUID()),
"[reference size must be between 6 and 48 but is \"12345\"]"),
REFERENCE_TOO_LONG(
"""
{
"membershipUuid": "%s",
"transactionType": "SUBSCRIPTION",
"sharesCount": 8,
"valueDate": "2022-10-13",
"reference": "0123456789012345678901234567890123456789012345678"
}
""".formatted(UUID.randomUUID()),
"[reference size must be between 6 and 48 but is \"0123456789012345678901234567890123456789012345678\"]");
private final String givenBody;
private final String expectedErrorMessage;
BadRequestTestCases(final String givenBody, final String expectedErrorMessage) {
this.givenBody = givenBody;
this.expectedErrorMessage = expectedErrorMessage;
}
}
@ParameterizedTest
@EnumSource(BadRequestTestCases.class)
void respondWithBadRequest(final BadRequestTestCases testCase) throws Exception {
// when
mockMvc.perform(MockMvcRequestBuilders
.post("/api/hs/office/coopsharestransactions")
.header("current-user", "superuser-alex@hostsharing.net")
.contentType(MediaType.APPLICATION_JSON)
.content(testCase.givenBody)
.accept(MediaType.APPLICATION_JSON))
// then
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("status", is(400)))
.andExpect(jsonPath("error", is("Bad Request")))
.andExpect(jsonPath("message", is(testCase.expectedErrorMessage)));
}
}

View File

@@ -147,24 +147,25 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
// then
allTheseCoopSharesTransactionsAreReturned(
result,
"CoopShareTransaction(10001, 2010-03-15, SUBSCRIPTION, 2, ref 10001-1)",
"CoopShareTransaction(10001, 2021-09-01, SUBSCRIPTION, 24, ref 10001-2)",
"CoopShareTransaction(10001, 2022-10-20, CANCELLATION, 12, ref 10001-3)",
"CoopShareTransaction(10001, 2010-03-15, SUBSCRIPTION, 4, ref 10001-1)",
"CoopShareTransaction(10001, 2021-09-01, CANCELLATION, -2, ref 10001-2)",
"CoopShareTransaction(10001, 2022-10-20, ADJUSTMENT, 2, ref 10001-3)",
"CoopShareTransaction(10002, 2010-03-15, SUBSCRIPTION, 2, ref 10002-1)",
"CoopShareTransaction(10002, 2021-09-01, SUBSCRIPTION, 24, ref 10002-2)",
"CoopShareTransaction(10002, 2022-10-20, CANCELLATION, 12, ref 10002-3)",
"CoopShareTransaction(10002, 2010-03-15, SUBSCRIPTION, 4, ref 10002-1)",
"CoopShareTransaction(10002, 2021-09-01, CANCELLATION, -2, ref 10002-2)",
"CoopShareTransaction(10002, 2022-10-20, ADJUSTMENT, 2, ref 10002-3)",
"CoopShareTransaction(10003, 2010-03-15, SUBSCRIPTION, 2, ref 10003-1)",
"CoopShareTransaction(10003, 2021-09-01, SUBSCRIPTION, 24, ref 10003-2)",
"CoopShareTransaction(10003, 2022-10-20, CANCELLATION, 12, ref 10003-3)");
"CoopShareTransaction(10003, 2010-03-15, SUBSCRIPTION, 4, ref 10003-1)",
"CoopShareTransaction(10003, 2021-09-01, CANCELLATION, -2, ref 10003-2)",
"CoopShareTransaction(10003, 2022-10-20, ADJUSTMENT, 2, ref 10003-3)");
}
@Test
public void globalAdmin_canViewCoopSharesTransactions_filteredByMembershipUuid() {
// given
context("superuser-alex@hostsharing.net");
final var givenMembership = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(null, 10002).get(0);
final var givenMembership = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(null, 10002)
.get(0);
// when
final var result = coopSharesTransactionRepo.findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
@@ -175,16 +176,17 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
// then
allTheseCoopSharesTransactionsAreReturned(
result,
"CoopShareTransaction(10002, 2010-03-15, SUBSCRIPTION, 2, ref 10002-1)",
"CoopShareTransaction(10002, 2021-09-01, SUBSCRIPTION, 24, ref 10002-2)",
"CoopShareTransaction(10002, 2022-10-20, CANCELLATION, 12, ref 10002-3)");
"CoopShareTransaction(10002, 2010-03-15, SUBSCRIPTION, 4, ref 10002-1)",
"CoopShareTransaction(10002, 2021-09-01, CANCELLATION, -2, ref 10002-2)",
"CoopShareTransaction(10002, 2022-10-20, ADJUSTMENT, 2, ref 10002-3)");
}
@Test
public void globalAdmin_canViewCoopSharesTransactions_filteredByMembershipUuidAndValueDateRange() {
// given
context("superuser-alex@hostsharing.net");
final var givenMembership = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(null, 10002).get(0);
final var givenMembership = membershipRepo.findMembershipsByOptionalPartnerUuidAndOptionalMemberNumber(null, 10002)
.get(0);
// when
final var result = coopSharesTransactionRepo.findCoopSharesTransactionByOptionalMembershipUuidAndDateRange(
@@ -195,7 +197,7 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
// then
allTheseCoopSharesTransactionsAreReturned(
result,
"CoopShareTransaction(10002, 2021-09-01, SUBSCRIPTION, 24, ref 10002-2)");
"CoopShareTransaction(10002, 2021-09-01, CANCELLATION, -2, ref 10002-2)");
}
@Test
@@ -213,9 +215,9 @@ class HsOfficeCoopSharesTransactionRepositoryIntegrationTest extends ContextBase
// then:
exactlyTheseCoopSharesTransactionsAreReturned(
result,
"CoopShareTransaction(10001, 2010-03-15, SUBSCRIPTION, 2, ref 10001-1)",
"CoopShareTransaction(10001, 2021-09-01, SUBSCRIPTION, 24, ref 10001-2)",
"CoopShareTransaction(10001, 2022-10-20, CANCELLATION, 12, ref 10001-3)");
"CoopShareTransaction(10001, 2010-03-15, SUBSCRIPTION, 4, ref 10001-1)",
"CoopShareTransaction(10001, 2021-09-01, CANCELLATION, -2, ref 10001-2)",
"CoopShareTransaction(10001, 2022-10-20, ADJUSTMENT, 2, ref 10001-3)");
}
}