-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7f4ec4
commit c3f11b2
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
api/src/test/kotlin/com/few/api/web/controller/image/ImageControllerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.few.api.web.controller.image | ||
|
||
import com.epages.restdocs.apispec.ResourceDocumentation.resource | ||
import com.epages.restdocs.apispec.ResourceSnippetParameters | ||
import com.epages.restdocs.apispec.Schema | ||
import com.few.api.domain.image.dto.PutImageUseCaseIn | ||
import com.few.api.domain.image.dto.PutImageUseCaseOut | ||
import com.few.api.domain.image.usecase.PutImageUseCase | ||
import com.few.api.web.controller.ControllerTestSpec | ||
import com.few.api.web.controller.description.Description | ||
import com.few.api.web.controller.helper.* | ||
import com.few.api.web.controller.image.request.ImageSourceRequest | ||
import com.few.api.web.controller.image.response.ImageSourceResponse | ||
import org.junit.jupiter.api.DisplayName | ||
import org.junit.jupiter.api.Test | ||
import org.mockito.Mockito.`when` | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.http.MediaType | ||
import org.springframework.mock.web.MockMultipartFile | ||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document | ||
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.multipart | ||
import org.springframework.restdocs.payload.PayloadDocumentation | ||
import org.springframework.test.web.servlet.MockMvc | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status | ||
import java.net.URL | ||
|
||
class ImageControllerTest : ControllerTestSpec() { | ||
@Autowired | ||
private lateinit var mockMvc: MockMvc | ||
|
||
@MockBean | ||
private lateinit var putImageUseCase: PutImageUseCase | ||
|
||
companion object { | ||
private val BASE_URL = "/api/v1/images" | ||
private val TAG = "ImageController" | ||
} | ||
|
||
@Test | ||
@DisplayName("[POST] /api/v1/images") | ||
fun putImage() { | ||
// given | ||
val api = "PutImage" | ||
val request = ImageSourceRequest(source = MockMultipartFile("source", "test.jpg", "image/jpeg", "test".toByteArray())) | ||
val response = ImageSourceResponse(URL("http://localhost:8080/test.jpg")) | ||
val useCaseOut = PutImageUseCaseOut(response.url) | ||
val useCaseIn = PutImageUseCaseIn(request.source) | ||
`when`(putImageUseCase.execute(useCaseIn)).thenReturn(useCaseOut) | ||
|
||
// when | ||
mockMvc.perform( | ||
multipart(BASE_URL) | ||
.file(request.source as MockMultipartFile) | ||
.contentType(MediaType.MULTIPART_FORM_DATA_VALUE) | ||
) | ||
.andExpect(status().isOk) | ||
.andDo( | ||
document( | ||
api.toIdentifier(), | ||
resource( | ||
ResourceSnippetParameters.builder() | ||
.summary(api.toIdentifier()) | ||
.description("이미지 업로드") | ||
.tag(TAG) | ||
.requestSchema(Schema.schema(api.toRequestSchema())) | ||
.responseSchema(Schema.schema(api.toResponseSchema())).responseFields( | ||
*Description.describe( | ||
arrayOf( | ||
PayloadDocumentation.fieldWithPath("data") | ||
.fieldWithObject("data"), | ||
PayloadDocumentation.fieldWithPath("data.url") | ||
.fieldWithString( | ||
"이미지 URL" | ||
) | ||
) | ||
) | ||
).build() | ||
) | ||
) | ||
|
||
) | ||
} | ||
} |