-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2434 from f-delahaye/2168-add-option-to-write-gat…
…ling-tests-in-java 2168 add option to write gatling tests in java
- Loading branch information
Showing
17 changed files
with
364 additions
and
29 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
51 changes: 51 additions & 0 deletions
51
karate-gatling/src/main/java/com/intuit/karate/gatling/javaapi/KarateDsl.java
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.intuit.karate.gatling.javaapi; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
|
||
import io.gatling.javaapi.core.*; | ||
import io.gatling.javaapi.core.internal.Converters; | ||
import io.gatling.javaapi.http.*; | ||
import scala.collection.mutable.ArrayBuffer; | ||
import scala.collection.mutable.Buffer; | ||
import scala.collection.immutable.Seq; | ||
|
||
import com.intuit.karate.gatling.PreDef; | ||
import com.intuit.karate.gatling.javaapi.KarateUriPattern; | ||
import com.intuit.karate.gatling.javaapi.KarateUriPattern.KarateUriPatternBuilder; | ||
import com.intuit.karate.gatling.MethodPause; | ||
|
||
import static io.gatling.javaapi.core.CoreDsl.*; | ||
import static io.gatling.javaapi.http.HttpDsl.*; | ||
|
||
public class KarateDsl { | ||
|
||
public static KarateUriPatternBuilder uri(String uri) { | ||
return new KarateUriPatternBuilder(uri); | ||
} | ||
|
||
public static KarateProtocolBuilder karateProtocol(KarateUriPattern... patterns) { | ||
return new KarateProtocolBuilder(Arrays.stream(patterns).collect(Collectors.toMap(KarateUriPattern::getUri, pattern -> Converters.toScalaSeq(pattern.getPauses())))); | ||
} | ||
|
||
public static ActionBuilder karateFeature(String name, String... tags) { | ||
return () -> PreDef.karateFeature(name, Converters.toScalaSeq(tags)); | ||
} | ||
|
||
|
||
public static ActionBuilder karateSet(String key, final Function<Session, Object> supplier) { | ||
return () -> PreDef.karateSet(key, session -> supplier.apply(new Session(session))); | ||
} | ||
|
||
public static MethodPause method(String method, int durationInMillis) { | ||
return new MethodPause(method, durationInMillis); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
karate-gatling/src/main/java/com/intuit/karate/gatling/javaapi/KarateProtocolBuilder.java
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.intuit.karate.gatling.javaapi; | ||
|
||
|
||
import java.util.Collections; | ||
import java.util.function.BiFunction; | ||
|
||
import com.intuit.karate.Runner; | ||
import com.intuit.karate.core.ScenarioRuntime; | ||
import com.intuit.karate.gatling.KarateProtocol; | ||
import com.intuit.karate.gatling.MethodPause; | ||
import com.intuit.karate.http.HttpRequest; | ||
|
||
import io.gatling.core.protocol.Protocol; | ||
import io.gatling.javaapi.core.ProtocolBuilder; | ||
import io.gatling.javaapi.core.internal.Converters; | ||
import scala.collection.immutable.Seq; | ||
import scala.collection.immutable.Map; | ||
|
||
public class KarateProtocolBuilder implements ProtocolBuilder { | ||
|
||
public BiFunction<HttpRequest, ScenarioRuntime, String> nameResolver; | ||
public Runner.Builder runner = new Runner.Builder(); | ||
|
||
private final Map<String, Seq<MethodPause>> uriPatterns; | ||
|
||
// Takes a JAVA Map (easier for testing) containaing SCALA MethodPauses (easier to read, save an extra Java MethodPause class and another conversion) | ||
public KarateProtocolBuilder(java.util.Map<String, Seq<MethodPause>> uriPatterns) { | ||
this.uriPatterns = Converters.toScalaMap(uriPatterns); | ||
} | ||
|
||
@Override | ||
public KarateProtocol protocol() { | ||
KarateProtocol protocol = new KarateProtocol(uriPatterns); | ||
if (nameResolver != null) { | ||
protocol.nameResolver_$eq((req, sr) -> nameResolver.apply(req, sr)); | ||
} | ||
protocol.runner_$eq(runner); | ||
return protocol; | ||
} | ||
|
||
|
||
} |
58 changes: 58 additions & 0 deletions
58
karate-gatling/src/main/java/com/intuit/karate/gatling/javaapi/KarateUriPattern.java
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.intuit.karate.gatling.javaapi; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import com.intuit.karate.gatling.MethodPause; | ||
|
||
/** CLass to be used as a parameter of KarateDsl.karateProtocol. | ||
* | ||
* Instances are obtained from KarateDsl.uri(<uri>) chained with nil() or pauseFor() and won't typically be created directly. | ||
*/ | ||
public class KarateUriPattern { | ||
final String uri; | ||
final List<MethodPause> pauses; | ||
|
||
KarateUriPattern(String uri, List<MethodPause> pauses) { | ||
this.uri = uri; | ||
this.pauses = pauses; | ||
} | ||
|
||
String getUri() { | ||
return uri; | ||
} | ||
|
||
List<MethodPause> getPauses() { | ||
return pauses; | ||
} | ||
|
||
public static class KarateUriPatternBuilder { | ||
private final String uri; | ||
|
||
KarateUriPatternBuilder(String uri) { | ||
this.uri = uri; | ||
} | ||
|
||
/** | ||
* Creates a uriPattern with no pauses | ||
* @return | ||
*/ | ||
public KarateUriPattern nil() { | ||
return new KarateUriPattern(uri, Collections.emptyList()); | ||
} | ||
|
||
public KarateUriPattern pauseFor(String method, int durationInMillis) { | ||
return pauseFor(KarateDsl.method(method, durationInMillis)); | ||
} | ||
|
||
public KarateUriPattern pauseFor(String method1, int durationInMillis1, String method2, int durationInMillis2) { | ||
return pauseFor(KarateDsl.method(method1, durationInMillis1), KarateDsl.method(method2, durationInMillis2)); | ||
} | ||
|
||
public KarateUriPattern pauseFor(MethodPause... pauses) { | ||
return new KarateUriPattern(uri, Arrays.asList(pauses)); | ||
} | ||
} | ||
} | ||
|
5 changes: 0 additions & 5 deletions
5
karate-gatling/src/main/scala/com/intuit/karate/gatling/Dummy.java
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
...te-gatling/src/test/java/com/intuit/karate/gatling/javaapi/KarateProtocolBuilderTest.java
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.intuit.karate.gatling.javaapi; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Collections; | ||
import java.util.function.BiFunction; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.intuit.karate.Runner; | ||
import com.intuit.karate.core.ScenarioRuntime; | ||
import com.intuit.karate.http.HttpRequest; | ||
|
||
import io.gatling.javaapi.core.internal.Converters; | ||
|
||
import com.intuit.karate.gatling.KarateProtocol; | ||
import com.intuit.karate.gatling.MethodPause; | ||
|
||
|
||
class KarateProtocolBuilderTest { | ||
|
||
// Validates that the supplied nameResolver, runner and uriPatterns are taken into account. | ||
@Test | ||
void karateProtocol() throws Exception { | ||
KarateProtocolBuilder protocolBuilder = new KarateProtocolBuilder(Collections.singletonMap("foo", Converters.toScalaSeq(Collections.singletonList(new MethodPause("get", 110))))); | ||
|
||
protocolBuilder.nameResolver = (req, sr) -> "test name resolver"; | ||
|
||
protocolBuilder.runner.karateEnv("test"); | ||
|
||
KarateProtocol protocol = protocolBuilder.protocol(); | ||
|
||
assertEquals("test name resolver", protocol.nameResolver().apply(null, null)); | ||
|
||
Field envField = Runner.Builder.class.getDeclaredField("env"); | ||
envField.setAccessible(true); | ||
String env = (String)envField.get(protocol.runner()); | ||
|
||
assertEquals("test", env); | ||
|
||
assertEquals(110, protocol.pauseFor("foo", "get")); | ||
} | ||
|
||
@Test | ||
void uriPatterns() { | ||
|
||
KarateProtocol protocol = KarateDsl.karateProtocol( | ||
KarateDsl.uri("foo").nil(), | ||
KarateDsl.uri("bar").pauseFor("get", 110, "post", 220) | ||
).protocol(); | ||
|
||
assertEquals(110, protocol.pauseFor("bar", "get")); | ||
assertEquals(220, protocol.pauseFor("bar", "post")); | ||
assertEquals(0, protocol.pauseFor("bar", "put")); | ||
assertEquals(0, protocol.pauseFor("foo", "get")); | ||
assertEquals(0, protocol.pauseFor("foobar", "get")); | ||
|
||
assertTrue(protocol.pathMatches("/foo").isDefined()); | ||
assertTrue(protocol.pathMatches("/bar").isDefined()); | ||
assertFalse(protocol.pathMatches("/foobar").isDefined()); | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
karate-gatling/src/test/java/mock/CatsChainedSimulation.java
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package mock; | ||
|
||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.stream.Stream; | ||
import static com.intuit.karate.gatling.javaapi.KarateDsl.*; | ||
import static io.gatling.javaapi.core.CoreDsl.details; | ||
import static io.gatling.javaapi.core.CoreDsl.feed; | ||
import static io.gatling.javaapi.core.CoreDsl.rampUsers; | ||
import static io.gatling.javaapi.core.CoreDsl.scenario; | ||
|
||
import io.gatling.javaapi.core.*; | ||
|
||
public class CatsChainedSimulation extends Simulation { | ||
|
||
public CatsChainedSimulation() { | ||
MockUtils.startServer(0); | ||
|
||
ProtocolBuilder protocol = karateProtocol(uri("/cats/{id}").nil()); | ||
|
||
Iterator<Map<String, Object>> feeder = Stream.generate(() -> Collections.<String, Object>singletonMap("name", new Random().nextInt(20)+"-name")).iterator(); | ||
|
||
ScenarioBuilder createAndRead = scenario("createAndRead") | ||
.group("createAndRead").on( | ||
feed(() -> feeder) | ||
.exec(karateSet("name", session -> session.getString("name"))) | ||
.exec(karateFeature("classpath:mock/cats-chained.feature@name=create")) | ||
// for demo: injecting a new variable name expected by the 'read' feature | ||
.exec(karateSet("expectedName", session -> session.getString("name"))) | ||
.exec(karateFeature("classpath:mock/cats-chained.feature@name=read")).exec(session -> { | ||
System.out.println("*** id in gatling: " + session.getString("id")); | ||
System.out.println("*** session status in gatling: " + session.asScala().status()); | ||
return session; | ||
})); | ||
|
||
|
||
setUp( | ||
createAndRead.injectOpen(rampUsers(10).during(5)).protocols(protocol) | ||
).assertions(details("createAndRead").failedRequests().percent().is(0d)); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.