restassured_cli 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +139 -0
  5. data/Rakefile +3 -0
  6. data/bin/console +14 -0
  7. data/bin/restassured_cli +46 -0
  8. data/bin/setup +7 -0
  9. data/lib/restassured_cli.rb +77 -0
  10. data/lib/restassured_cli/createcontroller.rb +81 -0
  11. data/lib/restassured_cli/filenames.rb +69 -0
  12. data/lib/restassured_cli/source/maven/pom.xml +14 -0
  13. data/lib/restassured_cli/source/maven/testing/README.md +26 -0
  14. data/lib/restassured_cli/source/maven/testing/pom.xml +27 -0
  15. data/lib/restassured_cli/source/maven/testing/sampleservice-perf-tests/LoginTest.java +14 -0
  16. data/lib/restassured_cli/source/maven/testing/sampleservice-perf-tests/pom.xml +14 -0
  17. data/lib/restassured_cli/source/maven/testing/sampleservice-perf-tests/testng-perf-notests.xml +9 -0
  18. data/lib/restassured_cli/source/maven/testing/sampleservice-perf-tests/testng-perf.xml +9 -0
  19. data/lib/restassured_cli/source/maven/testing/sampleservice-production-tests/logback.xml +15 -0
  20. data/lib/restassured_cli/source/maven/testing/sampleservice-production-tests/pom.xml +21 -0
  21. data/lib/restassured_cli/source/maven/testing/sampleservice-production-tests/testng-production.xml +9 -0
  22. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/default/SampleTest.java +30 -0
  23. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/default/testng-ci.xml +13 -0
  24. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/default/testng-e2e.xml +13 -0
  25. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/default/testng-qa.xml +9 -0
  26. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/SampleComponentTest.java +312 -0
  27. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/SampleServiceIntegrationTest.java +129 -0
  28. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/testng-ci.xml +13 -0
  29. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/testng-e2e.xml +9 -0
  30. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/full/testng-qa.xml +9 -0
  31. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/logback.xml +15 -0
  32. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/pom.xml +22 -0
  33. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/resources/ci/sample.properties +0 -0
  34. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/resources/default/sample.properties +2 -0
  35. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/resources/e2e/sample.properties +1 -0
  36. data/lib/restassured_cli/source/maven/testing/sampleservice-service-tests/resources/qa/sample.properties +0 -0
  37. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/BaseConstant.java +48 -0
  38. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/InternalConfigManager.java +52 -0
  39. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/ServiceTestBase.java +103 -0
  40. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/sample/base/Constant.java +12 -0
  41. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/default/sample/base/SampleTestBase.java +15 -0
  42. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/Customer.java +83 -0
  43. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/Phone.java +31 -0
  44. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/SampleServiceHelper.java +8 -0
  45. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/sample/base/Constant.java +12 -0
  46. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/full/sample/base/SampleTestBase.java +63 -0
  47. data/lib/restassured_cli/source/maven/testing/sampleservice-test-library/pom.xml +14 -0
  48. data/lib/restassured_cli/source/maven/testing/sampleservice-test-parent-pom/pom.xml +225 -0
  49. data/lib/restassured_cli/source/maven/testing/sampleservice-test-runner/pom.xml +84 -0
  50. data/lib/restassured_cli/userinterface.rb +55 -0
  51. data/lib/restassured_cli/version.rb +3 -0
  52. data/spec/restassured_cli_spec.rb +84 -0
  53. data/spec/spec_helper.rb +14 -0
  54. metadata +141 -0
