Get Account
- add AccountController - add AccountEntity toAccountResponse in Mapping.kt - add AccountService - add AccountRepository - add AccountEntity
This commit is contained in:
67
src/test/kotlin/ltd/hlaeja/service/AccountServiceTest.kt
Normal file
67
src/test/kotlin/ltd/hlaeja/service/AccountServiceTest.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
package ltd.hlaeja.service
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.UUID
|
||||
import ltd.hlaeja.entity.AccountEntity
|
||||
import ltd.hlaeja.repository.AccountRepository
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
import reactor.core.publisher.Mono
|
||||
import reactor.test.StepVerifier
|
||||
|
||||
class AccountServiceTest {
|
||||
companion object {
|
||||
val account = UUID.fromString("00000000-0000-0000-0000-000000000002")
|
||||
}
|
||||
|
||||
private lateinit var accountRepository: AccountRepository
|
||||
private lateinit var accountService: AccountService
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
accountRepository = mockk()
|
||||
accountService = AccountService(accountRepository)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get account by id - success`() {
|
||||
// given
|
||||
val accountEntity = AccountEntity(
|
||||
account,
|
||||
ZonedDateTime.now(),
|
||||
ZonedDateTime.now(),
|
||||
true,
|
||||
"username",
|
||||
"password",
|
||||
"ROLE_TEST",
|
||||
)
|
||||
|
||||
every { accountRepository.findById(any(UUID::class)) } returns Mono.just(accountEntity)
|
||||
|
||||
// when
|
||||
StepVerifier.create(accountService.getUserById(account))
|
||||
.expectNext(accountEntity)
|
||||
.verifyComplete()
|
||||
|
||||
// then
|
||||
verify { accountRepository.findById(any(UUID::class)) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `get account by id - fail does not exist`() {
|
||||
// given
|
||||
every { accountRepository.findById(any(UUID::class)) } returns Mono.empty()
|
||||
|
||||
// when
|
||||
StepVerifier.create(accountService.getUserById(account))
|
||||
.expectError(ResponseStatusException::class.java)
|
||||
.verify()
|
||||
|
||||
// then
|
||||
verify { accountRepository.findById(any(UUID::class)) }
|
||||
}
|
||||
}
|
||||
82
src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt
Normal file
82
src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt
Normal file
@@ -0,0 +1,82 @@
|
||||
package ltd.hlaeja.util
|
||||
|
||||
import io.mockk.every
|
||||
import io.mockk.mockkStatic
|
||||
import io.mockk.unmockkStatic
|
||||
import java.time.LocalDateTime
|
||||
import java.time.ZoneId
|
||||
import java.time.ZonedDateTime
|
||||
import java.util.UUID
|
||||
import kotlin.test.assertFailsWith
|
||||
import ltd.hlaeja.entity.AccountEntity
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.AfterEach
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Nested
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
class MappingKtTest {
|
||||
companion object {
|
||||
val account = UUID.fromString("00000000-0000-0000-0000-000000000002")
|
||||
val timestamp: ZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC"))
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun setUp() {
|
||||
mockkStatic(ZonedDateTime::class)
|
||||
every { ZonedDateTime.now() } returns timestamp
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
fun tearDown() {
|
||||
unmockkStatic(ZonedDateTime::class)
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class AccountMapping {
|
||||
|
||||
@Test
|
||||
fun `test toAccountResponse when id is not null`() {
|
||||
// Arrange
|
||||
val accountEntity = AccountEntity(
|
||||
id = account,
|
||||
createdAt = timestamp,
|
||||
updatedAt = timestamp,
|
||||
enabled = true,
|
||||
username = "username",
|
||||
password = "password",
|
||||
roles = "ROLE_ADMIN,ROLE_USER",
|
||||
)
|
||||
|
||||
// Act
|
||||
val result = accountEntity.toAccountResponse()
|
||||
|
||||
// Assert
|
||||
assertThat(result.id).isEqualTo(accountEntity.id)
|
||||
assertThat(result.timestamp).isEqualTo(accountEntity.updatedAt)
|
||||
assertThat(result.enabled).isEqualTo(accountEntity.enabled)
|
||||
assertThat(result.username).isEqualTo(accountEntity.username)
|
||||
assertThat(result.roles).contains("ROLE_ADMIN", "ROLE_USER")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `test toAccountResponse when id is null`() {
|
||||
// Arrange
|
||||
val accountEntity = AccountEntity(
|
||||
id = null,
|
||||
createdAt = timestamp,
|
||||
updatedAt = timestamp,
|
||||
enabled = true,
|
||||
username = "username",
|
||||
password = "password",
|
||||
roles = "ROLE_ADMIN,ROLE_USER",
|
||||
)
|
||||
|
||||
// Act and Assert
|
||||
assertFailsWith<ResponseStatusException> {
|
||||
accountEntity.toAccountResponse()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user