From 9f7034b83af2f300c86d5a7a9ca908b286f54651 Mon Sep 17 00:00:00 2001 From: Wooyeol Lee Date: Fri, 24 Nov 2023 01:02:28 +0900 Subject: [PATCH] feat: feat getMyEmojis API --- .../emoji/controller/EmojiController.kt | 19 +++ .../domain/emoji/service/EmojiService.kt | 22 +++ .../emoji/controller/EmojiControllerTest.kt | 153 +++++++++++------- .../domain/emoji/dao/EmojiDaoTest.kt | 3 + .../domain/emoji/service/EmojiServiceTest.kt | 123 ++++++++------ 5 files changed, 215 insertions(+), 105 deletions(-) diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/controller/EmojiController.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/controller/EmojiController.kt index e4db9c31..331a4605 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/controller/EmojiController.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/controller/EmojiController.kt @@ -13,6 +13,11 @@ import org.springframework.web.multipart.MultipartFile @RequestMapping("/api/emoji") class EmojiController(private val emojiService: EmojiService) { + companion object { + const val CREATED_EMOJIS = "created_emojis" + const val SAVED_EMOJIS = "saved_emojis" + } + @PostMapping fun postEmoji( @CurrentUser username: String, @@ -32,6 +37,20 @@ class EmojiController(private val emojiService: EmojiService) { return ResponseEntity.ok(emojiService.getEmojis(sortByDate, index, count)) } + @GetMapping("/me/created") + fun getMyCreatedEmojis( + @CurrentUser username: String + ): ResponseEntity> { + return ResponseEntity.ok(emojiService.getMyEmojis(username, CREATED_EMOJIS)) + } + + @GetMapping("/me/saved") + fun getMySavedEmojis( + @CurrentUser username: String + ): ResponseEntity> { + return ResponseEntity.ok(emojiService.getMyEmojis(username, SAVED_EMOJIS)) + } + @GetMapping("/{id}") fun getEmoji( @PathVariable(value = "id") id: String, diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt index 696824b0..bc64756b 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt @@ -29,6 +29,28 @@ class EmojiService( return emojiDao.getEmojis(sortByDate, index, count) } + fun getMyEmojis(username: String, field: String): List { + val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.") + val emojiIdList = if (field == CREATED_EMOJIS) { + user.created_emojis + } else { + user.saved_emojis + } + val emojiList = mutableListOf() + if (emojiIdList != null && emojiIdList.size != 0) { + for (emojiId in emojiIdList) { + val emoji = emojiDao.getEmoji(emojiId) + if (emoji != null) { + emojiList.add(emoji) + } + } + if (emojiList.size != 0) { + emojiList.sortByDescending { it.created_at } + } + } + return emojiList + } + fun getEmoji(emojiId: String): EmojiDto? { if (emojiDao.existsEmoji(emojiId).not()) throw CustomHttp404("Emoji doesn't exist.") return emojiDao.getEmoji(emojiId) diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/controller/EmojiControllerTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/controller/EmojiControllerTest.kt index f2472879..4602c959 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/controller/EmojiControllerTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/controller/EmojiControllerTest.kt @@ -6,10 +6,11 @@ import com.goliath.emojihub.springboot.domain.emoji.dto.EmojiDto import com.goliath.emojihub.springboot.domain.emoji.dto.PostEmojiRequest import com.goliath.emojihub.springboot.domain.emoji.service.EmojiService import org.hamcrest.Matchers +import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.Test import org.junit.jupiter.api.DisplayName -import org.mockito.ArgumentMatchers.anyInt +import org.mockito.Mockito import org.mockito.kotlin.any import org.mockito.kotlin.given import org.mockito.kotlin.times @@ -36,6 +37,30 @@ internal class EmojiControllerTest @Autowired constructor( @MockBean lateinit var emojiService: EmojiService + companion object { + const val CREATED_EMOJIS = "created_emojis" + const val SAVED_EMOJIS = "saved_emojis" + var emojiList: MutableList = mutableListOf() + const val size = 2 + + @BeforeAll + @JvmStatic + fun beforeAll() { + for (i in 0 until size) { + emojiList.add( + EmojiDto( + id = "test_id$i", + created_by = "test_created_by$i", + video_url = "test_video_url$i", + emoji_unicode = "test_video_url$i", + emoji_label = "test_emoji_label$i", + created_at = "test_created_at$i", + num_saved = i + ) + ) + } + } + } @Test @WithCustomUser @@ -45,34 +70,7 @@ internal class EmojiControllerTest @Autowired constructor( val sortByDate = 0 val index = 1 val count = 10 - val list = mutableListOf() - val size = 2 - val id = "test_id" - val createdBy = "test_created_by" - val videoUrl = "test_video_url" - val emojiUnicode = "test_emoji_unicode" - val emojiLabel = "test_emoji_label" - val createdAt = "test_created_at" - for (i in 0 until size) { - list.add( - EmojiDto( - id = id + i, - created_by = createdBy + i, - video_url = videoUrl + i, - emoji_unicode = emojiUnicode + i, - emoji_label = emojiLabel + i, - created_at = createdAt + i, - num_saved = i - ) - ) - } - given( - emojiService.getEmojis( - anyInt(), - anyInt(), - anyInt() - ) - ).willReturn(list) + Mockito.`when`(emojiService.getEmojis(sortByDate, index, count)).thenReturn(emojiList) // when val result = this.mockMvc.perform( @@ -86,15 +84,64 @@ internal class EmojiControllerTest @Autowired constructor( result.andExpect(status().isOk) .andExpect(content().contentType(MediaType.APPLICATION_JSON)) .andExpect(jsonPath("$.length()", Matchers.equalTo(size))) - .andExpect(jsonPath("$[0].id").value(id + 0)) - .andExpect(jsonPath("$[0].created_by").value(createdBy + 0)) - .andExpect(jsonPath("$[0].video_url").value(videoUrl + 0)) - .andExpect(jsonPath("$[0].emoji_unicode").value(emojiUnicode + 0)) - .andExpect(jsonPath("$[0].emoji_label").value(emojiLabel + 0)) - .andExpect(jsonPath("$[0].created_at").value(createdAt + 0)) - .andExpect(jsonPath("$[0].num_saved").value(0)) - verify(emojiService).getEmojis(sortByDate, index, count) + .andExpect(jsonPath("$[0].id").value(emojiList[0].id)) + .andExpect(jsonPath("$[0].created_by").value(emojiList[0].created_by)) + .andExpect(jsonPath("$[0].video_url").value(emojiList[0].video_url)) + .andExpect(jsonPath("$[0].emoji_unicode").value(emojiList[0].emoji_unicode)) + .andExpect(jsonPath("$[0].emoji_label").value(emojiList[0].emoji_label)) + .andExpect(jsonPath("$[0].created_at").value(emojiList[0].created_at)) + .andExpect(jsonPath("$[0].num_saved").value(emojiList[0].num_saved)) + verify(emojiService, times(1)).getEmojis(sortByDate, index, count) + } + + @Test + @WithCustomUser + @DisplayName("자신이 만든 이모지 데이터 가져오기 테스트") + fun getMyCreatedEmojis() { + // given + val username = "custom_username" + Mockito.`when`(emojiService.getMyEmojis(username, CREATED_EMOJIS)).thenReturn(emojiList) + + // when + val result = this.mockMvc.perform(get("/api/emoji/me/created")) + + // then + result.andExpect(status().isOk) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.length()", Matchers.equalTo(size))) + .andExpect(jsonPath("$[0].id").value(emojiList[0].id)) + .andExpect(jsonPath("$[0].created_by").value(emojiList[0].created_by)) + .andExpect(jsonPath("$[0].video_url").value(emojiList[0].video_url)) + .andExpect(jsonPath("$[0].emoji_unicode").value(emojiList[0].emoji_unicode)) + .andExpect(jsonPath("$[0].emoji_label").value(emojiList[0].emoji_label)) + .andExpect(jsonPath("$[0].created_at").value(emojiList[0].created_at)) + .andExpect(jsonPath("$[0].num_saved").value(emojiList[0].num_saved)) + verify(emojiService, times(1)).getMyEmojis(username, CREATED_EMOJIS) + } + @Test + @WithCustomUser + @DisplayName("자신이 저장한 이모지 데이터 가져오기 테스트") + fun getMySavedEmojis() { + // given + val username = "custom_username" + Mockito.`when`(emojiService.getMyEmojis(username, SAVED_EMOJIS)).thenReturn(emojiList) + + // when + val result = this.mockMvc.perform(get("/api/emoji/me/saved")) + + // then + result.andExpect(status().isOk) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.length()", Matchers.equalTo(size))) + .andExpect(jsonPath("$[0].id").value(emojiList[0].id)) + .andExpect(jsonPath("$[0].created_by").value(emojiList[0].created_by)) + .andExpect(jsonPath("$[0].video_url").value(emojiList[0].video_url)) + .andExpect(jsonPath("$[0].emoji_unicode").value(emojiList[0].emoji_unicode)) + .andExpect(jsonPath("$[0].emoji_label").value(emojiList[0].emoji_label)) + .andExpect(jsonPath("$[0].created_at").value(emojiList[0].created_at)) + .andExpect(jsonPath("$[0].num_saved").value(emojiList[0].num_saved)) + verify(emojiService, times(1)).getMyEmojis(username, SAVED_EMOJIS) } @Test @@ -102,30 +149,23 @@ internal class EmojiControllerTest @Autowired constructor( @DisplayName("특정 이모지 데이터 가져오기 테스트") fun getEmoji() { // given - val emojiDto = EmojiDto( - id = "test_id", - created_by = "test_created_by", - video_url = "test_video_url", - emoji_unicode = "test_emoji_unicode", - emoji_label = "test_emoji_label", - created_at = "test_created_at", - num_saved = 1 - ) - given(emojiService.getEmoji(any())).willReturn(emojiDto) + val emoji = emojiList[0] + given(emojiService.getEmoji(any())).willReturn(emoji) // when - val result = mockMvc.perform(get("/api/emoji/{id}", emojiDto.id)) + val result = mockMvc.perform(get("/api/emoji/{id}", emoji.id)) // then result.andExpect(status().isOk) - .andExpect(jsonPath("$.id").value(emojiDto.id)) - .andExpect(jsonPath("$.created_by").value(emojiDto.created_by)) - .andExpect(jsonPath("$.video_url").value(emojiDto.video_url)) - .andExpect(jsonPath("$.emoji_unicode").value(emojiDto.emoji_unicode)) - .andExpect(jsonPath("$.emoji_label").value(emojiDto.emoji_label)) - .andExpect(jsonPath("$.created_at").value(emojiDto.created_at)) - .andExpect(jsonPath("$.num_saved").value(emojiDto.num_saved)) - verify(emojiService).getEmoji(emojiDto.id) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andExpect(jsonPath("$.id").value(emoji.id)) + .andExpect(jsonPath("$.created_by").value(emoji.created_by)) + .andExpect(jsonPath("$.video_url").value(emoji.video_url)) + .andExpect(jsonPath("$.emoji_unicode").value(emoji.emoji_unicode)) + .andExpect(jsonPath("$.emoji_label").value(emoji.emoji_label)) + .andExpect(jsonPath("$.created_at").value(emoji.created_at)) + .andExpect(jsonPath("$.num_saved").value(emoji.num_saved)) + verify(emojiService).getEmoji(emoji.id) } @Test @@ -157,7 +197,6 @@ internal class EmojiControllerTest @Autowired constructor( .file(requestFile) .contentType(MediaType.MULTIPART_FORM_DATA) .characterEncoding("UTF-8") - .with { request -> request.method = "POST"; request } .with(csrf()) ) diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDaoTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDaoTest.kt index 5afc9984..dd11fd7c 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDaoTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDaoTest.kt @@ -129,14 +129,17 @@ internal class EmojiDaoTest { fun getEmoji() { // given val emojiId = emojiList[0].id + val wrongId = "wrong_emoji_id" Mockito.`when`(db.collection(EMOJI_COLLECTION_NAME)) .thenReturn(testDB.collection(EMOJI_COLLECTION_NAME)) // when val result = emojiDao.getEmoji(emojiId) + val wrongResult = emojiDao.getEmoji(wrongId) // then assertEquals(result, emojiList[0]) + assertEquals(wrongResult, null) } @Test diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiServiceTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiServiceTest.kt index 7cda3e84..0d209941 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiServiceTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiServiceTest.kt @@ -10,6 +10,7 @@ import com.goliath.emojihub.springboot.global.exception.CustomHttp404 import org.junit.jupiter.api.Test import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.BeforeAll import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.extension.ExtendWith import org.mockito.Mockito @@ -35,31 +36,41 @@ internal class EmojiServiceTest { @MockBean lateinit var userDao: UserDao + companion object { + const val CREATED_EMOJIS = "created_emojis" + const val SAVED_EMOJIS = "saved_emojis" + var emojiList: MutableList = mutableListOf() + const val size = 2 + + @BeforeAll + @JvmStatic + fun beforeAll() { + for (i in 0 until size) { + emojiList.add( + EmojiDto( + id = "test_id$i", + created_by = "test_created_by$i", + video_url = "test_video_url$i", + emoji_unicode = "test_video_url$i", + emoji_label = "test_emoji_label$i", + created_at = "test_created_at$i", + num_saved = i + ) + ) + } + } + } + @Test @DisplayName("이모지 데이터 가져오기") fun getEmojis() { // given - val list = mutableListOf() - val size = 2 - for (i in 0 until size) { - list.add( - EmojiDto( - id = "test_id$i", - created_by = "test_created_by$i", - video_url = "test_video_url$i", - emoji_unicode = "test_emoji_unicode$i", - emoji_label = "test_emoji_label$i", - created_at = "test_created_at$i", - num_saved = 0, - ) - ) - } val sortByDate = 0 val index = 1 val count = 10 val wrongIndex = 0 val wrongCount = 0 - Mockito.`when`(emojiDao.getEmojis(sortByDate, index, count)).thenReturn(list) + Mockito.`when`(emojiDao.getEmojis(sortByDate, index, count)).thenReturn(emojiList) // when val result = emojiService.getEmojis(sortByDate, index, count) @@ -72,28 +83,60 @@ internal class EmojiServiceTest { // then assertAll( - { assertEquals(result, list) }, + { assertEquals(result, emojiList) }, { assertEquals(assertThrows1.message, "Index should be positive integer.") }, { assertEquals(assertThrows2.message, "Count should be positive integer.") } ) verify(emojiDao, times(1)).getEmojis(sortByDate, index, count) } + @Test + @DisplayName("자신의 이모지 데이터 가져오기") + fun getMyEmojis() { + // given + val emoji = emojiList[0] + val username = emoji.created_by + val wrongUsername = "wrong_username" + val user = UserDto( + username = username, + email = "test_email", + password = "test_password" + ) + user.created_emojis!!.add(emojiList[0].id) + user.saved_emojis!!.add(emojiList[1].id) + Mockito.`when`(userDao.getUser(username)).thenReturn(user) + Mockito.`when`(userDao.getUser(wrongUsername)).thenReturn(null) + Mockito.`when`(emojiDao.getEmoji(user.created_emojis!![0])).thenReturn(emojiList[0]) + Mockito.`when`(emojiDao.getEmoji(user.saved_emojis!![0])).thenReturn(emojiList[1]) + + // when + val createdEmojisResult = emojiService.getMyEmojis(username, CREATED_EMOJIS) + val savedEmojisResult = emojiService.getMyEmojis(username, SAVED_EMOJIS) + val assertThrows = assertThrows(CustomHttp404::class.java) { + emojiService.getMyEmojis(wrongUsername, CREATED_EMOJIS) + } + + // then + assertAll( + { assertEquals(createdEmojisResult.size, 1) }, + { assertEquals(savedEmojisResult.size, 1) }, + { assertEquals(createdEmojisResult[0], emojiList[0]) }, + { assertEquals(savedEmojisResult[0], emojiList[1]) }, + { assertEquals(assertThrows.message, "User doesn't exist.") } + ) + verify(userDao, times(2)).getUser(username) + verify(userDao, times(1)).getUser(wrongUsername) + verify(emojiDao, times(1)).getEmoji(user.created_emojis!![0]) + verify(emojiDao, times(1)).getEmoji(user.saved_emojis!![0]) + } + @Test @DisplayName("특정 이모지 데이터 가져오기") fun getEmoji() { // given - val id = "test_id" + val emoji = emojiList[0] + val id = emoji.id val wrongId = "wrong_id" - val emoji = EmojiDto( - id = id, - created_by = "test_created_by", - video_url = "test_video_url", - emoji_unicode = "test_emoji_unicode", - emoji_label = "test_emoji_label", - created_at = "test_created_at", - num_saved = 0, - ) Mockito.`when`(emojiDao.existsEmoji(id)).thenReturn(true) Mockito.`when`(emojiDao.existsEmoji(wrongId)).thenReturn(false) Mockito.`when`(emojiDao.getEmoji(id)).thenReturn(emoji) @@ -125,15 +168,7 @@ internal class EmojiServiceTest { val thumbnail = MockMultipartFile("thumbnail", "test.jpeg", "image/jpeg", imageContent) val emojiUnicode = "test_emoji_unicode" val emojiLabel = "test_emoji_label" - val emoji = EmojiDto( - id = "test_emoji_id", - created_by = username, - video_url = "test_video_url", - emoji_unicode = "test_emoji_unicode", - emoji_label = "test_emoji_label", - created_at = "test_created_at", - num_saved = 0, - ) + val emoji = emojiList[0] Mockito.`when`(emojiDao.insertEmoji(any(), any(), any(), any(), any(), any())).thenReturn(emoji) // when @@ -141,7 +176,7 @@ internal class EmojiServiceTest { // then verify(emojiDao, times(1)).insertEmoji(any(), any(), any(), any(), any(), any()) - verify(userDao, times(1)).insertId(username, emoji.id, "created_emojis") + verify(userDao, times(1)).insertId(username, emoji.id, CREATED_EMOJIS) } @Test @@ -374,19 +409,11 @@ internal class EmojiServiceTest { @DisplayName("이모지 삭제하기") fun deleteEmoji() { // given - val username = "test_username" + val emoji = emojiList[0] + val username = emoji.created_by val wrongUsername = "wrong_username" - val emojiId = "test_emojiId" + val emojiId = emoji.id val wrongId = "wrong_emojiId" - val emoji = EmojiDto( - id = emojiId, - created_by = username, - video_url = "test_video_url", - emoji_unicode = "test_emoji_unicode", - emoji_label = "test_emoji_label", - created_at = "test_created_at", - num_saved = 0, - ) Mockito.`when`(emojiDao.getEmoji(emojiId)).thenReturn(emoji) Mockito.`when`(emojiDao.getEmoji(wrongId)).thenReturn(null)