From ef4ee70aac965cb9225f81500651e71b02c7721f Mon Sep 17 00:00:00 2001 From: kasun Date: Wed, 8 Apr 2026 22:33:04 +0200 Subject: [PATCH] added WrongServiceTypeException --- .../java/com/vaessl/app/connection/ConnectionService.java | 5 +++++ .../src/main/java/com/vaessl/app/exception/ErrorMessage.java | 2 +- .../com/vaessl/app/exception/GlobalExceptionHandler.java | 5 +++++ .../com/vaessl/app/exception/WrongServiceTypeException.java | 4 ++++ .../com/vaessl/app/connection/HomeboxIntegrationTest.java | 4 ++-- 5 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 backend/src/main/java/com/vaessl/app/exception/WrongServiceTypeException.java diff --git a/backend/src/main/java/com/vaessl/app/connection/ConnectionService.java b/backend/src/main/java/com/vaessl/app/connection/ConnectionService.java index bfd2ace..f68edf7 100644 --- a/backend/src/main/java/com/vaessl/app/connection/ConnectionService.java +++ b/backend/src/main/java/com/vaessl/app/connection/ConnectionService.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Service; import com.vaessl.app.dto.ConnectionRequest; import com.vaessl.app.dto.ConnectionResponse; +import com.vaessl.app.exception.WrongServiceTypeException; @Service public class ConnectionService { @@ -26,6 +27,10 @@ public class ConnectionService { ConnectionProvider provider = providerRegistry.get(request.serviceType()); + if (provider == null) { + throw new WrongServiceTypeException(); + } + provider.checkCredentials(request); ConnectionResponse response = provider.authenticate(request); diff --git a/backend/src/main/java/com/vaessl/app/exception/ErrorMessage.java b/backend/src/main/java/com/vaessl/app/exception/ErrorMessage.java index 2d6f693..ae14081 100644 --- a/backend/src/main/java/com/vaessl/app/exception/ErrorMessage.java +++ b/backend/src/main/java/com/vaessl/app/exception/ErrorMessage.java @@ -8,7 +8,7 @@ public enum ErrorMessage { BAD_REQUEST_EMPTY_FIELDS(HttpStatus.BAD_REQUEST, "Fields must not be empty."), UNAUTHORIZED_WRONG_LOGIN( HttpStatus.UNAUTHORIZED, "Invalid username or password."), SERVICE_UNAVAILABLE_UNREACHABLE_URL( HttpStatus.SERVICE_UNAVAILABLE, "The target URL is unreachable."), SERVER_ERROR_GENERAL( - "The external app returned a server error: "), WRONG_SERVICE_TYPE(HttpStatus.BAD_REQUEST, + "The external app returned a server error: "), WRONG_SERVICE_TYPE(HttpStatus.NOT_FOUND, "No such service type."); private final HttpStatus status; diff --git a/backend/src/main/java/com/vaessl/app/exception/GlobalExceptionHandler.java b/backend/src/main/java/com/vaessl/app/exception/GlobalExceptionHandler.java index 254e6bf..08994c0 100644 --- a/backend/src/main/java/com/vaessl/app/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/com/vaessl/app/exception/GlobalExceptionHandler.java @@ -48,6 +48,11 @@ public class GlobalExceptionHandler { SERVER_ERROR_GENERAL.getMessage() + e.getStatusText()); } + @ExceptionHandler(WrongServiceTypeException.class) + public ProblemDetail handleWrongServiceType(WrongServiceTypeException e) { + return ProblemDetail.forStatusAndDetail(WRONG_SERVICE_TYPE.getStatus(), WRONG_SERVICE_TYPE.getMessage()); + } + @ExceptionHandler(EmptyCredentialsException.class) public ProblemDetail handleEmptyCredentials(EmptyCredentialsException e) { return ProblemDetail.forStatusAndDetail(BAD_REQUEST_EMPTY_FIELDS.getStatus(), diff --git a/backend/src/main/java/com/vaessl/app/exception/WrongServiceTypeException.java b/backend/src/main/java/com/vaessl/app/exception/WrongServiceTypeException.java new file mode 100644 index 0000000..c2f72fa --- /dev/null +++ b/backend/src/main/java/com/vaessl/app/exception/WrongServiceTypeException.java @@ -0,0 +1,4 @@ +package com.vaessl.app.exception; + +public class WrongServiceTypeException extends RuntimeException { +} diff --git a/backend/src/test/java/com/vaessl/app/connection/HomeboxIntegrationTest.java b/backend/src/test/java/com/vaessl/app/connection/HomeboxIntegrationTest.java index 1ba9122..b22037a 100644 --- a/backend/src/test/java/com/vaessl/app/connection/HomeboxIntegrationTest.java +++ b/backend/src/test/java/com/vaessl/app/connection/HomeboxIntegrationTest.java @@ -145,7 +145,7 @@ class HomeboxIntegrationTest { * Test the custom ProviderNotFound exception. */ @Test - void shouldReturnProviderNotFound() { + void shouldReturnWrongServiceTypeException() { ConnectionRequest wrongServiceTypeReq = new ConnectionRequest( MOCK_URL, "wrong-service-type", @@ -155,7 +155,7 @@ class HomeboxIntegrationTest { ResponseEntity response = restTemplate.postForEntity(LOGIN.getValue(), wrongServiceTypeReq, String.class); - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); assertThat(response.getBody()).contains(WRONG_SERVICE_TYPE.getMessage()); }