14
14
import io .gatling .javaapi .http .*;
15
15
16
16
public class CommonSimulation extends Simulation {
17
+
18
+ static final String BASE_URL = System .getProperty ("base.url" , "http://localhost:8080/accounts" );
19
+
20
+ //-----------------------------------------------------------------------------------------------------------------
21
+
17
22
static Iterator <Map <String , Object >> feederPost = Stream .generate ((Supplier <Map <String , Object >>) () ->
18
23
Map .of (
19
24
"email" , UUID .randomUUID () + "@mail.com" ,
@@ -28,24 +33,54 @@ public class CommonSimulation extends Simulation {
28
33
)
29
34
).iterator ();
30
35
36
+ static FeederBuilder <String > feederInvalidPost = csv ("feeders/post-account-invalid-body.csv" ).random ();
37
+
31
38
32
39
//-----------------------------------------------------------------------------------------------------------------
33
40
34
41
private static ChainBuilder postAccount (String sessionFieldNameForId ) {
35
- return exec ().feed (feederPost ).exec (http ("Create One " )
42
+ return exec ().feed (feederPost ).exec (http ("Create" )
36
43
.post ("/" )
37
44
.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 ))
39
53
.check (jsonPath ("$.id" ).saveAs (sessionFieldNameForId ))
40
54
);
41
55
}
42
56
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
+
43
72
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
+ );
45
77
}
46
78
47
79
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
+ );
49
84
}
50
85
51
86
private static ChainBuilder postTransfer () {
@@ -59,32 +94,35 @@ private static ChainBuilder postTransfer() {
59
94
"moneyAmount": #{moneyAmount}
60
95
}
61
96
""" ))
97
+ .check (status ().is (204 ))
62
98
);
63
99
}
64
100
65
101
//-----------------------------------------------------------------------------------------------------------------
66
102
67
- HttpProtocolBuilder httpProtocol = http .baseUrl ("http://localhost:8080/accounts" )
103
+ HttpProtocolBuilder httpProtocol = http .baseUrl (BASE_URL )
68
104
.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" );
72
106
73
107
ScenarioBuilder scenarioGetAll = scenario ("Get All - Scenario" ).exec (
74
108
getAllAccounts ()
75
109
);
76
110
77
111
ScenarioBuilder scenarioGetOne = scenario ("Get One - Scenario" ).exec (
78
- postAccount ("id" ),
112
+ postAccount ("id" ). exitHereIfFailed () ,
79
113
getOneAccountById ()
80
114
);
81
115
82
116
ScenarioBuilder scenarioTransfer = scenario ("Transfer - Scenario" ).exec (
83
- postAccount ("senderId" ),
84
- postAccount ("recipientId" ),
117
+ postAccount ("senderId" ). exitHereIfFailed () ,
118
+ postAccount ("recipientId" ). exitHereIfFailed () ,
85
119
postTransfer ()
86
120
);
87
121
122
+ ScenarioBuilder scenarioValidation = scenario ("Validation 4xx - Scenario" ).exec (
123
+ postInvalidAccount ()
124
+ );
125
+
88
126
//-----------------------------------------------------------------------------------------------------------------
89
127
90
128
{
@@ -108,6 +146,9 @@ private static ChainBuilder postTransfer() {
108
146
.eachLevelLasting (8 )
109
147
.separatedByRampsLasting (8 )
110
148
.startingFrom (8 )
149
+ ),
150
+ scenarioValidation .injectOpen (
151
+ constantUsersPerSec (10 ).during (90 )
111
152
)
112
153
).protocols (httpProtocol );
113
154
}
0 commit comments