From 919ede7da86ef530b618cb4ea7d800c4a4486d75 Mon Sep 17 00:00:00 2001 From: Swordsteel Date: Fri, 22 Nov 2024 23:44:14 +0100 Subject: [PATCH] add toIdentityResponse to mapping --- src/main/kotlin/ltd/hlaeja/util/Mapping.kt | 7 +++ .../kotlin/ltd/hlaeja/util/MappingKtTest.kt | 44 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/src/main/kotlin/ltd/hlaeja/util/Mapping.kt b/src/main/kotlin/ltd/hlaeja/util/Mapping.kt index d526d22..19a8d5e 100644 --- a/src/main/kotlin/ltd/hlaeja/util/Mapping.kt +++ b/src/main/kotlin/ltd/hlaeja/util/Mapping.kt @@ -3,6 +3,7 @@ package ltd.hlaeja.util import java.time.ZonedDateTime import ltd.hlaeja.entity.NodeEntity import ltd.hlaeja.entity.TypeEntity +import ltd.hlaeja.library.deviceRegistry.Identity import ltd.hlaeja.library.deviceRegistry.Node import ltd.hlaeja.library.deviceRegistry.Type import org.springframework.http.HttpStatus.EXPECTATION_FAILED @@ -29,3 +30,9 @@ fun NodeEntity.toNodeResponse(): Node.Response = Node.Response( device, name, ) + +fun NodeEntity.toIdentityResponse(): Identity.Response = Identity.Response( + client, + id ?: throw ResponseStatusException(EXPECTATION_FAILED), + device, +) diff --git a/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt b/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt index 02849e4..6ee5f9b 100644 --- a/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt +++ b/src/test/kotlin/ltd/hlaeja/util/MappingKtTest.kt @@ -145,4 +145,48 @@ class MappingKtTest { assertThat(exception.message).isEqualTo("417 EXPECTATION_FAILED") } } + + @Nested + inner class IdentityMapping { + + @Test + fun `entity to identity response successful`() { + // given + val entity = NodeEntity( + UUID.fromString("00000000-0000-0000-0000-000000000001"), + timestamp, + UUID.fromString("00000000-0000-0000-0000-000000000002"), + UUID.fromString("00000000-0000-0000-0000-000000000003"), + "test", + ) + + // when + val result = entity.toIdentityResponse() + + // then + assertThat(result.node).isUUID("00000000-0000-0000-0000-000000000001") + assertThat(result.client).isUUID("00000000-0000-0000-0000-000000000002") + assertThat(result.device).isUUID("00000000-0000-0000-0000-000000000003") + } + + @Test + fun `entity to identity response exception`() { + // given + val entity = NodeEntity( + null, + timestamp, + UUID.fromString("00000000-0000-0000-0000-000000000002"), + UUID.fromString("00000000-0000-0000-0000-000000000003"), + "test", + ) + + // then exception + val exception = assertThrows(ResponseStatusException::class.java) { + entity.toIdentityResponse() + } + + // then + assertThat(exception.message).isEqualTo("417 EXPECTATION_FAILED") + } + } }