Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh committed Apr 22, 2024
1 parent 4ed37cc commit 1aa9649
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,18 @@ void shouldSuccessfullyDeleteGraffiti() throws IOException, GraffitiManagementEx
@Test
void shouldReturnErrorWhenIssueDeletingGraffiti()
throws IOException, GraffitiManagementException {
final String errorMessage = "Unable to delete graffiti for validator " + publicKey;
final GraffitiManagementException exception =
new GraffitiManagementException("Unable to delete graffiti for validator " + publicKey);
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));
doThrow(new GraffitiManagementException(errorMessage))
.when(graffitiManager)
.deleteGraffiti(any());
doThrow(exception).when(graffitiManager).deleteGraffiti(any());

handler.handleRequest(request);

verify(graffitiManager).deleteGraffiti(eq(publicKey));
assertThat(request.getResponseCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(request.getResponseBody())
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, errorMessage));
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, exception.getMessage()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -71,16 +72,11 @@ void shouldGetEmptyGraffiti() throws Throwable {
}

@Test
void shouldHandleGraffitiManagementException() throws JsonProcessingException {
void shouldHandleGraffitiManagementException() throws Throwable {
final GraffitiManagementException exception =
new GraffitiManagementException("Unable to retrieve graffiti from storage");
final UpdatableGraffitiProvider provider =
new UpdatableGraffitiProvider(
() -> {
throw exception;
},
Optional::empty);

final UpdatableGraffitiProvider provider = mock(UpdatableGraffitiProvider.class);
doThrow(exception).when(provider).getWithThrowable();
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, provider);
when(keyManager.getValidatorByPublicKey(eq(publicKey))).thenReturn(Optional.of(validator));

Expand All @@ -89,6 +85,7 @@ void shouldHandleGraffitiManagementException() throws JsonProcessingException {
assertThat(request.getResponseCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(request.getResponseBody())
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, exception.getMessage()));
verify(provider).getWithThrowable();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,21 +77,20 @@ void shouldSuccessfullySetGraffiti() throws IOException, GraffitiManagementExcep

@Test
void shouldReturnErrorWhenIssueSettingGraffiti() throws IOException, GraffitiManagementException {
final String errorMessage = "Unable to update graffiti for validator " + publicKey;
final GraffitiManagementException exception =
new GraffitiManagementException("Unable to update graffiti for validator " + publicKey);
request.setRequestBody(graffiti);

final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));
doThrow(new GraffitiManagementException(errorMessage))
.when(graffitiManager)
.setGraffiti(any(), eq(graffiti));
doThrow(exception).when(graffitiManager).setGraffiti(any(), eq(graffiti));

handler.handleRequest(request);

verify(graffitiManager).setGraffiti(eq(publicKey), eq(graffiti));
assertThat(request.getResponseCode()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(request.getResponseBody())
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, errorMessage));
.isEqualTo(new HttpErrorResponse(SC_INTERNAL_SERVER_ERROR, exception.getMessage()));
}

@Test
Expand Down

0 comments on commit 1aa9649

Please sign in to comment.