Skip to content
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

Feature/junit5 tests with it tests #18

Merged
merged 6 commits into from Sep 6, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
@@ -1,7 +1,7 @@
sudo: required
language: java
install: true
script: mvn clean org.jacoco:jacoco-maven-plugin:prepare-agent package -Pskip-integration-tests,release
script: mvn -U clean org.jacoco:jacoco-maven-plugin:prepare-agent package -Pskip-integration-tests,release
jdk:
- oraclejdk8
cache:
Expand Down
Expand Up @@ -46,7 +46,7 @@ public void before() {
}

@Test
@KnotxApplyConfiguration("knotx-test-app.json")
@KnotxApplyConfiguration({"knotx-test-app.conf"})
public void whenRequestingRemoteRepository_expectOnlyAllowedResponseHeaders(
VertxTestContext context, Vertx vertx) {
testGetRequest(context, vertx, REMOTE_REQUEST_URI);
Expand Down
Expand Up @@ -19,21 +19,17 @@

import io.knotx.dataobjects.AdapterResponse;
import io.knotx.dataobjects.ClientResponse;
import io.knotx.junit.util.FileReader;
import io.knotx.junit5.KnotxApplyConfiguration;
import io.knotx.junit5.KnotxExtension;
import io.knotx.junit5.KnotxTestUtils;
import io.knotx.junit5.util.FileReader;
import io.knotx.proxy.AdapterProxy;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.Observable;
import io.vertx.core.Future;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.json.JsonObject;
import io.vertx.junit5.VertxTestContext;
import io.vertx.reactivex.core.MultiMap;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.core.http.HttpClient;
import io.vertx.reactivex.core.http.HttpClientResponse;
import io.vertx.reactivex.ext.web.client.WebClient;
import io.vertx.serviceproxy.ServiceBinder;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -54,139 +50,126 @@ public class SampleApplicationIntegrationTest {
private static final String KNOTX_SERVER_ADDRESS = "localhost";

@Test
@KnotxApplyConfiguration("knotx-test-app.json")
@KnotxApplyConfiguration("knotx-test-app.conf")
public void whenRequestingLocalSimplePageWithGet_expectLocalSimpleHtml(
VertxTestContext context, Vertx vertx) {
testGetRequest(context, vertx, LOCAL_REQUEST_URI, "localSimpleResult.html");
}

@Test
@KnotxApplyConfiguration("knotx-test-app-custom-symbol.json")
@KnotxApplyConfiguration({"knotx-test-app.conf", "knotx-test-handlebars-custom-symbol.conf"})
public void whenRequestingLocalSimplePageWithGetCustomSymbol_expectLocalSimpleHtml(
VertxTestContext context, Vertx vertx) {
testGetRequest(context, vertx, "/content/local/customSymbol.html", "localSimpleResultAngular.html");
testGetRequest(context, vertx, "/content/local/customSymbol.html",
"localSimpleResultAngular.html");
}

@Test
@KnotxApplyConfiguration("knotx-test-app-custom-and-default-symbol.json")
@KnotxApplyConfiguration({"knotx-test-app.conf", "knotx-test-handlebars-custom-symbol.conf"})
public void whenRequestingLocalSimplePageWithGetCustomAndDefaultSymbol_expectLocalSimpleHtmlWithDefault(
VertxTestContext context, Vertx vertx) {
testGetRequest(context, vertx, "/content/local/customAndDefaultSymbol.html",
"localSimpleResultCustomAndDefault.html");
}

@Test
@KnotxApplyConfiguration("knotx-test-app-no-body.json")
@KnotxApplyConfiguration({"knotx-test-app.conf"})
public void whenRequestingLocalPageWhereInServiceIsMissingResponseBody_expectNoBodyHtml(
VertxTestContext context, Vertx vertx) {
testGetRequest(context, vertx, LOCAL_NO_BODY_REQUEST_URI, "noBody.html");
}

@Test
@KnotxApplyConfiguration("knotx-test-app.json")
@KnotxApplyConfiguration("knotx-test-app.conf")
public void whenRequestingPageWithMissingServiceWithoutConfiguration_expectServerError(
VertxTestContext context, Vertx vertx) {
testGetServerError(context, vertx, MISSING_SERVICE_CONFIG_REQUEST_URI);
}

@Test
@KnotxApplyConfiguration("knotx-test-app.json")
@KnotxApplyConfiguration("knotx-test-app.conf")
public void whenRequestingRemoteSimplePageWithGet_expectRemoteSimpleHtml(
VertxTestContext context, Vertx vertx) {
testGetRequest(context, vertx, REMOTE_REQUEST_URI, "remoteSimpleResult.html");
}

@Test
@KnotxApplyConfiguration("knotx-test-app.json")
@KnotxApplyConfiguration("knotx-test-app.conf")
public void whenRequestingRemoteSimplePageWithGetAndRequestParameterNameContainsSpace_expectRemoteSimpleHtml(
VertxTestContext context, Vertx vertx) {
testGetRequest(context, vertx, REMOTE_REQUEST_URI_WITH_PARAMETER_CONTAINING_SPACE,
"remoteSimpleResult.html");
}

@Test
@KnotxApplyConfiguration("knotx-test-app.json")
@KnotxApplyConfiguration("knotx-test-app.conf")
public void whenRequestingLocalMultipleFormsPageWithGet_expectMutlipleFormsWithGetResultHtml(
VertxTestContext context, Vertx vertx) {
testGetRequest(context, vertx, LOCAL_MULTIPLE_FORMS_URI, "multipleFormWithGetResult.html");
}

@Test
@KnotxApplyConfiguration("knotx-test-app.json")
@KnotxApplyConfiguration("knotx-test-app.conf")
public void whenRequestingWithPostMethodFirstForm_expectFirstFormPresentingFormActionResult(
VertxTestContext context, Vertx vertx) {
mockActionAdapter(vertx, getFirstTestFormData(), null);
testPostRequest(context, vertx, LOCAL_MULTIPLE_FORMS_URI, getFirstTestFormData().getMap(),
"multipleFormWithPostResult.html", false);
"multipleFormWithPostResult.html");
}

@Test
@KnotxApplyConfiguration("knotx-test-app.json")
@KnotxApplyConfiguration("knotx-test-app.conf")
public void whenRequestingWithPostFirstFormTwiceWithDifferentData_expectDifferentResultOfFirstFormForEachRequest(
VertxTestContext context, Vertx vertx) {
mockActionAdapter(vertx, getFirstTestFormData(), getSecondTestFormData());
testPostRequest(context, vertx, LOCAL_MULTIPLE_FORMS_URI, getFirstTestFormData().getMap(),
"multipleFormWithPostResult.html", false);
"multipleFormWithPostResult.html");
testPostRequest(context, vertx, LOCAL_MULTIPLE_FORMS_URI, getSecondTestFormData().getMap(),
"multipleFormWithPostResult2.html", false);
}

