From d069f025b7b112f9be46097b240e7ad1974107aa Mon Sep 17 00:00:00 2001 From: belljun3395 <195850@jnu.ac.kr> Date: Thu, 8 Aug 2024 23:18:55 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=ED=98=95=EC=8B=9D=20=ED=86=B5=EC=9D=BC=EB=90=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/AdminControllerTest.kt | 144 +++++++++--------- .../controller/admin/ApiLogControllerTest.kt | 9 +- .../article/ArticleControllerTest.kt | 106 ++++++------- .../controller/member/MemberControllerTest.kt | 19 ++- .../problem/ProblemControllerTest.kt | 15 +- .../SubscriptionControllerTest.kt | 28 ++-- .../workbook/WorkBookControllerTest.kt | 84 +++++----- .../article/WorkBookArticleControllerTest.kt | 43 +++--- 8 files changed, 222 insertions(+), 226 deletions(-) diff --git a/api/src/test/kotlin/com/few/api/web/controller/admin/AdminControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/admin/AdminControllerTest.kt index 70f3c1d80..d4567fd68 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/admin/AdminControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/admin/AdminControllerTest.kt @@ -3,11 +3,9 @@ package com.few.api.web.controller.admin import com.epages.restdocs.apispec.ResourceDocumentation.resource import com.epages.restdocs.apispec.ResourceSnippetParameters import com.epages.restdocs.apispec.Schema -import com.few.api.domain.admin.document.usecase.* import com.few.api.domain.admin.document.usecase.dto.* import com.few.api.web.controller.ControllerTestSpec import com.few.api.web.controller.admin.request.* -import com.few.api.web.controller.admin.response.ImageSourceResponse import com.few.api.web.controller.description.Description import com.few.api.web.controller.helper.* import com.few.data.common.code.CategoryType @@ -24,12 +22,13 @@ import org.springframework.restdocs.payload.PayloadDocumentation import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import org.springframework.web.util.UriComponentsBuilder import java.net.URL +import java.util.stream.IntStream class AdminControllerTest : ControllerTestSpec() { companion object { - private val BASE_URL = "/api/v1/admin" - private val TAG = "AdminController" + private const val BASE_URL = "/api/v1/admin" + private const val TAG = "AdminController" } @Test @@ -44,9 +43,10 @@ class AdminControllerTest : ControllerTestSpec() { val description = "description" val request = AddWorkbookRequest(title, mainImageUrl, category, description) val body = objectMapper.writeValueAsString(request) - `when`(addWorkbookUseCase.execute(AddWorkbookUseCaseIn(title, mainImageUrl, category, description))).thenReturn( - AddWorkbookUseCaseOut(1L) - ) + + val useCaseIn = AddWorkbookUseCaseIn(title, mainImageUrl, category, description) + val useCaseOut = AddWorkbookUseCaseOut(1L) + `when`(addWorkbookUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( @@ -83,76 +83,54 @@ class AdminControllerTest : ControllerTestSpec() { // given val api = "AddArticle" val uri = UriComponentsBuilder.newInstance().path("$BASE_URL/articles").build().toUriString() - val request = AddArticleRequest( - "writer@gmail.com", - URL("http://localhost:8080"), - "title", - CategoryType.fromCode(0)!!.name, - "md", - "content source", - listOf( - ProblemDto( - "title1", - listOf( - ProblemContentDto(1L, "content1"), - ProblemContentDto(2L, "content2"), - ProblemContentDto(3L, "content3"), - ProblemContentDto(4L, "content4") - ), - "1", - "explanation" - ), - ProblemDto( - "title2", - listOf( - ProblemContentDto(1L, "content1"), - ProblemContentDto(2L, "content2"), - ProblemContentDto(3L, "content3"), - ProblemContentDto(4L, "content4") - ), - "2", - "explanation" - ) + val writerEmail = "writer@gmail.com" + val articleImageURL = URL("http://localhost:8080") + val title = "title" + val category = CategoryType.fromCode(0)!!.name + val contentType = "md" + val contentSource = "content source" + val problemDTOs = IntStream.range(0, 2).mapToObj { + ProblemDto( + "title$it", + IntStream.range(0, 4).mapToObj { ProblemContentDto(it.toLong(), "content$it") }.toList(), + "$it", + "explanation$it" + ) + }.toList() + val problemDetails = problemDTOs.map { + ProblemDetail( + it.title, + it.contents.map { content -> ProblemContentDetail(content.number, content.content) }, + it.answer, + it.explanation ) + } + val request = AddArticleRequest( + writerEmail, + articleImageURL, + title, + category, + contentType, + contentSource, + problemDTOs ) val body = objectMapper.writeValueAsString(request) + val useCaseIn = AddArticleUseCaseIn( + writerEmail, + articleImageURL, + title, + category, + contentType, + contentSource, + problemDetails + ) + val useCaseOut = AddArticleUseCaseOut(1L) `when`( addArticleUseCase.execute( - AddArticleUseCaseIn( - "writer@gmail.com", - URL("http://localhost:8080"), - "title", - CategoryType.fromCode(0)!!.name, - "md", - "content source", - listOf( - ProblemDetail( - "title1", - listOf( - ProblemContentDetail(1L, "content1"), - ProblemContentDetail(2L, "content2"), - ProblemContentDetail(3L, "content3"), - ProblemContentDetail(4L, "content4") - ), - "1", - "explanation" - ), - ProblemDetail( - "title2", - listOf( - ProblemContentDetail(1L, "content1"), - ProblemContentDetail(2L, "content2"), - ProblemContentDetail(3L, "content3"), - ProblemContentDetail(4L, "content4") - ), - "2", - "explanation" - ) - ) - ) + useCaseIn ) - ).thenReturn(AddArticleUseCaseOut(1L)) + ).thenReturn(useCaseOut) // when mockMvc.perform( @@ -189,9 +167,14 @@ class AdminControllerTest : ControllerTestSpec() { // given val api = "MapArticle" val uri = UriComponentsBuilder.newInstance().path("$BASE_URL/relations/articles").build().toUriString() - val request = MapArticleRequest(1L, 1L, 1) + val workbookId = 1L + val articleId = 1L + val dayCol = 1 + val request = MapArticleRequest(workbookId, articleId, dayCol) val body = objectMapper.writeValueAsString(request) - doNothing().`when`(mapArticleUseCase).execute(MapArticleUseCaseIn(1L, 1L, 1)) + + val useCaseIn = MapArticleUseCaseIn(workbookId, articleId, dayCol) + doNothing().`when`(mapArticleUseCase).execute(useCaseIn) // when mockMvc.perform( @@ -221,7 +204,12 @@ class AdminControllerTest : ControllerTestSpec() { // given val api = "ConvertContent" val uri = UriComponentsBuilder.newInstance().path("$BASE_URL/utilities/conversion/content").build().toUriString() - val request = ConvertContentRequest(MockMultipartFile("content", "test.md", "text/markdown", "#test".toByteArray())) + val name = "content" + val originalFilename = "test.md" + val contentType = "text/markdown" + val content = "#test".toByteArray() + val request = ConvertContentRequest(MockMultipartFile(name, originalFilename, contentType, content)) + val useCaseOut = ConvertContentUseCaseOut("converted content", URL("http://localhost:8080/test.md")) val useCaseIn = ConvertContentUseCaseIn(request.content) `when`(convertContentUseCase.execute(useCaseIn)).thenReturn(useCaseOut) @@ -265,9 +253,13 @@ class AdminControllerTest : ControllerTestSpec() { // given val api = "PutImage" val uri = UriComponentsBuilder.newInstance().path("$BASE_URL/utilities/conversion/image").build().toUriString() - val request = ImageSourceRequest(source = MockMultipartFile("source", "test.jpg", "image/jpeg", "test".toByteArray())) - val response = ImageSourceResponse(URL("http://localhost:8080/test.jpg"), listOf("jpg", "webp")) - val useCaseOut = PutImageUseCaseOut(response.url, response.supportSuffix) + val name = "source" + val originalFilename = "test.jpg" + val contentType = "image/jpeg" + val content = "test".toByteArray() + val request = ImageSourceRequest(source = MockMultipartFile(name, originalFilename, contentType, content)) + + val useCaseOut = PutImageUseCaseOut(URL("http://localhost:8080/test.jpg"), listOf("jpg", "webp")) val useCaseIn = PutImageUseCaseIn(request.source) `when`(putImageUseCase.execute(useCaseIn)).thenReturn(useCaseOut) diff --git a/api/src/test/kotlin/com/few/api/web/controller/admin/ApiLogControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/admin/ApiLogControllerTest.kt index 09131b083..03da8a246 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/admin/ApiLogControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/admin/ApiLogControllerTest.kt @@ -20,8 +20,8 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status class ApiLogControllerTest : ControllerTestSpec() { companion object { - private val BASE_URL = "/api/v1/logs" - private val TAG = "ApiLogControllerTest" + private const val BASE_URL = "/api/v1/logs" + private const val TAG = "ApiLogControllerTest" } @Test @@ -32,14 +32,15 @@ class ApiLogControllerTest : ControllerTestSpec() { val history = objectMapper.writeValueAsString(mapOf("from" to "email", "to" to "readArticle")) val request = ApiLogRequest(history) - val content = objectMapper.writeValueAsString(request) + val body = objectMapper.writeValueAsString(request) + val useCaseIn = AddApiLogUseCaseIn(history) Mockito.doNothing().`when`(addApiLogUseCase).execute(useCaseIn) // When mockMvc.perform( post(BASE_URL) - .content(content) + .content(body) .contentType(MediaType.APPLICATION_JSON) ).andExpect( status().is2xxSuccessful diff --git a/api/src/test/kotlin/com/few/api/web/controller/article/ArticleControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/article/ArticleControllerTest.kt index 3c6838e77..19ef02f59 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/article/ArticleControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/article/ArticleControllerTest.kt @@ -21,12 +21,13 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import org.springframework.web.util.UriComponentsBuilder import java.net.URL import java.time.LocalDateTime +import java.util.stream.IntStream class ArticleControllerTest : ControllerTestSpec() { companion object { - private val BASE_URL = "/api/v1/articles" - private val TAG = "ArticleController" + private const val BASE_URL = "/api/v1/articles" + private const val TAG = "ArticleController" } /** @@ -37,35 +38,38 @@ class ArticleControllerTest : ControllerTestSpec() { fun readArticle() { // given val api = "ReadArticle" + val token = "thisisaccesstoken" val uri = UriComponentsBuilder.newInstance().path("$BASE_URL/{articleId}").build().toUriString() - // set usecase mock val articleId = 1L + val memberId = 1L + `when`(tokenResolver.resolveId(token)).thenReturn(memberId) - `when`(tokenResolver.resolveId("thisisaccesstoken")).thenReturn(memberId) - `when`(readArticleUseCase.execute(ReadArticleUseCaseIn(articleId, memberId))).thenReturn( - ReadArticleUseCaseOut( + val useCaseIn = ReadArticleUseCaseIn(articleId, memberId) + val useCaseOut = ReadArticleUseCaseOut( + id = 1L, + writer = WriterDetail( id = 1L, - writer = WriterDetail( - id = 1L, - name = "안나포", - url = URL("http://localhost:8080/api/v1/writers/1"), - imageUrl = URL("https://github.com/user-attachments/assets/28df9078-488c-49d6-9375-54ce5a250742") - ), - mainImageUrl = URL("https://github.com/YAPP-Github/24th-Web-Team-1-BE/assets/102807742/0643d805-5f3a-4563-8c48-2a7d51795326"), - title = "ETF(상장 지수 펀드)란? 모르면 손해라고?", - content = CategoryType.fromCode(0)!!.name, - problemIds = listOf(1L, 2L, 3L), - category = "경제", - createdAt = LocalDateTime.now(), - views = 1L - ) + name = "안나포", + url = URL("http://localhost:8080/api/v1/writers/1"), + imageUrl = URL("https://github.com/user-attachments/assets/28df9078-488c-49d6-9375-54ce5a250742") + ), + mainImageUrl = URL("https://github.com/YAPP-Github/24th-Web-Team-1-BE/assets/102807742/0643d805-5f3a-4563-8c48-2a7d51795326"), + title = "ETF(상장 지수 펀드)란? 모르면 손해라고?", + content = CategoryType.fromCode(0)!!.name, + problemIds = listOf(1L, 2L, 3L), + category = "경제", + createdAt = LocalDateTime.now(), + views = 1L + ) + `when`(readArticleUseCase.execute(useCaseIn)).thenReturn( + useCaseOut ) // when mockMvc.perform( get(uri, articleId) - .header("Authorization", "Bearer thisisaccesstoken") + .header("Authorization", "Bearer $token") .contentType(MediaType.APPLICATION_JSON) ).andExpect(status().is2xxSuccessful) .andDo( @@ -131,45 +135,41 @@ class ArticleControllerTest : ControllerTestSpec() { val prevArticleId = 1L val categoryCd: Byte = CategoryType.IT.code val uri = UriComponentsBuilder.newInstance() - .path("$BASE_URL") + .path(BASE_URL) .queryParam("prevArticleId", prevArticleId) .queryParam("categoryCd", categoryCd) .build() .toUriString() - // set usecase mock - `when`(readArticlesUseCase.execute(ReadArticlesUseCaseIn(prevArticleId, categoryCd))).thenReturn( - ReadArticlesUseCaseOut( - listOf( - ReadArticleUseCaseOut( + + val useCaseIn = ReadArticlesUseCaseIn(prevArticleId, categoryCd) + val useCaseOut = ReadArticlesUseCaseOut( + IntStream.range(0, 10).mapToObj { + ReadArticleUseCaseOut( + id = it.toLong(), + writer = WriterDetail( id = 1L, - writer = WriterDetail( - id = 1L, - name = "안나포", - url = URL("http://localhost:8080/api/v1/writers/1"), - imageUrl = URL("https://github.com/user-attachments/assets/28df9078-488c-49d6-9375-54ce5a250742") - ), - mainImageUrl = URL("https://github.com/YAPP-Github/24th-Web-Team-1-BE/assets/102807742/0643d805-5f3a-4563-8c48-2a7d51795326"), - title = "ETF(상장 지수 펀드)란? 모르면 손해라고?", - content = CategoryType.fromCode(0)!!.name, - problemIds = emptyList(), - category = CategoryType.IT.displayName, - createdAt = LocalDateTime.now(), - views = 9876543210L, - workbooks = listOf( - WorkbookDetail( - id = 1, - title = "워크북1 타이틀" - ), - WorkbookDetail( - id = 2, - title = "워크북2 타이틀" - ) + name = "writer$it", + url = URL("http://localhost:8080/api/v1/writers/$it"), + imageUrl = URL("http://localhost:8080/api/v1/writers/images/$it") + ), + mainImageUrl = URL("http://localhost:8080/api/v1/articles/main/images/$it"), + title = "title$it", + content = "content$it", + problemIds = emptyList(), + category = CategoryType.ECONOMY.displayName, + createdAt = LocalDateTime.now(), + views = it.toLong(), + workbooks = IntStream.range(0, 2).mapToObj { j -> + WorkbookDetail( + id = "$it$j".toLong(), + title = "workbook$it$j" ) - ) - ), - true - ) + }.toList() + ) + }.toList(), + true ) + `when`(readArticlesUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( diff --git a/api/src/test/kotlin/com/few/api/web/controller/member/MemberControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/member/MemberControllerTest.kt index cc7273daa..4652ebf0f 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/member/MemberControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/member/MemberControllerTest.kt @@ -26,8 +26,8 @@ import org.springframework.web.util.UriComponentsBuilder class MemberControllerTest : ControllerTestSpec() { companion object { - private val BASE_URL = "/api/v1/members" - private val TAG = "MemberController" + private const val BASE_URL = "/api/v1/members" + private const val TAG = "MemberController" } @Test @@ -42,9 +42,9 @@ class MemberControllerTest : ControllerTestSpec() { val email = "test@gmail.com" val body = objectMapper.writeValueAsString(SaveMemberRequest(email = email)) - // set mock val useCaseIn = SaveMemberUseCaseIn(email = email) - `when`(saveMemberUseCase.execute(useCaseIn)).thenReturn(SaveMemberUseCaseOut(isSendAuthEmail = true)) + val useCaseOut = SaveMemberUseCaseOut(isSendAuthEmail = true) + `when`(saveMemberUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( @@ -104,13 +104,12 @@ class MemberControllerTest : ControllerTestSpec() { rt = rt, refreshToken = tokenRequest.refreshToken ) - `when`(tokenUseCase.execute(useCaseIn)).thenReturn( - TokenUseCaseOut( - accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZW1iZXJJZCI6NTcsIm1lbWJlclJvbGUiOiJbUk9MRV9VU0VSXSIsImlhdCI6MTcyMjI1NTE4MywiZXhwIjoxNzUzODEyNzgzfQ.1KXRim0MVvz1vxOQB_700XPCD9zPQtHNItF_A9upvA8", - refreshToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZW1iZXJJZCI6NTcsIm1lbWJlclJvbGUiOiJbUk9MRV9VU0VSXSIsImlhdCI6MTcyMjI1NTE4MywiZXhwIjoxNzUzODEyNzgzfQ.1KXRim0MVvz1vxOQB_700XPCD9zPQtHNItF_A9upvA8", - isLogin = false - ) + val useCaseOut = TokenUseCaseOut( + accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZW1iZXJJZCI6NTcsIm1lbWJlclJvbGUiOiJbUk9MRV9VU0VSXSIsImlhdCI6MTcyMjI1NTE4MywiZXhwIjoxNzUzODEyNzgzfQ.1KXRim0MVvz1vxOQB_700XPCD9zPQtHNItF_A9upvA8", + refreshToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJtZW1iZXJJZCI6NTcsIm1lbWJlclJvbGUiOiJbUk9MRV9VU0VSXSIsImlhdCI6MTcyMjI1NTE4MywiZXhwIjoxNzUzODEyNzgzfQ.1KXRim0MVvz1vxOQB_700XPCD9zPQtHNItF_A9upvA8", + isLogin = false ) + `when`(tokenUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( diff --git a/api/src/test/kotlin/com/few/api/web/controller/problem/ProblemControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/problem/ProblemControllerTest.kt index 7c28df804..afaed0955 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/problem/ProblemControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/problem/ProblemControllerTest.kt @@ -17,7 +17,6 @@ import com.few.api.domain.problem.usecase.dto.ReadProblemContentsUseCaseOutDetai import com.few.api.domain.problem.usecase.dto.ReadProblemUseCaseOut import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test -import org.mockito.Mockito import org.mockito.Mockito.`when` import org.springframework.http.MediaType import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document @@ -30,8 +29,8 @@ import org.springframework.web.util.UriComponentsBuilder class ProblemControllerTest : ControllerTestSpec() { companion object { - private val BASE_URL = "/api/v1/problems" - private val TAG = "ProblemController" + private const val BASE_URL = "/api/v1/problems" + private const val TAG = "ProblemController" } @Test @@ -48,8 +47,7 @@ class ProblemControllerTest : ControllerTestSpec() { val useCaseIn = BrowseProblemsUseCaseIn(articleId) val useCaseOut = BrowseProblemsUseCaseOut(listOf(1L, 2L, 3L)) - `when`(browseProblemsUseCase.execute(useCaseIn)) - .thenReturn(useCaseOut) + `when`(browseProblemsUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( @@ -95,7 +93,6 @@ class ProblemControllerTest : ControllerTestSpec() { .build() .toUriString() - // set usecase mock val problemId = 1L val useCaseIn = ReadProblemUseCaseIn(problemId) val useCaseOut = ReadProblemUseCaseOut( @@ -108,8 +105,7 @@ class ProblemControllerTest : ControllerTestSpec() { ReadProblemContentsUseCaseOutDetail(4L, "투명성") ) ) - Mockito.`when`(readProblemUseCase.execute(useCaseIn)) - .thenReturn(useCaseOut) + `when`(readProblemUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( @@ -160,11 +156,10 @@ class ProblemControllerTest : ControllerTestSpec() { val api = "CheckProblem" val uri = UriComponentsBuilder.newInstance() .path("$BASE_URL/{problemId}").build().toUriString() - - // set usecase mock val problemId = 1L val sub = "제출답" val body = objectMapper.writeValueAsString(CheckProblemRequest(sub = sub)) + val useCaseIn = CheckProblemUseCaseIn(problemId, sub = sub) val useCaseOut = CheckProblemUseCaseOut( explanation = "ETF는 일반적으로 낮은 운용 비용을 특징으로 합니다.이는 ETF가 보통 지수 추종(passive management) 방식으로 운용되기 때문입니다. 지수를 추종하는 전략은 액티브 매니지먼트(active management)에 비해 관리가 덜 복잡하고, 따라서 비용이 낮습니다.", diff --git a/api/src/test/kotlin/com/few/api/web/controller/subscription/SubscriptionControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/subscription/SubscriptionControllerTest.kt index 50b92bfe6..2f9a76928 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/subscription/SubscriptionControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/subscription/SubscriptionControllerTest.kt @@ -27,8 +27,8 @@ import org.springframework.web.util.UriComponentsBuilder class SubscriptionControllerTest : ControllerTestSpec() { companion object { - private val BASE_URL = "/api/v1/" - private val TAG = "WorkbookSubscriptionController" + private const val BASE_URL = "/api/v1/" + private const val TAG = "WorkbookSubscriptionController" } @Test @@ -37,6 +37,7 @@ class SubscriptionControllerTest : ControllerTestSpec() { fun browseSubscribeWorkbooks() { // given val api = "BrowseSubscribeWorkBooks" + val token = "thisisaccesstoken" val view = ViewCategory.MAIN_CARD val uri = UriComponentsBuilder.newInstance() .path("$BASE_URL/subscriptions/workbooks") @@ -44,7 +45,6 @@ class SubscriptionControllerTest : ControllerTestSpec() { .build() .toUriString() - // set usecase mock val memberId = 1L val useCaseIn = BrowseSubscribeWorkbooksUseCaseIn(memberId) val useCaseOut = BrowseSubscribeWorkbooksUseCaseOut( @@ -78,13 +78,12 @@ class SubscriptionControllerTest : ControllerTestSpec() { ) ) ) - doReturn(useCaseOut).`when`(browseSubscribeWorkbooksUseCase).execute(useCaseIn) // when mockMvc.perform( get(uri) - .header("Authorization", "Bearer thisisaccesstoken") + .header("Authorization", "Bearer $token") .contentType(MediaType.APPLICATION_JSON) ).andExpect(MockMvcResultMatchers.status().is2xxSuccessful) .andDo( @@ -140,13 +139,11 @@ class SubscriptionControllerTest : ControllerTestSpec() { fun subscribeWorkbook() { // given val api = "SubscribeWorkBook" + val token = "thisisaccesstoken" val uri = UriComponentsBuilder.newInstance() .path("$BASE_URL/workbooks/{workbookId}/subs") .build().toUriString() - val email = "test@gmail.com" - - // set usecase mock val memberId = 1L val workbookId = 1L val useCaseIn = SubscribeWorkbookUseCaseIn(workbookId = workbookId, memberId = memberId) @@ -155,7 +152,7 @@ class SubscriptionControllerTest : ControllerTestSpec() { // when mockMvc.perform( post(uri, workbookId) - .header("Authorization", "Bearer thisisaccesstoken") + .header("Authorization", "Bearer $token") .contentType(MediaType.APPLICATION_JSON) ).andExpect(MockMvcResultMatchers.status().is2xxSuccessful) .andDo( @@ -191,15 +188,15 @@ class SubscriptionControllerTest : ControllerTestSpec() { fun unsubscribeWorkbook() { // given val api = "UnsubscribeWorkBook" + val token = "thisisaccesstoken" val uri = UriComponentsBuilder.newInstance() .path("$BASE_URL/workbooks/{workbookId}/unsubs") .build() .toUriString() - // set usecase mock val workbookId = 1L val memberId = 1L - val opinion = "취소합니다." + val opinion = "cancel." val body = objectMapper.writeValueAsString( UnsubscribeWorkbookRequest(opinion = opinion) ) @@ -213,7 +210,7 @@ class SubscriptionControllerTest : ControllerTestSpec() { // when mockMvc.perform( post(uri, workbookId) - .header("Authorization", "Bearer thisisaccesstoken") + .header("Authorization", "Bearer $token") .contentType(MediaType.APPLICATION_JSON) .content(body) ).andExpect(MockMvcResultMatchers.status().is2xxSuccessful) @@ -250,17 +247,18 @@ class SubscriptionControllerTest : ControllerTestSpec() { fun deactivateAllSubscriptions() { // given val api = "UnsubscribeAll" + val token = "thisisaccesstoken" val uri = UriComponentsBuilder.newInstance() .path("$BASE_URL/subscriptions/unsubs") .build() .toUriString() - // set usecase mock - val memberId = 1L val opinion = "전체 수신거부합니다." val body = objectMapper.writeValueAsString( UnsubscribeWorkbookRequest(opinion = opinion) ) + + val memberId = 1L val useCaseIn = UnsubscribeAllUseCaseIn( memberId = memberId, opinion = opinion @@ -270,7 +268,7 @@ class SubscriptionControllerTest : ControllerTestSpec() { // when mockMvc.perform( post(uri) - .header("Authorization", "Bearer thisisaccesstoken") + .header("Authorization", "Bearer $token") .content(body) .contentType(MediaType.APPLICATION_JSON) ).andExpect(MockMvcResultMatchers.status().is2xxSuccessful) diff --git a/api/src/test/kotlin/com/few/api/web/controller/workbook/WorkBookControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/workbook/WorkBookControllerTest.kt index 1e0717f1c..d00278892 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/workbook/WorkBookControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/workbook/WorkBookControllerTest.kt @@ -27,8 +27,8 @@ import java.time.LocalDateTime class WorkBookControllerTest : ControllerTestSpec() { companion object { - private val BASE_URL = "/api/v1/workbooks" - private val TAG = "WorkBookController" + private const val BASE_URL = "/api/v1/workbooks" + private const val TAG = "WorkBookController" } @Test @@ -83,8 +83,8 @@ class WorkBookControllerTest : ControllerTestSpec() { fun browseWorkBooks() { // given val api = "BrowseWorkBooks" + val token = "thisisaccesstoken" val viewCategory = ViewCategory.MAIN_CARD - val memberId = 1L val uri = UriComponentsBuilder.newInstance() .path(BASE_URL) .queryParam("category", WorkBookCategory.ECONOMY.code) @@ -92,33 +92,35 @@ class WorkBookControllerTest : ControllerTestSpec() { .build() .toUriString() - // set usecase mock - `when`(tokenResolver.resolveId("thisisaccesstoken")).thenReturn(memberId) - `when`(browseWorkBooksUseCase.execute(BrowseWorkbooksUseCaseIn(WorkBookCategory.ECONOMY, viewCategory, memberId))).thenReturn( - BrowseWorkbooksUseCaseOut( - workbooks = listOf( - BrowseWorkBookDetail( - id = 1L, - mainImageUrl = URL("http://localhost:8080/api/v1/workbooks/1/image"), - title = "재태크, 투자 필수 용어 모음집", - description = "사회 초년생부터, 직장인, 은퇴자까지 모두가 알아야 할 기본적인 재태크, 투자 필수 용어 모음집 입니다.", - category = CategoryType.fromCode(0)!!.name, - createdAt = LocalDateTime.now(), - writerDetails = listOf( - WriterDetail(1L, "안나포", URL("http://localhost:8080/api/v1/users/1")), - WriterDetail(2L, "퓨퓨", URL("http://localhost:8080/api/v1/users/2")), - WriterDetail(3L, "프레소", URL("http://localhost:8080/api/v1/users/3")) - ), - subscriptionCount = 100 - ) + val memberId = 1L + `when`(tokenResolver.resolveId(token)).thenReturn(memberId) + + val useCaseIn = + BrowseWorkbooksUseCaseIn(WorkBookCategory.ECONOMY, viewCategory, memberId) + val useCaseOut = BrowseWorkbooksUseCaseOut( + workbooks = listOf( + BrowseWorkBookDetail( + id = 1L, + mainImageUrl = URL("http://localhost:8080/api/v1/workbooks/1/image"), + title = "재태크, 투자 필수 용어 모음집", + description = "사회 초년생부터, 직장인, 은퇴자까지 모두가 알아야 할 기본적인 재태크, 투자 필수 용어 모음집 입니다.", + category = CategoryType.fromCode(0)!!.name, + createdAt = LocalDateTime.now(), + writerDetails = listOf( + WriterDetail(1L, "안나포", URL("http://localhost:8080/api/v1/users/1")), + WriterDetail(2L, "퓨퓨", URL("http://localhost:8080/api/v1/users/2")), + WriterDetail(3L, "프레소", URL("http://localhost:8080/api/v1/users/3")) + ), + subscriptionCount = 100 ) ) ) + `when`(browseWorkBooksUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( get(uri) - .header("Authorization", "Bearer thisisaccesstoken") + .header("Authorization", "Bearer $token") .contentType(MediaType.APPLICATION_JSON) ).andExpect( status().is2xxSuccessful @@ -187,24 +189,30 @@ class WorkBookControllerTest : ControllerTestSpec() { .path("$BASE_URL/{workbookId}") .build() .toUriString() - // set usecase mock + val workbookId = 1L - `when`(readWorkbookUseCase.execute(ReadWorkbookUseCaseIn(workbookId))).thenReturn( - ReadWorkbookUseCaseOut( - id = 1L, - mainImageUrl = URL("http://localhost:8080/api/v1/workbooks/1/image"), - title = "재태크, 투자 필수 용어 모음집", - description = "사회 초년생부터, 직장인, 은퇴자까지 모두가 알아야 할 기본적인 재태크, 투자 필수 용어 모음집 입니다.", - category = CategoryType.fromCode(0)!!.name, - createdAt = LocalDateTime.now(), - writers = listOf( - WriterDetail(1L, "안나포", URL("http://localhost:8080/api/v1/users/1")), - WriterDetail(2L, "퓨퓨", URL("http://localhost:8080/api/v1/users/2")), - WriterDetail(3L, "프레소", URL("http://localhost:8080/api/v1/users/3")) - ), - articles = listOf(ArticleDetail(1L, "ISA(개인종합자산관리계좌)란?"), ArticleDetail(2L, "ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란?")) + val useCaseIn = ReadWorkbookUseCaseIn(workbookId) + val useCaseOut = ReadWorkbookUseCaseOut( + id = 1L, + mainImageUrl = URL("http://localhost:8080/api/v1/workbooks/1/image"), + title = "재태크, 투자 필수 용어 모음집", + description = "사회 초년생부터, 직장인, 은퇴자까지 모두가 알아야 할 기본적인 재태크, 투자 필수 용어 모음집 입니다.", + category = CategoryType.fromCode(0)!!.name, + createdAt = LocalDateTime.now(), + writers = listOf( + WriterDetail(1L, "안나포", URL("http://localhost:8080/api/v1/users/1")), + WriterDetail(2L, "퓨퓨", URL("http://localhost:8080/api/v1/users/2")), + WriterDetail(3L, "프레소", URL("http://localhost:8080/api/v1/users/3")) + ), + articles = listOf( + ArticleDetail(1L, "ISA(개인종합자산관리계좌)란?"), + ArticleDetail( + 2L, + "ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란? ISA(개인종합자산관리계좌)란?" + ) ) ) + `when`(readWorkbookUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( diff --git a/api/src/test/kotlin/com/few/api/web/controller/workbook/article/WorkBookArticleControllerTest.kt b/api/src/test/kotlin/com/few/api/web/controller/workbook/article/WorkBookArticleControllerTest.kt index 4289d7856..0ce3c75c2 100644 --- a/api/src/test/kotlin/com/few/api/web/controller/workbook/article/WorkBookArticleControllerTest.kt +++ b/api/src/test/kotlin/com/few/api/web/controller/workbook/article/WorkBookArticleControllerTest.kt @@ -26,8 +26,8 @@ import java.time.LocalDateTime class WorkBookArticleControllerTest : ControllerTestSpec() { companion object { - private val BASE_URL = "/api/v1/workbooks/{workbookId}/articles" - private val TAG = "WorkBookArticleController" + private const val BASE_URL = "/api/v1/workbooks/{workbookId}/articles" + private const val TAG = "WorkBookArticleController" } @Test @@ -36,36 +36,39 @@ class WorkBookArticleControllerTest : ControllerTestSpec() { fun readWorkBookArticle() { // given val api = "ReadWorkBookArticle" + val token = "thisisaccesstoken" val uri = UriComponentsBuilder.newInstance() .path("$BASE_URL/{articleId}") .build() .toUriString() - // set usecase mock + + val memberId = 1L + `when`(tokenResolver.resolveId(token)).thenReturn(memberId) + val workbookId = 1L val articleId = 1L - val memberId = 1L - `when`(tokenResolver.resolveId("thisisaccesstoken")).thenReturn(memberId) - `when`(readWorkBookArticleUseCase.execute(ReadWorkBookArticleUseCaseIn(workbookId, articleId, memberId = memberId))).thenReturn( - ReadWorkBookArticleOut( + val useCaseIn = + ReadWorkBookArticleUseCaseIn(workbookId, articleId, memberId = memberId) + val useCaseOut = ReadWorkBookArticleOut( + id = 1L, + writer = WriterDetail( id = 1L, - writer = WriterDetail( - id = 1L, - name = "안나포", - url = URL("http://localhost:8080/api/v1/writers/1") - ), - title = "ETF(상장 지수 펀드)란? 모르면 손해라고?", - content = "content", - problemIds = listOf(1L, 2L, 3L), - category = CategoryType.fromCode(0)!!.name, - createdAt = LocalDateTime.now(), - day = 1L - ) + name = "안나포", + url = URL("http://localhost:8080/api/v1/writers/1") + ), + title = "ETF(상장 지수 펀드)란? 모르면 손해라고?", + content = "content", + problemIds = listOf(1L, 2L, 3L), + category = CategoryType.fromCode(0)!!.name, + createdAt = LocalDateTime.now(), + day = 1L ) + `when`(readWorkBookArticleUseCase.execute(useCaseIn)).thenReturn(useCaseOut) // when mockMvc.perform( get(uri, workbookId, articleId) - .header("Authorization", "Bearer thisisaccesstoken") + .header("Authorization", "Bearer $token") ).andExpect(MockMvcResultMatchers.status().is2xxSuccessful) .andDo( MockMvcRestDocumentation.document(