Skip to content

Commit

Permalink
add context
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Nov 27, 2023
1 parent baa24e7 commit 0b31cec
Show file tree
Hide file tree
Showing 12 changed files with 161 additions and 58 deletions.
27 changes: 21 additions & 6 deletions src/main/java/com/casinthecloud/simpletest/cas/CasLoginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import lombok.Getter;
import lombok.Setter;
import lombok.val;
import org.apache.commons.lang3.tuple.Pair;

import java.util.Map;

import static com.casinthecloud.simpletest.util.Utils.addUrlParameter;
import static com.casinthecloud.simpletest.util.Utils.after;
import static org.apache.commons.lang3.StringUtils.substringAfter;

/**
* A test performing a CAS login in the CAS server.
Expand All @@ -20,22 +23,34 @@ public class CasLoginTest extends CasTest {

private String serviceUrl = "http://localhost:8081/";

public void run() throws Exception {
public void run(final Map<String, Object> ctx) throws Exception {

startTimer();
val serviceUrl = getServiceUrl();

var loginUrl = getCasPrefixUrl() + "/login";
loginUrl = addUrlParameter(loginUrl, "service", serviceUrl);
var tgc = (Pair<String, String>) ctx.get(TGC);

if (tgc != null) {
info("Re-use " + tgc.getLeft() + " : " + tgc.getRight());
_cookies.put(getCasCookieName(), tgc.getRight());
}
_request = get(loginUrl);
execute();
assertStatus(200);
if (_status == 200) {

executePostCasCredentials(loginUrl);
val callbackUrl = getLocation();
val st = after(callbackUrl, "ticket=");
executePostCasCredentials(loginUrl);
tgc = getCookie(getCasCookieName());
ctx.put(TGC, tgc);
info("Found " + tgc.getLeft() + " : " + tgc.getRight());

} else {
assertStatus(302);
}

val callbackUrl = getLocation();
val st = substringAfter(callbackUrl, "ticket=");
var validateUrl = getCasPrefixUrl() + "/p3/serviceValidate";
validateUrl = addUrlParameter(validateUrl, "service", serviceUrl);
validateUrl = addUrlParameter(validateUrl, "ticket", st);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import lombok.Setter;
import lombok.val;

import static com.casinthecloud.simpletest.util.Utils.*;
import java.util.Map;

import static com.casinthecloud.simpletest.util.Utils.addUrlParameter;
import static com.casinthecloud.simpletest.util.Utils.random;
import static org.apache.commons.lang3.StringUtils.substringBetween;

/**
* A test performing an OIDC login in the CAS server.
Expand All @@ -22,7 +26,7 @@ public class CasOIDCLoginTest extends CasLoginTest {

private String scope = "openid email profile";

public void run() throws Exception {
public void run(final Map<String, Object> ctx) throws Exception {
startTimer();

val clientId = getClientId();
Expand Down Expand Up @@ -65,7 +69,7 @@ public void run() throws Exception {
execute();
assertStatus(302);
val clientAppUrl = getLocation();
val code = between(clientAppUrl, "code=", "&state");
val code = substringBetween(clientAppUrl, "code=", "&state");

var tokenUrl = getCasPrefixUrl() + "/oidc/token";
tokenUrl = addUrlParameter(tokenUrl, "grant_type", "authorization_code");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Map;

import static com.casinthecloud.simpletest.util.Utils.*;
import static org.apache.commons.lang3.StringUtils.substringBetween;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

Expand All @@ -26,7 +28,7 @@ public class CasSAML2LoginTest extends CasTest {

private String relayState = "https://specialurl";

public void run() throws Exception {
public void run(final Map<String, Object> ctx) throws Exception {

startTimer();

Expand Down Expand Up @@ -70,8 +72,8 @@ public void run() throws Exception {
execute();
assertStatus(200);

val pac4jCallbackUrl = htmlDecode(between(_body, "<form action=\"", "\" met"));
val samlResponse = htmlDecode(between(_body, "\"SAMLResponse\" value=\"", "\"/>"));
val pac4jCallbackUrl = htmlDecode(substringBetween(_body, "<form action=\"", "\" met"));
val samlResponse = htmlDecode(substringBetween(_body, "\"SAMLResponse\" value=\"", "\"/>"));
assertEquals(serviceUrl, pac4jCallbackUrl);
assertNotNull(base64Decode(samlResponse));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class Execution {

public Execution(final Supplier<BaseTest> supplierTest) {
this(1, 1, supplierTest);
displayInfos = true;
displayErrors = true;
}

public Execution(final int nbIterationsPerThread, final Supplier<BaseTest> supplierTest) {
Expand All @@ -40,6 +38,10 @@ public Execution(final int nbThreads, final int nbIterationsPerThread, final Sup
this.nbThreads = nbThreads;
this.nbIterationsPerThread = nbIterationsPerThread;
this.supplierTest = supplierTest;
if (nbThreads == 1 && nbIterationsPerThread < 100) {
displayInfos = true;
displayErrors = true;
}
}

@Getter
Expand All @@ -61,6 +63,8 @@ public void launch() throws Exception {
String textIteration;
if (nbIterationsPerThread == -1) {
textIteration = "infinite loop";
} else if (nbIterationsPerThread == 1) {
textIteration = nbIterationsPerThread + " iteration per thread";
} else {
textIteration = nbIterationsPerThread + " iterations per thread";
}
Expand All @@ -78,10 +82,9 @@ public void launch() throws Exception {

while (completed.get() < nbThreads) {}
if (!displayInfos) {
print(">");
println(">");
}

println();
val finalTime = time.get();
if (finalTime >= 5000) {
println("Execution ended and took: " + finalTime/1000 + " s");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import lombok.val;

import java.net.http.HttpClient;
import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;

import static com.casinthecloud.simpletest.util.Utils.print;
import static com.casinthecloud.simpletest.util.Utils.println;

/**
* A thread dedicated to the test execution (sequential).
Expand Down Expand Up @@ -44,11 +46,19 @@ public void run() {
val smallInterval = test.getSmallInterval();
val bigInterval = test.getBigInterval();

val ctx = new HashMap<String, Object>();

var stopError = false;
var nbError = 0;
for (var i = 1; (nbIterations == -1 || i <= nbIterations) && !stopError; i++) {
try {
test.run();
if (displayInfos) {
println("## iteration: " + i);
}
test.run(ctx);
if (displayInfos) {
println();
}
if (smallInterval != -1 && i % smallInterval == 0) {
print((char) (97 + id));
}
Expand Down
15 changes: 9 additions & 6 deletions src/main/java/com/casinthecloud/simpletest/test/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import static com.casinthecloud.simpletest.util.Utils.*;
import static com.casinthecloud.simpletest.util.Utils.println;
import static com.casinthecloud.simpletest.util.Utils.urlEncode;
import static org.apache.commons.lang3.StringUtils.substringBefore;
import static org.apache.commons.lang3.StringUtils.substringBetween;
import static org.junit.Assert.assertTrue;

/**
Expand Down Expand Up @@ -65,7 +68,7 @@ public abstract class BaseTest {
@Setter
private boolean displayInfos;

public abstract void run() throws Exception;
public abstract void run(final Map<String, Object> ctx) throws Exception;

protected void saveTimer() {
long t1 = System.currentTimeMillis();
Expand All @@ -85,8 +88,8 @@ protected Pair<String, String> getCookie(final String name) {
val listHeaders = _headers.get("set-cookie");
for (val header : listHeaders) {
if (header.startsWith(name)) {
val key = before(header, "=");
val value = between(header, "=", ";");
val key = substringBefore(header, "=");
val value = substringBetween(header, "=", ";");
return new ImmutablePair<String, String>(key, value);
}
}
Expand All @@ -110,7 +113,7 @@ protected void execute() throws Exception {

protected HttpRequest post(final String url) throws Exception {
if (displayInfos) {
println("POST : " + url);
println("POST: " + url);
}

val formBodyBuilder = new StringBuilder();
Expand Down Expand Up @@ -143,7 +146,7 @@ protected HttpRequest post(final String url) throws Exception {

protected HttpRequest get(final String url) throws Exception {
if (displayInfos) {
println("GET : " + url);
println("GET: " + url);
}

val builder = HttpRequest.newBuilder()
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/casinthecloud/simpletest/test/CasTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Setter;
import lombok.val;

import static com.casinthecloud.simpletest.util.Utils.between;
import static org.apache.commons.lang3.StringUtils.substringBetween;

/**
* A test for CAS.
Expand All @@ -22,12 +22,14 @@ public abstract class CasTest extends WebTest {

private String casPrefixUrl = "http://localhost:8080/cas";

private String casCookieName = TGC;

private String username = "jleleu";

private String password = "jleleu";

protected void executePostCasCredentials(final String casLoginUrl) throws Exception {
val webflow = between(_body, "name=\"execution\" value=\"", "\"/>");
val webflow = substringBetween(_body, "name=\"execution\" value=\"", "\"/>");

_data.put("username", getUsername());
_data.put("password", getPassword());
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/casinthecloud/simpletest/test/ChainingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.casinthecloud.simpletest.test;

import lombok.val;

import java.util.Map;

/**
* Run all tests.
*
* @author Jerome LELEU
* @since 1.0.0
*/
public class ChainingTest extends MultipleTest {

public ChainingTest(final BaseTest... tests) {
this.tests = tests;
}

@Override
public void run(final Map<String, Object> ctx) throws Exception {
for (val test : tests) {
test.run(ctx);
}
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/casinthecloud/simpletest/test/MultipleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.casinthecloud.simpletest.test;

import lombok.val;

import java.net.http.HttpClient;
import java.util.concurrent.atomic.AtomicLong;

/**
* Multiple tests
*
* @author Jerome LELEU
* @since 1.0.0
*/
public abstract class MultipleTest extends BaseTest {

protected BaseTest[] tests;

public void setClient(final HttpClient client) {
for (val test : tests) {
test.setClient(client);
}
}

public void setTime(final AtomicLong time) {
for (val test : tests) {
test.setTime(time);
}
}

public void setDisplayInfos(final boolean displayInfos) {
for (val test : tests) {
test.setDisplayInfos(displayInfos);
}
}
}
25 changes: 5 additions & 20 deletions src/main/java/com/casinthecloud/simpletest/test/RandomTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,26 @@

import lombok.val;

import java.net.http.HttpClient;
import java.util.concurrent.atomic.AtomicLong;
import java.util.Map;

import static com.casinthecloud.simpletest.util.Utils.random;

/**
* Randomly run test.
* Randomly run of the tests.
*
* @author Jerome LELEU
* @since 1.0.0
*/
public class RandomTest extends BaseTest {

private BaseTest[] tests;
public class RandomTest extends MultipleTest {

public RandomTest(final BaseTest... tests) {
this.tests = tests;
}

@Override
public void run() throws Exception {
public void run(final Map<String, Object> ctx) throws Exception {
val r = random(tests.length);

tests[r].run();
}

public void setClient(final HttpClient client) {
for (val test : tests) {
test.setClient(client);
}
}

public void setTime(final AtomicLong time) {
for (val test : tests) {
test.setTime(time);
}
tests[r].run(ctx);
}
}
Loading

0 comments on commit 0b31cec

Please sign in to comment.