/
GitHubCommitAPITest.java
124 lines (104 loc) · 4.45 KB
/
GitHubCommitAPITest.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
package com.example.tests;
import com.example.tests.model.CommitEntity;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import java.util.List;
import static io.restassured.RestAssured.baseURI;
import static io.restassured.RestAssured.given;
import static io.restassured.path.json.JsonPath.from;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class GitHubCommitAPITest {
@Test
public void filterCommits() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
given().accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.queryParam("page", 1)
.queryParam("per_page", 10)
.when()
.get("/commits")
.then()
.statusCode(200)
.body("findAll { it.commit.committer.email.equals('olzhy@qq.com') }.commit.message", hasItem("rest assured demo"));
}
@Test
public void filterCommitsUsingJsonPath() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
Response response = given().accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.queryParam("page", 1)
.queryParam("per_page", 10)
.get("/commits")
.then()
.extract()
.response();
// extract response
int statusCode = response.statusCode();
String responseBody = response.asString();
List<String> commitMessages = from(responseBody)
.getList("findAll { it.commit.committer.email.equals('olzhy@qq.com') }.commit.message");
// assertions
assertThat(statusCode, equalTo(200));
assertThat(commitMessages, hasItem("rest assured demo"));
}
@Test
public void latestCommit() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
given().accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.queryParam("page", 1)
.queryParam("per_page", 10)
.when()
.get("/commits")
.then()
.statusCode(200)
.body("max { it.commit.committer.date }.commit.message", equalTo("rest assured demo"));
}
@Test
public void latestCommitUsingJsonPath() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
Response response = given().accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.queryParam("page", 1)
.queryParam("per_page", 10)
.when()
.get("/commits")
.then()
.extract()
.response();
// extract response
int statusCode = response.statusCode();
String responseBody = response.asString();
String commitMessage = from(responseBody)
.getString("max { it.commit.committer.date }.commit.message");
// assertions
assertThat(statusCode, equalTo(200));
assertThat(commitMessage, equalTo("rest assured demo"));
}
@Test
public void deserializeCommits() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
// deserialization with generics
List<CommitEntity> commits = given().accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.queryParam("page", 1)
.queryParam("per_page", 10)
.when()
.get("/commits")
.then()
.statusCode(200)
.extract()
.as(new TypeRef<>() {});
// assertions
assertThat(commits, hasSize(10));
assertThat(commits.get(0).getCommit().getMessage(), equalTo("rest assured demo"));
}
}