Skip to content

Commit

Permalink
Merge pull request #139 from YAPP-19th/feature/#9-폴더
Browse files Browse the repository at this point in the history
[#9] 리팩토링 및 테스트 코드 추가
  • Loading branch information
JuHyun419 authored Jun 15, 2022
2 parents be23374 + 494fa0d commit bee3e01
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 10 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ fun excludedClassFilesForReport(classDirectories: ConfigurableFileCollection) {
"**/exception/**",
"**/config/**",
"**/Bookmarkers*",
"**/BaseTimeEntity*"
"**/BaseTimeEntity*",
"**/infra/**"
)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class FolderController(
return ResponseEntity.status(HttpStatus.OK).body(folderService.findAllParentFolderList(folderId))
}

@ApiOperation(value = "암호화된 폴더 ID 조회 API")
@GetMapping("encrypt/{folderId}")
fun getEncryptFolderId(@PathVariable folderId: Long): ResponseEntity<FolderTokenDto> {
return ResponseEntity.status(HttpStatus.OK).body(folderService.encryptFolderId(folderId))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.yapp.web2.domain.folder.repository.FolderRepository
import com.yapp.web2.domain.folder.service.move.factory.FolderMoveFactory
import com.yapp.web2.domain.folder.service.move.inner.FolderMoveInnerStrategy
import com.yapp.web2.domain.folder.service.move.inner.FolderMoveWithEqualParentOrTopFolder
import com.yapp.web2.exception.ObjectNotFoundException
import com.yapp.web2.exception.custom.AccountNotFoundException
import com.yapp.web2.exception.custom.FolderNotFoundException
import com.yapp.web2.security.jwt.JwtProvider
Expand Down Expand Up @@ -191,8 +190,7 @@ class FolderService(

@Transactional(readOnly = true)
fun findByFolderId(folderId: Long): Folder {
return folderRepository.findById(folderId)
.orElseThrow { folderNotFoundException }
return folderRepository.findById(folderId).orElseThrow { folderNotFoundException }
}

fun deleteFolder(folder: Folder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.yapp.web2.domain.ControllerTestUtil
import com.yapp.web2.domain.folder.entity.Folder
import com.yapp.web2.domain.folder.service.FolderService
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.util.FolderTokenDto
import com.yapp.web2.util.Message
import io.mockk.Runs
import io.mockk.every
Expand Down Expand Up @@ -219,4 +220,20 @@ internal class FolderControllerTest {
.andExpect(jsonPath("$[1].emoji").value("emoji2"))
.andExpect(jsonPath("$[1].name").value("parent2"))
}

@Test
fun `암호화된 폴더 ID를 조회한다`() {
// given
val expected = "AES256token"
every { folderService.encryptFolderId(any()) } returns FolderTokenDto(expected)

// when
val resultAction = util.getResultAction("/api/v1/folder/encrypt/1", mockMvc)

// then
resultAction
.andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk)
.andExpect(jsonPath("$.folderIdToken").value(expected))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.yapp.web2.domain.bookmark.entity.Bookmark
import com.yapp.web2.domain.bookmark.repository.BookmarkRepository
import com.yapp.web2.domain.folder.entity.Folder
import com.yapp.web2.domain.folder.repository.FolderRepository
import com.yapp.web2.exception.custom.AccountNotFoundException
import com.yapp.web2.exception.custom.FolderNotFoundException
import com.yapp.web2.security.jwt.JwtProvider
import com.yapp.web2.util.AES256Util
Expand All @@ -26,6 +27,7 @@ import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.data.repository.findByIdOrNull
import java.util.Optional
import javax.swing.text.html.Option

@ExtendWith(MockKExtension::class)
internal open class FolderServiceTest {
Expand Down Expand Up @@ -78,6 +80,17 @@ internal open class FolderServiceTest {
verify(exactly = 1) { folderRepository.save(any()) }
}

@Test
fun `폴더 생성 시 account가 존재하지 않으면 예외가 발생한다`() {
// given
val request = Folder.FolderCreateRequest(name = "Root Folder")
every { jwtProvider.getIdFromToken(any()) } returns 1L
every { accountRepository.findById(any()) } returns Optional.ofNullable(null)

// then
assertThrows<AccountNotFoundException> { folderService.createFolder(request, "test") }
}

@Test
fun `부모 폴더가 존재하지 않는 최상위 폴더를 생성한다`() {
// given
Expand Down Expand Up @@ -148,16 +161,136 @@ internal open class FolderServiceTest {
)
}

// TODO: 2022/04/03
@Test
fun `폴더를 드래그 앤 드랍 이동한다`() {
fun `0번째 보관함에서 3번째 보관함으로 폴더를 드래그 앤 드랍으로 이동한다`() {
// given
val moveFolder = Folder("Move Folder", 0, parentFolder = null)
val request = Folder.FolderMoveRequest("root", 2)
val topFolderList = getParentFolderList()

every { jwtProvider.getIdFromToken(any()) } returns 1L
every { accountRepository.findById(any()).get() } returns user
every { folderRepository.findById(any()).get() } returns moveFolder
every { folderRepository.findAllByParentFolderIsNull(any()) } returns topFolderList

// when
folderService.moveFolderByDragAndDrop(1L, request, "token")

// then : topFolderList Index - 1,3,4 -> 0,3,4
assertAll(
{ assertThat(moveFolder.index).isEqualTo(2) },
{ assertThat(topFolderList[0].index).isEqualTo(0)},
{ assertThat(topFolderList[1].index).isEqualTo(3) },
{ assertThat(topFolderList[2].index).isEqualTo(4) }
)
}

@Test
fun `3번째 보관함에서 0번째 보관함으로 폴더를 드래그 앤 드랍으로 이동한다`() {
// given
val moveFolder = Folder("Move Folder", 2, parentFolder = null)
val request = Folder.FolderMoveRequest("root", 0)
val topFolderList = getParentFolderList()

every { jwtProvider.getIdFromToken(any()) } returns 1L
every { accountRepository.findById(any()).get() } returns user
every { folderRepository.findById(any()).get() } returns moveFolder
every { folderRepository.findAllByParentFolderIsNull(any()) } returns topFolderList

// when
folderService.moveFolderByDragAndDrop(1L, request, "token")

// then : topFolderList Index - 1,3,4 -> 2,3,4
assertAll(
{ assertThat(moveFolder.index).isEqualTo(0) },
{ assertThat(topFolderList[0].index).isEqualTo(2)},
{ assertThat(topFolderList[1].index).isEqualTo(3) },
{ assertThat(topFolderList[2].index).isEqualTo(4) }
)
}

@Test
fun `이동하기 전 폴더가 존재하지 않을 경우 예외가 발생한다`() {
// given
val request = Folder.FolderMoveButtonRequest(mutableListOf(1L), 2L)
every { jwtProvider.getIdFromToken(any()) } returns 1L
every { accountRepository.findById(any()).get() } returns user
every { folderRepository.findById(any()) } returns Optional.ofNullable(null)

// then
assertThrows<FolderNotFoundException> { folderService.moveFolderByButton("token", request) }
}

@Test
fun `이동 하려는 폴더에 대한 정보가 존재하지 않을 경우 예외가 발생한다`() {
// given
val request = Folder.FolderMoveButtonRequest(mutableListOf(1L), 2L)
every { jwtProvider.getIdFromToken(any()) } returns 1L
every { accountRepository.findById(any()).get() } returns user
every { folderRepository.findById(1L) } returns Optional.of(folder) // beforeFolder
every { folderRepository.findAllByParentFolderIsNull(any()) } returns mutableListOf()
every { folderRepository.findById(request.nextFolderId) } returns Optional.ofNullable(null) // nextFolder

// then
assertThrows<FolderNotFoundException> { folderService.moveFolderByButton("token", request) }
}

@Test
fun `버튼을 클릭하여 0번째 폴더를 2번째 폴더로 이동한다`() {
// given
val beforeFolder = Folder("Before Folder", 0, parentFolder = null)
val nextFolder = Folder("Next Folder", 2, parentFolder = null)
beforeFolder.id = 1L
nextFolder.id = 2L
val beforeChildren = getParentFolderList()
val request = Folder.FolderMoveButtonRequest(mutableListOf(1L), 2L)

every { jwtProvider.getIdFromToken(any()) } returns 1L
every { accountRepository.findById(any()).get() } returns user
every { folderRepository.findById(1L) } returns Optional.of(beforeFolder)
every { folderRepository.findById(2L) } returns Optional.of(nextFolder)
every { folderRepository.findAllByParentFolderIsNull(any()) } returns beforeChildren
every { folderRepository.save(any()) } returns folder

// when
folderService.moveFolderByButton("token", request)

// then : 이동 전 폴더 리스트에서 자신보다 index가 큰 폴더들의 index가 1씩 감소한다
assertAll(
{ assertThat(beforeChildren[0].index).isEqualTo(0)},
{ assertThat(beforeChildren[1].index).isEqualTo(2) },
{ assertThat(beforeChildren[2].index).isEqualTo(3) }
)
}

private fun getParentFolderList(): MutableList<Folder> {
return mutableListOf(
Folder("Folder1", 1, parentFolder = null),
Folder("Folder2", 3, parentFolder = null),
Folder("Folder3", 4, parentFolder = null)
)
}

@Test
fun `folderId로 폴더를 조회한다`() {
// given
folder.id = 1L
every { folderRepository.findById(1L) } returns Optional.of(folder)

// when
val actual = folderService.findByFolderId(1L)

// then
assertThat(actual).isEqualTo(folder)
}

// TODO: 2022/04/03
@Test
fun `버튼 클릭에 의해 폴더가 이동된다`() {
fun `폴더 조회 시 존재하지 않는 folderId일 경우 예외를 반환한다`() {
// given
every { folderRepository.findById(any()) } returns Optional.ofNullable(null)

// then
assertThrows<FolderNotFoundException> { folderService.findByFolderId(1L) }
}

@Test
Expand All @@ -184,6 +317,25 @@ internal open class FolderServiceTest {
assertThrows<FolderNotFoundException> { folderService.changeFolder(1L, changeRequest) }
}

@Test
fun `부모 폴더와 자식 폴더들을 전부 삭제한다`() {
// given
val parentFolder: Folder = getParentFolder("parent")
parentFolder.id = 1L
val childFolders: MutableList<Folder> = getChildFolders(parentFolder, 1, 5)
val bookmark = makeBookmarks()

every { bookmarkRepository.findByFolderId(any()) } returns bookmark
every { folderRepository.deleteByFolder(any()) } just Runs
every { bookmarkRepository.save(any()) } returns Bookmark(1L, 1L, "test")

// when
folderService.deleteFolderRecursive(parentFolder)

// then
verify(exactly = childFolders.size + 1) { folderRepository.deleteByFolder(any()) }
}

@Test
fun `전체 폴더를 조회하고 출력한다`() {
// given
Expand All @@ -207,13 +359,28 @@ internal open class FolderServiceTest {
printJson(actual)
}

@Test
fun `폴더 id를 정상적으로 암호화한다`() {
// given
val expected = "YanblGzXpM13KWrqVqhMYA=="
folder.id = 1L
every { folderRepository.findFolderById(any()) } returns folder
every { aeS256Util.encrypt(any()) } returns expected

// when
val actual = folderService.encryptFolderId(folder.id!!)

// then
assertThat(actual.folderIdToken).isEqualTo(expected)
}

@Test
fun `폴더 id를 암호화할 때, 폴더 id가 존재하지 않으면 예외를 던진다`() {
// given
folder.id = 1
folder.id = 1L
every { folderRepository.findFolderById(folder.id!!) } returns null

// then &
// then
assertThrows<FolderNotFoundException> { folderService.encryptFolderId(folder.id!!) }
}

Expand Down

0 comments on commit bee3e01

Please sign in to comment.