-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepositoryControllerMockTestIT.kt
140 lines (122 loc) · 5.31 KB
/
RepositoryControllerMockTestIT.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
package com.tui.githubrepos.controller
import com.tui.githubrepos.dto.Branch
import com.tui.githubrepos.dto.Commit
import com.tui.githubrepos.dto.Owner
import com.tui.githubrepos.dto.Repository
import com.tui.githubrepos.exception.HttpClientException
import com.tui.githubrepos.exception.ResourceNotFoundException
import com.tui.githubrepos.service.RepositoryService
import kotlinx.coroutines.runBlocking
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest
import org.springframework.boot.test.mock.mockito.MockBean
import org.springframework.http.MediaType
import org.springframework.test.context.aot.DisabledInAotMode
import org.springframework.test.web.reactive.server.WebTestClient
// Follow this GitHub issue to see when the @DisabledInAotMode stops to be needed:
// https://github.com/spring-projects/spring-boot/issues/36997
@DisabledInAotMode
@WebFluxTest(controllers = [RepositoryController::class])
@AutoConfigureWebTestClient
class RepositoryControllerMockTestIT {
@MockBean
private lateinit var repositoryService: RepositoryService
@Autowired
private lateinit var webClient: WebTestClient
private val repository = Repository(
name = "repo1",
owner = Owner(
login = "username"
),
branches = listOf(
Branch(
name = "master",
commit = Commit(
sha = "sha",
)
)
),
fork = false
)
@Test
fun `Should get repositories when the username exists on GitHub`() {
val repositoryNoBranches = repository.copy(branches = listOf())
runBlocking {
Mockito.`when`(repositoryService.getRepositories(repository.owner.login)).thenReturn(
listOf(repository, repositoryNoBranches)
)
webClient.get()
.uri("/repository/${repository.owner.login}")
.exchange()
.expectStatus().isOk
.expectBody()
.jsonPath("size()").isEqualTo(2)
.jsonPath("$[0].name").isEqualTo(repository.name)
.jsonPath("$[0].owner.login").isEqualTo(repository.owner.login)
.jsonPath("$[0].branches.size()").isEqualTo(1)
.jsonPath("$[0].branches[0].name").isEqualTo(repository.branches[0].name)
.jsonPath("$[0].branches[0].commit.sha").isEqualTo(repository.branches[0].commit.sha)
.jsonPath("$[0].fork").doesNotExist()
.jsonPath("$[1].name").isEqualTo(repositoryNoBranches.name)
.jsonPath("$[1].owner.login").isEqualTo(repositoryNoBranches.owner.login)
.jsonPath("$[1].branches.size()").isEqualTo(0)
.jsonPath("$[1].branches[0].name").doesNotExist()
.jsonPath("$[1].fork").doesNotExist()
Mockito.verify(repositoryService, Mockito.times(1)).getRepositories(repository.owner.login)
}
}
@Test
fun `Should get 404 response when the username does not exist on GitHub`() {
val username = "Orlando-pt"
runBlocking {
Mockito.`when`(repositoryService.getRepositories(username)).thenThrow(
ResourceNotFoundException("Username not found: $username")
)
webClient.get()
.uri("/repository/$username")
.exchange()
.expectStatus().isNotFound
.expectBody()
.jsonPath("status").isEqualTo(404)
.jsonPath("message").isEqualTo("Username not found: $username")
Mockito.verify(repositoryService, Mockito.times(1)).getRepositories(username)
}
}
@Test
fun `Should get an error response when an unexpected error occurs fetching repositories from the GitHub API`() {
val username = "Orlando-pt"
val errorMessage = "Error fetching repositories for username: $username"
runBlocking {
Mockito.`when`(repositoryService.getRepositories(username)).thenThrow(
HttpClientException(errorMessage, 401)
)
webClient.get()
.uri("/repository/$username")
.exchange()
.expectStatus().isUnauthorized
.expectBody()
.jsonPath("status").isEqualTo(401)
.jsonPath("message").isEqualTo(errorMessage)
Mockito.verify(repositoryService, Mockito.times(1)).getRepositories(username)
}
}
@Test
fun `Should get 406 response when the media type is not supported`() {
val username = "Orlando-pt"
runBlocking {
webClient.get()
.uri("/repository/$username")
.accept(MediaType.APPLICATION_XML)
.exchange()
.expectStatus().isEqualTo(406)
.expectBody()
.jsonPath("status").isEqualTo(406)
.jsonPath("error").isEqualTo("Not Acceptable")
.jsonPath("path").isEqualTo("/repository/$username")
Mockito.verify(repositoryService, Mockito.times(0)).getRepositories(username)
}
}
}