From 27e89c80fc2b2d20aec5f15db935345e94bb35a5 Mon Sep 17 00:00:00 2001 From: oskar Date: Mon, 12 Jan 2026 22:26:08 +0100 Subject: [PATCH] works --- .../com/mosenioring/app/telemetry/TelemetryModels.kt | 3 ++- .../mosenioring/app/telemetry/TelemetryService.kt | 2 ++ .../mosenioring/app/api/TelemetryControllerTest.kt | 2 +- .../app/telemetry/TelemetryServiceTest.kt | 5 +++-- .../mosenioring/common/web/ProblemDetailsAdvice.kt | 12 ++++++++++++ 5 files changed, 20 insertions(+), 4 deletions(-) diff --git a/back001/app/src/main/kotlin/com/mosenioring/app/telemetry/TelemetryModels.kt b/back001/app/src/main/kotlin/com/mosenioring/app/telemetry/TelemetryModels.kt index 21ee74f..d9662f8 100644 --- a/back001/app/src/main/kotlin/com/mosenioring/app/telemetry/TelemetryModels.kt +++ b/back001/app/src/main/kotlin/com/mosenioring/app/telemetry/TelemetryModels.kt @@ -6,9 +6,10 @@ import jakarta.validation.constraints.NotBlank import jakarta.validation.constraints.NotNull import jakarta.validation.constraints.Size import java.time.Instant +import java.time.LocalDateTime data class TelemetryRequest( - @field:NotNull val timestamp: Instant?, + @field:NotNull val timestamp: LocalDateTime?, @field:NotNull val platform: TelemetryPlatform?, @field:NotBlank val appVersion: String, @field:Size(max = 200) val note: String? = null diff --git a/back001/app/src/main/kotlin/com/mosenioring/app/telemetry/TelemetryService.kt b/back001/app/src/main/kotlin/com/mosenioring/app/telemetry/TelemetryService.kt index 9b69f1e..70c9f3a 100644 --- a/back001/app/src/main/kotlin/com/mosenioring/app/telemetry/TelemetryService.kt +++ b/back001/app/src/main/kotlin/com/mosenioring/app/telemetry/TelemetryService.kt @@ -3,6 +3,7 @@ package com.mosenioring.app.telemetry import org.slf4j.LoggerFactory import org.springframework.stereotype.Service import java.time.Instant +import java.time.ZoneOffset @Service class TelemetryService { @@ -10,6 +11,7 @@ class TelemetryService { fun recordTestTelemetry(request: TelemetryRequest, user: AuthenticatedUser): TelemetryResponse { val timestamp = requireNotNull(request.timestamp) { "Missing timestamp" } + .toInstant(ZoneOffset.UTC) val platform = requireNotNull(request.platform) { "Missing platform" } val echo = TelemetryEcho( timestamp = timestamp, diff --git a/back001/app/src/test/kotlin/com/mosenioring/app/api/TelemetryControllerTest.kt b/back001/app/src/test/kotlin/com/mosenioring/app/api/TelemetryControllerTest.kt index bb327f9..fb7d8d8 100644 --- a/back001/app/src/test/kotlin/com/mosenioring/app/api/TelemetryControllerTest.kt +++ b/back001/app/src/test/kotlin/com/mosenioring/app/api/TelemetryControllerTest.kt @@ -75,7 +75,7 @@ class TelemetryControllerTest { val requestBody = """ { - "timestamp": "2024-01-01T00:00:00Z", + "timestamp": "2024-01-01T00:00:00", "platform": "android", "appVersion": "dev", "note": "test call from mobile" diff --git a/back001/app/src/test/kotlin/com/mosenioring/app/telemetry/TelemetryServiceTest.kt b/back001/app/src/test/kotlin/com/mosenioring/app/telemetry/TelemetryServiceTest.kt index ad95c85..6e79521 100644 --- a/back001/app/src/test/kotlin/com/mosenioring/app/telemetry/TelemetryServiceTest.kt +++ b/back001/app/src/test/kotlin/com/mosenioring/app/telemetry/TelemetryServiceTest.kt @@ -4,6 +4,7 @@ import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test import java.time.Instant +import java.time.LocalDateTime class TelemetryServiceTest { @@ -11,7 +12,7 @@ class TelemetryServiceTest { fun `records telemetry and returns response`() { val service = TelemetryService() val request = TelemetryRequest( - timestamp = Instant.parse("2024-01-02T12:00:00Z"), + timestamp = LocalDateTime.parse("2024-01-02T12:00:00"), platform = TelemetryPlatform.IOS, appVersion = "dev", note = "test call from mobile" @@ -28,7 +29,7 @@ class TelemetryServiceTest { assertEquals("ok", response.status) assertEquals("user-42", response.userId) - assertEquals(request.timestamp, response.echo.timestamp) + assertEquals(request.timestamp?.toInstant(java.time.ZoneOffset.UTC), response.echo.timestamp) assertEquals(request.platform, response.echo.platform) assertEquals(request.appVersion, response.echo.appVersion) assertEquals(request.note, response.echo.note) diff --git a/back001/common/src/main/kotlin/com/mosenioring/common/web/ProblemDetailsAdvice.kt b/back001/common/src/main/kotlin/com/mosenioring/common/web/ProblemDetailsAdvice.kt index 30c75b4..21a0ea2 100644 --- a/back001/common/src/main/kotlin/com/mosenioring/common/web/ProblemDetailsAdvice.kt +++ b/back001/common/src/main/kotlin/com/mosenioring/common/web/ProblemDetailsAdvice.kt @@ -5,6 +5,7 @@ import org.springframework.http.HttpStatus import org.springframework.http.ProblemDetail import org.springframework.validation.FieldError import org.springframework.web.ErrorResponseException +import org.springframework.http.converter.HttpMessageNotReadableException import org.springframework.web.bind.MethodArgumentNotValidException import org.springframework.web.bind.annotation.ExceptionHandler import org.springframework.web.bind.annotation.RestControllerAdvice @@ -42,6 +43,17 @@ class ProblemDetailsAdvice { return detail } + @ExceptionHandler(HttpMessageNotReadableException::class) + fun handleNotReadable(ex: HttpMessageNotReadableException, request: HttpServletRequest): ProblemDetail { + val detail = ProblemDetail.forStatus(HttpStatus.BAD_REQUEST) + detail.title = "Malformed request" + detail.detail = ex.mostSpecificCause.message ?: ex.message + detail.type = java.net.URI.create("https://httpstatuses.io/400") + detail.setProperty("code", "INVALID_JSON") + detail.setProperty("path", request.requestURI) + return detail + } + @ExceptionHandler(IllegalStateException::class) fun handleIllegalState(ex: IllegalStateException, request: HttpServletRequest): ProblemDetail { val detail = ProblemDetail.forStatus(HttpStatus.CONFLICT)