add node to mapping
This commit is contained in:
@@ -1,13 +1,31 @@
|
|||||||
package ltd.hlaeja.util
|
package ltd.hlaeja.util
|
||||||
|
|
||||||
import java.time.ZonedDateTime
|
import java.time.ZonedDateTime
|
||||||
|
import ltd.hlaeja.entity.NodeEntity
|
||||||
import ltd.hlaeja.entity.TypeEntity
|
import ltd.hlaeja.entity.TypeEntity
|
||||||
|
import ltd.hlaeja.library.deviceRegistry.Node
|
||||||
import ltd.hlaeja.library.deviceRegistry.Type
|
import ltd.hlaeja.library.deviceRegistry.Type
|
||||||
import org.springframework.http.HttpStatus.EXPECTATION_FAILED
|
import org.springframework.http.HttpStatus.EXPECTATION_FAILED
|
||||||
import org.springframework.web.server.ResponseStatusException
|
import org.springframework.web.server.ResponseStatusException
|
||||||
|
|
||||||
fun Type.Request.toTypeEntity(): TypeEntity = TypeEntity(null, ZonedDateTime.now(), name)
|
fun Type.Request.toTypeEntity(): TypeEntity = TypeEntity(null, ZonedDateTime.now(), name)
|
||||||
|
|
||||||
fun TypeEntity.toTypeResponse(): Type.Response {
|
fun TypeEntity.toTypeResponse(): Type.Response = Type.Response(
|
||||||
return Type.Response(id ?: throw ResponseStatusException(EXPECTATION_FAILED), name)
|
id ?: throw ResponseStatusException(EXPECTATION_FAILED),
|
||||||
}
|
name,
|
||||||
|
)
|
||||||
|
|
||||||
|
fun Node.Request.toEntity(): NodeEntity = NodeEntity(
|
||||||
|
null,
|
||||||
|
ZonedDateTime.now(),
|
||||||
|
client,
|
||||||
|
device,
|
||||||
|
name,
|
||||||
|
)
|
||||||
|
|
||||||
|
fun NodeEntity.toNodeResponse(): Node.Response = Node.Response(
|
||||||
|
id ?: throw ResponseStatusException(EXPECTATION_FAILED),
|
||||||
|
client,
|
||||||
|
device,
|
||||||
|
name,
|
||||||
|
)
|
||||||
|
|||||||
@@ -9,8 +9,10 @@ import java.time.ZonedDateTime
|
|||||||
import java.util.UUID
|
import java.util.UUID
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import ltd.hlaeja.assertj.assertThat
|
import ltd.hlaeja.assertj.assertThat
|
||||||
|
import ltd.hlaeja.entity.NodeEntity
|
||||||
import ltd.hlaeja.entity.TypeEntity
|
import ltd.hlaeja.entity.TypeEntity
|
||||||
import ltd.hlaeja.library.deviceRegistry.Type
|
import ltd.hlaeja.library.deviceRegistry.Type
|
||||||
|
import ltd.hlaeja.library.deviceRegistry.Node
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.junit.jupiter.api.AfterEach
|
import org.junit.jupiter.api.AfterEach
|
||||||
import org.junit.jupiter.api.Assertions.assertThrows
|
import org.junit.jupiter.api.Assertions.assertThrows
|
||||||
@@ -78,4 +80,69 @@ class MappingKtTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nested
|
||||||
|
inner class NodeMapping {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `request to entity successful`() {
|
||||||
|
// given
|
||||||
|
val request = Node.Request(
|
||||||
|
UUID.fromString("00000000-0000-0000-0000-000000000001"),
|
||||||
|
UUID.fromString("00000000-0000-0000-0000-000000000002"),
|
||||||
|
"test",
|
||||||
|
)
|
||||||
|
|
||||||
|
// when
|
||||||
|
val result = request.toEntity()
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result.id).isNull()
|
||||||
|
assertThat(result.timestamp.toString()).isEqualTo("2000-01-01T00:00:00.000000001Z[UTC]")
|
||||||
|
assertThat(result.client.toString()).isEqualTo("00000000-0000-0000-0000-000000000001")
|
||||||
|
assertThat(result.device.toString()).isEqualTo("00000000-0000-0000-0000-000000000002")
|
||||||
|
assertThat(result.name).isEqualTo("test")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `entity to 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.toNodeResponse()
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(result.id).isUUID("00000000-0000-0000-0000-000000000001")
|
||||||
|
assertThat(result.client).isUUID("00000000-0000-0000-0000-000000000002")
|
||||||
|
assertThat(result.device).isUUID("00000000-0000-0000-0000-000000000003")
|
||||||
|
assertThat(result.name).isEqualTo("test")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `entity to 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.toNodeResponse()
|
||||||
|
}
|
||||||
|
|
||||||
|
// then
|
||||||
|
assertThat(exception.message).isEqualTo("417 EXPECTATION_FAILED")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user