Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove : 의미없는 mutable 변수 제거 #172

Merged
merged 4 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -74,7 +74,7 @@ class AdminMultipleProblemServiceImpl(
findProblem.addChoices(choiceDataList)
findProblem.updateFromDto(updateRequestDto)

tagUpserter.updateTags(findProblem, updateRequestDto.tags)
tagUpserter.updateTags(findProblem, updateRequestDto.tags.toMutableList())

return id
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class AdminShortProblemServiceImpl(
val findProblem = shortProblemRepository.findByIdOrNull(id)
?: throw EntityNotFoundException("${id}번 문제는 존재하지 않는 단답형 문제입니다.")

tagUpserter.updateTags(findProblem, updateRequestDto.tags)
tagUpserter.updateTags(findProblem, updateRequestDto.tags.toMutableList())
findProblem.updateFromDto(updateRequestDto)

return id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class TagUpserter(
}

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
@@ -0,0 +1,77 @@
package io.csbroker.apiserver.controller.v1.admin.problem

import io.csbroker.apiserver.controller.IntegrationTest
import io.csbroker.apiserver.dto.problem.multiplechoiceproblem.MultipleChoiceProblemUpsertRequestDto
import io.csbroker.apiserver.model.MultipleChoiceProblem
import io.csbroker.apiserver.model.ProblemTag
import io.csbroker.apiserver.model.Tag
import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import org.springframework.http.HttpMethod
import org.springframework.test.web.servlet.result.MockMvcResultMatchers

class AdminMultipleProblemIntegrationTest : IntegrationTest() {
@Test
fun `multiple 문제 tag 업데이트`() {
// given
val problem = save(
MultipleChoiceProblem(
title = "문제 제목",
description = "문제 설명",
creator = adminUser,
score = 10.0,
isMultiple = false,
),
)
val tag = save(
Tag(
name = "tag1",
),
)
save(
Tag(
name = "tag2",
),
)
save(
ProblemTag(
problem = problem,
tag = tag,
),
)

// when
val response = request(
method = HttpMethod.PUT,
url = "/api/admin/problems/multiple/${problem.id}",
isAdmin = true,
body = MultipleChoiceProblemUpsertRequestDto(
title = problem.title,
description = problem.description,
choices = listOf(
MultipleChoiceProblemUpsertRequestDto.ChoiceData(
content = "choice1",
isAnswer = true,
),
MultipleChoiceProblemUpsertRequestDto.ChoiceData(
content = "choice2",
isAnswer = false,
),
),
tags = listOf("tag2"),
score = problem.score,
),
)

// then
response.andExpect(MockMvcResultMatchers.status().isOk)
.andExpect {
val problemTags = findAll<ProblemTag>(
"SELECT p FROM ProblemTag p join fetch p.tag where p.problem.id = :problemId",
mapOf("problemId" to problem.id),
)
problemTags.size shouldBe 1
problemTags[0].tag.name shouldBe "tag2"
}
}
}
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
Loading