Skip to content

Commit fca9647

Browse files
author
Mark Kelly
committed
few examples of api acceptance tests
1 parent ee57f5f commit fca9647

File tree

12 files changed

+327
-22
lines changed

12 files changed

+327
-22
lines changed

acceptance-tests/pom.xml

+35
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,41 @@
1111

1212
<artifactId>acceptance-tests</artifactId>
1313

14+
<build>
15+
<plugins>
16+
<plugin>
17+
<artifactId>maven-compiler-plugin</artifactId>
18+
<configuration>
19+
<source>1.6</source>
20+
<target>1.6</target>
21+
</configuration>
22+
</plugin>
23+
<!--Make this module run ONLY at the integration-test phase-
24+
NOTE:: In the public pom....the application is run in pre-integration test phase-->
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-surefire-plugin</artifactId>
28+
<configuration>
29+
<skip>true</skip>
30+
</configuration>
31+
<executions>
32+
<execution>
33+
<id>surefire-it</id>
34+
<phase>integration-test</phase>
35+
<configuration>
36+
<skip>false</skip>
37+
<forkMode>once</forkMode>
38+
<argLine>-enableassertions</argLine>
39+
</configuration>
40+
<goals>
41+
<goal>test</goal>
42+
</goals>
43+
</execution>
44+
</executions>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
1449
<dependencies>
1550
<dependency>
1651
<groupId>org.apache.httpcomponents</groupId>

acceptance-tests/src/main/java/com/mtt/api/test/client/base/response/ResponseValidator.java

+2
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ public interface ResponseValidator {
1919
void assertBodyContainsFieldError(String fieldName);
2020

2121
void assertBodyContainsMessageCode(String messageCode);
22+
23+
public void assertBodyContainsFieldError(String fieldName, String msg);
2224
}

