Skip to content

Commit

Permalink
Use common methods in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Wawrowski committed Sep 11, 2018
1 parent fde4491 commit e332821
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,23 @@
*/
package io.knotx.assembler;

import static io.knotx.junit5.util.RequestUtil.subscribeToResult_shouldSucceed;

import io.knotx.dataobjects.KnotContext;
import io.knotx.junit.util.FileReader;
import io.knotx.junit.util.KnotContextFactory;
import io.knotx.junit5.KnotxApplyConfiguration;
import io.knotx.junit5.KnotxExtension;
import io.knotx.reactivex.proxy.KnotProxy;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.reactivex.Single;
import io.reactivex.functions.Consumer;
import io.vertx.junit5.VertxTestContext;
import io.vertx.reactivex.core.Vertx;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -129,12 +132,9 @@ private void callAssemblerWithAssertions(
Consumer<KnotContext> verifyResultFunction) {
KnotProxy service = KnotProxy.createProxy(vertx, ADDRESS);

service.rxProcess(KnotContextFactory.create(fragments))
.doOnSuccess(verifyResultFunction::accept)
.subscribe(
success -> context.completeNow(),
context::failNow
);
Single<KnotContext> knotContextSingle = service.rxProcess(KnotContextFactory.create(fragments));

subscribeToResult_shouldSucceed(context, knotContextSingle, verifyResultFunction);
}

