From 16d870f8c5f19fb214f9e5e48b8cb9c1ca216c32 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Mon, 2 Jun 2025 21:28:28 -0700 Subject: [PATCH] test: added validate error test for malformed json payload --- .../SwitcherRelayControllerTests.java | 13 ++++++ .../fixture/ControllerTestUtils.java | 46 +++++++++++++++---- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java index 9639244..ba15d94 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java +++ b/src/test/java/com/github/switcherapi/ac/controller/SwitcherRelayControllerTests.java @@ -179,6 +179,19 @@ void shouldBeOkWhenValidate_unlimitedUseFeature() { 10000, expectedResponse, 200); } + @Test + void shouldNotBeOkWhenValidate_payloadMalformed() { + //given + givenAccount("masteradminid"); + + var plan = Plan.loadDefault(); + planService.createPlan(plan).block(); + + //test + var expectedResponse = ResponseRelayDTO.fail("com.google.gson.stream.MalformedJsonException"); + this.assertValidate500(SWITCHER.getValue(), expectedResponse); + } + @Test void shouldReturnTrue() { //given diff --git a/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java b/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java index 5547b9f..9ba12c0 100644 --- a/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java +++ b/src/test/java/com/github/switcherapi/ac/controller/fixture/ControllerTestUtils.java @@ -16,6 +16,8 @@ import org.springframework.test.web.reactive.server.WebTestClient; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class ControllerTestUtils { @@ -36,16 +38,21 @@ protected void assertValidate(String adminId, String featureName, var request = new RequestRelayDTO(null, gson.toJson(feature)); //test - webTestClient.post() - .uri("/switcher/v1/validate") - .contentType(MediaType.APPLICATION_JSON) - .header(HttpHeaders.AUTHORIZATION, "Bearer relay_token") - .bodyValue(gson.toJson(request)) - .exchange() - .expectStatus().isEqualTo(expectedStatus) - .expectBody(String.class) - .consumeWith(response -> - assertThat(response.getResponseBody()).contains(gson.toJson(expectedResponse))); + performValidatePost(expectedResponse, expectedStatus, request); + } + + protected void assertValidate500(String featureName, ResponseRelayDTO expectedResponse) { + //given + var feature = FeaturePayload.builder() + .feature(featureName) + .owner("masteradminid") + .total(10000) + .build(); + + var request = new RequestRelayDTO(null, gson.toJson(feature) + "}"); + + //test + performValidatePost(expectedResponse, 500, request); } protected void assertLimiter(String value, ResponseRelayDTO expectedResponse, @@ -82,4 +89,23 @@ protected void assertDtoResponse(Plan planObj, String response) assertThat(planDto.name()).isEqualTo(planObj.getName()); } + private void performValidatePost(ResponseRelayDTO expectedResponse, int expectedStatus, RequestRelayDTO request) { + webTestClient.post() + .uri("/switcher/v1/validate") + .contentType(MediaType.APPLICATION_JSON) + .header(HttpHeaders.AUTHORIZATION, "Bearer relay_token") + .bodyValue(gson.toJson(request)) + .exchange() + .expectStatus().isEqualTo(expectedStatus) + .expectBody(String.class) + .consumeWith(response -> { + var responseObject = gson.fromJson(response.getResponseBody(), ResponseRelayDTO.class); + + assertNotNull(responseObject); + assertEquals(expectedResponse.result(), responseObject.result()); + assertThat(String.valueOf(responseObject.message())).contains(String.valueOf(expectedResponse.message())); + }); + + } + }