This repository was archived by the owner on Jul 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterCommandServiceTest.kt
More file actions
431 lines (402 loc) · 18.3 KB
/
LetterCommandServiceTest.kt
File metadata and controls
431 lines (402 loc) · 18.3 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package com.asap.application.letter.service
import com.asap.application.letter.exception.LetterException
import com.asap.application.letter.port.`in`.*
import com.asap.application.letter.port.out.IndependentLetterManagementPort
import com.asap.application.letter.port.out.SendLetterManagementPort
import com.asap.application.letter.port.out.SpaceLetterManagementPort
import com.asap.application.user.port.out.UserManagementPort
import com.asap.domain.LetterFixture
import com.asap.domain.UserFixture
import com.asap.domain.common.DomainId
import com.asap.domain.letter.entity.SendLetter
import com.asap.domain.letter.vo.LetterContent
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.nulls.shouldNotBeNull
import io.mockk.clearMocks
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
class LetterCommandServiceTest :
BehaviorSpec({
val mockSendLetterManagementPort = mockk<SendLetterManagementPort>(relaxed = true)
val mockIndependentLetterManagementPort = mockk<IndependentLetterManagementPort>(relaxed = true)
val mockUserManagementPort = mockk<UserManagementPort>(relaxed = true)
val mockSpaceLetterManagementPort = mockk<SpaceLetterManagementPort>(relaxed = true)
val letterCommandService =
LetterCommandService(
mockSendLetterManagementPort,
mockIndependentLetterManagementPort,
mockSpaceLetterManagementPort,
mockUserManagementPort,
)
given("편지 전송 요청이 들어올 때") {
val command =
SendLetterUsecase.Command(
userId = "user-id",
receiverName = "receiver-name",
content = "content",
images = emptyList(),
templateType = 1,
draftId = null,
)
`when`("편지 전송 요청을 처리하면") {
val response = letterCommandService.send(command)
then("편지 코드가 생성되고, 편지가 저장되어야 한다") {
response.letterCode shouldNotBeNull {
this.isNotBlank()
this.isNotEmpty()
}
verify { mockSendLetterManagementPort.save(any()) }
}
}
}
given("익명 편지 전송 요청이 들어올 때") {
val command =
SendLetterUsecase.AnonymousCommand(
receiverName = "receiver-name",
content = "content",
images = emptyList(),
templateType = 1,
)
`when`("익명 편지 전송 요청을 처리하면") {
val response = letterCommandService.sendAnonymous(command)
then("편지 코드가 생성되고, 편지가 저장되어야 한다") {
response.letterCode shouldNotBeNull {
this.isNotBlank()
this.isNotEmpty()
}
verify { mockSendLetterManagementPort.save(any()) }
}
}
}
given("편지 검증 시에") {
val letterCode = "letter-code"
val mockUser = UserFixture.createUser(username = "receiver-name")
val verifyCommand =
VerifyLetterAccessibleUsecase.Command(
letterCode = letterCode,
userId = mockUser.id.value,
)
val sendLetter =
SendLetter.create(
receiverName = "receiver-name",
content =
LetterContent(
"content",
images = mutableListOf(),
templateType = 1,
),
senderId = DomainId("sender-id"),
letterCode = letterCode,
)
every { mockSendLetterManagementPort.verifiedLetter(any(), letterCode) } returns false
every { mockSendLetterManagementPort.getLetterByCodeNotNull(any()) } returns sendLetter
every { mockUserManagementPort.getUserNotNull(any()) } returns mockUser
`when`("이전에 열람한 적이 없고, 수신자 이름과 같다면") {
val response = letterCommandService.verify(verifyCommand)
then("편지 코드가 검증되고, 편지 ID가 반환되어야 한다") {
response.letterId shouldNotBeNull {
this.isNotBlank()
this.isNotEmpty()
}
}
}
every { mockSendLetterManagementPort.getLetterByCodeNotNull(any()) } throws LetterException.SendLetterNotFoundException()
`when`("코드가 존재하지 않는다면") {
then("예외가 발생해야 한다") {
shouldThrow<LetterException.SendLetterNotFoundException> {
letterCommandService.verify(verifyCommand)
}
}
}
val anotherUser = UserFixture.createUser()
every { mockSendLetterManagementPort.getLetterByCodeNotNull(any()) } returns sendLetter
every { mockUserManagementPort.getUserNotNull(any()) } returns anotherUser
`when`("편지의 수신자 이름과 사용자 이름이 다르면") {
then("예외가 발생해야 한다") {
shouldThrow<LetterException.InvalidLetterAccessException> {
letterCommandService.verify(verifyCommand)
}
}
}
every { mockSendLetterManagementPort.verifiedLetter(any(), letterCode) } returns true
every { mockSendLetterManagementPort.getReadLetterNotNull(any(), letterCode) } returns sendLetter
`when`("이전에 열람한 적이 있는 사용자라면") {
val response = letterCommandService.verify(verifyCommand)
then("편지 코드가 검증되고, 편지 ID가 반환되어야 한다") {
response.letterId shouldNotBeNull {
this.isNotBlank()
this.isNotEmpty()
}
}
}
}
given("검증된 편지를 추가할 때") {
val letterId = "letter-id"
val userId = UserFixture.createUser()
val command = AddLetterUsecase.Command.VerifyLetter(letterId, userId.id.value)
val sendLetter = LetterFixture.generateSendLetter(senderId = userId.id)
every {
mockSendLetterManagementPort.getReadLetterNotNull(
any(),
any(DomainId::class),
)
} returns sendLetter
`when`("해당 편지가 아직 받지 않은 상태라면") {
letterCommandService.addVerifiedLetter(command)
then("독립 편지로 저장된다.") {
verify {
mockIndependentLetterManagementPort.save(any())
}
}
}
}
given("실물 편지를 추가할 때") {
val command =
AddLetterUsecase.Command.AddPhysicalLetter(
senderName = "sender-name",
content = "content",
images = emptyList(),
templateType = 1,
userId = "user-id",
draftId = null,
)
`when`("편지를 추가하면") {
letterCommandService.addPhysicalLetter(command)
then("편지가 저장되어야 한다") {
verify { mockSendLetterManagementPort.save(any()) }
}
}
}
given("행성으로 편지를 이동할 때") {
val command =
MoveLetterUsecase.Command.ToSpace(
letterId = "letter-id",
userId = "user-id",
spaceId = "space-id",
)
val independentLetter =
LetterFixture.generateIndependentLetter(
id = DomainId("letter-id"),
senderId = DomainId("sender-id"),
receiverId = DomainId("user-id"),
)
every {
mockIndependentLetterManagementPort.getIndependentLetterByIdNotNull(DomainId("letter-id"))
} returns independentLetter
`when`("편지를 이동하면") {
letterCommandService.moveToSpace(command)
then("편지가 이동되어야 한다") {
verify {
mockSpaceLetterManagementPort.save(any())
}
}
}
}
given("무소속 편지로 이동할 때") {
val command =
MoveLetterUsecase.Command.ToIndependent(
letterId = "letter-id",
userId = "user-id",
)
val spaceLetter =
LetterFixture.generateSpaceLetter(
id = DomainId("letter-id"),
senderId = DomainId("sender-id"),
receiverId = DomainId("user-id"),
spaceId = DomainId("space-id"),
)
every {
mockSpaceLetterManagementPort.getSpaceLetterNotNull(
DomainId("letter-id"),
DomainId("user-id"),
)
} returns spaceLetter
`when`("편지를 이동하면") {
letterCommandService.moveToIndependent(command)
then("편지가 이동되어야 한다") {
verify {
mockIndependentLetterManagementPort.save(any())
}
}
}
}
given("행성 내의 편지 삭제 요청이 들어올 떄") {
val command =
RemoveLetterUsecase.Command.SpaceLetter(
letterId = "letter-id",
userId = "user-id",
)
val spaceLetter =
LetterFixture.generateSpaceLetter(
id = DomainId("letter-id"),
senderId = DomainId("sender-id"),
receiverId = DomainId("user-id"),
spaceId = DomainId("space-id"),
)
every {
mockSpaceLetterManagementPort.getSpaceLetterNotNull(
DomainId("letter-id"),
DomainId("user-id"),
)
} returns spaceLetter
`when`("편지를 삭제하면") {
letterCommandService.removeSpaceLetter(command)
then("편지가 삭제되어야 한다") {
verify { mockSpaceLetterManagementPort.delete(spaceLetter) }
}
}
clearMocks(mockSpaceLetterManagementPort)
every {
mockSpaceLetterManagementPort.getSpaceLetterNotNull(
DomainId("letter-id"),
DomainId("user-id"),
)
} throws LetterException.ReceiveLetterNotFoundException()
`when`("편지가 존재하지 않으면") {
then("예외가 발생해야 한다") {
shouldThrow<LetterException.ReceiveLetterNotFoundException> {
letterCommandService.removeSpaceLetter(command)
}.apply {
verify(exactly = 0) {
mockSpaceLetterManagementPort.delete(any())
}
}
}
}
}
given("궤도 편지 삭제 요청이 들어올 떄") {
val command = RemoveLetterUsecase.Command.IndependentLetter("letter-id", "user-id")
val independentLetter =
LetterFixture.generateIndependentLetter(
id = DomainId("letter-id"),
senderId = DomainId("sender-id"),
receiverId = DomainId("user-id"),
)
every { mockIndependentLetterManagementPort.getIndependentLetterByIdNotNull(DomainId("letter-id")) } returns independentLetter
`when`("편지를 삭제하면") {
letterCommandService.removeIndependentLetter(command)
then("편지가 삭제되어야 한다") {
verify { mockIndependentLetterManagementPort.delete(independentLetter) }
}
}
clearMocks(mockIndependentLetterManagementPort)
every { mockIndependentLetterManagementPort.getIndependentLetterByIdNotNull(DomainId("letter-id")) } throws
LetterException.ReceiveLetterNotFoundException()
`when`("편지가 존재하지 않으면") {
then("예외가 발생해야 한다") {
shouldThrow<LetterException.ReceiveLetterNotFoundException> {
letterCommandService.removeIndependentLetter(command)
}.apply {
verify(exactly = 0) { mockIndependentLetterManagementPort.delete(any()) }
}
}
}
}
given("궤도 편지 수정 요청이 들어올 떄") {
val command =
UpdateLetterUsecase.Command.Independent(
letterId = "letter-id",
senderName = "sender-name",
content = "content",
images = emptyList(),
userId = "user-id",
templateType = 1,
)
val independentLetter =
LetterFixture.generateIndependentLetter(
id = DomainId("letter-id"),
senderId = DomainId("sender-id"),
receiverId = DomainId("user-id"),
)
every { mockIndependentLetterManagementPort.getIndependentLetterByIdNotNull(DomainId("letter-id")) } returns independentLetter
`when`("편지를 수정하면") {
letterCommandService.updateIndependentLetter(command)
then("편지가 수정되어야 한다") {
verify { mockIndependentLetterManagementPort.save(independentLetter) }
}
}
}
given("행성 편지 수정 요청이 들어올 떄") {
val command = UpdateLetterUsecase.Command.Space("letter-id", "user-id", "name", "content", emptyList(), 1)
val spaceLetter =
LetterFixture.generateSpaceLetter(
id = DomainId("letter-id"),
senderId = DomainId("sender-id"),
receiverId = DomainId("user-id"),
spaceId = DomainId("space-id"),
)
every {
mockSpaceLetterManagementPort.getSpaceLetterNotNull(
DomainId("letter-id"),
DomainId("user-id"),
)
} returns spaceLetter
`when`("편지를 수정하면") {
letterCommandService.updateSpaceLetter(command)
then("편지가 수정되어야 한다") {
verify { mockSpaceLetterManagementPort.save(spaceLetter) }
}
}
}
given("익명 편지를 사용자의 편지로 추가할 때") {
val letterCode = "letter-code"
val userId = "user-id"
val command = AddLetterUsecase.Command.AddAnonymousLetter(
letterCode = letterCode,
userId = userId,
)
// Create an anonymous letter using SendLetter.createAnonymous
val content = LetterContent(
content = "content",
templateType = 1,
images = mutableListOf("image1", "image2"),
)
val sendLetter = SendLetter.createAnonymous(
content = content,
receiverName = "receiverName",
letterCode = letterCode,
)
every {
mockSendLetterManagementPort.getLetterByCodeNotNull(letterCode)
} returns sendLetter
`when`("익명 편지를 사용자의 편지로 추가하면") {
letterCommandService.addAnonymousLetter(command)
then("편지의 발신자 ID가 설정되고 저장되어야 한다") {
verify { mockSendLetterManagementPort.save(sendLetter) }
}
}
}
given("보낸 편지 삭제 요청이 들어올 때") {
val userId = "user-id"
val sendLetters =
(0..3).map {
LetterFixture.generateSendLetter()
}
`when`("하나의 편지만 삭제하면") {
val command = RemoveLetterUsecase.Command.SendLetter(sendLetters[0].id.value, userId)
every {
mockSendLetterManagementPort.getSendLetterBy(
DomainId(command.letterId),
DomainId(command.userId),
)
} returns sendLetters[0]
letterCommandService.removeSenderLetterBy(command)
then("편지가 삭제되어야 한다") {
verify { mockSendLetterManagementPort.delete(any()) }
verify { mockSendLetterManagementPort.save(any()) }
}
}
`when`("여러 편지를 삭제하면") {
val ids = sendLetters.map { it.id }
val command = RemoveLetterUsecase.Command.SendLetters(ids.map { it.value }, userId)
every { mockSendLetterManagementPort.getAllBy(DomainId(userId), ids) } returns sendLetters
letterCommandService.removeAllSenderLetterBy(command)
then("편지가 삭제되어야 한다") {
verify { mockSendLetterManagementPort.delete(any()) }
verify { mockSendLetterManagementPort.save(any()) }
}
}
}
})