1414import io .gatling .javaapi .http .*;
1515
1616public 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 }
0 commit comments