From 3fc6ed44eccecd3d9271488d59cc1a07b718e09b Mon Sep 17 00:00:00 2001 From: kshired Date: Sun, 7 Jan 2024 15:55:02 +0900 Subject: [PATCH] =?UTF-8?q?remove=20:=20=EC=9D=98=EB=AF=B8=EC=97=86?= =?UTF-8?q?=EB=8A=94=20mutable=20=EB=B3=80=EC=88=98=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../apiserver/auth/GithubOAuth2UserInfo.kt | 2 +- .../apiserver/auth/GoogleOAuth2UserInfo.kt | 2 +- .../apiserver/auth/OAuth2UserInfoFactory.kt | 2 +- .../csbroker/apiserver/auth/UserPrincipal.kt | 12 ++++++------ .../LongProblemUpsertRequestDto.kt | 4 ++-- .../MultipleChoiceProblemUpsertRequestDto.kt | 4 ++-- .../ShortProblemUpsertRequestDto.kt | 2 +- .../service/auth/CustomOAuth2UserService.kt | 19 +++++++++++-------- .../service/problem/admin/TagUpserter.kt | 17 ++--------------- .../problem/AdminLongProblemControllerTest.kt | 4 ++-- .../AdminMultipleProblemControllerTest.kt | 12 ++++++------ .../problem/AdminProblemControllerTest.kt | 2 +- .../AdminShortProblemControllerTest.kt | 6 +++--- .../admin/AdminLongProblemServiceTest.kt | 4 ++-- .../admin/AdminMultipleProblemServiceTest.kt | 16 ++++++++-------- .../admin/AdminShortProblemServiceTest.kt | 4 ++-- .../service/problem/admin/TagUpserterTest.kt | 6 +++--- 17 files changed, 54 insertions(+), 64 deletions(-) diff --git a/src/main/kotlin/io/csbroker/apiserver/auth/GithubOAuth2UserInfo.kt b/src/main/kotlin/io/csbroker/apiserver/auth/GithubOAuth2UserInfo.kt index 79b8c3ec..614b2f10 100644 --- a/src/main/kotlin/io/csbroker/apiserver/auth/GithubOAuth2UserInfo.kt +++ b/src/main/kotlin/io/csbroker/apiserver/auth/GithubOAuth2UserInfo.kt @@ -1,7 +1,7 @@ package io.csbroker.apiserver.auth class GithubOAuth2UserInfo( - attributes: MutableMap, + attributes: Map, ) : OAuth2UserInfo( id = (attributes["id"] as Int).toString(), name = attributes["login"] as String, diff --git a/src/main/kotlin/io/csbroker/apiserver/auth/GoogleOAuth2UserInfo.kt b/src/main/kotlin/io/csbroker/apiserver/auth/GoogleOAuth2UserInfo.kt index 4f02f5f0..a487bbe2 100644 --- a/src/main/kotlin/io/csbroker/apiserver/auth/GoogleOAuth2UserInfo.kt +++ b/src/main/kotlin/io/csbroker/apiserver/auth/GoogleOAuth2UserInfo.kt @@ -1,7 +1,7 @@ package io.csbroker.apiserver.auth class GoogleOAuth2UserInfo( - attributes: MutableMap, + attributes: Map, ) : OAuth2UserInfo( id = attributes["sub"] as String, name = attributes["name"] as String, diff --git a/src/main/kotlin/io/csbroker/apiserver/auth/OAuth2UserInfoFactory.kt b/src/main/kotlin/io/csbroker/apiserver/auth/OAuth2UserInfoFactory.kt index 5fae279c..830cef17 100644 --- a/src/main/kotlin/io/csbroker/apiserver/auth/OAuth2UserInfoFactory.kt +++ b/src/main/kotlin/io/csbroker/apiserver/auth/OAuth2UserInfoFactory.kt @@ -4,7 +4,7 @@ import io.csbroker.apiserver.common.exception.OAuthProviderMissMatchException class OAuth2UserInfoFactory { companion object { - fun getOauth2UserInfo(providerType: ProviderType, attributes: MutableMap): OAuth2UserInfo { + fun getOauth2UserInfo(providerType: ProviderType, attributes: Map): OAuth2UserInfo { return when (providerType) { ProviderType.GOOGLE -> GoogleOAuth2UserInfo(attributes) ProviderType.GITHUB -> GithubOAuth2UserInfo(attributes) diff --git a/src/main/kotlin/io/csbroker/apiserver/auth/UserPrincipal.kt b/src/main/kotlin/io/csbroker/apiserver/auth/UserPrincipal.kt index 61273bb5..e4a41a28 100644 --- a/src/main/kotlin/io/csbroker/apiserver/auth/UserPrincipal.kt +++ b/src/main/kotlin/io/csbroker/apiserver/auth/UserPrincipal.kt @@ -16,20 +16,20 @@ class UserPrincipal( private val password: String, private val providerType: ProviderType, private val roleType: Role, - private val authorities: MutableCollection, + private val authorities: Collection, ) : OAuth2User, UserDetails, OidcUser { - private lateinit var attributes: MutableMap + private lateinit var attributes: Map override fun getName(): String { return userId } - override fun getAttributes(): MutableMap { + override fun getAttributes(): Map { return attributes } - override fun getAuthorities(): MutableCollection { + override fun getAuthorities(): Collection { return authorities } @@ -57,7 +57,7 @@ class UserPrincipal( return true } - override fun getClaims(): MutableMap? { + override fun getClaims(): Map? { return null } @@ -80,7 +80,7 @@ class UserPrincipal( ) } - fun create(user: User, attributes: MutableMap): UserPrincipal { + fun create(user: User, attributes: Map): UserPrincipal { val userPrincipal = create(user) userPrincipal.attributes = attributes diff --git a/src/main/kotlin/io/csbroker/apiserver/dto/problem/longproblem/LongProblemUpsertRequestDto.kt b/src/main/kotlin/io/csbroker/apiserver/dto/problem/longproblem/LongProblemUpsertRequestDto.kt index b6c4c966..f56896ab 100644 --- a/src/main/kotlin/io/csbroker/apiserver/dto/problem/longproblem/LongProblemUpsertRequestDto.kt +++ b/src/main/kotlin/io/csbroker/apiserver/dto/problem/longproblem/LongProblemUpsertRequestDto.kt @@ -9,8 +9,8 @@ data class LongProblemUpsertRequestDto( val title: String, val description: String, val standardAnswers: List, - val tags: MutableList, - val gradingStandards: MutableList, + val tags: List, + val gradingStandards: List, val isGradable: Boolean = false, val isActive: Boolean = true, ) { diff --git a/src/main/kotlin/io/csbroker/apiserver/dto/problem/multiplechoiceproblem/MultipleChoiceProblemUpsertRequestDto.kt b/src/main/kotlin/io/csbroker/apiserver/dto/problem/multiplechoiceproblem/MultipleChoiceProblemUpsertRequestDto.kt index be882a78..6bccf7e5 100644 --- a/src/main/kotlin/io/csbroker/apiserver/dto/problem/multiplechoiceproblem/MultipleChoiceProblemUpsertRequestDto.kt +++ b/src/main/kotlin/io/csbroker/apiserver/dto/problem/multiplechoiceproblem/MultipleChoiceProblemUpsertRequestDto.kt @@ -7,8 +7,8 @@ import io.csbroker.apiserver.model.User data class MultipleChoiceProblemUpsertRequestDto( val title: String, val description: String, - val tags: MutableList, - val choices: MutableList, + val tags: List, + val choices: List, val score: Double, val isGradable: Boolean = true, val isActive: Boolean = true, diff --git a/src/main/kotlin/io/csbroker/apiserver/dto/problem/shortproblem/ShortProblemUpsertRequestDto.kt b/src/main/kotlin/io/csbroker/apiserver/dto/problem/shortproblem/ShortProblemUpsertRequestDto.kt index b501220c..df0cf2b7 100644 --- a/src/main/kotlin/io/csbroker/apiserver/dto/problem/shortproblem/ShortProblemUpsertRequestDto.kt +++ b/src/main/kotlin/io/csbroker/apiserver/dto/problem/shortproblem/ShortProblemUpsertRequestDto.kt @@ -6,7 +6,7 @@ import io.csbroker.apiserver.model.User data class ShortProblemUpsertRequestDto( val title: String, val description: String, - val tags: MutableList, + val tags: List, val answer: String, val score: Double, val isGradable: Boolean = true, diff --git a/src/main/kotlin/io/csbroker/apiserver/service/auth/CustomOAuth2UserService.kt b/src/main/kotlin/io/csbroker/apiserver/service/auth/CustomOAuth2UserService.kt index b1312431..2b63ccb2 100644 --- a/src/main/kotlin/io/csbroker/apiserver/service/auth/CustomOAuth2UserService.kt +++ b/src/main/kotlin/io/csbroker/apiserver/service/auth/CustomOAuth2UserService.kt @@ -42,10 +42,10 @@ class CustomOAuth2UserService( val accessToken = userRequest.accessToken.tokenValue - val attributes = user.attributes.toMutableMap() - - if (providerType == ProviderType.GITHUB && attributes["email"] == null) { - setGithubPrimaryEmail(accessToken, attributes) + val attributes = if (providerType == ProviderType.GITHUB && user.attributes["email"] == null) { + setGithubPrimaryEmail(accessToken, user.attributes) + } else { + user.attributes } return UserPrincipal.create(getOrCreateUser(providerType, attributes), attributes) @@ -53,15 +53,18 @@ class CustomOAuth2UserService( private fun setGithubPrimaryEmail( accessToken: String?, - attributes: MutableMap, - ) { + attributes: Map, + ): Map { val emailResponseDto = githubClient.getUserEmail("Bearer $accessToken").first { it.primary } - attributes["email"] = emailResponseDto.email + return hashMapOf().also { + it.putAll(attributes) + it["email"] = emailResponseDto.email + } } private fun getOrCreateUser( providerType: ProviderType, - attributes: MutableMap, + attributes: Map, ): User { val userInfo = OAuth2UserInfoFactory.getOauth2UserInfo(providerType, attributes) val savedUser = userRepository.findByEmail(userInfo.email) diff --git a/src/main/kotlin/io/csbroker/apiserver/service/problem/admin/TagUpserter.kt b/src/main/kotlin/io/csbroker/apiserver/service/problem/admin/TagUpserter.kt index 506cc0d2..4786f22a 100644 --- a/src/main/kotlin/io/csbroker/apiserver/service/problem/admin/TagUpserter.kt +++ b/src/main/kotlin/io/csbroker/apiserver/service/problem/admin/TagUpserter.kt @@ -31,23 +31,10 @@ class TagUpserter( problem.problemTags.addAll(problemTags) } - fun updateTags(problem: Problem, tagNames: MutableList) { - problem.problemTags.removeIf { - if (it.tag.name !in tagNames) { - problemTagRepository.delete(it) - return@removeIf true - } - return@removeIf false - } - - tagNames.removeIf { - it in problem.problemTags.map { pt -> - pt.tag.name - } - } + fun updateTags(problem: Problem, tagNames: List) { + problem.problemTags.clear() val tags = tagRepository.findTagsByNameIn(tagNames) - checkEveryTagExist(tags, tagNames) val problemTags = tags.map { diff --git a/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminLongProblemControllerTest.kt b/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminLongProblemControllerTest.kt index d64b0a83..7cc4bd2b 100644 --- a/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminLongProblemControllerTest.kt +++ b/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminLongProblemControllerTest.kt @@ -288,8 +288,8 @@ class AdminLongProblemControllerTest : RestDocsTest() { "test", "test", listOf("test"), - mutableListOf("db", "network"), - mutableListOf( + listOf("db", "network"), + listOf( LongProblemUpsertRequestDto.GradingStandardData( "keyword-1", 1.0, diff --git a/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminMultipleProblemControllerTest.kt b/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminMultipleProblemControllerTest.kt index ac3b9ed3..e061e1f2 100644 --- a/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminMultipleProblemControllerTest.kt +++ b/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminMultipleProblemControllerTest.kt @@ -42,8 +42,8 @@ class AdminMultipleProblemControllerTest : RestDocsTest() { val multipleChoiceProblemUpsertRequestDto = MultipleChoiceProblemUpsertRequestDto( "test", "test", - mutableListOf("db", "network"), - mutableListOf( + listOf("db", "network"), + listOf( ChoiceData( "choice-1", true, @@ -104,8 +104,8 @@ class AdminMultipleProblemControllerTest : RestDocsTest() { val multipleChoiceProblemUpsertRequestDto = MultipleChoiceProblemUpsertRequestDto( "test", "test", - mutableListOf("db", "network"), - mutableListOf( + listOf("db", "network"), + listOf( ChoiceData( "choice-1", true, @@ -170,9 +170,9 @@ class AdminMultipleProblemControllerTest : RestDocsTest() { 1L, "test", "test", - mutableListOf("db", "network"), + listOf("db", "network"), true, - mutableListOf( + listOf( ChoiceData( "choice-1", true, diff --git a/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminProblemControllerTest.kt b/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminProblemControllerTest.kt index 381192ba..ae052d55 100644 --- a/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminProblemControllerTest.kt +++ b/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminProblemControllerTest.kt @@ -64,7 +64,7 @@ class AdminProblemControllerTest : RestDocsTest() { fun `Delete Problems By Ids 200`() { // given val problemDeleteRequestDto = ProblemDeleteRequestDto( - mutableListOf(1L, 2L, 3L), + listOf(1L, 2L, 3L), ) justRun { problemService.removeProblemsById(any()) } diff --git a/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminShortProblemControllerTest.kt b/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminShortProblemControllerTest.kt index 80e91197..2c182200 100644 --- a/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminShortProblemControllerTest.kt +++ b/src/test/kotlin/io/csbroker/apiserver/controller/v1/admin/problem/AdminShortProblemControllerTest.kt @@ -41,7 +41,7 @@ class AdminShortProblemControllerTest : RestDocsTest() { val shortProblemUpsertRequestDto = ShortProblemUpsertRequestDto( "test", "test", - mutableListOf("db", "network"), + listOf("db", "network"), "test", 5.0, ) @@ -89,7 +89,7 @@ class AdminShortProblemControllerTest : RestDocsTest() { val shortProblemUpsertRequestDto = ShortProblemUpsertRequestDto( "test1", "test1", - mutableListOf("db", "network"), + listOf("db", "network"), "test", 5.0, true, @@ -144,7 +144,7 @@ class AdminShortProblemControllerTest : RestDocsTest() { 1L, "test", "test", - mutableListOf("db", "network"), + listOf("db", "network"), "test", 5.0, true, diff --git a/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminLongProblemServiceTest.kt b/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminLongProblemServiceTest.kt index 63f144b7..e640c24f 100644 --- a/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminLongProblemServiceTest.kt +++ b/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminLongProblemServiceTest.kt @@ -103,9 +103,9 @@ class AdminLongProblemServiceTest { return LongProblemUpsertRequestDto( title = "Test problem", description = "This is a test problem", - tags = mutableListOf("tag 1", "tag 2"), + tags = listOf("tag 1", "tag 2"), standardAnswers = emptyList(), - gradingStandards = mutableListOf(), + gradingStandards = emptyList(), ) } } diff --git a/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminMultipleProblemServiceTest.kt b/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminMultipleProblemServiceTest.kt index ac018315..950af3c2 100644 --- a/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminMultipleProblemServiceTest.kt +++ b/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminMultipleProblemServiceTest.kt @@ -141,8 +141,8 @@ class AdminMultipleProblemServiceTest { val createRequestDto = MultipleChoiceProblemUpsertRequestDto( title = "title", description = "description", - tags = mutableListOf(), - choices = mutableListOf(), + tags = emptyList(), + choices = emptyList(), score = 10.0, ) every { tagUpserter.setTags(any(), any()) } just runs @@ -161,8 +161,8 @@ class AdminMultipleProblemServiceTest { val createRequestDto = MultipleChoiceProblemUpsertRequestDto( title = "title", description = "description", - tags = mutableListOf(), - choices = mutableListOf(ChoiceData(content = "content", isAnswer = true)), + tags = emptyList(), + choices = listOf(ChoiceData(content = "content", isAnswer = true)), score = 10.0, ) every { tagUpserter.setTags(any(), any()) } just runs @@ -181,8 +181,8 @@ class AdminMultipleProblemServiceTest { val requestDto = MultipleChoiceProblemUpsertRequestDto( title = "title", description = "description", - tags = mutableListOf(), - choices = mutableListOf(), + tags = emptyList(), + choices = emptyList(), score = 10.0, ) every { multipleChoiceProblemRepository.findByIdOrNull(any()) } returns null @@ -200,8 +200,8 @@ class AdminMultipleProblemServiceTest { val requestDto = MultipleChoiceProblemUpsertRequestDto( title = "title", description = "description", - tags = mutableListOf(), - choices = mutableListOf(), + tags = emptyList(), + choices = emptyList(), score = 10.0, ) every { multipleChoiceProblemRepository.findByIdOrNull(problem.id) } returns problem diff --git a/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminShortProblemServiceTest.kt b/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminShortProblemServiceTest.kt index 97b30563..9a984e7c 100644 --- a/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminShortProblemServiceTest.kt +++ b/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/AdminShortProblemServiceTest.kt @@ -119,7 +119,7 @@ class AdminShortProblemServiceTest { val requestDto = ShortProblemUpsertRequestDto( title = "title", description = "description", - tags = mutableListOf(), + tags = emptyList(), answer = "answer", score = 10.0, ) @@ -143,7 +143,7 @@ class AdminShortProblemServiceTest { val requestDto = ShortProblemUpsertRequestDto( title = "updatedTitle", description = "updatedDescription", - tags = mutableListOf(), + tags = emptyList(), answer = "updatedAnswer", score = 10.0, ) diff --git a/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/TagUpserterTest.kt b/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/TagUpserterTest.kt index 7e1c8745..1625ea35 100644 --- a/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/TagUpserterTest.kt +++ b/src/test/kotlin/io/csbroker/apiserver/service/problem/admin/TagUpserterTest.kt @@ -44,7 +44,7 @@ class TagUpserterTest { val tag1 = Tag(id = 1, name = "tag1") val tag2 = Tag(id = 2, name = "tag2") every { tagRepository.findTagsByNameIn(tagNames) } returns listOf(tag1, tag2) - every { problemTagRepository.saveAll(any>()) } returns mutableListOf() + every { problemTagRepository.saveAll(any>()) } returns emptyList() // when tagUpserter.setTags(problem, tagNames) @@ -91,7 +91,7 @@ class TagUpserterTest { every { tagRepository.findTagsByNameIn(tagNames) } returns tags // when, then - assertThrows { tagUpserter.updateTags(problem, tagNames.toMutableList()) } + assertThrows { tagUpserter.updateTags(problem, tagNames) } } @Test @@ -103,7 +103,7 @@ class TagUpserterTest { every { tagRepository.findTagsByNameIn(tagNames) } returns tags // when, then - assertThrows { tagUpserter.updateTags(problem, tagNames.toMutableList()) } + assertThrows { tagUpserter.updateTags(problem, tagNames) } } private fun getLongProblem(): LongProblem = LongProblem(