diff --git a/src/main/kotlin/ltd/hlaeja/util/Mapping.kt b/src/main/kotlin/ltd/hlaeja/util/Mapping.kt new file mode 100644 index 0000000..d25e234 --- /dev/null +++ b/src/main/kotlin/ltd/hlaeja/util/Mapping.kt @@ -0,0 +1,10 @@ +package ltd.hlaeja.util + +import ltd.hlaeja.entity.TypeEntity +import ltd.hlaeja.library.deviceRegistry.Type +import org.springframework.http.HttpStatus.EXPECTATION_FAILED +import org.springframework.web.server.ResponseStatusException + +fun TypeEntity.toTypeResponse(): Type.Response { + return Type.Response(id ?: throw ResponseStatusException(EXPECTATION_FAILED), name) +} diff --git a/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt b/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt new file mode 100644 index 0000000..02169d7 --- /dev/null +++ b/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt @@ -0,0 +1,48 @@ +package ltd.hlaeja.util + +import java.time.LocalDateTime +import java.time.ZoneId +import java.time.ZonedDateTime +import java.util.UUID +import kotlin.test.Test +import ltd.hlaeja.assertj.assertThat +import ltd.hlaeja.entity.TypeEntity +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Nested +import org.springframework.web.server.ResponseStatusException + +class MappingKtTest { + companion object { + val timestamp: ZonedDateTime = ZonedDateTime.of(LocalDateTime.of(2000, 1, 1, 0, 0, 0, 1), ZoneId.of("UTC")) + } + + @Nested + inner class TypeMapping { + + @Test + fun `entity to response successful`() { + // given + val entity = TypeEntity( + UUID.fromString("00000000-0000-0000-0000-000000000000"), + timestamp, + "name", + ) + + // when + val response = entity.toTypeResponse() + + // then + assertThat(response.id).isUUID("00000000-0000-0000-0000-000000000000") + assertThat(response.name).isEqualTo("name") + } + + @Test + fun `entity to response exception`() { + // then exception + assertThrows(ResponseStatusException::class.java) { + TypeEntity(null, timestamp, "name").toTypeResponse() + } + } + } +}