add AccountUtil.kt with detectChanges
This commit is contained in:
25
src/main/kotlin/ltd/hlaeja/util/AccountUtil.kt
Normal file
25
src/main/kotlin/ltd/hlaeja/util/AccountUtil.kt
Normal file
@@ -0,0 +1,25 @@
|
||||
package ltd.hlaeja.util
|
||||
|
||||
import ltd.hlaeja.entity.AccountEntity
|
||||
import org.springframework.http.HttpStatus.ACCEPTED
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
fun AccountEntity.detectChanges(original: AccountEntity): List<String> {
|
||||
val changes: MutableList<String> = mutableListOf()
|
||||
if (original.password != password) {
|
||||
changes.add("password")
|
||||
}
|
||||
if (original.username != username) {
|
||||
changes.add("username")
|
||||
}
|
||||
if (original.enabled != enabled) {
|
||||
changes.add("enabled")
|
||||
}
|
||||
if (original.roles != roles) {
|
||||
changes.add("roles")
|
||||
}
|
||||
if (changes.isEmpty()) {
|
||||
throw ResponseStatusException(ACCEPTED)
|
||||
}
|
||||
return changes
|
||||
}
|
||||
109
src/test/kotlin/ltd/hlaeja/util/AccountUtilKtTest.kt
Normal file
109
src/test/kotlin/ltd/hlaeja/util/AccountUtilKtTest.kt
Normal file
@@ -0,0 +1,109 @@
|
||||
package ltd.hlaeja.util
|
||||
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.entity.AccountEntity
|
||||
import org.assertj.core.api.SoftAssertions
|
||||
import org.assertj.core.api.junit.jupiter.InjectSoftAssertions
|
||||
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.extension.ExtendWith
|
||||
import org.junit.jupiter.params.ParameterizedTest
|
||||
import org.junit.jupiter.params.provider.CsvSource
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
@ExtendWith(SoftAssertionsExtension::class)
|
||||
class AccountUtilKtTest {
|
||||
companion object {
|
||||
val account = UUID.fromString("00000000-0000-0000-0000-000000000000")
|
||||
val timestamp: ZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC"))
|
||||
val originalUser = AccountEntity(
|
||||
id = account,
|
||||
createdAt = timestamp,
|
||||
updatedAt = timestamp,
|
||||
enabled = true,
|
||||
username = "username",
|
||||
password = "password",
|
||||
roles = "ROLE_TEST",
|
||||
)
|
||||
}
|
||||
|
||||
@InjectSoftAssertions
|
||||
lateinit var softly: SoftAssertions
|
||||
|
||||
@Test
|
||||
fun `no change detected`() {
|
||||
// given
|
||||
val account = AccountEntity(
|
||||
id = account,
|
||||
createdAt = timestamp,
|
||||
updatedAt = timestamp,
|
||||
enabled = true,
|
||||
username = "username",
|
||||
password = "password",
|
||||
roles = "ROLE_TEST",
|
||||
)
|
||||
|
||||
// then exception
|
||||
assertThrows(ResponseStatusException::class.java) {
|
||||
account.detectChanges(originalUser)
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(
|
||||
"false, username, password, ROLE_TEST, enabled",
|
||||
"true, change, password, ROLE_TEST, username",
|
||||
"true, username, change, ROLE_TEST, password",
|
||||
"true, username, password, ROLE_CHANGE, roles",
|
||||
)
|
||||
fun `change one thing`(
|
||||
enabled: Boolean,
|
||||
username: String,
|
||||
password: String,
|
||||
roles: String,
|
||||
expected: String,
|
||||
) {
|
||||
// given
|
||||
val account = AccountEntity(
|
||||
id = account,
|
||||
createdAt = timestamp,
|
||||
updatedAt = timestamp,
|
||||
enabled = enabled,
|
||||
username = username,
|
||||
password = password,
|
||||
roles = roles,
|
||||
)
|
||||
|
||||
// when
|
||||
val result = account.detectChanges(originalUser)
|
||||
|
||||
// then
|
||||
softly.assertThat(result).hasSize(1)
|
||||
softly.assertThat(result[0]).isEqualTo(expected)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `change everything`() {
|
||||
// given
|
||||
val account = AccountEntity(
|
||||
id = account,
|
||||
createdAt = timestamp,
|
||||
updatedAt = timestamp,
|
||||
enabled = false,
|
||||
username = "change",
|
||||
password = "change",
|
||||
roles = "ROLE_CHANGE",
|
||||
)
|
||||
|
||||
// when
|
||||
val result = account.detectChanges(originalUser)
|
||||
|
||||
// then
|
||||
softly.assertThat(result).hasSize(4)
|
||||
softly.assertThat(result).contains("password", "username", "enabled", "roles")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user