Search in sources :

Example 1 with BadRequestException

use of uk.gov.pay.api.exception.BadRequestException in project pay-publicapi by alphagov.

the class RequestJsonParser method validateAndGetSource.

private static Source validateAndGetSource(JsonNode paymentRequest) {
    if (paymentRequest.has(INTERNAL)) {
        JsonNode internalNode = paymentRequest.get(INTERNAL);
        if (internalNode.has(SOURCE_FIELD_NAME)) {
            String errorMessage = "Accepted values are only CARD_PAYMENT_LINK, CARD_AGENT_INITIATED_MOTO";
            PaymentError paymentError = aPaymentError(SOURCE_FIELD_NAME, CREATE_PAYMENT_VALIDATION_ERROR, errorMessage);
            String sourceString = validateSkipNullValueAndGetString(internalNode.get(SOURCE_FIELD_NAME), paymentError);
            try {
                Source source = Source.valueOf(sourceString);
                if (ALLOWED_SOURCES.contains(source)) {
                    return source;
                }
                throw new BadRequestException(paymentError);
            } catch (IllegalArgumentException e) {
                throw new WebApplicationException(Response.status(SC_UNPROCESSABLE_ENTITY).entity(paymentError).build());
            }
        }
    }
    return CARD_API;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BadRequestException(uk.gov.pay.api.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Source(uk.gov.service.payments.commons.model.Source) PaymentError(uk.gov.pay.api.model.PaymentError) PaymentError.aPaymentError(uk.gov.pay.api.model.PaymentError.aPaymentError)

Example 2 with BadRequestException

use of uk.gov.pay.api.exception.BadRequestException in project pay-publicapi by alphagov.

the class CreatePaymentRefundRequestDeserializerTest method deserialize_shouldThrowBadRequestException_whenJsonIsNotWellFormed.

@Test
public void deserialize_shouldThrowBadRequestException_whenJsonIsNotWellFormed() throws Exception {
    String invalidJson = "{" + "  \"amount\" : " + "}";
    BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> deserializer.deserialize(jsonFactory.createParser(invalidJson), ctx));
    assertThat(badRequestException, aBadRequestExceptionWithError("P0697", "Unable to parse JSON"));
}
Also used : BadRequestException(uk.gov.pay.api.exception.BadRequestException) Test(org.junit.Test)

Example 3 with BadRequestException

use of uk.gov.pay.api.exception.BadRequestException in project pay-publicapi by alphagov.

the class CreatePaymentRefundRequestDeserializerTest method deserialize_shouldThrowValidationException_whenAmountIsNotInteger.

@Test
public void deserialize_shouldThrowValidationException_whenAmountIsNotInteger() throws Exception {
    String json = "{" + "  \"amount\" : \"\"" + "}";
    BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> deserializer.deserialize(jsonFactory.createParser(json), ctx));
    assertThat(badRequestException, aBadRequestExceptionWithError("P0602", "Invalid attribute value: amount. Must be a valid numeric format"));
}
Also used : BadRequestException(uk.gov.pay.api.exception.BadRequestException) Test(org.junit.Test)

Example 4 with BadRequestException

use of uk.gov.pay.api.exception.BadRequestException in project pay-publicapi by alphagov.

the class CreatePaymentRefundRequestDeserializerTest method deserialize_shouldThrowValidationException_asAmountIsMissing_whenAmountIsNullValue.

@Test
public void deserialize_shouldThrowValidationException_asAmountIsMissing_whenAmountIsNullValue() throws Exception {
    String json = "{" + "  \"amount\" : null" + "}";
    BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> deserializer.deserialize(jsonFactory.createParser(json), ctx));
    assertThat(badRequestException, aBadRequestExceptionWithError("P0601", "Missing mandatory attribute: amount"));
}
Also used : BadRequestException(uk.gov.pay.api.exception.BadRequestException) Test(org.junit.Test)

Example 5 with BadRequestException

use of uk.gov.pay.api.exception.BadRequestException in project pay-publicapi by alphagov.

the class TransactionSearchServiceTest method shouldThrowBadRequestException.

@Test
public void shouldThrowBadRequestException() {
    TransactionSearchParams searchParams = mock(TransactionSearchParams.class);
    when(searchParams.getQueryMap()).thenReturn(Map.of("not_supported", "hello"));
    BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> transactionSearchService.doSearch(new Account("1", TokenPaymentType.CARD, "a-token-link"), searchParams));
    assertThat(badRequestException, aBadRequestExceptionWithError("P0401", "Invalid parameters: not_supported. See Public API documentation for the correct data formats"));
}
Also used : Account(uk.gov.pay.api.auth.Account) TransactionSearchParams(uk.gov.pay.api.ledger.model.TransactionSearchParams) BadRequestException(uk.gov.pay.api.exception.BadRequestException) Test(org.junit.Test)

Aggregations

BadRequestException (uk.gov.pay.api.exception.BadRequestException)46 Test (org.junit.jupiter.api.Test)38 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)21 JsonNode (com.fasterxml.jackson.databind.JsonNode)19 Test (org.junit.Test)5 IOException (java.io.IOException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1 Account (uk.gov.pay.api.auth.Account)1 TransactionSearchParams (uk.gov.pay.api.ledger.model.TransactionSearchParams)1 CreatePaymentRefundRequest (uk.gov.pay.api.model.CreatePaymentRefundRequest)1 PaymentError (uk.gov.pay.api.model.PaymentError)1 PaymentError.aPaymentError (uk.gov.pay.api.model.PaymentError.aPaymentError)1 Source (uk.gov.service.payments.commons.model.Source)1