diff --git a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java index 85e8d7b..9b5c7d0 100644 --- a/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/linkurlshorter/urlshortener/exception/GlobalExceptionHandler.java @@ -76,7 +76,7 @@ public ResponseEntity handleEmailAlreadyTakenException( * @return {@link ResponseEntity} object with the corresponding status and error message */ @ExceptionHandler({NoSuchEmailFoundException.class, - NoUserFoundByEmailException.class, NoUserFoundByIdException.class}) + NoUserFoundByEmailException.class, NoUserFoundByIdException.class, NoLinkFoundByShortLinkException.class}) public ResponseEntity handleNotFoundExceptions( RuntimeException ex, HttpServletRequest request) { ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, @@ -112,14 +112,6 @@ public ResponseEntity handleForbiddenException( return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse); } - @ExceptionHandler({NoLinkFoundByIdException.class, NoLinkFoundByShortLinkException.class}) - public ResponseEntity handleNoLinkFoundByIdException( - RuntimeException ex, HttpServletRequest request) { - ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND, - ex.getMessage(), request.getRequestURI()); - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); - } - @ExceptionHandler({DeletedLinkException.class, InactiveLinkException.class}) public ResponseEntity handleDeletedAndInactiveLinkException( RuntimeException ex, HttpServletRequest request) { diff --git a/src/main/java/com/linkurlshorter/urlshortener/link/exception/NoLinkFoundByIdException.java b/src/main/java/com/linkurlshorter/urlshortener/link/exception/NoLinkFoundByIdException.java deleted file mode 100644 index 5e03866..0000000 --- a/src/main/java/com/linkurlshorter/urlshortener/link/exception/NoLinkFoundByIdException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.linkurlshorter.urlshortener.link.exception; - -/** - * Exception thrown when no link is found by the provided ID. - */ -public class NoLinkFoundByIdException extends RuntimeException { - private static final String DEFAULT_MSG = "No link by provided id found"; - - public NoLinkFoundByIdException() { - super(DEFAULT_MSG); - } -} diff --git a/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java b/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java index 8a80eb2..bb5d22e 100644 --- a/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java +++ b/src/test/java/com/linkurlshorter/urlshortener/link/LinkServiceTest.java @@ -183,6 +183,22 @@ void getLongLinkFromShortLinkInactiveTest() throws JsonProcessingException { .isInstanceOf(InactiveLinkException.class); } + /** + * Test case for the {@link LinkService#getLongLinkFromShortLink(String)} method when link expiration time + * has passed. + */ + @Test + void getLongLinkFromShortLinkExpiredTest() throws JsonProcessingException { + link.setExpirationTime(LocalDateTime.now().minusDays(1)); + when(jedisPool.getResource()).thenReturn(jedis); + when(jedis.exists(anyString())).thenReturn(true); + when(jedis.get(anyString())).thenReturn("{}"); + when(mapper.readValue(anyString(), eq(Link.class))).thenReturn(link); + + assertThatThrownBy(() -> linkService.getLongLinkFromShortLink(link.getShortLink())) + .isInstanceOf(InactiveLinkException.class); + } + /** * Test case for the {@link LinkService#findByShortLink(String)} method. */