Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix errors #63

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.linkurlshorter.urlshortener.exception;

import com.linkurlshorter.urlshortener.auth.exception.EmailAlreadyTakenException;
import com.linkurlshorter.urlshortener.link.NoLinkFoundByIdException;
import com.linkurlshorter.urlshortener.security.ForbiddenException;
import com.linkurlshorter.urlshortener.user.NoSuchEmailFoundException;
import com.linkurlshorter.urlshortener.user.NoUserFoundByEmailException;
Expand Down Expand Up @@ -101,12 +102,6 @@ public ResponseEntity<Object> handleBadCredentialsException(BadCredentialsExcept
* @param ex denied access error
* @return {@link ResponseEntity} object with the corresponding status and error message
*/
@ExceptionHandler(ForbiddenException.class)
public ResponseEntity<Object> handleForbiddenException(ForbiddenException ex) {
ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN,
"Forbidden!", ex.getMessage());
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse);
}

/**
* Handles no resource (404) exceptions for different types of requests.
Expand Down Expand Up @@ -152,21 +147,20 @@ private ErrorResponse buildErrorResponse(HttpStatus status, String message, Stri
* Returns a response with a 403 status and the corresponding error message.
*
* @param ex forbidden exception
* @param request HttpServletRequest object representing the HTTP request
* @return {@link ResponseEntity} object with the corresponding status and error message
*/
@ExceptionHandler(ForbiddenException.class)
public ResponseEntity<Object> handleForbiddenException(
ForbiddenException ex, HttpServletRequest request) {
ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN,
ex.getMessage(), request.getRequestURI());
ForbiddenException ex) {
ErrorResponse errorResponse = buildErrorResponse(HttpStatus.FORBIDDEN, "Operation forbidden!",
ex.getMessage());
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(errorResponse);
}
@ExceptionHandler(NoLinkFoundByIdException.class)
public ResponseEntity<Object> handleNoLinkFoundByIdException(
NoLinkFoundByIdException ex, HttpServletRequest request) {
ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND,
ex.getMessage(), request.getRequestURI());
NoLinkFoundByIdException ex) {
ErrorResponse errorResponse = buildErrorResponse(HttpStatus.NOT_FOUND,"No link by provided id found",
ex.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ void loginFailedWhenUserDoesNotExistTest() throws Exception {
.content(objectMapper.writeValueAsString(authRequest)))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.statusCode").value(401))
.andExpect(jsonPath("$.message").value("Authentication failed!"))
.andExpect(jsonPath("$.message").value("Authentication failed!"));
}

/**
* Test case to verify login failure when password does not match.
Expand Down Expand Up @@ -192,4 +193,4 @@ void registerFailedWhenInvalidEmailGivenTest(String email) throws Exception {
.andExpect(jsonPath("$.statusCode").value(400))
.andExpect(jsonPath("$.message").value("Validation failed!"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ void createShortLinkFailsWhenUrlIsInvalid(String url) throws Exception {
.content(objectMapper.writeValueAsString(createLinkRequest)))
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.statusCode").value(400))
.andExpect(jsonPath("$.message").value("Not valid format url!"))
.andExpect(jsonPath("$.path").value("/api/V1/link/create"));
.andExpect(jsonPath("$.message").value("Validation failed!"))
.andExpect(jsonPath("$.exceptionMessage").value("Not valid format url!"));
}

@Test
Expand All @@ -118,14 +118,14 @@ void deleteLinkFailsWhenIdIsInvalid() throws Exception {
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.statusCode").value(404))
.andExpect(jsonPath("$.message").value("No link by provided id found"))
.andExpect(jsonPath("$.path").value("/api/V1/link/delete"));
.andExpect(jsonPath("$.exceptionMessage").value("No link by provided id found"));
}
@Test
void deleteLinkFailsWhenIdIsNull() throws Exception {
mockMvc.perform(post(baseUrl + "delete" + "?id=" + null)
.contentType(MediaType.APPLICATION_JSON)
.header("Authorization", token))
.andExpect(status().is4xxClientError());
.andExpect(status().isInternalServerError());
}
@Test
void deleteLinkFailsWhenUserHasNoRightsForThisLink() throws Exception {
Expand All @@ -144,6 +144,6 @@ void deleteLinkFailsWhenUserHasNoRightsForThisLink() throws Exception {
.andExpect(status().is4xxClientError())
.andExpect(jsonPath("$.statusCode").value(403))
.andExpect(jsonPath("$.message").value("Operation forbidden!"))
.andExpect(jsonPath("$.path").value("/api/V1/link/delete"));
.andExpect(jsonPath("$.exceptionMessage").value("Operation forbidden!"));
}
}
Loading