private void testPostRequest(VertxTestContext context, Vertx vertx, String url, Map<String, Object> formData,
String expectedResponseFile, boolean ajaxCall) {
HttpClient client = vertx.createHttpClient();

Observable<HttpClientResponse> request = KnotxTestUtils
.asyncRequest(client, HttpMethod.POST, KNOTX_SERVER_PORT,
KNOTX_SERVER_ADDRESS, url, req -> {
String bodyForm = formData.entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.reduce((p1, p2) -> p1 + "&" + p2).get();
req.headers().set("content-length", String.valueOf(bodyForm.length()));
req.headers().set("content-type", "application/x-www-form-urlencoded");
if (ajaxCall) {
req.headers().set("X-Requested-With", "XMLHttpRequest");
}
req.write(bodyForm);
});
"multipleFormWithPostResult2.html");
}

request.subscribe(resp -> resp.bodyHandler(body -> {
assertEquals(resp.statusCode(), HttpResponseStatus.OK.code());
try {
assertEquals(Jsoup.parse(body.toString()).body().html(),
Jsoup.parse(FileReader.readText(expectedResponseFile)).body().html());
} catch (Exception e) {
context.failNow(e);
}
private void testPostRequest(VertxTestContext context, Vertx vertx, String url,
Map<String, Object> formData,
String expectedResponseFile) {

context.completeNow();
}));
WebClient client = WebClient.create(vertx);
client.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url)
.rxSendForm(getMultiMap(formData))
.doOnSuccess(resp -> {
assertEquals(resp.statusCode(), HttpResponseStatus.OK.code());
assertEquals(Jsoup.parse(resp.body().toString()).body().html(),
Jsoup.parse(FileReader.readTextSafe(expectedResponseFile)).body().html());
})
.subscribe(onSuccess -> context.completeNow(), context::failNow);
}

private void testGetRequest(VertxTestContext context, Vertx vertx, String url, String expectedResponseFile) {
HttpClient client = vertx.createHttpClient(new HttpClientOptions());
private MultiMap getMultiMap(Map<String, Object> formData) {
MultiMap formMap = MultiMap.caseInsensitiveMultiMap();
formData.forEach((key, value) -> formMap.add(key, (String) value));
return formMap;
}

client.getNow(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url,
resp -> resp.bodyHandler(body -> {
private void testGetRequest(VertxTestContext context, Vertx vertx, String url,
String expectedResponseFile) {
WebClient client = WebClient.create(vertx);
client.get(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url).rxSend()
.doOnSuccess(resp -> {
assertEquals(resp.statusCode(), HttpResponseStatus.OK.code());
try {
assertEquals(Jsoup.parse(body.toString()).body().html().trim(),
Jsoup.parse(FileReader.readText(expectedResponseFile)).body().html().trim());
} catch (Exception e) {
context.failNow(e);
}
assertEquals(Jsoup.parse(resp.body().toString()).body().html().trim(),
Jsoup.parse(FileReader.readTextSafe(expectedResponseFile)).body().html().trim());
client.close();
context.completeNow();
}));
})
.subscribe(onSuccess -> context.completeNow(), context::failNow);
}

private void testGetServerError(VertxTestContext context, Vertx vertx, String url) {
HttpClient client = vertx.createHttpClient();
client.getNow(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url,
resp -> resp.bodyHandler(body -> {
WebClient client = WebClient.create(vertx);
client.get(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url).rxSend()
.doOnSuccess(resp -> {
assertEquals(resp.statusCode(), HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
client.close();
context.completeNow();
}));
})
.subscribe(onSuccess -> context.completeNow(), context::failNow);
}

private JsonObject getFirstTestFormData() {
Expand All @@ -209,7 +192,7 @@ private void mockActionAdapter(Vertx vertx, JsonObject competitionData,
AdapterResponse resp = new AdapterResponse().setResponse(clientResponse);

new ServiceBinder(vertx.getDelegate())
.setAddress("mock.action.adapter")
.setAddress("knotx.action.adapter")
.register(AdapterProxy.class, (request, result) -> {
String path = request.getParams().getString("path");
if (StringUtils.isNotBlank(path)) {
Expand Down