Skip to content

Commit

Permalink
remove : 의미없는 mutable 변수 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
kshired committed Jan 7, 2024
1 parent c20b56c commit 3fc6ed4
Show file tree
Hide file tree
Showing 17 changed files with 54 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.csbroker.apiserver.auth

class GithubOAuth2UserInfo(
attributes: MutableMap<String, Any>,
attributes: Map<String, Any>,
) : OAuth2UserInfo(
id = (attributes["id"] as Int).toString(),
name = attributes["login"] as String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.csbroker.apiserver.auth

class GoogleOAuth2UserInfo(
attributes: MutableMap<String, Any>,
attributes: Map<String, Any>,
) : OAuth2UserInfo(
id = attributes["sub"] as String,
name = attributes["name"] as String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.csbroker.apiserver.common.exception.OAuthProviderMissMatchException

class OAuth2UserInfoFactory {
companion object {
fun getOauth2UserInfo(providerType: ProviderType, attributes: MutableMap<String, Any>): OAuth2UserInfo {
fun getOauth2UserInfo(providerType: ProviderType, attributes: Map<String, Any>): OAuth2UserInfo {
return when (providerType) {
ProviderType.GOOGLE -> GoogleOAuth2UserInfo(attributes)
ProviderType.GITHUB -> GithubOAuth2UserInfo(attributes)
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/io/csbroker/apiserver/auth/UserPrincipal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ class UserPrincipal(
private val password: String,
private val providerType: ProviderType,
private val roleType: Role,
private val authorities: MutableCollection<GrantedAuthority>,
private val authorities: Collection<GrantedAuthority>,
) : OAuth2User, UserDetails, OidcUser {

private lateinit var attributes: MutableMap<String, Any>
private lateinit var attributes: Map<String, Any>

override fun getName(): String {
return userId
}

override fun getAttributes(): MutableMap<String, Any> {
override fun getAttributes(): Map<String, Any> {
return attributes
}

override fun getAuthorities(): MutableCollection<out GrantedAuthority> {
override fun getAuthorities(): Collection<GrantedAuthority> {
return authorities
}

Expand Down Expand Up @@ -57,7 +57,7 @@ class UserPrincipal(
return true
}

override fun getClaims(): MutableMap<String, Any>? {
override fun getClaims(): Map<String, Any>? {
return null
}

Expand All @@ -80,7 +80,7 @@ class UserPrincipal(
)
}

fun create(user: User, attributes: MutableMap<String, Any>): UserPrincipal {
fun create(user: User, attributes: Map<String, Any>): UserPrincipal {
val userPrincipal = create(user)
userPrincipal.attributes = attributes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ data class LongProblemUpsertRequestDto(
val title: String,
val description: String,
val standardAnswers: List<String>,
val tags: MutableList<String>,
val gradingStandards: MutableList<GradingStandardData>,
val tags: List<String>,
val gradingStandards: List<GradingStandardData>,
val isGradable: Boolean = false,
val isActive: Boolean = true,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import io.csbroker.apiserver.model.User
data class MultipleChoiceProblemUpsertRequestDto(
val title: String,
val description: String,
val tags: MutableList<String>,
val choices: MutableList<ChoiceData>,
val tags: List<String>,
val choices: List<ChoiceData>,
val score: Double,
val isGradable: Boolean = true,
val isActive: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.csbroker.apiserver.model.User
data class ShortProblemUpsertRequestDto(
val title: String,
val description: String,
val tags: MutableList<String>,
val tags: List<String>,
val answer: String,
val score: Double,
val isGradable: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,29 @@ 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)
}

private fun setGithubPrimaryEmail(
accessToken: String?,
attributes: MutableMap<String, Any>,
) {
attributes: Map<String, Any>,
): Map<String, Any> {
val emailResponseDto = githubClient.getUserEmail("Bearer $accessToken").first { it.primary }
attributes["email"] = emailResponseDto.email
return hashMapOf<String, Any>().also {
it.putAll(attributes)
it["email"] = emailResponseDto.email
}
}

private fun getOrCreateUser(
providerType: ProviderType,
attributes: MutableMap<String, Any>,
attributes: Map<String, Any>,
): User {
val userInfo = OAuth2UserInfoFactory.getOauth2UserInfo(providerType, attributes)
val savedUser = userRepository.findByEmail(userInfo.email)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,10 @@ class TagUpserter(
problem.problemTags.addAll(problemTags)
}

fun updateTags(problem: Problem, tagNames: MutableList<String>) {
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<String>) {
problem.problemTags.clear()

val tags = tagRepository.findTagsByNameIn(tagNames)

checkEveryTagExist(tags, tagNames)

val problemTags = tags.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class AdminMultipleProblemControllerTest : RestDocsTest() {
val multipleChoiceProblemUpsertRequestDto = MultipleChoiceProblemUpsertRequestDto(
"test",
"test",
mutableListOf("db", "network"),
mutableListOf(
listOf("db", "network"),
listOf(
ChoiceData(
"choice-1",
true,
Expand Down Expand Up @@ -104,8 +104,8 @@ class AdminMultipleProblemControllerTest : RestDocsTest() {
val multipleChoiceProblemUpsertRequestDto = MultipleChoiceProblemUpsertRequestDto(
"test",
"test",
mutableListOf("db", "network"),
mutableListOf(
listOf("db", "network"),
listOf(
ChoiceData(
"choice-1",
true,
Expand Down Expand Up @@ -170,9 +170,9 @@ class AdminMultipleProblemControllerTest : RestDocsTest() {
1L,
"test",
"test",
mutableListOf("db", "network"),
listOf("db", "network"),
true,
mutableListOf(
listOf(
ChoiceData(
"choice-1",
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class AdminShortProblemControllerTest : RestDocsTest() {
val shortProblemUpsertRequestDto = ShortProblemUpsertRequestDto(
"test",
"test",
mutableListOf("db", "network"),
listOf("db", "network"),
"test",
5.0,
)
Expand Down Expand Up @@ -89,7 +89,7 @@ class AdminShortProblemControllerTest : RestDocsTest() {
val shortProblemUpsertRequestDto = ShortProblemUpsertRequestDto(
"test1",
"test1",
mutableListOf("db", "network"),
listOf("db", "network"),
"test",
5.0,
true,
Expand Down Expand Up @@ -144,7 +144,7 @@ class AdminShortProblemControllerTest : RestDocsTest() {
1L,
"test",
"test",
mutableListOf("db", "network"),
listOf("db", "network"),
"test",
5.0,
true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class AdminShortProblemServiceTest {
val requestDto = ShortProblemUpsertRequestDto(
title = "title",
description = "description",
tags = mutableListOf(),
tags = emptyList(),
answer = "answer",
score = 10.0,
)
Expand All @@ -143,7 +143,7 @@ class AdminShortProblemServiceTest {
val requestDto = ShortProblemUpsertRequestDto(
title = "updatedTitle",
description = "updatedDescription",
tags = mutableListOf(),
tags = emptyList(),
answer = "updatedAnswer",
score = 10.0,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<ProblemTag>>()) } returns mutableListOf()
every { problemTagRepository.saveAll(any<List<ProblemTag>>()) } returns emptyList()

// when
tagUpserter.setTags(problem, tagNames)
Expand Down Expand Up @@ -91,7 +91,7 @@ class TagUpserterTest {
every { tagRepository.findTagsByNameIn(tagNames) } returns tags

// when, then
assertThrows<ConditionConflictException> { tagUpserter.updateTags(problem, tagNames.toMutableList()) }
assertThrows<ConditionConflictException> { tagUpserter.updateTags(problem, tagNames) }
}

@Test
Expand All @@ -103,7 +103,7 @@ class TagUpserterTest {
every { tagRepository.findTagsByNameIn(tagNames) } returns tags

// when, then
assertThrows<ConditionConflictException> { tagUpserter.updateTags(problem, tagNames.toMutableList()) }
assertThrows<ConditionConflictException> { tagUpserter.updateTags(problem, tagNames) }
}

private fun getLongProblem(): LongProblem = LongProblem(
Expand Down

0 comments on commit 3fc6ed4

Please sign in to comment.