Skip to content

Commit

Permalink
refactor: 테스트 형식 통일되도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Aug 8, 2024
1 parent dc7f085 commit d069f02
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -83,76 +83,54 @@ class AdminControllerTest : ControllerTestSpec() {
// given
val api = "AddArticle"
val uri = UriComponentsBuilder.newInstance().path("$BASE_URL/articles").build().toUriString()
val request = AddArticleRequest(
"[email protected]",
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 = "[email protected]"
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(
"[email protected]",
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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

/**
Expand All @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Loading

0 comments on commit d069f02

Please sign in to comment.