private Pair<List<String>, String> toPair(String filePath, String... knots) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import io.knotx.dataobjects.Fragment;
import io.knotx.fragments.SnippetPatterns;
import io.knotx.junit5.KnotxTestUtils;
import io.knotx.junit5.KnotxArgumentConverter;
import io.knotx.junit5.util.FileReader;
import io.knotx.options.SnippetOptions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.converter.ConvertWith;
Expand All @@ -41,7 +41,7 @@ public void asIs_whenConfiguredSnippetTag_expectIgnoredContent(
String expectedContentFileName, String snippetTagName, String paramsPrefix) throws Exception {
final String unwrappedContent = UnprocessedFragmentStrategy.AS_IS
.get(fragment, new SnippetPatterns(buildOptions(snippetTagName, paramsPrefix)));
final String expectedContent = KnotxTestUtils.readText(expectedContentFileName);
final String expectedContent = FileReader.readText(expectedContentFileName);

assertThat(unwrappedContent, equalToIgnoringWhiteSpace(expectedContent));
}
Expand All @@ -58,7 +58,7 @@ public void unwrap_whenConfiguredSnippetTag_expectDefinedContentWithComments(
String expectedContentFileName, String snippetTagName, String paramsPrefix) throws Exception {
final String unwrappedContent = UnprocessedFragmentStrategy.UNWRAP
.get(fragment, new SnippetPatterns(buildOptions(snippetTagName, paramsPrefix)));
final String expectedContent = KnotxTestUtils.readText(expectedContentFileName);
final String expectedContent = FileReader.readText(expectedContentFileName);

assertThat(unwrappedContent, equalToIgnoringWhiteSpace(expectedContent));
}
Expand All @@ -75,7 +75,7 @@ public void ignore_whenConfiguredSnippetTag_expectIgnoredContent(
String expectedContentFileName, String snippetTagName, String paramsPrefix) throws Exception {
final String unwrappedContent = UnprocessedFragmentStrategy.IGNORE
.get(fragment, new SnippetPatterns(buildOptions(snippetTagName, paramsPrefix)));
final String expectedContent = KnotxTestUtils.readText(expectedContentFileName);
final String expectedContent = FileReader.readText(expectedContentFileName);

assertThat(unwrappedContent, equalToIgnoringWhiteSpace(expectedContent));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package io.knotx.server;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down
83 changes: 36 additions & 47 deletions knotx-core/src/test/java/io/knotx/server/KnotxServerCsrfTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@
package io.knotx.server;


import static io.knotx.junit5.util.RequestUtil.subscribeToResult_shouldSucceed;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import io.knotx.dataobjects.KnotContext;
import io.knotx.junit5.KnotxApplyConfiguration;
import io.knotx.junit5.KnotxExtension;
import io.knotx.junit5.util.RequestUtil;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.buffer.Buffer;
import io.reactivex.Single;
import io.vertx.ext.web.handler.CSRFHandler;
import io.vertx.junit5.VertxTestContext;
import io.vertx.reactivex.core.MultiMap;
import io.vertx.reactivex.core.Vertx;
import io.vertx.reactivex.core.buffer.Buffer;
import io.vertx.reactivex.ext.web.client.HttpResponse;
import io.vertx.reactivex.ext.web.client.WebClient;
import java.util.List;
import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

Expand All @@ -53,23 +55,18 @@ public void whenRequestingGetLocalPath_expectLocalAC(

WebClient client = WebClient.create(vertx);

client.get(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html").send(
ar -> {
if (ar.succeeded()) {
assertEquals(HttpResponseStatus.OK.code(), ar.result().statusCode());
assertTrue(ar.result().getHeader(EXPECTED_RESPONSE_HEADER) != null);
assertEquals(EXPECTED_XSERVER_HEADER_VALUE,
ar.result().getHeader(EXPECTED_RESPONSE_HEADER));
assertTrue(ar.result().cookies().stream()
.anyMatch(cookie -> cookie.contains(CSRFHandler.DEFAULT_COOKIE_NAME)));
client.close();
context.completeNow();
} else {
context.failNow(ar.cause());
context.completeNow();
}
}
);
Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> httpResponseSingle = client
.get(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html")
.rxSend();

subscribeToResult_shouldSucceed(context, httpResponseSingle, response -> {
assertEquals(HttpResponseStatus.OK.code(), response.statusCode());
assertTrue(response.getHeader(EXPECTED_RESPONSE_HEADER) != null);
assertEquals(EXPECTED_XSERVER_HEADER_VALUE,
response.getHeader(EXPECTED_RESPONSE_HEADER));
assertTrue(response.cookies().stream()
.anyMatch(cookie -> cookie.contains(CSRFHandler.DEFAULT_COOKIE_NAME)));
});
}

@Test
Expand All @@ -83,16 +80,13 @@ public void whenDoPostSecureWithoutCSRF_expectForbidden(
MultiMap body = MultiMap.caseInsensitiveMultiMap().add("field", "value");

WebClient client = WebClient.create(vertx);
client.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html")
.sendForm(body, ar -> {
if (ar.succeeded()) {
assertEquals(HttpResponseStatus.FORBIDDEN.code(), ar.result().statusCode());
context.completeNow();
} else {
context.failNow(ar.cause());
context.completeNow();
}
});
Single<HttpResponse<Buffer>> httpResponseSingle = client
.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html")
.rxSendForm(body);

RequestUtil.subscribeToResult_shouldSucceed(context, httpResponseSingle, result -> {
assertEquals(HttpResponseStatus.FORBIDDEN.code(), result.statusCode());
});
}

@Test
Expand All @@ -106,16 +100,13 @@ public void whenDoPostPublicWithoutCSRF_expectOk(
MultiMap body = MultiMap.caseInsensitiveMultiMap().add("field", "value");

WebClient client = WebClient.create(vertx);
client.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/public.html")
.sendForm(body, ar -> {
if (ar.succeeded()) {
assertEquals(HttpResponseStatus.OK.code(), ar.result().statusCode());
context.completeNow();
} else {
context.failNow(ar.cause());
context.completeNow();
}
});
Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> httpResponseSingle = client
.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/public.html")
.rxSendForm(body);

RequestUtil.subscribeToResult_shouldSucceed(context, httpResponseSingle, resp -> {
assertEquals(HttpResponseStatus.OK.code(), resp.statusCode());
});
}

@Test
Expand Down Expand Up @@ -143,12 +134,10 @@ public void whenDoPostSecureWithCSRF_expectOK(
context.completeNow();
} else {
context.failNow(ar.cause());
context.completeNow();
}
});
} else {
context.failNow(ar.cause());
context.completeNow();
}
});
}
Expand All @@ -159,12 +148,12 @@ private void createPassThroughKnot(Vertx vertx, String address) {

private void createSimpleKnot(Vertx vertx, final String address, final String addToBody,
final String transition) {
Consumer<KnotContext> simpleKnot = knotContext -> {
Buffer inBody = knotContext.getClientResponse().getBody();
knotContext.getClientResponse().setBody(inBody.appendString(addToBody));
MockKnotProxy.register(vertx.getDelegate(), address, knotContext -> {
knotContext.getClientResponse().setBody(
knotContext.getClientResponse().getBody().appendString(addToBody)
);
knotContext.setTransition(transition);
};
MockKnotProxy.register(vertx.getDelegate(), address, simpleKnot);
});
}

private String getToken(List<String> result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import io.knotx.junit.util.FileReader;
import io.knotx.junit5.util.FileReader;
import io.knotx.server.configuration.KnotxFlowSettings;
import io.knotx.server.configuration.KnotxServerOptions;
import io.vertx.core.json.JsonObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.MatcherAssert.assertThat;

import io.knotx.junit.util.FileReader;
import io.knotx.junit5.util.FileReader;
import io.knotx.server.configuration.KnotxServerOptions;
import io.knotx.server.configuration.MethodRoutingEntries;
import io.vertx.core.http.HttpMethod;
Expand Down
Loading

0 comments on commit e332821

Please sign in to comment.