/
GitHubBranchAPITest.java
136 lines (114 loc) · 5.02 KB
/
GitHubBranchAPITest.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
package com.example.tests;
import com.example.tests.model.BranchEntity;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.*;
import static io.restassured.path.json.JsonPath.from;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class GitHubBranchAPITest {
@Test
public void listBranches() {
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("/branches")
.then()
.statusCode(200)
.body("$", hasItem(hasEntry("name", "main")));
}
@Test
public void getBranch() {
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")
.pathParam("branch", "main")
.when()
.get("/branches/{branch}")
.then()
.statusCode(200)
.extract()
.response();
// extract fields
String link = response.path("_links.html");
Boolean protectionEnabled = response.path("protection.enabled");
// assertions
assertThat(link, equalTo("https://github.com/olzhy/java-exercises/tree/main"));
assertThat(protectionEnabled, equalTo(false));
}
@Test
public void getBranchUsingJsonPath() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
String responseBody = given().accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.pathParam("branch", "main")
.when()
.get("/branches/{branch}")
.then()
.statusCode(200)
.extract()
.asString();
// extract fields
JsonPath jsonPath = from(responseBody);
String link = jsonPath.getString("_links.html");
Boolean protectionEnabled = jsonPath.getBoolean("protection.enabled");
// assertions
assertThat(link, equalTo("https://github.com/olzhy/java-exercises/tree/main"));
assertThat(protectionEnabled, equalTo(false));
}
@Test
public void getBranchUsingDeserialization() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
BranchEntity branchEntity = given().accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.pathParam("branch", "main")
.when()
.get("/branches/{branch}")
.then()
.statusCode(200)
.extract()
.as(BranchEntity.class);
// assertions
assertThat(branchEntity.getLinks().getHtml(), equalTo("https://github.com/olzhy/java-exercises/tree/main"));
assertThat(branchEntity.getProtection().getEnabled(), equalTo(false));
}
@Test
public void getBranchWithLog() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
given().log().all() // Log all request details
.accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.pathParam("branch", "main")
.when()
.get("/branches/{branch}")
.then()
.log().body() // Log only the response body
.statusCode(200)
.body("_links.html", equalTo("https://github.com/olzhy/java-exercises/tree/main"));
}
@Test
public void getBranchWithLogOnWhenValidationFails() {
baseURI = "https://api.github.com/repos/olzhy/java-exercises";
// Log request and response details only when validation fails
enableLoggingOfRequestAndResponseIfValidationFails();
given().accept(ContentType.JSON)
.header("Authorization", "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
.header("X-GitHub-Api-Version", "2022-11-28")
.pathParam("branch", "main")
.when()
.get("/branches/{branch}")
.then()
.statusCode(200)
.body("_links.html", equalTo("https://github.com/olzhy/java-exercises/tree/main"));
}
}