hs-office-coopshares: refactored RestTest
This commit is contained in:
parent
5f4f50a325
commit
e2b6986d2b
@ -1,6 +1,7 @@
|
|||||||
package net.hostsharing.hsadminng.hs.office.coopshares;
|
package net.hostsharing.hsadminng.hs.office.coopshares;
|
||||||
|
|
||||||
import net.hostsharing.hsadminng.context.Context;
|
import net.hostsharing.hsadminng.context.Context;
|
||||||
|
import net.hostsharing.test.JsonBuilder;
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.EnumSource;
|
import org.junit.jupiter.params.provider.EnumSource;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
@ -11,7 +12,9 @@ import org.springframework.test.web.servlet.MockMvc;
|
|||||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
import static net.hostsharing.test.JsonBuilder.jsonObject;
|
||||||
import static org.hamcrest.Matchers.is;
|
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.jsonPath;
|
||||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
@ -28,118 +31,73 @@ class HsOfficeCoopSharesTransactionControllerRestTest {
|
|||||||
@MockBean
|
@MockBean
|
||||||
HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
|
HsOfficeCoopSharesTransactionRepository coopSharesTransactionRepo;
|
||||||
|
|
||||||
enum BadRequestTestCases {
|
static final String VALID_INSERT_REQUEST_BODY = """
|
||||||
MEMBERSHIP_UUID_MISSING(
|
|
||||||
"""
|
|
||||||
{
|
{
|
||||||
|
"membershipUuid": "%s",
|
||||||
"transactionType": "SUBSCRIPTION",
|
"transactionType": "SUBSCRIPTION",
|
||||||
"sharesCount": 8,
|
"sharesCount": 8,
|
||||||
"valueDate": "2022-10-13",
|
"valueDate": "2022-10-13",
|
||||||
"reference": "temp ref A"
|
"reference": "valid reference",
|
||||||
|
"comment": "valid comment"
|
||||||
}
|
}
|
||||||
""",
|
""".formatted(UUID.randomUUID());
|
||||||
|
|
||||||
|
enum BadRequestTestCases {
|
||||||
|
MEMBERSHIP_UUID_MISSING(
|
||||||
|
requestBody -> requestBody.without("membershipUuid"),
|
||||||
"[membershipUuid must not be null but is \"null\"]"),
|
"[membershipUuid must not be null but is \"null\"]"),
|
||||||
|
|
||||||
TRANSACTION_TYPE_MISSING(
|
TRANSACTION_TYPE_MISSING(
|
||||||
"""
|
requestBody -> requestBody.without("transactionType"),
|
||||||
{
|
|
||||||
"membershipUuid": "%s",
|
|
||||||
"sharesCount": 8,
|
|
||||||
"valueDate": "2022-10-13",
|
|
||||||
"reference": "temp ref A"
|
|
||||||
}
|
|
||||||
""".formatted(UUID.randomUUID()),
|
|
||||||
"[transactionType must not be null but is \"null\"]"),
|
"[transactionType must not be null but is \"null\"]"),
|
||||||
|
|
||||||
VALUE_DATE_MISSING(
|
VALUE_DATE_MISSING(
|
||||||
"""
|
requestBody -> requestBody.without("valueDate"),
|
||||||
{
|
|
||||||
"membershipUuid": "%s",
|
|
||||||
"transactionType": "SUBSCRIPTION",
|
|
||||||
"sharesCount": 8,
|
|
||||||
"reference": "temp ref A"
|
|
||||||
}
|
|
||||||
""".formatted(UUID.randomUUID()),
|
|
||||||
"[valueDate must not be null but is \"null\"]"),
|
"[valueDate must not be null but is \"null\"]"),
|
||||||
|
|
||||||
SHARES_COUNT_FOR_SUBSCRIPTION_MUST_BE_POSITIVE(
|
SHARES_COUNT_FOR_SUBSCRIPTION_MUST_BE_POSITIVE(
|
||||||
"""
|
requestBody -> requestBody
|
||||||
{
|
.with("transactionType", "SUBSCRIPTION")
|
||||||
"membershipUuid": "%s",
|
.with("sharesCount", -1),
|
||||||
"transactionType": "SUBSCRIPTION",
|
|
||||||
"sharesCount": -1,
|
|
||||||
"valueDate": "2022-10-13",
|
|
||||||
"reference": "temp ref A"
|
|
||||||
}
|
|
||||||
""".formatted(UUID.randomUUID()),
|
|
||||||
"[for SUBSCRIPTION, sharesCount must be positive but is \"-1\"]"),
|
"[for SUBSCRIPTION, sharesCount must be positive but is \"-1\"]"),
|
||||||
|
|
||||||
SHARES_COUNT_FOR_CANCELLATION_MUST_BE_NEGATIVE(
|
SHARES_COUNT_FOR_CANCELLATION_MUST_BE_NEGATIVE(
|
||||||
"""
|
requestBody -> requestBody
|
||||||
{
|
.with("transactionType", "CANCELLATION")
|
||||||
"membershipUuid": "%s",
|
.with("sharesCount", 1),
|
||||||
"transactionType": "CANCELLATION",
|
|
||||||
"sharesCount": 1,
|
|
||||||
"valueDate": "2022-10-13",
|
|
||||||
"reference": "temp ref A"
|
|
||||||
}
|
|
||||||
""".formatted(UUID.randomUUID()),
|
|
||||||
"[for CANCELLATION, sharesCount must be negative but is \"1\"]"),
|
"[for CANCELLATION, sharesCount must be negative but is \"1\"]"),
|
||||||
|
|
||||||
SHARES_COUNT_MUST_NOT_BE_NULL(
|
SHARES_COUNT_MUST_NOT_BE_NULL(
|
||||||
"""
|
requestBody -> requestBody
|
||||||
{
|
.with("transactionType", "ADJUSTMENT")
|
||||||
"membershipUuid": "%s",
|
.with("sharesCount", 0),
|
||||||
"transactionType": "ADJUSTMENT",
|
|
||||||
"sharesCount": 0,
|
|
||||||
"valueDate": "2022-10-13",
|
|
||||||
"reference": "temp ref A"
|
|
||||||
}
|
|
||||||
""".formatted(UUID.randomUUID()),
|
|
||||||
"[sharesCount must not be 0 but is \"0\"]"),
|
"[sharesCount must not be 0 but is \"0\"]"),
|
||||||
|
|
||||||
REFERENCE_MISSING(
|
REFERENCE_MISSING(
|
||||||
"""
|
requestBody -> requestBody.without("reference"),
|
||||||
{
|
|
||||||
"membershipUuid": "%s",
|
|
||||||
"transactionType": "SUBSCRIPTION",
|
|
||||||
"sharesCount": 8,
|
|
||||||
"valueDate": "2022-10-13"
|
|
||||||
}
|
|
||||||
""".formatted(UUID.randomUUID()),
|
|
||||||
"[reference must not be null but is \"null\"]"),
|
"[reference must not be null but is \"null\"]"),
|
||||||
|
|
||||||
REFERENCE_TOO_SHORT(
|
REFERENCE_TOO_SHORT(
|
||||||
"""
|
requestBody -> requestBody.with("reference", "12345"),
|
||||||
{
|
|
||||||
"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 size must be between 6 and 48 but is \"12345\"]"),
|
||||||
|
|
||||||
REFERENCE_TOO_LONG(
|
REFERENCE_TOO_LONG(
|
||||||
"""
|
requestBody -> requestBody.with("reference", "0123456789012345678901234567890123456789012345678"),
|
||||||
{
|
|
||||||
"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\"]");
|
"[reference size must be between 6 and 48 but is \"0123456789012345678901234567890123456789012345678\"]");
|
||||||
|
|
||||||
private final String givenBody;
|
private final Function<JsonBuilder, JsonBuilder> givenBodyTransformation;
|
||||||
private final String expectedErrorMessage;
|
private final String expectedErrorMessage;
|
||||||
|
|
||||||
BadRequestTestCases(final String givenBody, final String expectedErrorMessage) {
|
BadRequestTestCases(
|
||||||
this.givenBody = givenBody;
|
final Function<JsonBuilder, JsonBuilder> givenBodyTransformation,
|
||||||
|
final String expectedErrorMessage) {
|
||||||
|
this.givenBodyTransformation = givenBodyTransformation;
|
||||||
this.expectedErrorMessage = expectedErrorMessage;
|
this.expectedErrorMessage = expectedErrorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String givenRequestBody() {
|
||||||
|
return givenBodyTransformation.apply(jsonObject(VALID_INSERT_REQUEST_BODY)).toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@ -151,7 +109,7 @@ class HsOfficeCoopSharesTransactionControllerRestTest {
|
|||||||
.post("/api/hs/office/coopsharestransactions")
|
.post("/api/hs/office/coopsharestransactions")
|
||||||
.header("current-user", "superuser-alex@hostsharing.net")
|
.header("current-user", "superuser-alex@hostsharing.net")
|
||||||
.contentType(MediaType.APPLICATION_JSON)
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
.content(testCase.givenBody)
|
.content(testCase.givenRequestBody())
|
||||||
.accept(MediaType.APPLICATION_JSON))
|
.accept(MediaType.APPLICATION_JSON))
|
||||||
|
|
||||||
// then
|
// then
|
||||||
|
@ -21,7 +21,7 @@ public class JsonBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add a property (key/value pair).
|
* Add a string property (key/value pair).
|
||||||
*
|
*
|
||||||
* @param key JSON key
|
* @param key JSON key
|
||||||
* @param value JSON value
|
* @param value JSON value
|
||||||
@ -36,6 +36,33 @@ public class JsonBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a numeric property (key/value pair).
|
||||||
|
*
|
||||||
|
* @param key JSON key
|
||||||
|
* @param value JSON value
|
||||||
|
* @return this JsonBuilder
|
||||||
|
*/
|
||||||
|
public JsonBuilder with(final String key, final Number value) {
|
||||||
|
try {
|
||||||
|
jsonObject.put(key, value);
|
||||||
|
} catch (JSONException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a property (key/value pair).
|
||||||
|
*
|
||||||
|
* @param name JSON key
|
||||||
|
* @return this JsonBuilder
|
||||||
|
*/
|
||||||
|
public JsonBuilder without(final String name) {
|
||||||
|
jsonObject.remove(name);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
try {
|
try {
|
||||||
@ -52,5 +79,4 @@ public class JsonBuilder {
|
|||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user