From d81f200d63644b72354fced1b2abb72cb6322d2d Mon Sep 17 00:00:00 2001 From: Ji-Ha Date: Thu, 23 Jun 2022 17:07:57 +0900 Subject: [PATCH] =?UTF-8?q?[#132]=20refactor(share)=20-=20build.gradle(moc?= =?UTF-8?q?kK=20=EB=B2=84=EC=A0=84=20=EC=97=85=EA=B7=B8=EB=A0=88=EC=9D=B4?= =?UTF-8?q?=EB=93=9C)=20=EC=88=98=EC=A0=95=20-=20bookmarkInterface=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20-=20SharedBookmark=20constructor=20=EC=A1=B0?= =?UTF-8?q?=EC=A0=95=20-=20=EA=B3=B5=EC=9C=A0=20=EB=B3=B4=EA=B4=80?= =?UTF-8?q?=ED=95=A8=EC=9D=84=20=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20isS?= =?UTF-8?q?ameRootFolderId=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../domain/bookmark/entity/SharedBookmark.kt | 53 +++---- .../service/BookmarkInterfaceService.kt | 100 ------------- .../service/BookmarkInterfaceServiceTest.kt | 133 ------------------ .../service/BookmarkPageServiceTest.kt | 44 ------ 5 files changed, 21 insertions(+), 311 deletions(-) delete mode 100644 src/main/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkInterfaceService.kt delete mode 100644 src/test/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkInterfaceServiceTest.kt delete mode 100644 src/test/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkPageServiceTest.kt diff --git a/build.gradle.kts b/build.gradle.kts index 961156bb..c3dfcaf7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -71,7 +71,7 @@ dependencies { exclude(group = "org.mockito") } testImplementation("org.springframework.batch:spring-batch-test") - testImplementation("com.ninja-squad:springmockk:2.0.3") + testImplementation("com.ninja-squad:springmockk:3.0.1") testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc") } diff --git a/src/main/kotlin/com/yapp/web2/domain/bookmark/entity/SharedBookmark.kt b/src/main/kotlin/com/yapp/web2/domain/bookmark/entity/SharedBookmark.kt index fe3e5720..338b9455 100644 --- a/src/main/kotlin/com/yapp/web2/domain/bookmark/entity/SharedBookmark.kt +++ b/src/main/kotlin/com/yapp/web2/domain/bookmark/entity/SharedBookmark.kt @@ -1,40 +1,23 @@ package com.yapp.web2.domain.bookmark.entity - -import java.time.LocalDateTime -import javax.persistence.Id - -class SharedBookmark(var rootFolderId: Long) : BookmarkInterface { - @Id - override var id: String = "" - override var userId: Long? = null - override var link: String = "" - override var title: String = "" - override var description: String = "" - override var image: String? = null - - override var folderId: Long? = null - override var folderEmoji: String? = "" - override var folderName: String = "" - override var clickCount: Int = 0 - - override var deleteTime: LocalDateTime? = null - override var deleted: Boolean = false - - override var saveTime: LocalDateTime = LocalDateTime.now() - var remindAccountList = mutableListOf() - - constructor(bookmark: BookmarkInterface, rootFolderId: Long): this(rootFolderId) { - this.userId = bookmark.userId - this.link = bookmark.link - this.title = bookmark.title - this.description = bookmark.description - this.image = bookmark.image - this.folderId = bookmark.folderId - this.folderEmoji = bookmark.folderEmoji - this.folderName = bookmark.folderName +import com.yapp.web2.domain.account.entity.Account + +class SharedBookmark : BookmarkInterface { + var remindAccountList = mutableListOf() + var rootFolderId: Long? = -1 + + constructor( + account: Account, + link: String, + title: String, + image: String?, + description: String, + rootFolderId: Long? + ) : super(account, link, title, image, description) { + this.rootFolderId = rootFolderId } + constructor() override fun moveFolder(nextFolderId: Long) { this.folderId = nextFolderId @@ -51,4 +34,8 @@ class SharedBookmark(var rootFolderId: Long) : BookmarkInterface { override fun remindOff() { throw RuntimeException() } + + fun isSameRootFolderId(folderId: Long?): Boolean { + return this.rootFolderId == folderId + } } \ No newline at end of file diff --git a/src/main/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkInterfaceService.kt b/src/main/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkInterfaceService.kt deleted file mode 100644 index 7e954c79..00000000 --- a/src/main/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkInterfaceService.kt +++ /dev/null @@ -1,100 +0,0 @@ -package com.yapp.web2.domain.bookmark.service - -import com.yapp.web2.domain.bookmark.entity.* -import com.yapp.web2.domain.bookmark.repository.BookmarkInterfaceRepository -import com.yapp.web2.domain.folder.entity.Folder -import com.yapp.web2.domain.folder.repository.FolderRepository -import com.yapp.web2.exception.ObjectNotFoundException -import com.yapp.web2.exception.custom.BookmarkNotFoundException -import com.yapp.web2.security.jwt.JwtProvider -import org.springframework.stereotype.Service -import org.springframework.transaction.annotation.Transactional - -@Service -class BookmarkInterfaceService( - private val bookmarkInterfaceRepository: BookmarkInterfaceRepository, - private val folderRepository: FolderRepository, - private val jwtProvider: JwtProvider -) { - fun moveBookmarkList(moveBookmarkDto: BookmarkDto.MoveBookmarkDto) { - val bookmarkIdList = moveBookmarkDto.bookmarkIdList - for (bookmarkId in bookmarkIdList) - moveBookmark(bookmarkId, moveBookmarkDto) - } - - fun moveBookmark(bookmarkId: String, moveBookmarkDto: BookmarkDto.MoveBookmarkDto) { - val bookmark = getBookmarkIfPresent(bookmarkId) - val nextFolder = checkFolderAbsence(moveBookmarkDto.nextFolderId) - - bookmark.folderId?.let { - if(isSameFolder(it, moveBookmarkDto.nextFolderId)) return - val beforeFolder = checkFolderAbsence(it) - updateBookmarkCountByFolderId(beforeFolder, -1) - } - - bookmark.changeFolderInfo(nextFolder) - updateBookmarkCountByFolderId(nextFolder, 1) - - bookmarkInterfaceRepository.save(bookmark) - } - - @Transactional - protected fun updateBookmarkCountByFolderId(folder: Folder, count: Int) { - folder.bookmarkCount += count - } - - private fun isSameFolder(prevFolderId: Long, nextFolderId: Long) = prevFolderId == nextFolderId - - private fun getBookmarkIfPresent(bookmarkId: String): BookmarkInterface { - return bookmarkInterfaceRepository.findBookmarkInterfaceById(bookmarkId) - ?: throw BookmarkNotFoundException() - } - private fun checkFolderAbsence(folderId: Long): Folder { - return when (val folder = folderRepository.findFolderById(folderId)) { - null -> throw ObjectNotFoundException() - else -> folder - } - } - - fun remindToggleOn(token: String, bookmarkId: String) { - val account = jwtProvider.getAccountFromToken(token) - val bookmark = getBookmarkIfPresent(bookmarkId) - - if(bookmark is SharedBookmark) { - bookmark.remindOn(account.id!!) - val personalBookmark = BookmarkUtils.sharedBookmarkToPersonalBookmark(bookmark, account) - personalBookmark.remindOn(account.remindCycle.toLong()) - bookmarkInterfaceRepository.save(personalBookmark) - } - - if(bookmark is PersonalBookmark) - bookmark.remindOn(account.remindCycle.toLong()) - } - - fun remindToggleOff(token: String, bookmarkId: String) { - val account = jwtProvider.getAccountFromToken(token) - val bookmark = getBookmarkIfPresent(bookmarkId) - - if(bookmark is SharedBookmark) { - bookmark.remindOff(account.id!!) - - val bookmarkList = bookmarkInterfaceRepository.findBookmarkInterfacesByUserId(account.id!!) - val deleteBookmarkList = getDeleteBookmarkList(bookmarkList, bookmarkId) - - bookmarkInterfaceRepository.deleteAll(deleteBookmarkList) - } - - if(bookmark is PersonalBookmark) - bookmark.remindOff() - } - - private fun getDeleteBookmarkList(bookmarkList: List, remindBookmarkId: String): List { - val deleteBookmarkList = mutableListOf() - - for (bm in bookmarkList) { - if(bm is PersonalBookmark && bm.parentBookmarkId == remindBookmarkId) - deleteBookmarkList.add(bm) - } - return deleteBookmarkList - } -} \ No newline at end of file diff --git a/src/test/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkInterfaceServiceTest.kt b/src/test/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkInterfaceServiceTest.kt deleted file mode 100644 index eab2a549..00000000 --- a/src/test/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkInterfaceServiceTest.kt +++ /dev/null @@ -1,133 +0,0 @@ -package com.yapp.web2.domain.bookmark.service - -import com.yapp.web2.domain.account.entity.Account -import com.yapp.web2.domain.bookmark.entity.BookmarkDto -import com.yapp.web2.domain.bookmark.entity.PersonalBookmark -import com.yapp.web2.domain.bookmark.entity.SharedBookmark -import com.yapp.web2.domain.bookmark.repository.BookmarkInterfaceRepository -import com.yapp.web2.domain.folder.entity.Folder -import com.yapp.web2.domain.folder.repository.FolderRepository -import com.yapp.web2.security.jwt.JwtProvider -import io.mockk.MockKAnnotations -import io.mockk.every -import io.mockk.impl.annotations.MockK -import org.junit.jupiter.api.Assertions.* -import org.junit.jupiter.api.BeforeEach -import org.junit.jupiter.api.Nested -import org.junit.jupiter.api.Test - -internal class BookmarkInterfaceServiceTest { - @MockK - private lateinit var bookmarkInterfaceRepository: BookmarkInterfaceRepository - - @MockK - private lateinit var jwtProvider: JwtProvider - - @MockK - private lateinit var folderRepository: FolderRepository - - private lateinit var bookmarkInterfaceService: BookmarkInterfaceService - - @BeforeEach - internal fun init() { - MockKAnnotations.init(this) - bookmarkInterfaceService = BookmarkInterfaceService(bookmarkInterfaceRepository, folderRepository, jwtProvider) - } - - @Nested - inner class SaveBookmark { - val testAccount = Account("test@email.com") - val testDto = - BookmarkDto.AddBookmarkDto("testLink.test.com", "testTitle", false, "testImage", "testDescription") - val testToken = "testToken" - val testFolder = Folder("testName", 0, 0, null) - var testFolderId: Long? = 0L - val testFolderEmoji = "testEmoji" - - @Test - fun `모든 도토리에 추가되는 개인북마크를 저장한다`() { - // given - testAccount.id = 0L - every { jwtProvider.getAccountFromToken(testToken) } returns testAccount - every { bookmarkInterfaceRepository.save(any()) } returns PersonalBookmark( - testAccount, - testDto.link, - testDto.title, - testDto.image, - testDto.description, - null, - null, - testDto.remind - ) - testFolderId = null - - // when - val actualBookmark = bookmarkInterfaceService.addBookmark(testToken, testFolderId, testDto) - - // then - assertEquals(PersonalBookmark::class, actualBookmark::class) - } - - @Test - fun `공유 북마크를 저장한다`() { - // given - testAccount.id = 0L - testFolder.share = true - every { jwtProvider.getAccountFromToken(testToken) } returns testAccount - every { folderRepository.findFolderById(any()) } returns testFolder - every { bookmarkInterfaceRepository.save(any()) } returns SharedBookmark( - testAccount, - testDto.link, - testDto.title, - testDto.image, - testDto.description, - null, - null, - testDto.remind - ) - - // when - val actualBookmark = bookmarkInterfaceService.addBookmark(testToken, testFolderId, testDto) - - // then - assertEquals(SharedBookmark::class, actualBookmark::class) - } - - @Test - fun `개인 북마크를 저장한다`() { - //given - testAccount.id = 0L - testFolder.share = false - every { jwtProvider.getAccountFromToken(testToken) } returns testAccount - every { folderRepository.findFolderById(any()) } returns testFolder - every { bookmarkInterfaceRepository.save(any()) } returns PersonalBookmark( - testAccount, - testDto.link, - testDto.title, - testDto.image, - testDto.description, - null, - null, - testDto.remind - ) - - // when - val actualBookmark = bookmarkInterfaceService.addBookmark(testToken, testFolderId, testDto) - - // then - assertEquals(PersonalBookmark::class, actualBookmark::class) - } - - @Test - fun `저장할 폴더가 없을 때는 예외를 던진다`() { - // given - testAccount.id = 0L - every { jwtProvider.getAccountFromToken(testToken) } returns testAccount - - // when & then - assertThrows(RuntimeException::class.java) { - bookmarkInterfaceService.addBookmark(testToken, testFolderId, testDto) - } - } - } -} \ No newline at end of file diff --git a/src/test/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkPageServiceTest.kt b/src/test/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkPageServiceTest.kt deleted file mode 100644 index 63aa8a0f..00000000 --- a/src/test/kotlin/com/yapp/web2/domain/bookmark/service/BookmarkPageServiceTest.kt +++ /dev/null @@ -1,44 +0,0 @@ -package com.yapp.web2.domain.bookmark.service - -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 io.mockk.impl.annotations.MockK -import org.junit.jupiter.api.Assertions.* -import org.springframework.data.domain.PageRequest -import java.util.* - -internal class BookmarkPageServiceTest { - @MockK - private lateinit var bookmarkRepository: BookmarkRepository - - @MockK - private lateinit var folderRepository: FolderRepository - - private lateinit var bookmarkPageService: BookmarkPageService - - private lateinit var testFolder: Folder - private lateinit var testPageRequest: PageRequest - private var testFolderId: Long = 1 - -// @BeforeEach -// internal fun init() { -// MockKAnnotations.init(this) -// bookmarkPageService = BookmarkPageService(bookmarkRepository, folderRepository) -// testPageRequest = PageRequest.of(1, 3, Sort.by("saveTime").descending()) -// testFolder = Folder("Folder", 0, parentFolder = null) -// } -// -// @Test -// fun `폴더가 존재하지 않으면, 예외를 던진다`() { -// // given -// val expectException = ObjectNotFoundException("해당 폴더가 존재하지 않습니다.") -// every { folderRepository.findById(testFolderId) } returns Optional.empty() -// -// // when -// val actualException = assertThrows(ObjectNotFoundException::class.java) { bookmarkPageService.getAllPageByFolderId(testFolderId, testPageRequest, true) } -// -// //then -// assertEquals(expectException.message, actualException.message) -// } -} \ No newline at end of file