Skip to content

Commit

Permalink
Violations fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
dukris committed Feb 21, 2024
1 parent bdbfc57 commit 5fb0db3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 62 deletions.
75 changes: 16 additions & 59 deletions src/test/java/git/tracehub/pmo/controller/AdviceControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@

import git.tracehub.pmo.exception.ResourceAlreadyExistsException;
import git.tracehub.pmo.exception.ResourceNotFoundException;
import io.github.eocqrs.eokson.Jocument;
import io.github.eocqrs.eokson.MutableJson;
import git.tracehub.pmo.exception.RestError;
import org.cactoos.list.ListOf;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
Expand All @@ -40,17 +36,11 @@
*
* @since 0.0.0
*/
@ExtendWith(MockitoExtension.class)
final class AdviceControllerTest {

/**
* Controller.
*/
private final AdviceController controller = new AdviceController();

@Test
void handlesMethodArgumentNotValidException() {
final String message = "Message";
final String message = "MethodArgumentNotValidException";
final MethodArgumentNotValidException exception =
Mockito.mock(MethodArgumentNotValidException.class);
final BindingResult result = Mockito.mock(BindingResult.class);
Expand All @@ -60,99 +50,66 @@ void handlesMethodArgumentNotValidException() {
);
MatcherAssert.assertThat(
"MethodArgumentNotValidException isn't handled",
this.controller.handle(exception),
new AdviceController().handle(exception),
new IsEqual<>(
new ResponseEntity<>(
new Jocument(
new MutableJson()
.with("message", message)
).byteArray(),
HttpStatus.BAD_REQUEST
)
new RestError(message, HttpStatus.BAD_REQUEST).value()
)
);
}

@Test
void handlesResourceAlreadyExistsException() {
final String message = "Message";
final String message = "ResourceAlreadyExistsException";
final ResourceAlreadyExistsException exception =
new ResourceAlreadyExistsException(message);
MatcherAssert.assertThat(
"ResourceAlreadyExistsException isn't handled",
this.controller.handle(exception),
new AdviceController().handle(exception),
new IsEqual<>(
new ResponseEntity<>(
new Jocument(
new MutableJson()
.with("message", message)
).byteArray(),
HttpStatus.BAD_REQUEST
)
new RestError(message, HttpStatus.BAD_REQUEST).value()
)
);
}

@Test
void handlesResourceNotFoundException() {
final String message = "Message";
final String message = "ResourceNotFoundException";
final ResourceNotFoundException exception =
new ResourceNotFoundException(message);
MatcherAssert.assertThat(
"ResourceNotFoundException isn't handled",
this.controller.handle(exception),
new AdviceController().handle(exception),
new IsEqual<>(
new ResponseEntity<>(
new Jocument(
new MutableJson()
.with("message", message)
).byteArray(),
HttpStatus.NOT_FOUND
)
new RestError(message, HttpStatus.NOT_FOUND).value()
)
);
}

@Test
void handlesAccessDeniedException() {
final String message = "Message";
final String message = "AccessDeniedException";
final AccessDeniedException exception =
new AccessDeniedException(message);
MatcherAssert.assertThat(
"AccessDeniedException isn't handled",
this.controller.handle(exception),
new AdviceController().handle(exception),
new IsEqual<>(
new ResponseEntity<>(
new Jocument(
new MutableJson()
.with("message", message)
).byteArray(),
HttpStatus.FORBIDDEN
)
new RestError(message, HttpStatus.FORBIDDEN).value()
)
);
}

@Test
void handlesException() {
final String message = "Message";
final String message = "Exception";
final Exception exception = new Exception(message);
MatcherAssert.assertThat(
"Exception isn't handled",
this.controller.handle(exception),
new AdviceController().handle(exception),
new IsEqual<>(
new ResponseEntity<>(
new Jocument(
new MutableJson()
.with("message", message)
).byteArray(),
HttpStatus.INTERNAL_SERVER_ERROR
)
new RestError(message, HttpStatus.INTERNAL_SERVER_ERROR).value()
)
);
}




}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ void invitesCollaboratorSuccessfully() throws IOException {
void throwsOnInvalidRepository() throws IOException {
final String collaborator = "name";
final Repo repo = Mockito.mock(Repo.class);
final Collaborators collaborators = Mockito.mock(Collaborators.class);
Mockito.when(repo.collaborators()).thenReturn(collaborators);
Mockito.doThrow(IOException.class).when(collaborators).add(collaborator);
final Collaborators collab = Mockito.mock(Collaborators.class);
Mockito.when(repo.collaborators()).thenReturn(collab);
Mockito.doThrow(IOException.class).when(collab).add(collaborator);
Assertions.assertThrows(
IOException.class,
() -> new InviteCollaborator(repo, collaborator).exec(),
Expand Down

0 comments on commit 5fb0db3

Please sign in to comment.