acceptance-tests/src/main/java/com/mtt/api/test/client/base/response/impl/DefaultResponseValidator.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public void assertResponseHeader(String headerName, String expectedRegEx) {
6363
@Override
6464
public void assertBodyContainsErrorCode(String errorCode) {
6565
JsonNode errorCodeNode = responseBody.get("error_code");
66-
assertThat(errorCodeNode.getValueAsText(), equalTo(errorCode));
66+
assertThat(errorCodeNode.getTextValue(), equalTo(errorCode));
6767
}
6868

6969
@Override
@@ -74,14 +74,31 @@ public void assertBodyContainsFieldError(String fieldName) {
7474
ArrayNode errorsNode = (ArrayNode) node;
7575
for (JsonNode errorNode : errorsNode) {
7676
JsonNode fieldNameNode = errorNode.findPath("field");
77-
if (fieldNameNode.getValueAsText().equals(fieldName)) {
77+
if (fieldNameNode.getTextValue().equals(fieldName)) {
7878
foundError = true;
7979
break;
8080
}
8181
}
8282
assertThat(foundError, equalTo(true));
8383
}
8484

85+
@Override
86+
public void assertBodyContainsFieldError(String fieldName, String msg) {
87+
boolean foundError = false;
88+
JsonNode node = responseBody.get("errors");
89+
assertThat(node, instanceOf(ArrayNode.class));
90+
ArrayNode errorsNode = (ArrayNode) node;
91+
for (JsonNode errorNode : errorsNode) {
92+
JsonNode fieldNameNode = errorNode.findPath("field");
93+
if (fieldNameNode.getTextValue().equals(fieldName)) {
94+
foundError = true;
95+
assertThat(errorNode.findPath("message_code").getTextValue(), equalTo(msg));
96+
break;
97+
}
98+
}
99+
assertThat(foundError, equalTo(true));
100+
}
101+
85102
@Override
86103
public void assertBodyContainsMessageCode(String messageCode) {
87104
boolean foundCode = false;
@@ -90,7 +107,7 @@ public void assertBodyContainsMessageCode(String messageCode) {
90107
ArrayNode errorsNode = (ArrayNode) node;
91108
for (JsonNode errorNode : errorsNode) {
92109
JsonNode messageCodeNode = errorNode.findPath("message_code");
93-
if (messageCodeNode.getValueAsText().equals(messageCode)) {
110+
if (messageCodeNode.getTextValue().equals(messageCode)) {
94111
foundCode = true;
95112
break;
96113
}

acceptance-tests/src/main/java/com/mtt/api/test/client/task/response/TaskResponseValidator.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,6 @@
55

66
public interface TaskResponseValidator extends ResponseValidator {
77

8-
// long getId();
9-
//
10-
// long getUserId();
11-
//
12-
// boolean isChecked();
13-
//
14-
// String getTitle();
15-
//
16-
// String getDescription();
17-
188
void assertId(long id);
199

2010
void assertUserEmail(String email);
@@ -26,4 +16,6 @@ public interface TaskResponseValidator extends ResponseValidator {
2616
void assertIsChecked(boolean isChecked);
2717

2818
void assertCreatedDate(String dateTime);
19+
20+
void assertField(String field, String value);
2921
}

acceptance-tests/src/main/java/com/mtt/api/test/client/task/response/impl/TaskResponseValidatorImpl.java

+11
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,15 @@ public void assertCreatedDate(String dateTime) {
6464
assertThat(statusNode, notNullValue());
6565
assertThat(statusNode.getTextValue(), equalTo(dateTime));
6666
}
67+
68+
@Override
69+
public void assertField(String field, String value) {
70+
JsonNode statusNode = getResponseBody().get(field);
71+
assertThat(statusNode, notNullValue());
72+
73+
if (value.equals("false") || value.equals("true"))
74+
assertThat(statusNode.getBooleanValue(), equalTo(new Boolean(value)));
75+
else
76+
assertThat(statusNode.getTextValue(), equalTo(value));
77+
}
6778
}

acceptance-tests/src/test/java/com/mtt/api/test/steps/task/TaskSteps.java

+54-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.mtt.api.test.steps.task;
22

3+
import com.mtt.api.model.CreateTaskBean;
4+
import com.mtt.api.model.request.CreateTaskRequest;
35
import com.mtt.api.test.client.task.client.TaskTester;
46
import com.mtt.api.test.client.task.client.impl.HttpTaskApiClientTester;
57
import com.mtt.api.test.client.task.response.TaskResponseValidator;
@@ -21,12 +23,14 @@
2123

2224
public class TaskSteps {
2325

26+
//NOTE:: Alternatively can use restEasy :) ..see the APIClient for an example
2427
private TaskTester taskHttpClient;
2528
private TaskResponseValidator taskResponse;
2629

2730
private Wiser wiser;
2831

2932
private Long taskId;
33+
private CreateTaskRequest createTaskRequest;
3034

3135
@Autowired
3236
@Qualifier("taskFixture")
@@ -45,6 +49,10 @@ public synchronized void loadFixture() {
4549
wiser.start();
4650

4751
taskHttpClient = new HttpTaskApiClientTester(apiHost, new DefaultHttpClient(new ThreadSafeClientConnManager()));
52+
53+
taskId = null;
54+
createTaskRequest = null;
55+
taskResponse = null;
4856
}
4957

5058
@After("@task-fixture")
@@ -56,8 +64,6 @@ public synchronized void cleanUpFixture() {
5664
wiser.stop();
5765
wiser = null;
5866
}
59-
60-
taskId = null;
6167
}
6268

6369
@Given("^the user enters a task id of (.*?)$")
@@ -75,6 +81,52 @@ public void responseCodeIs(Integer responseCode) {
7581
taskResponse.assertResponseStatusCode(responseCode);
7682
}
7783

84+
@Then("^the (.*?) field should be \"(.*?)\"$")
85+
public void checkFieldValue(String field, String value) {
86+
taskResponse.assertField(field, value);
87+
}
88+
89+
@Given("^the user wants to create a new task$")
90+
public void initialiseTask() {
91+
createTaskRequest = new CreateTaskRequest();
92+
}
93+
94+
@Given("^the title field is set to \"(.*?)\"$")
95+
public void updateTitleTaskRequest(String value) {
96+
createTaskRequest.setTitle(value);
97+
taskHttpClient.withTitle(value);
98+
}
99+
100+
@Given("^the description field is set to \"(.*?)\"$")
101+
public void updateDescriptionTaskRequest(String value) {
102+
createTaskRequest.setDescription(value);
103+
taskHttpClient.withDescription(value);
104+
}
105+
106+
@Given("^the checked field is set to \"(.*?)\"$")
107+
public void updateCheckedTaskRequest(String checked) {
108+
boolean boolValue = Boolean.valueOf(checked);
109+
createTaskRequest.setChecked(boolValue);
110+
taskHttpClient.withCheckedValue(boolValue);
111+
}
112+
113+
@Given("^the userId field is set to \"(.*?)\"$")
114+
public void updateUserIdTaskRequest(String value) {
115+
createTaskRequest.setUserId(Long.valueOf(value));
116+
taskHttpClient.withUserId(Long.valueOf(value));
117+
}
118+
119+
@When("^the user tries to create the task$")
120+
public void createTask() throws Exception {
121+
taskResponse = taskHttpClient.createTask();
122+
}
123+
124+
@Then("^the response should contain an error \"(.*?)\" for the (.*?) field$")
125+
public void checkError(String errorCode, String field) {
126+
taskResponse.assertBodyContainsFieldError(field);
127+
taskResponse.assertBodyContainsFieldError(field, errorCode);
128+
}
129+
78130
}
79131

80132

acceptance-tests/src/test/resources/com/mtt/api/test/steps/task/task.feature

+27-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,30 @@ Feature: Task Functions for the API
44
Scenario: User specifies a Task Id to Get from the API
55
Given the user enters a task id of 10
66
When the user tries to get the task
7-
Then the response status code should be 404
7+
Then the response status code should be 404
8+
9+
Scenario: User specifies a valid task Id
10+
Given the user enters a task id of 1
11+
When the user tries to get the task
12+
Then the response status code should be 200
13+
And the title field should be "title 1"
14+
And the description field should be "description 1"
15+
And the checked field should be "false"
16+
And the user_email field should be "mkelly"
17+
18+
Scenario: User tries to create a task with validation errors
19+
Given the user wants to create a new task
20+
And the title field is set to "<html>"
21+
And the userId field is set to "1"
22+
When the user tries to create the task
23+
Then the response status code should be 400
24+
And the response should contain an error "task.title.html" for the title field
25+
And the response should contain an error "task.description.blank" for the description field
26+
27+
Scenario: User tries to create a task with no validation errors
28+
Given the user wants to create a new task
29+
And the title field is set to "title"
30+
And the description field is set to "description"
31+
And the userId field is set to "1"
32+
When the user tries to create the task
33+
Then the response status code should be 201

core/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@
9393
<groupId>${project.groupId}</groupId>
9494
<artifactId>test</artifactId>
9595
</dependency>
96+
<dependency>
97+
<groupId>${project.groupId}</groupId>
98+
<artifactId>model</artifactId>
99+
</dependency>
96100
<dependency>
97101
<groupId>joda-time</groupId>
98102
<artifactId>joda-time</artifactId>

core/src/main/java/com/mtt/service/request/CreateTaskRequest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
public class CreateTaskRequest {
1010

11-
@NotBlank(message = "task.create.title.blank")
12-
@Size(min = 1, max = 100, message = "task.create.title.length")
13-
@NotHtml(message = "task.create.title.html")
11+
@NotBlank(message = "task.title.blank")
12+
@Size(min = 1, max = 100, message = "task.title.length")
13+
@NotHtml(message = "task.title.html")
1414
private String title;
1515

16-
@NotBlank(message = "task.create.description.blank")
17-
@Size(min = 1, max = 100, message = "task.create.description.length")
18-
@NotHtml(message = "task.create.description.html")
16+
@NotBlank(message = "task.description.blank")
17+
@Size(min = 1, max = 100, message = "task.description.length")
18+
@NotHtml(message = "task.description.html")
1919
private String description;
2020

2121
private boolean checked;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.mtt.api.model.request;
2+
3+
import com.mtt.validation.NotHtml;
4+
import org.codehaus.jackson.annotate.JsonProperty;
5+
import org.hibernate.validator.constraints.NotBlank;
6+
7+
import javax.validation.constraints.Size;
8+
9+
public class CreateTaskRequest {
10+
11+
@NotBlank(message = "task.title.blank")
12+
@Size(min = 1, max = 100, message = "task.title.length")
13+
@NotHtml(message = "task.title.html")
14+
private String title;
15+
16+
@NotBlank(message = "task.description.blank")
17+
@Size(min = 1, max = 100, message = "task.create.description.length")
18+
@NotHtml(message = "task.description.html")
19+
private String description;
20+
21+
private boolean checked;
22+
23+
@JsonProperty("user_id")
24+
private Long userId;
25+
26+
public String getTitle() {
27+
return title;
28+
}
29+
30+
public void setTitle(String title) {
31+
this.title = title;
32+
}
33+
34+
public String getDescription() {
35+
return description;
36+
}
37+
38+
public void setDescription(String description) {
39+
this.description = description;
40+
}
41+
42+
public Long getUserId() {
43+
return userId;
44+
}
45+
46+
public void setUserId(Long userId) {
47+
this.userId = userId;
48+
}
49+
50+
public boolean isChecked() {
51+
return checked;
52+
}
53+
54+
public void setChecked(boolean checked) {
55+
this.checked = checked;
56+
}
57+
}

0 commit comments

Comments
 (0)