From 1aa9649de22e5ffce3268806f079363905ac4573 Mon Sep 17 00:00:00 2001 From: courtneyeh Date: Mon, 22 Apr 2024 23:24:31 +1000 Subject: [PATCH] Update tests --- .../client/restapi/apis/DeleteGraffitiTest.java | 9 ++++----- .../client/restapi/apis/GetGraffitiTest.java | 13 +++++-------- .../client/restapi/apis/SetGraffitiTest.java | 9 ++++----- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/DeleteGraffitiTest.java b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/DeleteGraffitiTest.java index f3aa4c39fd4..ef6419f27eb 100644 --- a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/DeleteGraffitiTest.java +++ b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/DeleteGraffitiTest.java @@ -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 diff --git a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/GetGraffitiTest.java b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/GetGraffitiTest.java index b120b7e30c0..925c52e468a 100644 --- a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/GetGraffitiTest.java +++ b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/GetGraffitiTest.java @@ -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; @@ -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)); @@ -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 diff --git a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetGraffitiTest.java b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetGraffitiTest.java index 213acea7c47..30ffb39cba2 100644 --- a/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetGraffitiTest.java +++ b/validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetGraffitiTest.java @@ -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