diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt index cca480ee..7948a969 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiService.kt @@ -8,6 +8,14 @@ import com.goliath.emojihub.springboot.domain.reaction.dao.ReactionDao import com.goliath.emojihub.springboot.domain.user.dao.UserDao import com.goliath.emojihub.springboot.global.exception.CustomHttp400 import com.goliath.emojihub.springboot.global.exception.CustomHttp403 +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.INDEX_OUT_OF_BOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.COUNT_OUT_OF_BOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_CREATED +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_SAVED +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_UNSAVED +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.EMOJI_DELETE_FORBIDDEN +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.EMOJI_NOT_FOUND import com.goliath.emojihub.springboot.global.util.getDateTimeNow import org.springframework.stereotype.Service import org.springframework.web.multipart.MultipartFile @@ -29,18 +37,18 @@ class EmojiService( fun getEmojis(sortByDate: Int, index: Int, count: Int): List { // index는 양의 정수여야 함 - if (index <= 0) throw CustomHttp400("Index should be positive integer.") + if (index <= 0) throw CustomHttp400(INDEX_OUT_OF_BOUND) // count는 0보다 커야 함 - if (count <= 0) throw CustomHttp400("Count should be positive integer.") + if (count <= 0) throw CustomHttp400(COUNT_OUT_OF_BOUND) return emojiDao.getEmojis(sortByDate, index, count) } fun getMyEmojis(username: String, field: String, index: Int, count: Int): List { // index는 양의 정수여야 함 - if (index <= 0) throw CustomHttp400("Index should be positive integer.") + if (index <= 0) throw CustomHttp400(INDEX_OUT_OF_BOUND) // count는 0보다 커야 함 - if (count <= 0) throw CustomHttp400("Count should be positive integer.") - val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.") + if (count <= 0) throw CustomHttp400(COUNT_OUT_OF_BOUND) + val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND) val emojiIdList = if (field == CREATED_EMOJIS) { user.created_emojis } else { @@ -66,7 +74,7 @@ class EmojiService( } fun getEmoji(emojiId: String): EmojiDto? { - if (emojiDao.existsEmoji(emojiId).not()) throw CustomHttp404("Emoji doesn't exist.") + if (emojiDao.existsEmoji(emojiId).not()) throw CustomHttp404(EMOJI_NOT_FOUND) return emojiDao.getEmoji(emojiId) } @@ -84,31 +92,31 @@ class EmojiService( fun saveEmoji(username: String, emojiId: String) { if (emojiDao.existsEmoji(emojiId).not()) - throw CustomHttp404("Emoji doesn't exist.") - val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.") + throw CustomHttp404(EMOJI_NOT_FOUND) + val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND) if (user.created_emojis?.contains(emojiId) == true) - throw CustomHttp403("User created this emoji.") + throw CustomHttp403(USER_CREATED) if (user.saved_emojis?.contains(emojiId) == true) - throw CustomHttp403("User already saved this emoji.") + throw CustomHttp403(USER_ALREADY_SAVED) emojiDao.numSavedChange(emojiId, 1) userDao.insertId(username, emojiId, SAVED_EMOJIS) } fun unSaveEmoji(username: String, emojiId: String) { if (emojiDao.existsEmoji(emojiId).not()) - throw CustomHttp404("Emoji doesn't exist.") - val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.") + throw CustomHttp404(EMOJI_NOT_FOUND) + val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND) if (user.created_emojis?.contains(emojiId) == true) - throw CustomHttp403("User created this emoji.") + throw CustomHttp403(USER_CREATED) if (user.saved_emojis == null || !user.saved_emojis!!.contains(emojiId)) - throw CustomHttp403("User already unsaved this emoji.") + throw CustomHttp403(USER_ALREADY_UNSAVED) emojiDao.numSavedChange(emojiId, -1) userDao.deleteId(username, emojiId, SAVED_EMOJIS) } fun deleteEmoji(username: String, emojiId: String) { - val emoji = emojiDao.getEmoji(emojiId) ?: throw CustomHttp404("Emoji doesn't exist.") - if (username != emoji.created_by) throw CustomHttp403("You can't delete this emoji.") + val emoji = emojiDao.getEmoji(emojiId) ?: throw CustomHttp404(EMOJI_NOT_FOUND) + if (username != emoji.created_by) throw CustomHttp403(EMOJI_DELETE_FORBIDDEN) // delete file and thumbnail in DB val fileBlobName = username + "_" + emoji.created_at + ".mp4" val thumbnailBlobName = username + "_" + emoji.created_at + ".jpeg" @@ -117,7 +125,7 @@ class EmojiService( // delete all reactions(and reaction id in posts) using this emoji val reactions = reactionDao.getReactionsWithField(emojiId, EMOJI_ID) for (reaction in reactions) { - postDao.deleteReactionId(reaction.post_id, reaction.id) + postDao.deleteReaction(reaction.post_id, reaction.id) reactionDao.deleteReaction(reaction.id) } // delete all saved_emoji ids in users diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/dao/PostDao.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/dao/PostDao.kt index e83fb19f..a3181c5e 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/dao/PostDao.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/dao/PostDao.kt @@ -1,6 +1,7 @@ package com.goliath.emojihub.springboot.domain.post.dao import com.goliath.emojihub.springboot.domain.post.dto.PostDto +import com.goliath.emojihub.springboot.domain.post.dto.ReactionWithEmojiUnicode import com.goliath.emojihub.springboot.global.util.getDateTimeNow import com.google.cloud.firestore.* import lombok.extern.slf4j.Slf4j @@ -33,7 +34,8 @@ class PostDao( // 정렬 val postQuery = db.collection(POST_COLLECTION_NAME).orderBy(CREATED_AT, Query.Direction.DESCENDING) // 페이지네이션 - val documents: List = postQuery.offset((index - 1) * count).limit(count).get().get().documents + val documents: List = + postQuery.offset((index - 1) * count).limit(count).get().get().documents for (document in documents) { list.add(document.toObject(PostDto::class.java)) } @@ -45,7 +47,8 @@ class PostDao( val postsRef = db.collection(POST_COLLECTION_NAME) val postQuery = postsRef.whereEqualTo("created_by", username) .orderBy(CREATED_AT, Query.Direction.DESCENDING) - val documents: List = postQuery.offset((index - 1) * count).limit(count).get().get().documents + val documents: List = + postQuery.offset((index - 1) * count).limit(count).get().get().documents for (document in documents) { list.add(document.toObject(PostDto::class.java)) } @@ -75,13 +78,23 @@ class PostDao( db.collection(POST_COLLECTION_NAME).document(postId).delete() } - fun insertReactionId(postId: String, reactionId: String) { + fun insertReaction(postId: String, reactionId: String, emojiUnicode: String) { val postRef = db.collection(POST_COLLECTION_NAME).document(postId) - postRef.update(REACTIONS, FieldValue.arrayUnion(reactionId)) + val reactionWithEmojiUnicode = ReactionWithEmojiUnicode( + id = reactionId, + emoji_unicode = emojiUnicode + ) + postRef.update(REACTIONS, FieldValue.arrayUnion(reactionWithEmojiUnicode)) } - fun deleteReactionId(postId: String, reactionId: String) { + fun deleteReaction(postId: String, reactionId: String) { val postRef = db.collection(POST_COLLECTION_NAME).document(postId) - postRef.update(REACTIONS, FieldValue.arrayRemove(reactionId)) + val post = postRef.get().get().toObject(PostDto::class.java) + for (reaction in post!!.reactions) { + if (reaction.id == reactionId) { + postRef.update(REACTIONS, FieldValue.arrayRemove(reaction)) + return + } + } } } \ No newline at end of file diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/dto/PostDto.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/dto/PostDto.kt index b875d377..0148b91e 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/dto/PostDto.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/dto/PostDto.kt @@ -8,7 +8,7 @@ data class PostDto( var content: String = "", var created_at: String = "", var modified_at: String = "", - var reactions: MutableList? = mutableListOf() + var reactions: MutableList = mutableListOf(), ) { constructor(username: String, content: String, dateTime: String) : this() { val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" @@ -22,4 +22,9 @@ data class PostDto( created_at = dateTime modified_at = dateTime } -} \ No newline at end of file +} + +data class ReactionWithEmojiUnicode( + var id: String = "", + var emoji_unicode: String = "" +) \ No newline at end of file diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/service/PostService.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/service/PostService.kt index a716e4fc..d4a06452 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/service/PostService.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/post/service/PostService.kt @@ -7,6 +7,12 @@ import com.goliath.emojihub.springboot.domain.user.dao.UserDao import com.goliath.emojihub.springboot.global.exception.CustomHttp400 import com.goliath.emojihub.springboot.global.exception.CustomHttp403 import com.goliath.emojihub.springboot.global.exception.CustomHttp404 +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.INDEX_OUT_OF_BOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.COUNT_OUT_OF_BOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.POST_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.POST_UPDATE_FORBIDDEN +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.POST_DELETE_FORBIDDEN import org.springframework.stereotype.Service @Service @@ -17,7 +23,7 @@ class PostService( ) { companion object { - const val CREATED_POSTS= "created_posts" + const val CREATED_POSTS = "created_posts" } fun postPost(username: String, content: String) { @@ -26,43 +32,41 @@ class PostService( } fun getPosts(index: Int, count: Int): List { - if (index <= 0) throw CustomHttp400("Index should be positive integer.") - if (count <= 0) throw CustomHttp400("Count should be positive integer.") + if (index <= 0) throw CustomHttp400(INDEX_OUT_OF_BOUND) + if (count <= 0) throw CustomHttp400(COUNT_OUT_OF_BOUND) return postDao.getPosts(index, count) } fun getMyPosts(username: String, index: Int, count: Int): List { - if (index <= 0) throw CustomHttp400("Index should be positive integer.") - if (count <= 0) throw CustomHttp400("Count should be positive integer.") - if (!userDao.existUser(username)) throw CustomHttp404("User doesn't exist.") + if (index <= 0) throw CustomHttp400(INDEX_OUT_OF_BOUND) + if (count <= 0) throw CustomHttp400(COUNT_OUT_OF_BOUND) + if (!userDao.existUser(username)) throw CustomHttp404(USER_NOT_FOUND) return postDao.getMyPosts(username, index, count) } fun getPost(postId: String): PostDto? { if (postDao.existPost(postId).not()) - throw CustomHttp404("Post doesn't exist.") + throw CustomHttp404(POST_NOT_FOUND) return postDao.getPost(postId) } fun patchPost(username: String, postId: String, content: String) { - val post = postDao.getPost(postId) ?: throw CustomHttp404("Post doesn't exist.") + val post = postDao.getPost(postId) ?: throw CustomHttp404(POST_NOT_FOUND) if (username != post.created_by) - throw CustomHttp403("You can't update this post.") + throw CustomHttp403(POST_UPDATE_FORBIDDEN) postDao.updatePost(postId, content) } fun deletePost(username: String, postId: String) { - val post = postDao.getPost(postId) ?: throw CustomHttp404("Post doesn't exist.") + val post = postDao.getPost(postId) ?: throw CustomHttp404(POST_NOT_FOUND) if (username != post.created_by) - throw CustomHttp403("You can't delete this post.") + throw CustomHttp403(POST_DELETE_FORBIDDEN) // delete post(and post's reactions) - val reactionIds = post.reactions - if (reactionIds != null) { - for (reactionId in reactionIds) { - val reaction = reactionDao.getReaction(reactionId) ?: continue - if (postId != reaction.post_id) continue - reactionDao.deleteReaction(reactionId) - } + val reactionWithEmojiUnicodes = post.reactions + for (reactionWithEmojiUnicode in reactionWithEmojiUnicodes) { + val reaction = reactionDao.getReaction(reactionWithEmojiUnicode.id) ?: continue + if (postId != reaction.post_id) continue + reactionDao.deleteReaction(reactionWithEmojiUnicode.id) } // delete created_post id in user userDao.deleteId(username, postId, CREATED_POSTS) diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/service/ReactionService.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/service/ReactionService.kt index 8a35f991..7ee62f5e 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/service/ReactionService.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/service/ReactionService.kt @@ -7,6 +7,12 @@ import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDto import com.goliath.emojihub.springboot.domain.user.dao.UserDao import com.goliath.emojihub.springboot.global.exception.CustomHttp403 import com.goliath.emojihub.springboot.global.exception.CustomHttp404 +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_REACT +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.REACTION_DELETE_FORBIDDEN +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.POST_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.EMOJI_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.REACTION_NOT_FOUND import org.springframework.stereotype.Service @Service @@ -22,25 +28,25 @@ class ReactionService( fun getReactionsOfPost(postId: String): List { - if (!postDao.existPost(postId)) throw CustomHttp404("Post doesn't exist.") + if (!postDao.existPost(postId)) throw CustomHttp404(POST_NOT_FOUND) return reactionDao.getReactionsWithField(postId, POST_ID) } fun postReaction(username: String, postId: String, emojiId: String) { - if (!userDao.existUser(username)) throw CustomHttp404("User doesn't exist.") - if (!postDao.existPost(postId)) throw CustomHttp404("Post doesn't exist.") - if (!emojiDao.existsEmoji(emojiId)) throw CustomHttp404("Emoji doesn't exist.") - if (reactionDao.existSameReaction(username, postId, emojiId)) throw CustomHttp403("User already react to this post with this emoji.") + if (!userDao.existUser(username)) throw CustomHttp404(USER_NOT_FOUND) + if (!postDao.existPost(postId)) throw CustomHttp404(POST_NOT_FOUND) + val emoji = emojiDao.getEmoji(emojiId) ?: throw CustomHttp404(EMOJI_NOT_FOUND) + if (reactionDao.existSameReaction(username, postId, emojiId)) throw CustomHttp403(USER_ALREADY_REACT) val reaction = reactionDao.insertReaction(username, postId, emojiId) - postDao.insertReactionId(postId, reaction.id) + postDao.insertReaction(postId, reaction.id, emoji.emoji_unicode) } fun deleteReaction(username: String, reactionId: String) { - val reaction = reactionDao.getReaction(reactionId) ?: throw CustomHttp404("Reaction doesn't exist.") + val reaction = reactionDao.getReaction(reactionId) ?: throw CustomHttp404(REACTION_NOT_FOUND) if (username != reaction.created_by) - throw CustomHttp403("You can't delete this reaction.") + throw CustomHttp403(REACTION_DELETE_FORBIDDEN) // delete reaction id in post - postDao.deleteReactionId(reaction.post_id, reactionId) + postDao.deleteReaction(reaction.post_id, reactionId) // delete reaction reactionDao.deleteReaction(reactionId) } diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserDetailService.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserDetailService.kt index 676939a4..12968239 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserDetailService.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserDetailService.kt @@ -3,6 +3,7 @@ package com.goliath.emojihub.springboot.domain.user.service import com.goliath.emojihub.springboot.domain.user.dao.UserDao import com.goliath.emojihub.springboot.domain.user.model.UserAdapter import com.goliath.emojihub.springboot.global.exception.CustomHttp404 +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_FROM_TOKEN_NOT_FOUND import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.UserDetailsService import org.springframework.stereotype.Service @@ -12,7 +13,7 @@ class UserDetailService( private val userDao: UserDao ): UserDetailsService { override fun loadUserByUsername(username: String): UserDetails { - val userDto = userDao.getUser(username) ?: throw CustomHttp404("Username from the token doesn't exist.") + val userDto = userDao.getUser(username) ?: throw CustomHttp404(USER_FROM_TOKEN_NOT_FOUND) return UserAdapter(userDto) } diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserService.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserService.kt index c5324368..e3cfc0aa 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserService.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserService.kt @@ -9,6 +9,10 @@ import com.goliath.emojihub.springboot.global.exception.CustomHttp409 import com.goliath.emojihub.springboot.domain.user.dao.UserDao import com.goliath.emojihub.springboot.domain.user.dto.UserDto import com.goliath.emojihub.springboot.global.auth.JwtTokenProvider +import com.goliath.emojihub.springboot.global.exception.ErrorType.Unauthorized.PASSWORD_INCORRECT +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.ID_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.Conflict.ID_EXIST import org.springframework.security.crypto.password.PasswordEncoder import org.springframework.stereotype.Service @@ -32,7 +36,7 @@ class UserService( fun signUp(email: String, username: String, password: String): UserDto.AuthToken { if (userDao.existUser(username)) { - throw CustomHttp409("Id already exists.") + throw CustomHttp409(ID_EXIST) } val encodedPassword = passwordEncoder.encode(password) val user = UserDto( @@ -46,9 +50,9 @@ class UserService( } fun login(username: String, password: String): UserDto.AuthToken { - val user = userDao.getUser(username) ?: throw CustomHttp404("Id doesn't exist.") + val user = userDao.getUser(username) ?: throw CustomHttp404(ID_NOT_FOUND) if (!passwordEncoder.matches(password, user.password)) { - throw CustomHttp401("Password is incorrect.") + throw CustomHttp401(PASSWORD_INCORRECT) } val authToken = jwtTokenProvider.createToken(user.username) return UserDto.AuthToken(authToken) @@ -59,14 +63,14 @@ class UserService( } fun signOut(username: String) { - val user = userDao.getUser(username) ?: throw CustomHttp404("User doesn't exist.") + val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND) val createdEmojiIds = user.created_emojis val savedEmojiIds = user.saved_emojis val postIds = user.created_posts // delete all reactions(and reaction id in posts) created by user val myReactions = reactionDao.getReactionsWithField(username, CREATED_BY) for (reaction in myReactions) { - postDao.deleteReactionId(reaction.post_id, reaction.id) + postDao.deleteReaction(reaction.post_id, reaction.id) reactionDao.deleteReaction(reaction.id) } // delete all posts(and posts' reactions) created by user @@ -74,13 +78,11 @@ class UserService( for (postId in postIds) { val post = postDao.getPost(postId) ?: continue if (username != post.created_by) continue - val reactionIds = post.reactions - if (reactionIds != null) { - for (reactionId in reactionIds) { - val reaction = reactionDao.getReaction(reactionId) ?: continue - if (postId != reaction.post_id) continue - reactionDao.deleteReaction(reactionId) - } + val reactionWithEmojiUnicodes = post.reactions + for (reactionWithEmojiUnicode in reactionWithEmojiUnicodes) { + val reaction = reactionDao.getReaction(reactionWithEmojiUnicode.id) ?: continue + if (postId != reaction.post_id) continue + reactionDao.deleteReaction(reactionWithEmojiUnicode.id) } postDao.deletePost(postId) } @@ -96,7 +98,7 @@ class UserService( emojiDao.deleteFileInStorage(thumbnailBlobName) val reactions = reactionDao.getReactionsWithField(emojiId, EMOJI_ID) for (reaction in reactions) { - postDao.deleteReactionId(reaction.post_id, reaction.id) + postDao.deleteReaction(reaction.post_id, reaction.id) reactionDao.deleteReaction(reaction.id) } userDao.deleteAllSavedEmojiId(emojiId) diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/auth/JwtTokenProvider.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/auth/JwtTokenProvider.kt index d5c618a6..32e46210 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/auth/JwtTokenProvider.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/auth/JwtTokenProvider.kt @@ -2,6 +2,9 @@ package com.goliath.emojihub.springboot.global.auth import com.goliath.emojihub.springboot.global.exception.CustomHttp400 import com.goliath.emojihub.springboot.global.exception.CustomHttp401 +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.INVALID_TOKEN +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.NO_TOKEN +import com.goliath.emojihub.springboot.global.exception.ErrorType.Unauthorized.EXPIRED_TOKEN import io.jsonwebtoken.Claims import io.jsonwebtoken.ExpiredJwtException import io.jsonwebtoken.Jwts @@ -49,9 +52,9 @@ class JwtTokenProvider ( return try { getAllClaims(authToken)["username"] as String } catch (e: ExpiredJwtException) { - throw CustomHttp401("Token is expired.") + throw CustomHttp401(EXPIRED_TOKEN) } catch (e: Exception) { - throw CustomHttp400("Token is invalid.") + throw CustomHttp400(INVALID_TOKEN) } } @@ -66,16 +69,16 @@ class JwtTokenProvider ( } fun resolveToken(request: HttpServletRequest): String { - return request.getHeader("Authorization") ?: throw CustomHttp400("There is no token.") + return request.getHeader("Authorization") ?: throw CustomHttp400(NO_TOKEN) } fun validateToken(authToken: String): Boolean { try { return getAllClaims(authToken).expiration.after(Date()) } catch (e: ExpiredJwtException) { - throw CustomHttp401("Token is expired.") + throw CustomHttp401(EXPIRED_TOKEN) } catch (e: Exception) { - throw CustomHttp400("Token is invalid.") + throw CustomHttp400(INVALID_TOKEN) } } diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/exception/ErrorType.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/exception/ErrorType.kt new file mode 100644 index 00000000..07da7043 --- /dev/null +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/exception/ErrorType.kt @@ -0,0 +1,56 @@ +package com.goliath.emojihub.springboot.global.exception + +enum class ErrorType { + ; + + interface ErrorTypeInterface { + fun getMessage(): String + } + + enum class BadRequest(private val message: String): ErrorTypeInterface { + NO_TOKEN("There is no token."), + INVALID_TOKEN("Token is invalid."), + INDEX_OUT_OF_BOUND("Index should be positive integer."), + COUNT_OUT_OF_BOUND("Count should be positive integer."), + ; + override fun getMessage(): String = message + } + + enum class Unauthorized(private val message: String): ErrorTypeInterface { + EXPIRED_TOKEN("Token is expired."), + PASSWORD_INCORRECT("Password is incorrect."), + ; + override fun getMessage(): String = message + } + + enum class Forbidden(private val message: String): ErrorTypeInterface { + USER_CREATED("User created this emoji."), + USER_ALREADY_SAVED("User already saved this emoji."), + USER_ALREADY_UNSAVED("User already unsaved this emoji."), + USER_ALREADY_REACT("User already react to this post with this emoji."), + POST_UPDATE_FORBIDDEN("You can't update this post."), + POST_DELETE_FORBIDDEN("You can't delete this post."), + EMOJI_DELETE_FORBIDDEN("You can't delete this emoji."), + REACTION_DELETE_FORBIDDEN("You can't delete this reaction."), + ; + override fun getMessage(): String = message + } + + enum class NotFound(private val message: String): ErrorTypeInterface { + USER_FROM_TOKEN_NOT_FOUND("Username from the token doesn't exist."), + USER_NOT_FOUND("User doesn't exist."), + ID_NOT_FOUND("Id doesn't exist."), + POST_NOT_FOUND("Post doesn't exist"), + EMOJI_NOT_FOUND("Emoji doesn't exist."), + REACTION_NOT_FOUND("Reaction doesn't exist."), + ; + + override fun getMessage(): String = message + } + + enum class Conflict(private val message: String): ErrorTypeInterface { + ID_EXIST("Id already exists.") + ; + override fun getMessage(): String = message + } +} \ No newline at end of file diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/exception/Exceptions.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/exception/Exceptions.kt index c2c5e7ce..ff2e8bf5 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/exception/Exceptions.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/exception/Exceptions.kt @@ -1,8 +1,9 @@ package com.goliath.emojihub.springboot.global.exception import org.springframework.http.HttpStatus +import com.goliath.emojihub.springboot.global.exception.ErrorType.* -open class CustomHttpException(msg: String, val status: HttpStatus) : RuntimeException(msg) +open class CustomHttpException(errorTypeInterface: ErrorTypeInterface, val status: HttpStatus) : RuntimeException(errorTypeInterface.getMessage()) data class ErrorResponse( val errorCode: Int, @@ -14,12 +15,12 @@ data class ErrorResponse( ) } -class CustomHttp400(msg: String) : CustomHttpException(msg, HttpStatus.BAD_REQUEST) +class CustomHttp400(badRequest: BadRequest) : CustomHttpException(badRequest, HttpStatus.BAD_REQUEST) -class CustomHttp401(msg: String) : CustomHttpException(msg, HttpStatus.UNAUTHORIZED) +class CustomHttp401(unauthorized: Unauthorized) : CustomHttpException(unauthorized, HttpStatus.UNAUTHORIZED) -class CustomHttp403(msg: String) : CustomHttpException(msg, HttpStatus.FORBIDDEN) +class CustomHttp403(forbidden: Forbidden) : CustomHttpException(forbidden, HttpStatus.FORBIDDEN) -class CustomHttp404(msg: String) : CustomHttpException(msg, HttpStatus.NOT_FOUND) +class CustomHttp404(notFound: NotFound) : CustomHttpException(notFound, HttpStatus.NOT_FOUND) -class CustomHttp409(msg: String) : CustomHttpException(msg, HttpStatus.CONFLICT) \ No newline at end of file +class CustomHttp409(conflict: Conflict) : CustomHttpException(conflict, HttpStatus.CONFLICT) \ No newline at end of file diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/TestDto.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/TestDto.kt index 81d5c6a8..eafcd40b 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/TestDto.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/TestDto.kt @@ -2,6 +2,7 @@ package com.goliath.emojihub.springboot.domain import com.goliath.emojihub.springboot.domain.emoji.dto.EmojiDto import com.goliath.emojihub.springboot.domain.post.dto.PostDto +import com.goliath.emojihub.springboot.domain.post.dto.ReactionWithEmojiUnicode import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDto import com.goliath.emojihub.springboot.domain.user.dto.UserDto @@ -89,7 +90,21 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성 for (i in 0 until userSize) { for (j in 0 until userSize) { val reactionIdWithCreatedEmoji = "test_reaction${i}_${j}_c" + val createdEmojiUnicode = emojiList[createdEmojiSize * i].emoji_unicode val reactionIdWithSavedEmoji = "test_reaction${i}_${j}_s" + val savedEmojiUnicode = + if (i != userSize - 1) + emojiList[createdEmojiSize * (i + 1)].emoji_unicode + else + emojiList[0].emoji_unicode + val createdReactionWithEmojiUnicode = ReactionWithEmojiUnicode( + id = reactionIdWithCreatedEmoji, + emoji_unicode = createdEmojiUnicode + ) + val savedReactionWithEmojiUnicode = ReactionWithEmojiUnicode( + id = reactionIdWithSavedEmoji, + emoji_unicode = savedEmojiUnicode + ) reactionList.add( ReactionDto( id = reactionIdWithCreatedEmoji, @@ -108,8 +123,8 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성 created_at = "test_created_at${i}_${j}_s" ) ) - postList[postSize * j].reactions!!.add(reactionIdWithCreatedEmoji) - postList[postSize * j].reactions!!.add(reactionIdWithSavedEmoji) + postList[postSize * j].reactions.add(createdReactionWithEmojiUnicode) + postList[postSize * j].reactions.add(savedReactionWithEmojiUnicode) } } } diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiServiceTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiServiceTest.kt index f806962c..ccafddbc 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiServiceTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/service/EmojiServiceTest.kt @@ -9,6 +9,14 @@ import com.goliath.emojihub.springboot.domain.user.dao.UserDao import com.goliath.emojihub.springboot.global.exception.CustomHttp400 import com.goliath.emojihub.springboot.global.exception.CustomHttp403 import com.goliath.emojihub.springboot.global.exception.CustomHttp404 +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.INDEX_OUT_OF_BOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.COUNT_OUT_OF_BOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.EMOJI_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_CREATED +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_SAVED +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_UNSAVED +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.EMOJI_DELETE_FORBIDDEN import org.junit.jupiter.api.Test import org.junit.jupiter.api.Assertions.* @@ -73,8 +81,8 @@ internal class EmojiServiceTest { // then assertAll( { assertEquals(result, testDto.emojiList) }, - { assertEquals(assertThrows1.message, "Index should be positive integer.") }, - { assertEquals(assertThrows2.message, "Count should be positive integer.") } + { assertEquals(assertThrows1.message, INDEX_OUT_OF_BOUND.getMessage()) }, + { assertEquals(assertThrows2.message, COUNT_OUT_OF_BOUND.getMessage()) } ) verify(emojiDao, times(1)).getEmojis(sortByDate, index, count) } @@ -106,7 +114,7 @@ internal class EmojiServiceTest { assertAll( { assertEquals(createdEmojisResult.size, testDto.createdEmojiSize) }, { assertEquals(savedEmojisResult.size, testDto.savedEmojiSize) }, - { assertEquals(assertThrows.message, "User doesn't exist.") } + { assertEquals(assertThrows.message, USER_NOT_FOUND.getMessage()) } ) verify(userDao, times(2)).getUser(username) verify(userDao, times(1)).getUser(wrongUsername) @@ -138,7 +146,7 @@ internal class EmojiServiceTest { // then assertAll( { assertEquals(result, emoji) }, - { assertEquals(assertThrows.message, "Emoji doesn't exist.") } + { assertEquals(assertThrows.message, EMOJI_NOT_FOUND.getMessage()) } ) verify(emojiDao, times(1)).existsEmoji(id) verify(emojiDao, times(1)).existsEmoji(wrongId) @@ -200,7 +208,7 @@ internal class EmojiServiceTest { } // then - assertEquals(assertThrows.message, "Emoji doesn't exist.") + assertEquals(assertThrows.message, EMOJI_NOT_FOUND.getMessage()) verify(emojiDao, times(1)).existsEmoji(wrongId) } @@ -219,7 +227,7 @@ internal class EmojiServiceTest { } // then - assertEquals(assertThrows.message, "User doesn't exist.") + assertEquals(assertThrows.message, USER_NOT_FOUND.getMessage()) verify(emojiDao, times(1)).existsEmoji(emojiId) verify(userDao, times(1)).getUser(wrongUsername) } @@ -240,7 +248,7 @@ internal class EmojiServiceTest { } // then - assertEquals(assertThrows.message, "User created this emoji.") + assertEquals(assertThrows.message, USER_CREATED.getMessage()) verify(emojiDao, times(1)).existsEmoji(emojiId) verify(userDao, times(1)).getUser(username) } @@ -261,7 +269,7 @@ internal class EmojiServiceTest { } // then - assertEquals(assertThrows.message, "User already saved this emoji.") + assertEquals(assertThrows.message, USER_ALREADY_SAVED.getMessage()) verify(emojiDao, times(1)).existsEmoji(emojiId) verify(userDao, times(1)).getUser(username) } @@ -299,7 +307,7 @@ internal class EmojiServiceTest { } // then - assertEquals(assertThrows.message, "Emoji doesn't exist.") + assertEquals(assertThrows.message, EMOJI_NOT_FOUND.getMessage()) verify(emojiDao, times(1)).existsEmoji(wrongId) } @@ -318,7 +326,7 @@ internal class EmojiServiceTest { } // then - assertEquals(assertThrows.message, "User doesn't exist.") + assertEquals(assertThrows.message, USER_NOT_FOUND.getMessage()) verify(emojiDao, times(1)).existsEmoji(emojiId) verify(userDao, times(1)).getUser(wrongUsername) } @@ -339,7 +347,7 @@ internal class EmojiServiceTest { } // then - assertEquals(assertThrows.message, "User created this emoji.") + assertEquals(assertThrows.message, USER_CREATED.getMessage()) verify(emojiDao, times(1)).existsEmoji(emojiId) verify(userDao, times(1)).getUser(username) } @@ -360,7 +368,7 @@ internal class EmojiServiceTest { } // then - assertEquals(assertThrows.message, "User already unsaved this emoji.") + assertEquals(assertThrows.message, USER_ALREADY_UNSAVED.getMessage()) verify(emojiDao, times(1)).existsEmoji(emojiId) verify(userDao, times(1)).getUser(username) } @@ -397,8 +405,8 @@ internal class EmojiServiceTest { // then assertAll( - { assertEquals(assertThrows1.message, "Emoji doesn't exist.") }, - { assertEquals(assertThrows2.message, "You can't delete this emoji.") } + { assertEquals(assertThrows1.message, EMOJI_NOT_FOUND.getMessage()) }, + { assertEquals(assertThrows2.message, EMOJI_DELETE_FORBIDDEN.getMessage()) } ) verify(emojiDao, times(2)).getEmoji(emojiId) verify(emojiDao, times(1)).getEmoji(wrongId) @@ -406,7 +414,7 @@ internal class EmojiServiceTest { verify(emojiDao, times(1)).deleteFileInStorage(thumbnailBlobName) verify(reactionDao, times(1)).getReactionsWithField(emojiId, EMOJI_ID) for (reaction in reactions) { - verify(postDao, times(1)).deleteReactionId(reaction.post_id, reaction.id) + verify(postDao, times(1)).deleteReaction(reaction.post_id, reaction.id) verify(reactionDao, times(1)).deleteReaction(reaction.id) } verify(userDao, times(1)).deleteAllSavedEmojiId(emojiId) diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/post/dao/PostDaoTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/post/dao/PostDaoTest.kt index 1f5d3e76..16c04411 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/post/dao/PostDaoTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/post/dao/PostDaoTest.kt @@ -2,6 +2,7 @@ package com.goliath.emojihub.springboot.domain.post.dao import com.goliath.emojihub.springboot.domain.TestDto import com.goliath.emojihub.springboot.domain.post.dto.PostDto +import com.goliath.emojihub.springboot.domain.post.dto.ReactionWithEmojiUnicode import com.google.auth.oauth2.GoogleCredentials import com.google.cloud.firestore.Firestore import com.google.firebase.FirebaseApp @@ -221,33 +222,38 @@ internal class PostDaoTest { } @Test - fun insertAndDeleteReactionId() { + fun insertAndDeleteReactionIdWithEmojiUnicode() { // given val postId = postList[0].id val reactionId = "new_test_reaction_id" + val emojiUnicode = "new_test_emoji_unicode" + val reactionWithEmojiUnicode = ReactionWithEmojiUnicode( + id = reactionId, + emoji_unicode = emojiUnicode, + ) Mockito.`when`(db.collection(POST_COLLECTION_NAME)) .thenReturn(testDB.collection(POST_COLLECTION_NAME)) // when - postDao.insertReactionId(postId, reactionId) + postDao.insertReaction(postId, reactionId, emojiUnicode) // then var post = postDao.getPost(postId) var a = 1 - while (!post!!.reactions!!.contains(reactionId) && a <= 5) { + while (!post!!.reactions.contains(reactionWithEmojiUnicode) && a <= 5) { post = postDao.getPost(postId) a++ } - assertThat(post.reactions).contains(reactionId) + assertThat(post.reactions).contains(reactionWithEmojiUnicode) // after work - postDao.deleteReactionId(postId, reactionId) + postDao.deleteReaction(postId, reactionId) post = postDao.getPost(postId) var b = 1 - while(post!!.reactions!!.contains(reactionId) && b <= 5) { + while(post!!.reactions.contains(reactionWithEmojiUnicode) && b <= 5) { post = postDao.getPost(postId) b++ } - assertThat(post.reactions).doesNotContain(reactionId) + assertThat(post.reactions).doesNotContain(reactionWithEmojiUnicode) } } \ No newline at end of file diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/post/service/PostServiceTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/post/service/PostServiceTest.kt index ba80d523..0848e3ce 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/post/service/PostServiceTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/post/service/PostServiceTest.kt @@ -7,6 +7,12 @@ import com.goliath.emojihub.springboot.domain.user.dao.UserDao import com.goliath.emojihub.springboot.global.exception.CustomHttp400 import com.goliath.emojihub.springboot.global.exception.CustomHttp403 import com.goliath.emojihub.springboot.global.exception.CustomHttp404 +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.POST_UPDATE_FORBIDDEN +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.POST_DELETE_FORBIDDEN +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.INDEX_OUT_OF_BOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.COUNT_OUT_OF_BOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.POST_NOT_FOUND import org.junit.jupiter.api.Test import org.junit.jupiter.api.Assertions.* @@ -81,8 +87,8 @@ internal class PostServiceTest { // then assertAll( { assertEquals(result, list) }, - { assertEquals(assertThrows1.message, "Index should be positive integer.") }, - { assertEquals(assertThrows2.message, "Count should be positive integer.") } + { assertEquals(assertThrows1.message, INDEX_OUT_OF_BOUND.getMessage()) }, + { assertEquals(assertThrows2.message, COUNT_OUT_OF_BOUND.getMessage()) } ) verify(postDao, times(1)).getPosts(index, count) } @@ -109,7 +115,7 @@ internal class PostServiceTest { // then assertAll( { assertEquals(result, list) }, - { assertEquals(assertThrows.message, "User doesn't exist.") } + { assertEquals(assertThrows.message, USER_NOT_FOUND.getMessage()) } ) verify(userDao, times(1)).existUser(username) verify(userDao, times(1)).existUser(wrongUsername) @@ -136,7 +142,7 @@ internal class PostServiceTest { // then assertAll( { assertEquals(result, post) }, - { assertEquals(assertThrows.message, "Post doesn't exist.") } + { assertEquals(assertThrows.message, POST_NOT_FOUND.getMessage()) } ) verify(postDao, times(1)).existPost(id) verify(postDao, times(1)).existPost(wrongId) @@ -168,8 +174,8 @@ internal class PostServiceTest { // then assertAll( { assertEquals(result, Unit) }, - { assertEquals(assertThrows1.message, "Post doesn't exist.") }, - { assertEquals(assertThrows2.message, "You can't update this post.") } + { assertEquals(assertThrows1.message, POST_NOT_FOUND.getMessage()) }, + { assertEquals(assertThrows2.message, POST_UPDATE_FORBIDDEN.getMessage()) } ) verify(postDao, times(2)).getPost(id) verify(postDao, times(1)).getPost(wrongId) @@ -203,14 +209,14 @@ internal class PostServiceTest { // then assertAll( { assertEquals(result, Unit) }, - { assertEquals(assertThrows1.message, "Post doesn't exist.") }, - { assertEquals(assertThrows2.message, "You can't delete this post.") } + { assertEquals(assertThrows1.message, POST_NOT_FOUND.getMessage()) }, + { assertEquals(assertThrows2.message, POST_DELETE_FORBIDDEN.getMessage()) } ) verify(postDao, times(2)).getPost(id) verify(postDao, times(1)).getPost(wrongId) - for (reactionId in post.reactions!!) { - verify(reactionDao, times(1)).getReaction(reactionId) - verify(reactionDao, times(1)).deleteReaction(reactionId) + for (reactionWithEmojiUnicode in post.reactions) { + verify(reactionDao, times(1)).getReaction(reactionWithEmojiUnicode.id) + verify(reactionDao, times(1)).deleteReaction(reactionWithEmojiUnicode.id) } verify(userDao, times(1)).deleteId(username, id, CREATED_POSTS) verify(postDao, times(1)).deletePost(id) diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/reaction/service/ReactionServiceTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/reaction/service/ReactionServiceTest.kt index e902259a..2539e68b 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/reaction/service/ReactionServiceTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/reaction/service/ReactionServiceTest.kt @@ -8,6 +8,12 @@ import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDto import com.goliath.emojihub.springboot.domain.user.dao.UserDao import com.goliath.emojihub.springboot.global.exception.CustomHttp403 import com.goliath.emojihub.springboot.global.exception.CustomHttp404 +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.POST_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.EMOJI_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.REACTION_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.USER_ALREADY_REACT +import com.goliath.emojihub.springboot.global.exception.ErrorType.Forbidden.REACTION_DELETE_FORBIDDEN import org.junit.jupiter.api.Test import org.junit.jupiter.api.Assertions.* @@ -70,7 +76,7 @@ internal class ReactionServiceTest { // then assertAll( { assertEquals(result, reactions) }, - { assertEquals(assertThrows.message, "Post doesn't exist.") } + { assertEquals(assertThrows.message, POST_NOT_FOUND.getMessage()) } ) verify(postDao, times(1)).existPost(postId) verify(postDao, times(1)).existPost(wrongId) @@ -85,6 +91,7 @@ internal class ReactionServiceTest { val username = reaction.created_by val postId = reaction.post_id val emojiId = reaction.emoji_id + val emoji = testDto.emojiList[0] val wrongUsername = "wrong_username" val wrongPostId = "wrong_post_id" val wrongEmojiId = "wrong_emoji_id" @@ -93,9 +100,9 @@ internal class ReactionServiceTest { Mockito.`when`(userDao.existUser(wrongUsername)).thenReturn(false) Mockito.`when`(postDao.existPost(postId)).thenReturn(true) Mockito.`when`(postDao.existPost(wrongPostId)).thenReturn(false) - Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true) - Mockito.`when`(emojiDao.existsEmoji(sameEmojiId)).thenReturn(true) - Mockito.`when`(emojiDao.existsEmoji(wrongEmojiId)).thenReturn(false) + Mockito.`when`(emojiDao.getEmoji(emojiId)).thenReturn(emoji) + Mockito.`when`(emojiDao.getEmoji(sameEmojiId)).thenReturn(emoji) + Mockito.`when`(emojiDao.getEmoji(wrongEmojiId)).thenReturn(null) Mockito.`when`(reactionDao.existSameReaction(username, postId, emojiId)).thenReturn(false) Mockito.`when`(reactionDao.existSameReaction(username, postId, sameEmojiId)).thenReturn(true) Mockito.`when`(reactionDao.insertReaction(username, postId, emojiId)).thenReturn(reaction) @@ -117,21 +124,22 @@ internal class ReactionServiceTest { // then assertAll( - { assertEquals(assertThrows1.message, "User doesn't exist.") }, - { assertEquals(assertThrows2.message, "Post doesn't exist.") }, - { assertEquals(assertThrows3.message, "Emoji doesn't exist.") }, - { assertEquals(assertThrows4.message, "User already react to this post with this emoji.") } + { assertEquals(assertThrows1.message, USER_NOT_FOUND.getMessage()) }, + { assertEquals(assertThrows2.message, POST_NOT_FOUND.getMessage()) }, + { assertEquals(assertThrows3.message, EMOJI_NOT_FOUND.getMessage()) }, + { assertEquals(assertThrows4.message, USER_ALREADY_REACT.getMessage()) } ) verify(userDao, times(4)).existUser(username) verify(userDao, times(1)).existUser(wrongUsername) verify(postDao, times(3)).existPost(postId) verify(postDao, times(1)).existPost(wrongPostId) - verify(emojiDao, times(1)).existsEmoji(emojiId) - verify(emojiDao, times(1)).existsEmoji(wrongEmojiId) + verify(emojiDao, times(1)).getEmoji(emojiId) + verify(emojiDao, times(1)).getEmoji(wrongEmojiId) + verify(emojiDao, times(1)).getEmoji(sameEmojiId) verify(reactionDao, times(1)).existSameReaction(username, postId, emojiId) verify(reactionDao, times(1)).existSameReaction(username, postId, sameEmojiId) verify(reactionDao, times(1)).insertReaction(username, postId, emojiId) - verify(postDao, times(1)).insertReactionId(postId, reaction.id) + verify(postDao, times(1)).insertReaction(postId, reaction.id, emoji.emoji_unicode) } @Test @@ -157,12 +165,12 @@ internal class ReactionServiceTest { // then assertAll( - { assertEquals(assertThrows1.message, "Reaction doesn't exist.") }, - { assertEquals(assertThrows2.message, "You can't delete this reaction.") } + { assertEquals(assertThrows1.message, REACTION_NOT_FOUND.getMessage()) }, + { assertEquals(assertThrows2.message, REACTION_DELETE_FORBIDDEN.getMessage()) } ) verify(reactionDao, times(2)).getReaction(reactionId) verify(reactionDao, times(1)).getReaction(wrongReactionId) - verify(postDao, times(1)).deleteReactionId(reaction.post_id, reactionId) + verify(postDao, times(1)).deleteReaction(reaction.post_id, reactionId) verify(reactionDao, times(1)).deleteReaction(reactionId) } } \ No newline at end of file diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/controller/UserControllerTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/controller/UserControllerTest.kt index c906989f..1379aea7 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/controller/UserControllerTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/controller/UserControllerTest.kt @@ -8,6 +8,7 @@ import com.goliath.emojihub.springboot.domain.user.dto.SignUpRequest import com.goliath.emojihub.springboot.domain.user.dto.UserDto import com.goliath.emojihub.springboot.domain.user.service.UserService import com.goliath.emojihub.springboot.global.exception.CustomHttp409 +import com.goliath.emojihub.springboot.global.exception.ErrorType.Conflict.ID_EXIST import org.hamcrest.Matchers.equalTo import org.junit.jupiter.api.Test @@ -153,7 +154,7 @@ internal class UserControllerTest @Autowired constructor( @DisplayName("에러 핸들링 테스트1: 커스텀 에러") fun errorHandling1() { // given - val exception = CustomHttp409("Id already exists.") + val exception = CustomHttp409(ID_EXIST) val request = SignUpRequest( email = "test_email", username = "test_username", diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserServiceTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserServiceTest.kt index 7418c08a..018f0933 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserServiceTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserServiceTest.kt @@ -10,6 +10,10 @@ import com.goliath.emojihub.springboot.global.auth.JwtTokenProvider import com.goliath.emojihub.springboot.global.exception.CustomHttp401 import com.goliath.emojihub.springboot.global.exception.CustomHttp404 import com.goliath.emojihub.springboot.global.exception.CustomHttp409 +import com.goliath.emojihub.springboot.global.exception.ErrorType.Conflict.ID_EXIST +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.ID_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_NOT_FOUND +import com.goliath.emojihub.springboot.global.exception.ErrorType.Unauthorized.PASSWORD_INCORRECT import org.junit.jupiter.api.Test import org.junit.jupiter.api.Assertions.* @@ -82,7 +86,7 @@ internal class UserServiceTest { } // then - assertEquals(assertThrows.message, "Id already exists.") + assertEquals(assertThrows.message, ID_EXIST.getMessage()) verify(userDao, times(1)).existUser(user.username) } @@ -120,7 +124,7 @@ internal class UserServiceTest { } // then - assertEquals(assertThrows.message, "Id doesn't exist.") + assertEquals(assertThrows.message, ID_NOT_FOUND.getMessage()) verify(userDao, times(1)).getUser(user.username) } @@ -139,7 +143,7 @@ internal class UserServiceTest { } // then - assertEquals(assertThrows.message, "Password is incorrect.") + assertEquals(assertThrows.message, PASSWORD_INCORRECT.getMessage()) verify(userDao, times(1)).getUser(user.username) verify(passwordEncoder, times(1)).matches(wrongPassword, user.password) } @@ -187,7 +191,7 @@ internal class UserServiceTest { } // then - assertEquals(assertThrows.message, "User doesn't exist.") + assertEquals(assertThrows.message, USER_NOT_FOUND.getMessage()) verify(userDao, times(1)).getUser(username) } diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/global/auth/JwtTokenProviderTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/global/auth/JwtTokenProviderTest.kt index fe194682..2272a0fd 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/global/auth/JwtTokenProviderTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/global/auth/JwtTokenProviderTest.kt @@ -5,6 +5,8 @@ import com.goliath.emojihub.springboot.domain.user.model.UserAdapter import com.goliath.emojihub.springboot.domain.user.service.UserDetailService import com.goliath.emojihub.springboot.global.exception.CustomHttp400 import com.goliath.emojihub.springboot.global.exception.CustomHttp401 +import com.goliath.emojihub.springboot.global.exception.ErrorType.BadRequest.INVALID_TOKEN +import com.goliath.emojihub.springboot.global.exception.ErrorType.Unauthorized.EXPIRED_TOKEN import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertThrows import org.junit.jupiter.api.DisplayName @@ -65,8 +67,8 @@ internal class JwtTokenProviderTest { // then assertAll( - { assertEquals(assertThrows1.message, "Token is expired.") }, - { assertEquals(assertThrows2.message, "Token is invalid.") }, + { assertEquals(assertThrows1.message, EXPIRED_TOKEN.getMessage()) }, + { assertEquals(assertThrows2.message, INVALID_TOKEN.getMessage()) }, ) } @@ -82,8 +84,8 @@ internal class JwtTokenProviderTest { // then assertAll( - { assertEquals(assertThrows1.message, "Token is expired.") }, - { assertEquals(assertThrows2.message, "Token is invalid.") }, + { assertEquals(assertThrows1.message, EXPIRED_TOKEN.getMessage()) }, + { assertEquals(assertThrows2.message, INVALID_TOKEN.getMessage()) }, ) }