-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMessagingBddStepDefinition.java
221 lines (192 loc) · 7.89 KB
/
MessagingBddStepDefinition.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package fr.redfroggy.bdd.messaging.glue;
import com.fasterxml.jackson.core.JsonProcessingException;
import fr.redfroggy.bdd.messaging.scope.ScenarioScope;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.http.MediaType;
import org.springframework.messaging.MessageChannel;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.List;
public class MessagingBddStepDefinition extends AbstractBddStepDefinition {
MessagingBddStepDefinition(MessageCollector collector, List<MessageChannel> channels) {
super(collector, channels);
}
/**
* Set the request body
* A json string structure is accepted
* The body will be parsed to be sure the json is valid
*/
@Given("^I set queue message body to (.*)$")
public void setBodyTo(String body) {
this.setBody(body);
}
@And("^I set queue message body path (.*) to (.*)$")
public void setBodyWithJsonPath(String jsonPath, String value) {
this.setBodyPathWithValue(jsonPath, value);
}
/**
* Set the request body by reading a file
* A json string structure is accepted
* The body will be parsed to be sure the json is valid
*/
@Given("^I set queue message body with file (.*)$")
public void setBodyWithFile(String filePath) throws IOException {
super.setBodyWithFile(filePath);
}
/**
* Add a new header
*/
@Given("^I set (.*) queue message header to (.*)$")
public void header(String headerName, String headerValue) {
this.setHeader(headerName, headerValue);
}
@When("^I PUSH to queue (.*)$")
public void get(String channelName) {
this.pushToQueue(channelName);
}
@When("^I POLL first message from queue (.*)$")
public void pollFromQueue(String channelName) {
this.readMessageFromQueue(channelName, MessageChannelAction.POLL);
}
@When("^I PEEK first message from queue (.*)$")
public void peekFromQueue(String channelName) {
this.readMessageFromQueue(channelName, MessageChannelAction.PEEK);
}
@Then("^queue should have (.*) messages left$")
public void pollFromQueue(int expectedSize) {
checkChannelHasSize(expectedSize);
}
/**
* Test that a given header exists
*/
@Then("^queue message header (.*) should exist$")
public void headerExists(String headerName) {
this.checkHeaderExists(headerName, false);
}
/**
* Test that a given header does not exists
*/
@Then("^queue message header (.*) should not exist$")
public void headerNotExists(String headerName) {
this.checkHeaderExists(headerName, true);
}
/**
* Test if a given header value is matching the expected value
*
*/
@Then("^queue message header (.*) should be (.*)$")
public void headerEqual(String headerName, String headerValue) {
this.checkHeaderHasValue(headerName, headerValue, false);
}
/**
* Test if a given header value is not matching the expected value
*/
@Then("^queue message header (.*) should not be (.*)$")
public void headerNotEqual(String headerName, String headerValue) {
this.checkHeaderHasValue(headerName, headerValue, true);
}
/**
* Test if the response body is a valid json. The string response is parsed as a
* JSON object ot check the integrity
*/
@Then("^queue message body should be valid json$")
public void bodyIsValid() throws IOException {
this.checkJsonBody();
}
/**
* Test if the response body contains a given value
*/
@Then("^queue message body should contain (.*)$")
public void bodyContains(String bodyValue) {
this.checkBodyContains(bodyValue);
}
/**
* Test the given json path query exists in the response body
*/
@Then("^queue message body path (.*) should exist$")
public void bodyPathExists(String jsonPath) throws JsonProcessingException {
this.checkJsonPathExists(jsonPath);
}
/**
* Test the given json path query doesn't exist in the response body
*/
@Then("^queue message body path (.*) should not exist$")
public void bodyPathDoesntExist(String jsonPath) throws JsonProcessingException {
this.checkJsonPathDoesntExist(jsonPath);
}
/**
* Test the given json path exists in the response body and match the given value
*/
@Then("^queue message body path (.*) should be (.*)$")
public void bodyPathEqual(String jsonPath, String value) throws JsonProcessingException {
this.checkJsonPath(jsonPath, value, false);
}
/**
* Test the given json path exists and does not match the given value
*/
@Then("^queue message body path (.*) should not be (.*)$")
public void bodyPathNotEqual(String jsonPath, String value) throws JsonProcessingException {
this.checkJsonPath(jsonPath, value, true);
}
/**
* Test if the json path exists in the response body and is array typed
*/
@Then("^queue message body is typed as array for path (.*)$")
public void bodyPathIsArray(String jsonPath) throws JsonProcessingException {
this.checkJsonPathIsArray(jsonPath, -1);
}
/**
* Test if the json path exists in the response body, is array typed and as the
* expected length
*/
@Then("^queue message body is typed as array using path (.*) with length (\\d+)$")
public void bodyPathIsArrayWithLength(String jsonPath, int length) throws JsonProcessingException {
this.checkJsonPathIsArray(jsonPath, length);
}
/**
* Store a given response header to the scenario scope The purpose is to reuse
* its value in another scenario The most common use case is the authentication
* process
*
* @see ScenarioScope
*/
@Then("^I store the value of queue message header (.*) as (.*) in scenario scope$")
public void storeResponseHeader(String headerName, String headerAlias) {
this.storeHeader(headerName, headerAlias);
}
/**
* Store a given json path value to the scenario scope The purpose is to reuse
* its value in another scenario The most common use case is the authentication
* process
*
* @see ScenarioScope
*/
@Then("^I store the value of queue message path (.*) as (.*) in scenario scope$")
public void storeResponseJsonPath(String jsonPath, String jsonPathAlias) throws JsonProcessingException {
this.storeJsonPath(jsonPath, jsonPathAlias);
}
/**
* Test a scenario scope variable value match the expected one
* @see ScenarioScope
*/
@Then("^queue value of scenario variable (.*) should be (.*)$")
public void scenarioVariableIsValid(String property, String value) {
this.checkScenarioVariable(property, value);
}
@Given("^I mock third party call (.*) (.*) with return code (.*), content type: (.*) and body: (.*)$")
public void mockThirdPartyApiCallWithJSON(String method, String resource, int status, String contentType, String willReturnJson) throws URISyntaxException {
mockThirdPartyApiCall(method, resource, status, MediaType.valueOf(contentType).toString(), willReturnJson);
}
@Given("^I mock third party call (.*) (.*) with return code (.*), content type: (.*) and file: (.*)$")
public void mockThirdPartyApiCallWithFileContent(String method, String resource, int status, String contentType, String filePath) throws IOException, URISyntaxException {
mockThirdPartyApiCall(method, resource, status, MediaType.valueOf(contentType).toString(), StreamUtils
.copyToString(getClass().getClassLoader()
.getResourceAsStream(filePath), StandardCharsets.UTF_8));
}
}