-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add backpressure tests, use assetj
Signed-off-by: Akshay Patidar <akshaypatidar1999@gmail.com>
- Loading branch information
1 parent
8f537eb
commit 6c26ca0
Showing
8 changed files
with
161 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,161 @@ | ||
package com.dream11.rest; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.reactivex.rxjava3.core.Single; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.junit5.VertxExtension; | ||
import io.vertx.rxjava3.core.MultiMap; | ||
import io.vertx.rxjava3.core.Vertx; | ||
import io.vertx.rxjava3.core.buffer.Buffer; | ||
import io.vertx.rxjava3.ext.web.client.HttpResponse; | ||
import io.vertx.rxjava3.ext.web.client.WebClient; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.commons.io.IOUtils; | ||
import org.apache.http.HttpResponse; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.CsvSource; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
|
||
@ExtendWith({VertxExtension.class, Setup.class}) | ||
@Slf4j | ||
class RestApiIT { | ||
|
||
final CloseableHttpClient httpClient = HttpClients.createDefault(); | ||
final WebClient webClient = WebClient.create(Vertx.vertx()); | ||
|
||
@Test | ||
void nullHeaderTest() throws IOException { | ||
void nullHeaderTest() { | ||
// arrange | ||
String uri = String.format("http://127.0.0.1:%s%s/1?testFilter=query&double=1.1&float=1.1&integer=1&long=1", | ||
Constants.APPLICATION_PORT, Constants.VALIDATION_ROUTE_PATH); | ||
String path = String.format("%s/1?testFilter=query&double=1.1&float=1.1&integer=1&long=1", Constants.VALIDATION_ROUTE_PATH); | ||
// act | ||
HttpResponse response = httpClient.execute(new HttpGet(uri)); | ||
int statusCode = this.makeGetRequest(path) | ||
.map(HttpResponse::statusCode) | ||
.blockingGet(); | ||
// assert | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(400)); | ||
assertThat(statusCode).isEqualTo(400); | ||
} | ||
|
||
@Test | ||
void nullQueryParamTest() throws IOException { | ||
void nullQueryParamTest() { | ||
// arrange | ||
String uri = String.format("http://127.0.0.1:%s%s/1", Constants.APPLICATION_PORT, Constants.VALIDATION_ROUTE_PATH); | ||
HttpGet request = new HttpGet(uri); | ||
request.setHeader("testHeader", "1"); | ||
String path = String.format("%s/1", Constants.VALIDATION_ROUTE_PATH); | ||
MultiMap headers = MultiMap.caseInsensitiveMultiMap().add("testHeader", "1"); | ||
// act | ||
HttpResponse response = httpClient.execute(request); | ||
int statusCode = this.makeGetRequest(path, headers) | ||
.map(HttpResponse::statusCode) | ||
.blockingGet(); | ||
// assert | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(400)); | ||
assertThat(statusCode).isEqualTo(400); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({"/class", "/method"}) | ||
void timeoutAnnotationTest(String path) throws IOException { | ||
void timeoutAnnotationTest(String param) { | ||
// arrange | ||
String uri = String.format("http://127.0.0.1:%s%s%s", Constants.APPLICATION_PORT, Constants.TIMEOUT_ROUTE_PATH, path); | ||
String path = String.format("%s%s", Constants.TIMEOUT_ROUTE_PATH, param); | ||
// act | ||
HttpResponse response = httpClient.execute(new HttpGet(uri)); | ||
int statusCode = this.makeGetRequest(path) | ||
.map(HttpResponse::statusCode) | ||
.blockingGet(); | ||
// assert | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(503)); | ||
assertThat(statusCode).isEqualTo(503); | ||
} | ||
|
||
@ParameterizedTest | ||
@CsvSource({"integer", "long", "float", "double"}) | ||
void typeTypeValidationParamTest(String param) throws IOException { | ||
void typeTypeValidationParamTest(String param) { | ||
// arrange | ||
String path = String.format("%s/1?testFilter=query&%s=param", Constants.VALIDATION_ROUTE_PATH, param); | ||
MultiMap headers = MultiMap.caseInsensitiveMultiMap().add("testHeader", "1"); | ||
// act | ||
HttpResponse<Buffer> response = this.makeGetRequest(path, headers) | ||
.blockingGet(); | ||
JsonObject responseBody = response.bodyAsJsonObject(); | ||
// assert | ||
assertThat(response.statusCode()).isEqualTo(400); | ||
assertThat(responseBody.getJsonObject("error").getString("code")).isEqualTo("BAD_REQUEST"); | ||
assertThat(responseBody.getJsonObject("error").getString("message")).isEqualTo( | ||
String.format("Query param '%s' must be %s", param, param)); | ||
} | ||
|
||
@Test | ||
void validateBodyTest() { | ||
// arrange | ||
String uri = | ||
String.format("http://127.0.0.1:%s%s/1?testFilter=query&%s=param", Constants.APPLICATION_PORT, Constants.VALIDATION_ROUTE_PATH, | ||
param); | ||
HttpGet request = new HttpGet(uri); | ||
request.setHeader("testHeader", "1"); | ||
String path = String.format("%s", Constants.VALIDATION_ROUTE_PATH); | ||
JsonObject body = new JsonObject().put("resourceId", "Hello"); | ||
// act | ||
HttpResponse response = httpClient.execute(request); | ||
JsonObject responseBody = new JsonObject(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); | ||
HttpResponse<Buffer> response = this.makePostRequest(path, body) | ||
.blockingGet(); | ||
JsonObject responseBody = response.bodyAsJsonObject(); | ||
// assert | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(400)); | ||
assertThat(responseBody.getJsonObject("error").getString("code"), equalTo("BAD_REQUEST")); | ||
assertThat(responseBody.getJsonObject("error").getString("message"), | ||
equalTo(String.format("Query param '%s' must be %s", param, param))); | ||
assertThat(response.statusCode()).isEqualTo(400); | ||
assertThat(responseBody.getJsonObject("error").getString("code")).isEqualTo("BAD_REQUEST"); | ||
assertThat(responseBody.getJsonObject("error").getString("message")).isEqualTo("resourceId must be integer"); | ||
} | ||
|
||
@Test | ||
void validateBodyTest() throws IOException { | ||
void positiveBodyTest() { | ||
// arrange | ||
String uri = String.format("http://127.0.0.1:%s%s", Constants.APPLICATION_PORT, Constants.VALIDATION_ROUTE_PATH); | ||
HttpPost request = new HttpPost(uri); | ||
request.setHeader("Content-type", "application/json"); | ||
JsonObject json = new JsonObject().put("resourceId", "Hello"); | ||
request.setEntity(new StringEntity(json.toString())); | ||
String path = String.format("%s", Constants.VALIDATION_ROUTE_PATH); | ||
JsonObject body = new JsonObject().put("resourceId", "1"); | ||
// act | ||
HttpResponse response = httpClient.execute(request); | ||
JsonObject responseBody = new JsonObject(IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8)); | ||
HttpResponse<Buffer> response = this.makePostRequest(path, body) | ||
.blockingGet(); | ||
// assert | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(400)); | ||
assertThat(responseBody.getJsonObject("error").getString("code"), equalTo("BAD_REQUEST")); | ||
assertThat(responseBody.getJsonObject("error").getString("message"), equalTo("resourceId must be integer")); | ||
assertThat(response.statusCode()).isEqualTo(200); | ||
} | ||
|
||
@Test | ||
void positiveBodyTest() throws IOException { | ||
void routeNotFoundTest() { | ||
// arrange | ||
String uri = String.format("http://127.0.0.1:%s%s", Constants.APPLICATION_PORT, Constants.VALIDATION_ROUTE_PATH); | ||
HttpPost request = new HttpPost(uri); | ||
request.setHeader("Content-type", "application/json"); | ||
JsonObject json = new JsonObject().put("resourceId", "1"); | ||
request.setEntity(new StringEntity(json.toString())); | ||
String path = "/nonexistent"; | ||
// act | ||
HttpResponse response = httpClient.execute(request); | ||
int statusCode = this.makeGetRequest(path) | ||
.map(HttpResponse::statusCode) | ||
.blockingGet(); | ||
// assert | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(200)); | ||
assertThat(statusCode).isEqualTo(404); | ||
} | ||
|
||
|
||
@Test | ||
void routeNotFoundTest() throws IOException { | ||
@SneakyThrows | ||
void testBackPressure() { | ||
// arrange | ||
String uri = String.format("http://127.0.0.1:%s%s", Constants.APPLICATION_PORT, "/nonexistent"); | ||
JsonObject body = JsonObject.of("resourceId", "1"); | ||
List<Single<HttpResponse<Buffer>>> responseSingles = new ArrayList<>(); | ||
for (int i = 0; i < 10; i++) { | ||
responseSingles.add(this.makePostRequest(Constants.VALIDATION_ROUTE_PATH, body)); | ||
} | ||
|
||
// act | ||
HttpResponse response = httpClient.execute(new HttpGet(uri)); | ||
List<Integer> statusCodes = Single.merge(responseSingles) | ||
.map(HttpResponse::statusCode) | ||
.toList() | ||
.blockingGet(); | ||
|
||
// assert | ||
assertThat(response.getStatusLine().getStatusCode(), equalTo(404)); | ||
assertThat(statusCodes).contains(200, 503); | ||
} | ||
|
||
private Single<HttpResponse<Buffer>> makePostRequest(String path, JsonObject body) { | ||
String uri = String.format("http://127.0.0.1:%s%s", Constants.APPLICATION_PORT, path); | ||
return this.webClient.postAbs(uri) | ||
.putHeader("Content-type", "application/json") | ||
.rxSendJsonObject(body); | ||
} | ||
|
||
private Single<HttpResponse<Buffer>> makeGetRequest(String path, MultiMap headers) { | ||
String uri = String.format("http://127.0.0.1:%s%s", Constants.APPLICATION_PORT, path); | ||
return this.webClient.getAbs(uri) | ||
.putHeaders(headers) | ||
.rxSend(); | ||
} | ||
|
||
private Single<HttpResponse<Buffer>> makeGetRequest(String path) { | ||
return makeGetRequest(path, MultiMap.caseInsensitiveMultiMap()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.