diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDao.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDao.kt index 095985d9..3c2706d7 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDao.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDao.kt @@ -1,10 +1,12 @@ package com.goliath.emojihub.springboot.domain.emoji.dao import com.goliath.emojihub.springboot.domain.emoji.dto.EmojiDto +import com.goliath.emojihub.springboot.domain.emoji.dto.EmojiDtoBuilder import com.goliath.emojihub.springboot.global.util.StringValue.Bucket.EMOJI_STORAGE_BUCKET_NAME import com.goliath.emojihub.springboot.global.util.StringValue.Collection.EMOJI_COLLECTION_NAME import com.goliath.emojihub.springboot.global.util.StringValue.EmojiField.NUM_SAVED import com.goliath.emojihub.springboot.global.util.StringValue.EmojiField.CREATED_AT +import com.goliath.emojihub.springboot.global.util.generateId import com.google.cloud.firestore.* import com.google.cloud.storage.BlobId import com.google.cloud.storage.BlobInfo @@ -71,7 +73,15 @@ class EmojiDao( .build() storage.createFrom(thumbnailBlob, ByteArrayInputStream(thumbnail.bytes)) val thumbnailUrl = storage.get(thumbnailBlobId).signUrl(100, TimeUnit.DAYS) - val emoji = EmojiDto(username, emojiUnicode, emojiLabel, emojiVideoUrl.toString(), dateTime, thumbnailUrl.toString()) + val emoji = EmojiDtoBuilder() + .id(generateId()) + .createdBy(username) + .emojiUnicode(emojiUnicode) + .emojiLabel(emojiLabel) + .videoUrl(emojiVideoUrl.toString()) + .createdAt(dateTime) + .thumbnailUrl(thumbnailUrl.toString()) + .build() db.collection(EMOJI_COLLECTION_NAME.string) .document(emoji.id) .set(emoji) diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/dto/EmojiDto.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/dto/EmojiDto.kt index 45b09c3e..374361c6 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/dto/EmojiDto.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/emoji/dto/EmojiDto.kt @@ -1,7 +1,5 @@ package com.goliath.emojihub.springboot.domain.emoji.dto -import kotlin.streams.asSequence - data class EmojiDto( var id: String = "", var created_by: String = "", @@ -11,21 +9,52 @@ data class EmojiDto( var created_at: String = "", var num_saved: Int = 0, var thumbnail_url: String = "" -){ - constructor(username: String, emojiUnicode: String, emojiLabel: String, emojiVideoUrl: String, dateTime: String, thumbnailUrl: String) : this() { - val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - val outputStrLength: Long = 20 - id = java.util.Random().ints(outputStrLength, 0, source.length) - .asSequence() - .map(source::get) - .joinToString("") - - created_by = username - video_url = emojiVideoUrl - emoji_unicode = emojiUnicode - emoji_label = emojiLabel - created_at = dateTime - num_saved = 0 - thumbnail_url = thumbnailUrl +) + +class EmojiDtoBuilder { + private val emojiDto: EmojiDto = EmojiDto() + + fun id(id: String): EmojiDtoBuilder { + emojiDto.id = id + return this + } + + fun createdBy(createdBy: String): EmojiDtoBuilder { + emojiDto.created_by = createdBy + return this + } + + fun videoUrl(videoUrl: String): EmojiDtoBuilder { + emojiDto.video_url = videoUrl + return this + } + + fun emojiUnicode(emojiUnicode: String): EmojiDtoBuilder { + emojiDto.emoji_unicode = emojiUnicode + return this + } + + fun emojiLabel(emojiLabel: String): EmojiDtoBuilder { + emojiDto.emoji_label = emojiLabel + return this + } + + fun createdAt(createdAt: String): EmojiDtoBuilder { + emojiDto.created_at = createdAt + return this + } + + fun numSaved(numSaved: Int): EmojiDtoBuilder { + emojiDto.num_saved = numSaved + return this + } + + fun thumbnailUrl(thumbnailUrl: String): EmojiDtoBuilder { + emojiDto.thumbnail_url = thumbnailUrl + return this + } + + fun build(): EmojiDto { + return emojiDto } } \ No newline at end of file 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 41792e45..b3007efb 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 @@ -51,18 +51,21 @@ class EmojiService( } else { user.saved_emojis } - val emojiArrayDeque = ArrayDeque() - var emojiList = listOf() - if (emojiIdList != null && emojiIdList.size != 0) { + var emojiList = mutableListOf() + if (emojiIdList.size != 0) { for (emojiId in emojiIdList) { val emoji = emojiDao.getEmoji(emojiId) ?: continue - emojiArrayDeque.addFirst(emoji) + emojiList.add(emoji) + } + // sort + if (emojiList.size != 0) { + emojiList.reverse() + // pagination + emojiList = emojiList.subList( + min((index - 1) * count, emojiList.size - 1), + min(index * count, emojiList.size) + ) } - // pagination - // FIXME: remind - CreatedEmojiList should already be sorted by created_at - val start = min((index - 1) * count, emojiArrayDeque.size - 1) - val end = min(index * count, emojiArrayDeque.size) - emojiList = emojiArrayDeque.toList().subList(start, end) } return emojiList } @@ -88,9 +91,9 @@ class EmojiService( if (emojiDao.existsEmoji(emojiId).not()) throw CustomHttp404(EMOJI_NOT_FOUND) val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND) - if (user.created_emojis?.contains(emojiId) == true) + if (user.created_emojis.contains(emojiId)) throw CustomHttp403(USER_CREATED) - if (user.saved_emojis?.contains(emojiId) == true) + if (user.saved_emojis.contains(emojiId)) throw CustomHttp403(USER_ALREADY_SAVED) emojiDao.numSavedChange(emojiId, 1) userDao.insertId(username, emojiId, SAVED_EMOJIS.string) @@ -100,9 +103,9 @@ class EmojiService( if (emojiDao.existsEmoji(emojiId).not()) throw CustomHttp404(EMOJI_NOT_FOUND) val user = userDao.getUser(username) ?: throw CustomHttp404(USER_NOT_FOUND) - if (user.created_emojis?.contains(emojiId) == true) + if (user.created_emojis.contains(emojiId)) throw CustomHttp403(USER_CREATED) - if (user.saved_emojis == null || !user.saved_emojis!!.contains(emojiId)) + if (!user.saved_emojis.contains(emojiId)) throw CustomHttp403(USER_ALREADY_UNSAVED) emojiDao.numSavedChange(emojiId, -1) userDao.deleteId(username, emojiId, SAVED_EMOJIS.string) 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 31a4a254..ae5dad64 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,12 +1,14 @@ 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.PostDtoBuilder import com.goliath.emojihub.springboot.domain.post.dto.ReactionWithEmojiUnicode import com.goliath.emojihub.springboot.global.util.StringValue.Collection.POST_COLLECTION_NAME import com.goliath.emojihub.springboot.global.util.StringValue.PostField.CONTENT import com.goliath.emojihub.springboot.global.util.StringValue.PostField.CREATED_AT import com.goliath.emojihub.springboot.global.util.StringValue.PostField.MODIFIED_AT import com.goliath.emojihub.springboot.global.util.StringValue.PostField.REACTIONS +import com.goliath.emojihub.springboot.global.util.generateId import com.goliath.emojihub.springboot.global.util.getDateTimeNow import com.google.cloud.firestore.* import lombok.extern.slf4j.Slf4j @@ -20,7 +22,12 @@ class PostDao( fun insertPost(username: String, content: String): PostDto { val dateTime = getDateTimeNow() - val post = PostDto(username, content, dateTime) + val post = PostDtoBuilder() + .id(generateId()) + .createdBy(username) + .content(content) + .createdAt(dateTime) + .build() db.collection(POST_COLLECTION_NAME.string) .document(post.id) .set(post) 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 0148b91e..c7e88d1d 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 @@ -1,7 +1,5 @@ package com.goliath.emojihub.springboot.domain.post.dto -import kotlin.streams.asSequence - data class PostDto( var id: String = "", var created_by: String = "", @@ -9,22 +7,42 @@ data class PostDto( var created_at: String = "", var modified_at: String = "", var reactions: MutableList = mutableListOf(), -) { - constructor(username: String, content: String, dateTime: String) : this() { - val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - val outputStrLength: Long = 20 - id = java.util.Random().ints(outputStrLength, 0, source.length) - .asSequence() - .map(source::get) - .joinToString("") - created_by = username - this.content = content - created_at = dateTime - modified_at = dateTime - } -} +) data class ReactionWithEmojiUnicode( var id: String = "", var emoji_unicode: String = "" -) \ No newline at end of file +) + +class PostDtoBuilder { + private val postDto: PostDto = PostDto() + + fun id(id: String): PostDtoBuilder { + postDto.id = id + return this + } + + fun createdBy(createdBy: String): PostDtoBuilder { + postDto.created_by = createdBy + return this + } + + fun content(content: String): PostDtoBuilder { + postDto.content = content + return this + } + + fun createdAt(createdAt: String): PostDtoBuilder { + postDto.created_at = createdAt + return this + } + + fun modifiedAt(modifiedAt: String): PostDtoBuilder { + postDto.modified_at = modifiedAt + return this + } + + fun build(): PostDto { + return postDto + } +} \ No newline at end of file diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/dao/ReactionDao.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/dao/ReactionDao.kt index b402b89c..20547aeb 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/dao/ReactionDao.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/dao/ReactionDao.kt @@ -1,11 +1,13 @@ package com.goliath.emojihub.springboot.domain.reaction.dao import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDto +import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDtoBuilder import com.goliath.emojihub.springboot.global.util.StringValue.Collection.REACTION_COLLECTION_NAME import com.goliath.emojihub.springboot.global.util.StringValue.ReactionField.CREATED_BY import com.goliath.emojihub.springboot.global.util.StringValue.ReactionField.POST_ID import com.goliath.emojihub.springboot.global.util.StringValue.ReactionField.EMOJI_ID import com.goliath.emojihub.springboot.global.util.StringValue.ReactionField.CREATED_AT +import com.goliath.emojihub.springboot.global.util.generateId import com.goliath.emojihub.springboot.global.util.getDateTimeNow import com.google.cloud.firestore.DocumentSnapshot import com.google.cloud.firestore.Firestore @@ -21,7 +23,13 @@ class ReactionDao( fun insertReaction(username: String, postId: String, emojiId: String): ReactionDto { val dateTime = getDateTimeNow() - val reaction = ReactionDto(username, postId, emojiId, dateTime) + val reaction = ReactionDtoBuilder() + .id(generateId()) + .createdBy(username) + .postId(postId) + .emojiId(emojiId) + .createdAt(dateTime) + .build() db.collection(REACTION_COLLECTION_NAME.string) .document(reaction.id) .set(reaction) diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/dto/ReactionDto.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/dto/ReactionDto.kt index 6d9b4152..f7a5c0c4 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/dto/ReactionDto.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/reaction/dto/ReactionDto.kt @@ -1,6 +1,5 @@ package com.goliath.emojihub.springboot.domain.reaction.dto -import kotlin.streams.asSequence data class ReactionDto( var id: String = "", @@ -8,17 +7,37 @@ data class ReactionDto( var post_id: String = "", var emoji_id: String = "", var created_at: String = "" -) { - constructor(username: String, post_id: String, emoji_id: String, dateTime: String) : this() { - val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" - val outputStrLength: Long = 20 - id = java.util.Random().ints(outputStrLength, 0, source.length) - .asSequence() - .map(source::get) - .joinToString("") - created_by = username - this.post_id = post_id - this.emoji_id = emoji_id - created_at = dateTime +) + +class ReactionDtoBuilder { + private val reactionDto: ReactionDto = ReactionDto() + + fun id(id: String): ReactionDtoBuilder { + reactionDto.id = id + return this + } + + fun createdBy(createdBy: String): ReactionDtoBuilder { + reactionDto.created_by = createdBy + return this + } + + fun postId(postId: String): ReactionDtoBuilder { + reactionDto.post_id = postId + return this + } + + fun emojiId(emojiId: String): ReactionDtoBuilder { + reactionDto.emoji_id = emojiId + return this + } + + fun createdAt(createdAt: String): ReactionDtoBuilder { + reactionDto.created_at = createdAt + return this + } + + fun build(): ReactionDto { + return reactionDto } } \ No newline at end of file diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/dto/UserDto.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/dto/UserDto.kt index e8d5e13d..04a60d46 100644 --- a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/dto/UserDto.kt +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/domain/user/dto/UserDto.kt @@ -4,11 +4,34 @@ data class UserDto ( var email: String = "", var username: String = "", var password: String = "", - var created_emojis: MutableList? = mutableListOf(), - var saved_emojis: MutableList? = mutableListOf(), - var created_posts: MutableList? = mutableListOf(), + var created_emojis: MutableList = mutableListOf(), + var saved_emojis: MutableList = mutableListOf(), + var created_posts: MutableList = mutableListOf(), ) { data class AuthToken( val accessToken: String, ) } + +class UserDtoBuilder { + private val userDto: UserDto = UserDto() + + fun email(email: String): UserDtoBuilder { + userDto.email = email + return this + } + + fun username(username: String): UserDtoBuilder { + userDto.username = username + return this + } + + fun password(password: String): UserDtoBuilder { + userDto.password = password + return this + } + + fun build(): UserDto { + return userDto + } +} \ No newline at end of file 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 c1abb2d3..f20e9990 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 @@ -8,6 +8,7 @@ import com.goliath.emojihub.springboot.global.exception.CustomHttp404 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.domain.user.dto.UserDtoBuilder 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 @@ -41,11 +42,11 @@ class UserService( throw CustomHttp409(ID_EXIST) } val encodedPassword = passwordEncoder.encode(password) - val user = UserDto( - email = email, - username = username, - password = encodedPassword - ) + val user = UserDtoBuilder() + .email(email) + .username(username) + .password(encodedPassword) + .build() userDao.insertUser(user) val authToken = jwtTokenProvider.createToken(username) return UserDto.AuthToken(authToken) @@ -76,43 +77,37 @@ class UserService( reactionDao.deleteReaction(reaction.id) } // delete all posts(and posts' reactions) created by user - if (postIds != null) { - for (postId in postIds) { - val post = postDao.getPost(postId) ?: continue - if (username != post.created_by) continue - 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) + for (postId in postIds) { + val post = postDao.getPost(postId) ?: continue + if (username != post.created_by) continue + 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) } // delete all emojis(and reactions(and reaction id in posts) using these emojis) created by user - if (createdEmojiIds != null) { - for (emojiId in createdEmojiIds) { - val emoji = emojiDao.getEmoji(emojiId) ?: continue - if (username != emoji.created_by) continue - val fileBlobName = username + "_" + emoji.created_at + ".mp4" - val thumbnailBlobName = username + "_" + emoji.created_at + ".jpeg" - emojiDao.deleteFileInStorage(fileBlobName) - emojiDao.deleteFileInStorage(thumbnailBlobName) - val reactions = reactionDao.getReactionsWithField(emojiId, EMOJI_ID.string) - for (reaction in reactions) { - postDao.deleteReaction(reaction.post_id, reaction.id) - reactionDao.deleteReaction(reaction.id) - } - userDao.deleteAllSavedEmojiId(emojiId) - emojiDao.deleteEmoji(emojiId) + for (emojiId in createdEmojiIds) { + val emoji = emojiDao.getEmoji(emojiId) ?: continue + if (username != emoji.created_by) continue + val fileBlobName = username + "_" + emoji.created_at + ".mp4" + val thumbnailBlobName = username + "_" + emoji.created_at + ".jpeg" + emojiDao.deleteFileInStorage(fileBlobName) + emojiDao.deleteFileInStorage(thumbnailBlobName) + val reactions = reactionDao.getReactionsWithField(emojiId, EMOJI_ID.string) + for (reaction in reactions) { + postDao.deleteReaction(reaction.post_id, reaction.id) + reactionDao.deleteReaction(reaction.id) } + userDao.deleteAllSavedEmojiId(emojiId) + emojiDao.deleteEmoji(emojiId) } // unsave all emojis saved by user - if (savedEmojiIds != null) { - for (emojiId in savedEmojiIds) { - if (!emojiDao.existsEmoji(emojiId)) continue - emojiDao.numSavedChange(emojiId, -1) - } + for (emojiId in savedEmojiIds) { + if (!emojiDao.existsEmoji(emojiId)) continue + emojiDao.numSavedChange(emojiId, -1) } // delete user return userDao.deleteUser(username) diff --git a/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/util/IdGenerator.kt b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/util/IdGenerator.kt new file mode 100644 index 00000000..fa092803 --- /dev/null +++ b/springboot/src/main/kotlin/com/goliath/emojihub/springboot/global/util/IdGenerator.kt @@ -0,0 +1,12 @@ +package com.goliath.emojihub.springboot.global.util + +import kotlin.streams.asSequence + +fun generateId(): String { + val source = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" + val outputStrLength: Long = 20 + return java.util.Random().ints(outputStrLength, 0, source.length) + .asSequence() + .map(source::get) + .joinToString("") +} \ No newline at end of file diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/CustomUserSecurityContextFactory.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/CustomUserSecurityContextFactory.kt index b820dab0..0b1cbe09 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/CustomUserSecurityContextFactory.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/CustomUserSecurityContextFactory.kt @@ -1,6 +1,6 @@ package com.goliath.emojihub.springboot.domain -import com.goliath.emojihub.springboot.domain.user.dto.UserDto +import com.goliath.emojihub.springboot.domain.user.dto.UserDtoBuilder import com.goliath.emojihub.springboot.domain.user.model.UserAdapter import org.springframework.security.core.context.SecurityContext import org.springframework.security.core.context.SecurityContextImpl @@ -8,11 +8,11 @@ import org.springframework.security.test.context.support.WithSecurityContextFact class CustomUserSecurityContextFactory : WithSecurityContextFactory { override fun createSecurityContext(customUserAnnotation: WithCustomUser): SecurityContext { - val user = UserDto( - email = customUserAnnotation.email, - username = customUserAnnotation.username, - password = customUserAnnotation.password - ) + val user = UserDtoBuilder() + .email(customUserAnnotation.email) + .username(customUserAnnotation.username) + .password(customUserAnnotation.password) + .build() val userPrincipal = UserAdapter(user) val userToken = UserToken(userPrincipal) 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 eafcd40b..8b421ab0 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 @@ -1,17 +1,21 @@ package com.goliath.emojihub.springboot.domain import com.goliath.emojihub.springboot.domain.emoji.dto.EmojiDto +import com.goliath.emojihub.springboot.domain.emoji.dto.EmojiDtoBuilder import com.goliath.emojihub.springboot.domain.post.dto.PostDto +import com.goliath.emojihub.springboot.domain.post.dto.PostDtoBuilder import com.goliath.emojihub.springboot.domain.post.dto.ReactionWithEmojiUnicode import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDto +import com.goliath.emojihub.springboot.domain.reaction.dto.ReactionDtoBuilder import com.goliath.emojihub.springboot.domain.user.dto.UserDto +import com.goliath.emojihub.springboot.domain.user.dto.UserDtoBuilder class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성 // 각 user는 postSize 만큼 post 생성 // 각 user는 다른 user들의 첫번째 created emoji를 save함 (자신의 다음 user부터 순서대로) // 각 user 'i'는 각 user 'j'(자신 포함)의 첫번째 post에 자신의 이모지(첫번째), 저장한 이모지(첫번째)로 reaction // userSize 만큼 user 생성 - () { +{ var userSize: Int var userList: MutableList @@ -39,51 +43,51 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성 this.reactionList = mutableListOf() for (i in 0 until userSize) { userList.add( - UserDto( - username = "test_username$i", - password = "test_password$i", - email = "test_email$i", - ) + UserDtoBuilder() + .username("test_username$i") + .password("test_password$i") + .email("test_email$i") + .build() ) // 각 user는 createdEmojiSize 만큼 emoji 생성 for (j in 0 until createdEmojiSize) { val emojiId = "test_emoji${i}_${j}" emojiList.add( - EmojiDto( - created_at = "test_created_at${i}_${j}", - created_by = userList[i].username, - emoji_label = "test_emoji_label${i}_${j}", - emoji_unicode = "test_emoji_unicode${i}_${j}", - id = emojiId, - num_saved = 0, - thumbnail_url = "test_thumbnail_url${i}_${j}", - video_url = "test_video_url${i}_${j}", - ) + EmojiDtoBuilder() + .createdAt("test_created_at${i}_${j}") + .createdBy(userList[i].username) + .emojiLabel("test_emoji_label${i}_${j}") + .emojiUnicode("test_emoji_unicode${i}_${j}") + .id(emojiId) + .numSaved(0) + .thumbnailUrl("test_thumbnail_url${i}_${j}") + .videoUrl("test_video_url${i}_${j}") + .build() ) - userList[i].created_emojis!!.add(emojiId) + userList[i].created_emojis.add(emojiId) } // 각 user는 postSize 만큼 post 생성 for (j in 0 until postSize) { val postId = "test_post${i}_${j}" postList.add( - PostDto( - content = "test_content${i}_${j}", - created_at = "test_created_at${i}_${j}", - created_by = userList[i].username, - id = postId, - modified_at = "test_modified_at${i}_${j}", - ) + PostDtoBuilder() + .content("test_content${i}_${j}") + .createdAt("test_created_at${i}_${j}") + .createdBy(userList[i].username) + .id(postId) + .modifiedAt("test_modified_at${i}_${j}") + .build() ) - userList[i].created_posts!!.add(postId) + userList[i].created_posts.add(postId) } } for (i in 0 until userSize) { for (j in i + 1 until userSize) { - userList[i].saved_emojis!!.add(emojiList[createdEmojiSize * j].id) + userList[i].saved_emojis.add(emojiList[createdEmojiSize * j].id) emojiList[createdEmojiSize * j].num_saved++ } for (j in 0 until i) { - userList[i].saved_emojis!!.add(emojiList[createdEmojiSize * j].id) + userList[i].saved_emojis.add(emojiList[createdEmojiSize * j].id) emojiList[createdEmojiSize * j].num_saved++ } } @@ -106,22 +110,22 @@ class TestDto// 각 user는 createdEmojiSize 만큼 emoji 생성 emoji_unicode = savedEmojiUnicode ) reactionList.add( - ReactionDto( - id = reactionIdWithCreatedEmoji, - created_by = userList[i].username, - post_id = postList[postSize * j].id, - emoji_id = userList[i].created_emojis!![0], - created_at = "test_created_at${i}_${j}_c" - ) + ReactionDtoBuilder() + .id(reactionIdWithCreatedEmoji) + .createdBy(userList[i].username) + .postId(postList[postSize * j].id) + .emojiId(userList[i].created_emojis[0]) + .createdAt("test_created_at${i}_${j}_c") + .build() ) reactionList.add( - ReactionDto( - id = reactionIdWithSavedEmoji, - created_by = userList[i].username, - post_id = postList[postSize * j].id, - emoji_id = userList[i].saved_emojis!![0], - created_at = "test_created_at${i}_${j}_s" - ) + ReactionDtoBuilder() + .id(reactionIdWithSavedEmoji) + .createdBy(userList[i].username) + .postId(postList[postSize * j].id) + .emojiId(userList[i].saved_emojis[0]) + .createdAt("test_created_at${i}_${j}_s") + .build() ) 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/dao/EmojiDaoTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDaoTest.kt index dc5df96b..f3be699a 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDaoTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/emoji/dao/EmojiDaoTest.kt @@ -70,6 +70,15 @@ internal class EmojiDaoTest { FirebaseApp.initializeApp(options) } testDB = FirestoreClient.getFirestore() + // Initialization of firestore Database Emojis + val emojiDocuments = testDB.collection(EMOJI_COLLECTION_NAME.string).get().get().documents + for (document in emojiDocuments) { + val emoji = document.toObject(EmojiDto::class.java) + testDB.collection(EMOJI_COLLECTION_NAME.string).document(emoji.id).delete() + } + for (emoji in testDto.emojiList) { + testDB.collection(EMOJI_COLLECTION_NAME.string).document(emoji.id).set(emoji) + } } } 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 5974b379..4c50925a 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 @@ -118,10 +118,10 @@ internal class EmojiServiceTest { ) verify(userDao, times(2)).getUser(username) verify(userDao, times(1)).getUser(wrongUsername) - for (emojiId in user.created_emojis!!) { + for (emojiId in user.created_emojis) { verify(emojiDao, times(1)).getEmoji(emojiId) } - for (emojiId in user.saved_emojis!!) { + for (emojiId in user.saved_emojis) { verify(emojiDao, times(1)).getEmoji(emojiId) } } @@ -238,7 +238,7 @@ internal class EmojiServiceTest { // given val user = testDto.userList[0] val username = user.username - val emojiId = user.created_emojis!![0] + val emojiId = user.created_emojis[0] Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true) Mockito.`when`(userDao.getUser(username)).thenReturn(user) @@ -259,7 +259,7 @@ internal class EmojiServiceTest { // given val user = testDto.userList[0] val username = user.username - val emojiId = user.saved_emojis!![0] + val emojiId = user.saved_emojis[0] Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true) Mockito.`when`(userDao.getUser(username)).thenReturn(user) @@ -280,7 +280,7 @@ internal class EmojiServiceTest { // given val user = testDto.userList[0] val username = user.username - val emojiId = user.saved_emojis!![0] + val emojiId = user.saved_emojis[0] Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true) Mockito.`when`(userDao.getUser(username)).thenReturn(user) @@ -337,7 +337,7 @@ internal class EmojiServiceTest { // given val user = testDto.userList[0] val username = user.username - val emojiId = user.created_emojis!![0] + val emojiId = user.created_emojis[0] Mockito.`when`(emojiDao.existsEmoji(emojiId)).thenReturn(true) Mockito.`when`(userDao.getUser(username)).thenReturn(user) 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 efb2100c..0069f279 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 @@ -56,6 +56,15 @@ internal class PostDaoTest { FirebaseApp.initializeApp(options) } testDB = FirestoreClient.getFirestore() + // Initialization of firestore Database Posts + val postDocuments = testDB.collection(POST_COLLECTION_NAME.string).get().get().documents + for (document in postDocuments) { + val post = document.toObject(PostDto::class.java) + testDB.collection(POST_COLLECTION_NAME.string).document(post.id).delete() + } + for (post in testDto.postList) { + testDB.collection(POST_COLLECTION_NAME.string).document(post.id).set(post) + } } } diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/reaction/dao/ReactionDaoTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/reaction/dao/ReactionDaoTest.kt index b1da82dd..43796abe 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/reaction/dao/ReactionDaoTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/reaction/dao/ReactionDaoTest.kt @@ -53,6 +53,15 @@ internal class ReactionDaoTest { FirebaseApp.initializeApp(options) } testDB = FirestoreClient.getFirestore() + // Initialization of firestore Database Posts + val reactionDocuments = testDB.collection(REACTION_COLLECTION_NAME.string).get().get().documents + for (document in reactionDocuments) { + val reaction = document.toObject(ReactionDto::class.java) + testDB.collection(REACTION_COLLECTION_NAME.string).document(reaction.id).delete() + } + for (reaction in testDto.reactionList) { + testDB.collection(REACTION_COLLECTION_NAME.string).document(reaction.id).set(reaction) + } } } 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 c894ce78..93cc1b8b 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 @@ -79,9 +79,9 @@ internal class UserControllerTest @Autowired constructor( .andExpect(jsonPath("$.email").value(user.email)) .andExpect(jsonPath("$.username").value(user.username)) .andExpect(jsonPath("$.password").value(user.password)) - .andExpect(jsonPath("$.created_emojis.length()", equalTo(user.created_emojis!!.size))) - .andExpect(jsonPath("$.saved_emojis.length()", equalTo(user.saved_emojis!!.size))) - .andExpect(jsonPath("$.created_posts.length()", equalTo(user.created_posts!!.size))) + .andExpect(jsonPath("$.created_emojis.length()", equalTo(user.created_emojis.size))) + .andExpect(jsonPath("$.saved_emojis.length()", equalTo(user.saved_emojis.size))) + .andExpect(jsonPath("$.created_posts.length()", equalTo(user.created_posts.size))) verify(userService, times(1)).getMe(username) } diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/dao/UserDaoTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/dao/UserDaoTest.kt index a8bb5e40..b9ad5973 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/dao/UserDaoTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/dao/UserDaoTest.kt @@ -2,6 +2,7 @@ package com.goliath.emojihub.springboot.domain.user.dao import com.goliath.emojihub.springboot.domain.TestDto import com.goliath.emojihub.springboot.domain.user.dto.UserDto +import com.goliath.emojihub.springboot.domain.user.dto.UserDtoBuilder import com.goliath.emojihub.springboot.global.util.StringValue.FilePathName.TEST_SERVICE_ACCOUNT_KEY import com.goliath.emojihub.springboot.global.util.StringValue.Bucket.TEST_EMOJI_STORAGE_BUCKET_NAME import com.goliath.emojihub.springboot.global.util.StringValue.Collection.USER_COLLECTION_NAME @@ -54,6 +55,15 @@ internal class UserDaoTest { FirebaseApp.initializeApp(options) } testDB = FirestoreClient.getFirestore() + // Initialization of firestore database Users + val userDocuments = testDB.collection(USER_COLLECTION_NAME.string).get().get().documents + for (document in userDocuments) { + val user = document.toObject(UserDto::class.java) + testDB.collection(USER_COLLECTION_NAME.string).document(user.username).delete() + } + for (user in testDto.userList) { + testDB.collection(USER_COLLECTION_NAME.string).document(user.username).set(user) + } } } @@ -110,11 +120,11 @@ internal class UserDaoTest { // given Mockito.`when`(db.collection(USER_COLLECTION_NAME.string)) .thenReturn(testDB.collection(USER_COLLECTION_NAME.string)) - val user = UserDto( - email = "new_test_email", - username = "new_test_username", - password = "new_test_password" - ) + val user = UserDtoBuilder() + .email("new_test_email") + .username("new_test_username") + .password("new_test_password") + .build() // when userDao.insertUser(user) @@ -186,7 +196,7 @@ internal class UserDaoTest { // then var result = userDao.getUser(username) var a = 1 - while (!result!!.created_posts!!.contains(postId) && a <= 5) { + while (!result!!.created_posts.contains(postId) && a <= 5) { result = userDao.getUser(username) a++ } @@ -196,7 +206,7 @@ internal class UserDaoTest { userDao.deleteId(username, postId, CREATED_POSTS.string) result = userDao.getUser(username) var b = 1 - while (result!!.created_posts!!.contains(postId) && b <= 5) { + while (result!!.created_posts.contains(postId) && b <= 5) { result = userDao.getUser(username) b++ } @@ -207,7 +217,7 @@ internal class UserDaoTest { fun deleteId() { // given val username = userList[1].username - val postId = userList[1].created_posts!![1] + val postId = userList[1].created_posts[1] Mockito.`when`(db.collection(USER_COLLECTION_NAME.string)) .thenReturn(testDB.collection(USER_COLLECTION_NAME.string)) @@ -217,7 +227,7 @@ internal class UserDaoTest { // then var result = userDao.getUser(username) var a = 1 - while (result!!.created_posts!!.size != 1 && a <= 5) { + while (result!!.created_posts.size != 1 && a <= 5) { result = userDao.getUser(username) a++ } @@ -227,7 +237,7 @@ internal class UserDaoTest { userDao.insertId(username, postId, CREATED_POSTS.string) result = userDao.getUser(username) var b = 1 - while (!result!!.created_posts!!.contains(postId) && b <= 5) { + while (!result!!.created_posts.contains(postId) && b <= 5) { result = userDao.getUser(username) b++ } @@ -238,7 +248,7 @@ internal class UserDaoTest { fun deleteAllSavedEmojiId() { // given val username = userList[0].username - val emojiId = userList[0].saved_emojis!![0] + val emojiId = userList[0].saved_emojis[0] Mockito.`when`(db.collection(USER_COLLECTION_NAME.string)) .thenReturn(testDB.collection(USER_COLLECTION_NAME.string)) @@ -248,21 +258,21 @@ internal class UserDaoTest { // then var result = userDao.getUser(username) var a = 1 - while (result!!.saved_emojis!!.contains(emojiId) && a <= 5) { + while (result!!.saved_emojis.contains(emojiId) && a <= 5) { result = userDao.getUser(username) a++ } - assertEquals(result.saved_emojis!!.contains(emojiId), false) + assertEquals(result.saved_emojis.contains(emojiId), false) // after work userDao.insertId(username, emojiId, SAVED_EMOJIS.string) result = userDao.getUser(username) var b = 1 - while (!result!!.saved_emojis!!.contains(emojiId) && b <= 5) { + while (!result!!.saved_emojis.contains(emojiId) && b <= 5) { userDao.insertId(username, emojiId, SAVED_EMOJIS.string) result = userDao.getUser(username) b++ } - assertEquals(result.saved_emojis!!.contains(emojiId), true) + assertEquals(result.saved_emojis.contains(emojiId), true) } } \ No newline at end of file diff --git a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserDetailServiceTest.kt b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserDetailServiceTest.kt index bd49b14b..2005a19f 100644 --- a/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserDetailServiceTest.kt +++ b/springboot/src/test/kotlin/com/goliath/emojihub/springboot/domain/user/service/UserDetailServiceTest.kt @@ -1,7 +1,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.dto.UserDto +import com.goliath.emojihub.springboot.domain.user.dto.UserDtoBuilder import com.goliath.emojihub.springboot.global.exception.CustomHttp404 import com.goliath.emojihub.springboot.global.exception.ErrorType.NotFound.USER_FROM_TOKEN_NOT_FOUND import org.junit.jupiter.api.Test @@ -49,11 +49,11 @@ internal class UserDetailServiceTest { fun loadUserByUsernameSucceed() { // given val username = "test_username" - val user = UserDto( - email = "test_email", - username = username, - password = "test_password" - ) + val user = UserDtoBuilder() + .email("test_email") + .username(username) + .password("test_password") + .build() Mockito.`when`(userDao.getUser(username)).thenReturn(user) // when 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 4391832d..32fdb2e1 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 @@ -247,7 +247,7 @@ internal class UserServiceTest { Mockito.`when`(emojiDao.existsEmoji(emoji.id)).thenReturn(true) } for (userDto in testDto.userList) { - val firstEmojiId = userDto.created_emojis!![0] + val firstEmojiId = userDto.created_emojis[0] val reactions = mutableListOf() for (reaction in testDto.reactionList) { if (reaction.emoji_id == firstEmojiId) { 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 48bde940..e1a38ecd 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 @@ -1,6 +1,6 @@ package com.goliath.emojihub.springboot.global.auth -import com.goliath.emojihub.springboot.domain.user.dto.UserDto +import com.goliath.emojihub.springboot.domain.user.dto.UserDtoBuilder 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 @@ -94,11 +94,11 @@ internal class JwtTokenProviderTest { fun getAuthentication() { // given val userDetails = UserAdapter( - UserDto( - username = username, - email = "test_email", - password = "test_password" - ) + UserDtoBuilder() + .username(username) + .email("test_email") + .password("test_password") + .build() ) Mockito.`when`(userDetailService.loadUserByUsername(username)).thenReturn(userDetails)