From 3effd930ad1951ff875f2785752db302dca9e740 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Tue, 5 Aug 2025 06:52:02 +0200 Subject: [PATCH] add AccountUtil.kt with detectChanges --- .../kotlin/ltd/hlaeja/util/AccountUtil.kt | 25 ++++ .../ltd/hlaeja/util/AccountUtilKtTest.kt | 109 ++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 src/main/kotlin/ltd/hlaeja/util/AccountUtil.kt create mode 100644 src/test/kotlin/ltd/hlaeja/util/AccountUtilKtTest.kt diff --git a/src/main/kotlin/ltd/hlaeja/util/AccountUtil.kt b/src/main/kotlin/ltd/hlaeja/util/AccountUtil.kt new file mode 100644 index 0000000..cbf45ae --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/util/AccountUtil.kt @@ -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 { + val changes: MutableList = 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 +} diff --git a/src/test/kotlin/ltd/hlaeja/util/AccountUtilKtTest.kt b/src/test/kotlin/ltd/hlaeja/util/AccountUtilKtTest.kt new file mode 100644 index 0000000..c8f9985 --- /dev/null +++ b/src/test/kotlin/ltd/hlaeja/util/AccountUtilKtTest.kt @@ -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") + } +}