Skip to content

test: added validate error test for malformed json payload #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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,
Expand Down Expand Up @@ -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()));
});

}

}