Skip to content

Commit aa56273

Browse files
committed
test: 특정 뱃지 조회 API 테스트 코드 작성
1 parent dae41fc commit aa56273

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

nmnb-application/src/test/kotlin/nmnb/application/domain/badge/controller/BadgeControllerTest.kt

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequ
1515
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get
1616
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath
1717
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status
18+
import org.mockito.kotlin.eq
19+
import nmnb.common.response.exception.BadgeException
20+
import nmnb.common.response.status.ErrorStatus
1821

1922
class BadgeControllerTest : ControllerTestSupport() {
2023

@@ -89,4 +92,66 @@ class BadgeControllerTest : ControllerTestSupport() {
8992
.andExpect(jsonPath("$.result.badgeInfos").isArray)
9093
.andExpect(jsonPath("$.result.badgeInfos").isEmpty)
9194
}
95+
96+
@WithMockUser
97+
@DisplayName("특정 뱃지 단건 조회에 성공한다")
98+
@Test
99+
fun getBadge() {
100+
// given
101+
val deviceId = "deviceId"
102+
val accessToken = "access.token"
103+
val user = User.fixture()
104+
105+
mockUserAuthentication(user, accessToken, deviceId)
106+
107+
val badgeId = 1L
108+
val response = BadgeInfoResponse(
109+
id = 1L,
110+
name = "멍1 Badge",
111+
description = "멍멍이1 배지",
112+
imageUrl = "mong.png",
113+
price = 100,
114+
isPurchased = false,
115+
purchasedAt = null,
116+
)
117+
118+
whenever(badgeService.getBadge(eq(user.id!!), eq(badgeId))).thenReturn(response)
119+
120+
// when & then
121+
mockMvc.perform(
122+
get("/v1/api/badge/$badgeId")
123+
.contentType(MediaType.APPLICATION_JSON)
124+
.header("X-Access-Token", accessToken)
125+
.header("Device-Id", deviceId)
126+
.with(csrf()),
127+
)
128+
.andExpect(status().isOk)
129+
.andExpect(jsonPath("$.code").value(SuccessStatus.OK.code))
130+
.andExpect(jsonPath("$.result.id").value(badgeId))
131+
}
132+
133+
@WithMockUser
134+
@DisplayName("존재하지 않는 뱃지 조회시 404를 반환한다")
135+
@Test
136+
fun getBadge_notFound() {
137+
// given
138+
val deviceId = "deviceId"
139+
val accessToken = "access.token"
140+
val user = User.fixture()
141+
mockUserAuthentication(user, accessToken, deviceId)
142+
143+
val badgeId = 404L
144+
whenever(badgeService.getBadge(eq(user.id!!), eq(badgeId))).thenThrow(BadgeException(ErrorStatus.BADGE_NOTFOUND))
145+
146+
// when & then
147+
mockMvc.perform(
148+
get("/v1/api/badge/$badgeId")
149+
.contentType(MediaType.APPLICATION_JSON)
150+
.header("X-Access-Token", accessToken)
151+
.header("Device-Id", deviceId)
152+
.with(csrf()),
153+
)
154+
.andExpect(status().isNotFound)
155+
.andExpect(jsonPath("$.code").value(ErrorStatus.BADGE_NOTFOUND.code))
156+
}
92157
}

0 commit comments

Comments
 (0)