-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSessionControllerTest.kt
More file actions
99 lines (77 loc) · 3.42 KB
/
SessionControllerTest.kt
File metadata and controls
99 lines (77 loc) · 3.42 KB
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
package dev.robothanzo.werewolf.controller
import dev.robothanzo.werewolf.WerewolfApplication
import dev.robothanzo.werewolf.controller.dto.SessionResponse
import dev.robothanzo.werewolf.controller.dto.SessionSummaryResponse
import dev.robothanzo.werewolf.database.documents.AuthSession
import dev.robothanzo.werewolf.database.documents.Session
import dev.robothanzo.werewolf.database.documents.UserRole
import dev.robothanzo.werewolf.service.GameSessionService
import dev.robothanzo.werewolf.utils.IdentityUtils
import net.dv8tion.jda.api.JDA
import net.dv8tion.jda.api.entities.Guild
import net.dv8tion.jda.api.entities.Member
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import org.springframework.http.HttpStatus
import java.util.*
class SessionControllerTest {
@Mock
private lateinit var gameSessionService: GameSessionService
@Mock
private lateinit var identityUtils: IdentityUtils
private lateinit var sessionController: SessionController
@BeforeEach
fun setup() {
MockitoAnnotations.openMocks(this)
sessionController = SessionController(gameSessionService, identityUtils)
val mockJda = mock<JDA>()
WerewolfApplication.jda = mockJda
}
@Test
fun testGetAllSessions_Unauthenticated() {
whenever(identityUtils.getCurrentUser()).thenReturn(Optional.empty())
val response = sessionController.getAllSessions()
assertEquals(HttpStatus.UNAUTHORIZED, response.statusCode)
}
@Test
fun testGetAllSessions_Success() {
val user = AuthSession(userId = "user1", guildId = "123", role = UserRole.SPECTATOR)
whenever(identityUtils.getCurrentUser()).thenReturn(Optional.of(user))
val guildMock = mock<Guild>()
whenever(guildMock.name).thenReturn("Test Guild")
whenever(guildMock.iconUrl).thenReturn("icon.png")
whenever(guildMock.getMemberById("user1")).thenReturn(mock<Member>())
val session = Session(guildId = 123L)
// We need to mock WerewolfApplication.jda.getGuildById(123L) to return guildMock
whenever(WerewolfApplication.jda.getGuildById(123L)).thenReturn(guildMock)
whenever(gameSessionService.getAllSessions()).thenReturn(listOf(session))
val response = sessionController.getAllSessions()
assertEquals(HttpStatus.OK, response.statusCode)
val body = response.body as SessionSummaryResponse
assertEquals(1, body.data.size)
assertEquals("123", body.data[0].guildId)
}
@Test
fun testGetSession_Found() {
val guildId = 123L
val session = Session(guildId = guildId)
whenever(gameSessionService.getSession(guildId)).thenReturn(Optional.of(session))
val response = sessionController.getSession(guildId.toString())
assertEquals(HttpStatus.OK, response.statusCode)
val body = response.body as SessionResponse
assertEquals(guildId, body.data.guildId)
}
@Test
fun testGetSession_NotFound() {
val guildId = 123L
whenever(gameSessionService.getSession(guildId)).thenReturn(Optional.empty())
val response = sessionController.getSession(guildId.toString())
assertEquals(HttpStatus.NOT_FOUND, response.statusCode)
}
}