Skip to content

Commit 7af9f64

Browse files
committed
Gatling: Add negative validation scenario
1 parent c73330c commit 7af9f64

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

common-api/src/main/java/bitxon/api/model/Account.java

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package bitxon.api.model;
22

3+
import javax.validation.constraints.Email;
34
import javax.validation.constraints.NotEmpty;
5+
import javax.validation.constraints.NotNull;
46
import javax.validation.constraints.Pattern;
57
import javax.validation.constraints.PositiveOrZero;
68

@@ -16,7 +18,9 @@
1618
public class Account {
1719
Long id;
1820
@NotEmpty
21+
@Email
1922
String email;
23+
@NotNull
2024
@Pattern(regexp = "USD|EUR|GBP")
2125
String currency;
2226
@PositiveOrZero

dropwizard-app/src/main/resources/config.yml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ server:
55
adminConnectors:
66
- type: http
77
port: 8081
8+
requestLog:
9+
appenders: [] # Disable default logging (response status)
810

911
database:
1012
driverClass: org.postgresql.Driver

loadtest/src/gatling/java/gatling/simulation/CommonSimulation.java

+52-11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
import io.gatling.javaapi.http.*;
1515

1616
public class CommonSimulation extends Simulation {
17+
18+
static final String BASE_URL = System.getProperty("base.url", "http://localhost:8080/accounts");
19+
20+
//-----------------------------------------------------------------------------------------------------------------
21+
1722
static Iterator<Map<String, Object>> feederPost = Stream.generate((Supplier<Map<String, Object>>) () ->
1823
Map.of(
1924
"email", UUID.randomUUID() + "@mail.com",
@@ -28,24 +33,54 @@ public class CommonSimulation extends Simulation {
2833
)
2934
).iterator();
3035

36+
static FeederBuilder<String> feederInvalidPost = csv("feeders/post-account-invalid-body.csv").random();
37+
3138

3239
//-----------------------------------------------------------------------------------------------------------------
3340

3441
private static ChainBuilder postAccount(String sessionFieldNameForId) {
35-
return exec().feed(feederPost).exec(http("Create One")
42+
return exec().feed(feederPost).exec(http("Create")
3643
.post("/")
3744
.header("Content-Type", "application/json")
38-
.body(StringBody("{\"email\": \"#{email}\",\"currency\": \"#{currency}\",\"moneyAmount\": #{moneyAmount}}"))
45+
.body(StringBody("""
46+
{
47+
"email": "#{email}",
48+
"currency": "#{currency}",
49+
"moneyAmount": #{moneyAmount}
50+
}
51+
"""))
52+
.check(status().is(200))
3953
.check(jsonPath("$.id").saveAs(sessionFieldNameForId))
4054
);
4155
}
4256

57+
private static ChainBuilder postInvalidAccount() {
58+
return exec().feed(feederInvalidPost).exec(http("Create (400,422)")
59+
.post("/")
60+
.header("Content-Type", "application/json")
61+
.body(StringBody("""
62+
{
63+
"email": "#{email}",
64+
"currency": "#{currency}",
65+
"moneyAmount": #{moneyAmount}
66+
}
67+
"""))
68+
.check(status().in(400, 422))
69+
);
70+
}
71+
4372
private static ChainBuilder getOneAccountById() {
44-
return exec(http("Get One by Id").get("/#{id}"));
73+
return exec(http("Get One")
74+
.get("/#{id}")
75+
.check(status().is(200))
76+
);
4577
}
4678

4779
private static ChainBuilder getAllAccounts() {
48-
return exec(http("Get All").get("/"));
80+
return exec(http("Get All")
81+
.get("/")
82+
.check(status().is(200))
83+
);
4984
}
5085

5186
private static ChainBuilder postTransfer() {
@@ -59,32 +94,35 @@ private static ChainBuilder postTransfer() {
5994
"moneyAmount": #{moneyAmount}
6095
}
6196
"""))
97+
.check(status().is(204))
6298
);
6399
}
64100

65101
//-----------------------------------------------------------------------------------------------------------------
66102

67-
HttpProtocolBuilder httpProtocol = http.baseUrl("http://localhost:8080/accounts")
103+
HttpProtocolBuilder httpProtocol = http.baseUrl(BASE_URL)
68104
.acceptHeader("application/json")
69-
.acceptLanguageHeader("en-US,en;q=0.5")
70-
.acceptEncodingHeader("gzip, deflate")
71-
.userAgentHeader("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0");
105+
.acceptLanguageHeader("en-US,en;q=0.5");
72106

73107
ScenarioBuilder scenarioGetAll = scenario("Get All - Scenario").exec(
74108
getAllAccounts()
75109
);
76110

77111
ScenarioBuilder scenarioGetOne = scenario("Get One - Scenario").exec(
78-
postAccount("id"),
112+
postAccount("id").exitHereIfFailed(),
79113
getOneAccountById()
80114
);
81115

82116
ScenarioBuilder scenarioTransfer = scenario("Transfer - Scenario").exec(
83-
postAccount("senderId"),
84-
postAccount("recipientId"),
117+
postAccount("senderId").exitHereIfFailed(),
118+
postAccount("recipientId").exitHereIfFailed(),
85119
postTransfer()
86120
);
87121

122+
ScenarioBuilder scenarioValidation = scenario("Validation 4xx - Scenario").exec(
123+
postInvalidAccount()
124+
);
125+
88126
//-----------------------------------------------------------------------------------------------------------------
89127

90128
{
@@ -108,6 +146,9 @@ private static ChainBuilder postTransfer() {
108146
.eachLevelLasting(8)
109147
.separatedByRampsLasting(8)
110148
.startingFrom(8)
149+
),
150+
scenarioValidation.injectOpen(
151+
constantUsersPerSec(10).during(90)
111152
)
112153
).protocols(httpProtocol);
113154
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
email,currency,moneyAmount
2+
invalidEmail,USD,340
3+
[email protected],INVALID,340
4+

0 commit comments

Comments
 (0)