@@ -0,0 +1,14 @@
1
+ <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+ <modelVersion>4.0.0</modelVersion>
4
+ <groupId>GROUPID</groupId>
5
+ <artifactId>PROJECTNAME</artifactId>
6
+ <version>PROJECTVERSION</version>
7
+ <packaging>pom</packaging>
8
+ <name>PROJECTNAME</name>
9
+
10
+ <modules>
11
+ <module>testing</module>
12
+ </modules>
13
+
14
+ </project>
@@ -0,0 +1,26 @@
1
+ ## About
2
+ This testing project is generated by [restassured-cli](https://github.com/eing/restassured-cli).
3
+
4
+ ### To run pre-production tests
5
+ - Tests are run *without* recompiling. Artifacts are downloaded and testng files are extracted.
6
+ ```
7
+ $ cd testing
8
+ $ mvn clean install
9
+
10
+ $ cd testing/<project>-test-runner
11
+ $ mvn clean install -U -P ci
12
+ $ mvn clean install -U -P qa
13
+ $ mvn clean install -U -P e2e
14
+ $ mvn clean install -U -P perf
15
+ ```
16
+ - For non-standard profiles (other than ci, qa, e2e, perf and production), create your own profiles in test-parent-pom.
17
+
18
+ ## Eclipse
19
+
20
+ * To import testing project into Eclipse :
21
+
22
+ - cd testing; mvn eclipse:eclipse
23
+
24
+ - File > Import > Maven > Existing Maven Projects > Browse to testing root directory (all selections checked) and click Finish
25
+
26
+ * To run tests for environments other than qa, e.g. for CI, add **-Dtargetenv=ci** in Run Configuration’s VM arguments/options.
@@ -0,0 +1,27 @@
1
+ <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+
4
+ <!-- TODO: add your project parent pom here -->
5
+ <!--
6
+ <parent>
7
+ <groupId>com.intuit.sbg</groupId>
8
+ <artifactId>SBG-Parent-pom</artifactId>
9
+ <version>1.0.0</version>
10
+ </parent>
11
+ -->
12
+
13
+ <modelVersion>4.0.0</modelVersion>
14
+ <groupId>GROUPID.tests</groupId>
15
+ <artifactId>SERVICENAME-tests</artifactId>
16
+ <version>PROJECTVERSION</version>
17
+ <name>SERVICENAME-tests</name>
18
+ <packaging>pom</packaging>
19
+
20
+ <modules>
21
+ <module>SERVICENAME-test-parent-pom</module>
22
+ <module>SERVICENAME-test-library</module>
23
+ <module>SERVICENAME-service-tests</module>
24
+ <module>SERVICENAME-perf-tests</module>
25
+ <module>SERVICENAME-production-tests</module>
26
+ </modules>
27
+ </project>
@@ -0,0 +1,14 @@
1
+ package GROUPID.tests.perf;
2
+
3
+ import org.testng.annotations.Test;
4
+
5
+ /**
6
+ * Placeholder for Perf tests.
7
+ */
8
+ public class LoginTest {
9
+
10
+ @Test
11
+ public void sampleTest() {
12
+
13
+ }
14
+ }
@@ -0,0 +1,14 @@
1
+ <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+ <parent>
4
+ <groupId>GROUPID.tests</groupId>
5
+ <artifactId>SERVICENAME-test-parent-pom</artifactId>
6
+ <version>PROJECTVERSION</version>
7
+ <relativePath>../SERVICENAME-test-parent-pom</relativePath>
8
+ </parent>
9
+
10
+ <modelVersion>4.0.0</modelVersion>
11
+ <artifactId>SERVICENAME-perf-tests</artifactId>
12
+ <name>SERVICENAME-perf-tests</name>
13
+
14
+ </project>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-perf-tests">
4
+ <test name="SERVICENAME performance tests">
5
+ <packages>
6
+ <package name="GROUPID.tests.perf.*"/>
7
+ </packages>
8
+ </test>
9
+ </suite>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-perf-tests">
4
+ <test name="SERVICENAME performance tests">
5
+ <classes>
6
+ <class name="GROUPID.tests.perf.LoginTest"/>
7
+ </classes>
8
+ </test>
9
+ </suite>
@@ -0,0 +1,15 @@
1
+ <configuration>
2
+ <statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener"/>
3
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4
+ <!-- encoders are assigned the type
5
+ ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
6
+ <encoder>
7
+ <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
8
+ </encoder>
9
+ </appender>
10
+
11
+ <root level="DEBUG">
12
+ <appender-ref ref="STDOUT"/>
13
+ </root>
14
+
15
+ </configuration>
@@ -0,0 +1,21 @@
1
+ <project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
2
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3
+ <parent>
4
+ <groupId>GROUPID.tests</groupId>
5
+ <artifactId>SERVICENAME-test-parent-pom</artifactId>
6
+ <version>PROJECTVERSION</version>
7
+ <relativePath>../SERVICENAME-test-parent-pom</relativePath>
8
+ </parent>
9
+
10
+ <modelVersion>4.0.0</modelVersion>
11
+ <artifactId>SERVICENAME-production-tests</artifactId>
12
+ <name>SERVICENAME-production-tests</name>
13
+
14
+ <dependencies>
15
+ <dependency>
16
+ <groupId>GROUPID.tests</groupId>
17
+ <artifactId>SERVICENAME-test-library</artifactId>
18
+ <version>${project.version}</version>
19
+ </dependency>
20
+ </dependencies>
21
+ </project>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-production-tests">
4
+ <test name="SERVICENAME production tests">
5
+ <packages>
6
+ <package name="GROUPID.tests.production.*"/>
7
+ </packages>
8
+ </test>
9
+ </suite>
@@ -0,0 +1,30 @@
1
+ package GROUPID.tests.service;
2
+
3
+ import GROUPID.tests.library.sample.base.SampleTestBase;
4
+ import GROUPID.tests.library.sample.base.Constant;
5
+ import com.jayway.restassured.path.json.JsonPath;
6
+ import org.testng.Assert;
7
+ import org.testng.annotations.BeforeClass;
8
+ import org.testng.annotations.Test;
9
+
10
+ import static com.jayway.restassured.RestAssured.given;
11
+ import static org.hamcrest.Matchers.equalTo;
12
+
13
+ /**
14
+ * Default test generated to demonstrate coding best practices with
15
+ * 1. SampleTestBase (business domain base class) and
16
+ * 2. ServiceTestBase (business agnostic base class).
17
+ */
18
+ public class SampleTest extends SampleTestBase {
19
+
20
+ public SampleTest() {
21
+ }
22
+
23
+ /**
24
+ */
25
+ @Test
26
+ public void testDummy() {
27
+ logger.debug("use Logger and not System.out");
28
+ }
29
+
30
+ }
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-ci-tests">
4
+ <test name="SERVICENAME service BATS">
5
+ <classes>
6
+ <class name="GROUPID.tests.service.SampleTest">
7
+ <methods>
8
+ <include name="testDummy"/>
9
+ </methods>
10
+ </class>
11
+ </classes>
12
+ </test>
13
+ </suite>
@@ -0,0 +1,13 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-e2e-tests">
4
+ <test name="SERVICENAME service integration tests">
5
+ <classes>
6
+ <class name="GROUPID.tests.service.SampleTest">
7
+ <methods>
8
+ <include name="testDummy"/>
9
+ </methods>
10
+ </class>
11
+ </classes>
12
+ </test>
13
+ </suite>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
3
+ <suite name="SERVICENAME-qa-tests">
4
+ <test name="SERVICENAME service tests">
5
+ <packages>
6
+ <package name="GROUPID.tests.service.*"/>
7
+ </packages>
8
+ </test>
9
+ </suite>
@@ -0,0 +1,312 @@
1
+ package GROUPID.tests.service;
2
+
3
+ import GROUPID.tests.library.sample.base.SampleTestBase;
4
+ import GROUPID.tests.library.schema.Customer;
5
+ import GROUPID.tests.library.schema.Phone;
6
+
7
+ import com.fasterxml.jackson.databind.node.JsonNodeFactory;
8
+ import com.fasterxml.jackson.databind.node.ObjectNode;
9
+ import com.jayway.restassured.filter.session.SessionFilter;
10
+ import com.jayway.restassured.http.ContentType;
11
+ import com.jayway.restassured.response.Response;
12
+ import org.hamcrest.Matchers;
13
+ import org.testng.Assert;
14
+ import org.testng.annotations.BeforeClass;
15
+ import org.testng.annotations.Test;
16
+
17
+ import javax.xml.soap.MessageFactory;
18
+ import javax.xml.soap.Name;
19
+ import javax.xml.soap.SOAPBody;
20
+ import javax.xml.soap.SOAPBodyElement;
21
+ import javax.xml.soap.SOAPElement;
22
+ import javax.xml.soap.SOAPEnvelope;
23
+ import javax.xml.soap.SOAPMessage;
24
+ import javax.xml.soap.SOAPPart;
25
+ import java.io.ByteArrayOutputStream;
26
+ import java.util.ArrayList;
27
+ import java.util.HashMap;
28
+ import java.util.List;
29
+
30
+ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
31
+ import static com.github.tomakehurst.wiremock.client.WireMock.any;
32
+ import static com.github.tomakehurst.wiremock.client.WireMock.get;
33
+ import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
34
+ import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
35
+ import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
36
+ import static com.jayway.restassured.RestAssured.given;
37
+ import static org.hamcrest.CoreMatchers.hasItems;
38
+ import static org.hamcrest.CoreMatchers.is;
39
+ import static org.hamcrest.MatcherAssert.assertThat;
40
+ import static org.hamcrest.Matchers.equalTo;
41
+ import static org.hamcrest.Matchers.notNullValue;
42
+
43
+ /**
44
+ * Sample tests to show how to use RestAssured, and hamcrest assertions.
45
+ * WireMock is used to stub responses to http://localhost:9999.
46
+ * @author eing
47
+ */
48
+ public class SampleComponentTest extends SampleTestBase {
49
+
50
+ public SampleComponentTest() {
51
+ }
52
+
53
+ /**
54
+ * Add stubs to WireMock server for this set of tests.
55
+ */
56
+ @BeforeClass
57
+ public void setup() {
58
+ /**
59
+ * Create a JSON response using JsonNodeFactory instead of hardcoding in string which is error prone.
60
+ * {
61
+ "customer": {
62
+ "firstname": "Jane", "lastname": "Smith",
63
+ "phone": [
64
+ { "number": "650-123-4566", "type": "work" },
65
+ { "number": "650-123-4567", "type": "work" },
66
+ { "number": "650-123-4568", "type": "fax" } ],
67
+ "city": "Mountain View", "state": "CA", "zip": 94040 }
68
+ }
69
+ */
70
+ ObjectNode phone1 = new ObjectNode(JsonNodeFactory.instance);
71
+ phone1.put("number", "650-123-4566");
72
+ phone1.put("type", "work");
73
+ ObjectNode phone2 = new ObjectNode(JsonNodeFactory.instance);
74
+ phone2.put("number", "650-123-4567");
75
+ phone2.put("type", "work");
76
+ ObjectNode phone3 = new ObjectNode(JsonNodeFactory.instance);
77
+ phone3.put("number", "650-123-4568");
78
+ phone3.put("type", "fax");
79
+ ArrayList<ObjectNode> phones = new ArrayList<ObjectNode>();
80
+ phones.add(phone1);
81
+ phones.add(phone2);
82
+ phones.add(phone3);
83
+ ObjectNode customerJson = new ObjectNode(JsonNodeFactory.instance);
84
+ customerJson.
85
+ putObject("customer").
86
+ put("firstname", "Jane").
87
+ put("lastname", "Smith").
88
+ putPOJO("phone", phones).
89
+ put("city", "Mountain View").
90
+ put("state", "CA").
91
+ put("zip", 94040);
92
+ // Stub response for SimpleGet
93
+ wireMockServer.
94
+ givenThat(
95
+ (any(urlMatching("/customers/[0-9]+")).
96
+ willReturn(aResponse().
97
+ withHeader("Content-Type", ContentType.JSON.toString()).
98
+ withStatus(200).
99
+ withBody(customerJson.toString()))));
100
+ // Stub response for Auth
101
+ wireMockServer.
102
+ givenThat(
103
+ (get(urlEqualTo("/auth")).
104
+ willReturn(aResponse().
105
+ withHeader("Content-Type", ContentType.ANY.toString()).
106
+ withHeader("JSESSIONID", "AS34979870H").
107
+ withStatus(200))));
108
+ // Stub response for all other requests (setting to lowest priority)
109
+ wireMockServer.
110
+ givenThat(
111
+ (any(urlPathMatching(".*")).atPriority(10).
112
+ willReturn(aResponse().
113
+ withHeader("Content-Type", ContentType.ANY.toString()).
114
+ withStatus(200))));
115
+
116
+ }
117
+
118
+ /**
119
+ * Example of simple get request.
120
+ */
121
+ @Test
122
+ public void testSimpleGet() {
123
+ //@formatter:off
124
+ given().
125
+ spec(requestSpec).
126
+ contentType(ContentType.JSON).
127
+ when().
128
+ get("/customers/1").
129
+ then().assertThat().
130
+ statusCode(200).
131
+ body("customer.firstname", equalTo("Jane")).
132
+ body("customer.lastname", equalTo("Smith"));
133
+ //@formatter:on
134
+ }
135
+
136
+ /**
137
+ * Example of session filter in request.
138
+ */
139
+ @Test
140
+ public void testSessionFilter() {
141
+ SessionFilter filter = new SessionFilter();
142
+ //@formatter:on
143
+ given().
144
+ spec(requestSpec).
145
+ auth().basic("janesmith", "xx93k2m3b1").
146
+ filter(filter).
147
+ when().
148
+ get("/auth").
149
+ then().assertThat().
150
+ header("JSESSIONID", notNullValue());
151
+
152
+ // Reuse session filter that is now populated with session id
153
+ given().
154
+ spec(requestSpec).
155
+ filter(filter).
156
+ when().
157
+ get("/customers/2");
158
+ //@formatter:on
159
+ }
160
+
161
+ /**
162
+ * Example of object serialization in request.
163
+ */
164
+ @Test
165
+ public void testObjectSerialization() {
166
+ Customer customer = new Customer();
167
+ customer.setFirstname("Jane");
168
+ customer.setLastname("Smith");
169
+ //@formatter:off
170
+ Response response =
171
+ given().
172
+ spec(requestSpec).
173
+ body(customer).
174
+ when().
175
+ post("/customers/");
176
+ //@formatter:on
177
+ }
178
+
179
+ /**
180
+ * Example of object deserialization in response.
181
+ */
182
+ @Test
183
+ public void testObjectDeserialization() {
184
+ //@formatter:off
185
+ Customer customer =
186
+ given().
187
+ spec(requestSpec).
188
+ when().
189
+ get("/customers/3").
190
+ then().assertThat().
191
+ extract().jsonPath().getObject("customer", Customer.class);
192
+ //@formatter:on
193
+ logger.debug(customer.toString());
194
+ }
195
+
196
+ /**
197
+ * Example of asserting an array item.
198
+ */
199
+ @Test
200
+ public void testComplexAssertionsGetArrayItem() {
201
+ //@formatter:off
202
+ Phone phone =
203
+ given().
204
+ spec(requestSpec).
205
+ when().
206
+ get("/customers/4").
207
+ then().
208
+ body("customer.phone.type", hasItems("work", "fax")).
209
+ extract().jsonPath().getObject("customer.phone[0]", Phone.class);
210
+ //@formatter:on
211
+ assertThat(phone.getType(), is("work"));
212
+ }
213
+
214
+ /**
215
+ * Example of asserting arrays.
216
+ */
217
+ @Test
218
+ public void testComplexAssertionsGetList() {
219
+ //@formatter:off
220
+ List<HashMap> phoneList =
221
+ given().
222
+ spec(requestSpec).
223
+ when().
224
+ get("/customers/4").
225
+ then().
226
+ body("customer.phone.type", hasItems("work", "fax")).
227
+ extract().jsonPath().getList("customer.phone", HashMap.class);
228
+ //@formatter:on
229
+ assertThat(phoneList.size(), Matchers.is(3));
230
+ for (HashMap phone : phoneList) {
231
+ logger.debug(phone.toString());
232
+ String phoneNumber = phone.get("number").toString();
233
+ switch (phoneNumber) {
234
+ case "650-123-4566":
235
+ assertThat(phone.get("type").toString(), is("work"));
236
+ break;
237
+ case "650-123-4567":
238
+ assertThat(phone.get("type").toString(), is("work"));
239
+ break;
240
+ case "650-123-4568":
241
+ assertThat(phone.get("type").toString(), is("fax"));
242
+ break;
243
+ default:
244
+ Assert.fail(phoneNumber + "Did not match any of the expected phone numbers");
245
+ break;
246
+ }
247
+ }
248
+ }
249
+
250
+ /**
251
+ * Example of asserting using groovy closures.
252
+ */
253
+ @Test
254
+ public void testComplexAssertionsUsingGroovyClosures() {
255
+ //@formatter:off
256
+ List<HashMap> phoneList =
257
+ given().
258
+ spec(requestSpec).
259
+ when().
260
+ get("/customers/5").
261
+ then().
262
+ extract().jsonPath().get("customer.phone.findAll { phone -> phone.type == \"work\" }");
263
+ //@formatter:on
264
+ assertThat(phoneList.size(), Matchers.is(2));
265
+ }
266
+
267
+ /**
268
+ * Example of soap messaging in RestAssured.
269
+ * @throws Exception
270
+ */
271
+ @Test
272
+ public void testSOAPMessage() throws Exception {
273
+ /*
274
+ Construct SOAP Request Message instead of hardcoding as a string which is error prone:
275
+ <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
276
+ <SOAP-ENV:Header/>
277
+ <SOAP-ENV:Body>
278
+ <GetVendor>
279
+ <id>1290</id>
280
+ </GetVendor>
281
+ </SOAP-ENV:Body>
282
+ </SOAP-ENV:Envelope>
283
+ */
284
+ MessageFactory messageFactory = MessageFactory.newInstance();
285
+ SOAPMessage soapMessage = messageFactory.createMessage();
286
+ SOAPPart soapPart = soapMessage.getSOAPPart();
287
+ SOAPEnvelope envelope = soapPart.getEnvelope();
288
+ SOAPBody soapBody = envelope.getBody();
289
+
290
+ Name bodyName = envelope.createName("GetVendor");
291
+ SOAPBodyElement vendorElement = soapBody.addBodyElement(bodyName);
292
+ SOAPElement firstnameElement = vendorElement.addChildElement(envelope.createName("id"));
293
+ firstnameElement.addTextNode("1290");
294
+ soapMessage.saveChanges();
295
+
296
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
297
+ soapMessage.writeTo(out);
298
+ String soapMessageInText = new String(out.toByteArray());
299
+ //@formatter:off
300
+ given().
301
+ spec(requestSpec).
302
+ header("SOAPAction", "http://localhost/getVendor").
303
+ contentType("application/soap+xml; charset=UTF-8;").
304
+ body(soapMessageInText).
305
+ when().
306
+ post("/v1/vendors").
307
+ then().assertThat().
308
+ statusCode(200);
309
+ //@formatter:off
310
+ }
311
+
312
+ }