-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryControllerTestIT.kt
237 lines (196 loc) · 9.08 KB
/
RepositoryControllerTestIT.kt
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package com.tui.githubrepos.controller
import com.tui.githubrepos.dto.Repository
import com.tui.githubrepos.exception.ErrorResponse
import com.tui.githubrepos.util.Utils
import okhttp3.mockwebserver.Dispatcher
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.test.context.ActiveProfiles
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class RepositoryControllerTestIT {
@Autowired
private lateinit var testRestTemplate: TestRestTemplate
private lateinit var mockGitHubServer: MockWebServer
@BeforeAll
fun setup() {
mockGitHubServer = MockWebServer()
mockGitHubServer.start(54234)
}
@AfterAll
fun tearDown() {
mockGitHubServer.shutdown()
}
@Test
fun `Should get list of repositories when user has any`() {
val username = "Orlando-pt"
val jsonRepositories = Utils.getJsonFileContent("repositories.json")
val jsonBranches = Utils.getJsonFileContent("repository-ads-branches.json")
mockGitHubServer.dispatcher = object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
return when (request.path) {
"/users/$username/repos" -> {
MockResponse()
.setResponseCode(200)
.setHeader("Content-Type", "application/json")
.setBody(jsonRepositories)
}
"/repos/$username/ads/branches" -> {
MockResponse()
.setResponseCode(200)
.setHeader("Content-Type", "application/json")
.setBody(jsonBranches)
}
"/repos/$username/aoc/branches" -> {
MockResponse()
.setResponseCode(200)
.setHeader("Content-Type", "application/json")
.setBody("[]")
}
else -> MockResponse().setResponseCode(404)
}
}
}
val result = testRestTemplate.getForObject(
"/api/repository/$username",
Array<Repository>::class.java
)!!
Assertions.assertThat(result.size).isEqualTo(2)
Assertions.assertThat(result[0].name).isEqualTo("ads")
Assertions.assertThat(result[0].owner.login).isEqualTo(username)
Assertions.assertThat(result[0].branches)
.hasSize(4)
.extracting("name")
.containsExactlyInAnyOrder(
"master",
"feature/#14-export_tree_as_graphviz",
"feature/queries-update",
"feature/export_and_load_places"
)
Assertions.assertThat(result[1].name).isEqualTo("aoc")
Assertions.assertThat(result[1].owner.login).isEqualTo(username)
Assertions.assertThat(result[1].branches.size).isEqualTo(0)
val request1 = mockGitHubServer.takeRequest()
Assertions.assertThat(request1.path).isEqualTo("/users/$username/repos")
Assertions.assertThat(request1.method).isEqualTo("GET")
val request2 = mockGitHubServer.takeRequest()
Assertions.assertThat(request2.path).isEqualTo("/repos/$username/ads/branches")
Assertions.assertThat(request2.method).isEqualTo("GET")
val request3 = mockGitHubServer.takeRequest()
Assertions.assertThat(request3.path).isEqualTo("/repos/$username/aoc/branches")
Assertions.assertThat(request3.method).isEqualTo("GET")
}
@Test
fun `Should get list of repositories with empty branches when an error occurs fetching them`() {
val username = "Orlando-pt"
val jsonRepositories = Utils.getJsonFileContent("repositories.json")
mockGitHubServer.dispatcher = object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
return when (request.path) {
"/users/$username/repos" -> {
MockResponse()
.setResponseCode(200)
.setHeader("Content-Type", "application/json")
.setBody(jsonRepositories)
}
"/repos/$username/ads/branches" -> {
MockResponse()
.setResponseCode(500)
.setHeader("Content-Type", "application/json")
.setBody("")
}
"/repos/$username/aoc/branches" -> {
MockResponse()
.setResponseCode(200)
.setHeader("Content-Type", "application/json")
.setBody("[]")
}
else -> MockResponse().setResponseCode(404)
}
}
}
val result = testRestTemplate.getForObject(
"/api/repository/$username",
Array<Repository>::class.java
)!!
Assertions.assertThat(result.size).isEqualTo(2)
Assertions.assertThat(result[0].name).isEqualTo("ads")
Assertions.assertThat(result[0].owner.login).isEqualTo(username)
Assertions.assertThat(result[0].branches).hasSize(0)
Assertions.assertThat(result[1].name).isEqualTo("aoc")
Assertions.assertThat(result[1].owner.login).isEqualTo(username)
Assertions.assertThat(result[1].branches.size).isEqualTo(0)
val request1 = mockGitHubServer.takeRequest()
Assertions.assertThat(request1.path).isEqualTo("/users/$username/repos")
Assertions.assertThat(request1.method).isEqualTo("GET")
val request2 = mockGitHubServer.takeRequest()
Assertions.assertThat(request2.path).isEqualTo("/repos/$username/ads/branches")
Assertions.assertThat(request2.method).isEqualTo("GET")
val request3 = mockGitHubServer.takeRequest()
Assertions.assertThat(request3.path).isEqualTo("/repos/$username/aoc/branches")
Assertions.assertThat(request3.method).isEqualTo("GET")
}
@Test
fun `Should get Not Found response when user does not exist`() {
val username = "JohnDoe"
mockGitHubServer.dispatcher = object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
return when (request.path) {
"/users/$username/repos" -> {
MockResponse()
.setResponseCode(404)
.setHeader("Content-Type", "application/json")
.setBody("")
}
else -> MockResponse().setResponseCode(404)
}
}
}
val result = testRestTemplate.getForObject(
"/api/repository/$username",
ErrorResponse::class.java
)!!
Assertions.assertThat(result.message).isEqualTo("Username not found: $username")
Assertions.assertThat(result.status).isEqualTo(404)
val request = mockGitHubServer.takeRequest()
Assertions.assertThat(request.path).isEqualTo("/users/$username/repos")
Assertions.assertThat(request.method).isEqualTo("GET")
}
@Test
fun `Should get Http Client Exception response when GitHub client gives an error`() {
val username = "JohnDoe"
mockGitHubServer.dispatcher = object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
return when (request.path) {
"/users/$username/repos" -> {
MockResponse()
.setResponseCode(500)
.setHeader("Content-Type", "application/json")
.setBody("")
}
else -> MockResponse().setResponseCode(404)
}
}
}
val result = testRestTemplate.getForObject(
"/api/repository/$username",
ErrorResponse::class.java
)!!
Assertions.assertThat(result.message).isEqualTo("Error fetching repositories for username: $username")
Assertions.assertThat(result.status).isEqualTo(500)
val request = mockGitHubServer.takeRequest()
Assertions.assertThat(request.path).isEqualTo("/users/$username/repos")
Assertions.assertThat(request.method).isEqualTo("GET")
}
}