Skip to content

Commit

Permalink
Fix testing
Browse files Browse the repository at this point in the history
  • Loading branch information
courtneyeh committed Apr 22, 2024
1 parent 9693c3e commit 4ed37cc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private Optional<Bytes32> getFromStorage() {
}
}

public Optional<Bytes32> getGraffitiWithThrowable() throws Throwable {
public Optional<Bytes32> getWithThrowable() throws Throwable {
return storageProvider.get().or(defaultProvider::get);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ void setGraffiti_shouldReturnErrorMessageWhenUnableToWriteFile(@TempDir final Pa
assertThat(file.setWritable(false)).isTrue();

assertThatThrownBy(() -> manager.setGraffiti(publicKey, graffiti))
.isInstanceOf(IOException.class);
.isInstanceOf(GraffitiManagementException.class)
.hasMessage("Unable to update graffiti for validator " + publicKey);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
package tech.pegasys.teku.validator.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Optional;
import org.apache.tuweni.bytes.Bytes32;
import org.junit.jupiter.api.Test;
import tech.pegasys.teku.infrastructure.async.ExceptionThrowingSupplier;
import tech.pegasys.teku.spec.TestSpecFactory;
import tech.pegasys.teku.spec.util.DataStructureUtil;

Expand All @@ -29,28 +33,77 @@ class UpdatableGraffitiProviderTest {
private UpdatableGraffitiProvider provider;

@Test
void shouldGetStorageGraffitiWhenAvailable() {
void get_shouldGetStorageGraffitiWhenAvailable() {
provider = new UpdatableGraffitiProvider(() -> Optional.of(storageGraffiti), Optional::empty);
assertThat(provider.get()).hasValue(storageGraffiti);
}

@Test
void shouldGetStorageGraffitiWhenBothAvailable() {
void get_shouldGetStorageGraffitiWhenBothAvailable() {
provider =
new UpdatableGraffitiProvider(
() -> Optional.of(storageGraffiti), () -> Optional.of(defaultGraffiti));
assertThat(provider.get()).hasValue(storageGraffiti);
}

@Test
void shouldGetDefaultGraffitiWhenStorageEmpty() {
void get_shouldGetDefaultGraffitiWhenStorageEmpty() {
provider = new UpdatableGraffitiProvider(Optional::empty, () -> Optional.of(defaultGraffiti));
assertThat(provider.get()).hasValue(defaultGraffiti);
}

@Test
void shouldBeEmptyWhenBothEmpty() {
void get_shouldBeEmptyWhenBothEmpty() {
provider = new UpdatableGraffitiProvider(Optional::empty, Optional::empty);
assertThat(provider.get()).isEmpty();
}

@Test
@SuppressWarnings("unchecked")
public void get_shouldDelegateToDefaultProviderWhenStorageProviderFails() throws Throwable {
final ExceptionThrowingSupplier<Optional<Bytes32>> storageProvider =
mock(ExceptionThrowingSupplier.class);
when(storageProvider.get()).thenThrow(new RuntimeException("Error"));

provider = new UpdatableGraffitiProvider(storageProvider, () -> Optional.of(defaultGraffiti));
assertThat(provider.get()).hasValue(defaultGraffiti);
}

@Test
void getWithThrowable_shouldGetStorageGraffitiWhenAvailable() {
provider = new UpdatableGraffitiProvider(() -> Optional.of(storageGraffiti), Optional::empty);
assertThat(provider.get()).hasValue(storageGraffiti);
}

@Test
void getWithThrowable_shouldGetStorageGraffitiWhenBothAvailable() {
provider =
new UpdatableGraffitiProvider(
() -> Optional.of(storageGraffiti), () -> Optional.of(defaultGraffiti));
assertThat(provider.get()).hasValue(storageGraffiti);
}

@Test
void getWithThrowable_shouldGetDefaultGraffitiWhenStorageEmpty() {
provider = new UpdatableGraffitiProvider(Optional::empty, () -> Optional.of(defaultGraffiti));
assertThat(provider.get()).hasValue(defaultGraffiti);
}

@Test
void getWithThrowable_shouldBeEmptyWhenBothEmpty() {
provider = new UpdatableGraffitiProvider(Optional::empty, Optional::empty);
assertThat(provider.get()).isEmpty();
}

@Test
@SuppressWarnings("unchecked")
public void getWithThrowable_shouldThrowExceptionWhenStorageProviderFails() throws Throwable {
final RuntimeException exception = new RuntimeException("Error");
final ExceptionThrowingSupplier<Optional<Bytes32>> storageProvider =
mock(ExceptionThrowingSupplier.class);
when(storageProvider.get()).thenThrow(exception);

provider = new UpdatableGraffitiProvider(storageProvider, () -> Optional.of(defaultGraffiti));
assertThatThrownBy(() -> provider.getWithThrowable()).isEqualTo(exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
try {
final UpdatableGraffitiProvider provider =
(UpdatableGraffitiProvider) maybeValidator.get().getGraffitiProvider();
request.respondOk(new GraffitiResponse(publicKey, provider.getGraffitiWithThrowable()));
request.respondOk(new GraffitiResponse(publicKey, provider.getWithThrowable()));
} catch (Throwable e) {
request.respondError(SC_INTERNAL_SERVER_ERROR, e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DeleteGraffitiTest {
.build();

@Test
void shouldSuccessfullyDeleteGraffiti() throws IOException {
void shouldSuccessfullyDeleteGraffiti() throws IOException, GraffitiManagementException {
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));

Expand All @@ -72,7 +72,8 @@ void shouldSuccessfullyDeleteGraffiti() throws IOException {
}

@Test
void shouldReturnErrorWhenIssueDeletingGraffiti() throws IOException {
void shouldReturnErrorWhenIssueDeletingGraffiti()
throws IOException, GraffitiManagementException {
final String errorMessage = "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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void metadata_shouldHandle500() throws JsonProcessingException {

private void checkGraffiti(final Optional<Bytes32> graffiti) throws Throwable {
final UpdatableGraffitiProvider provider = mock(UpdatableGraffitiProvider.class);
when(provider.getGraffitiWithThrowable()).thenReturn(graffiti);
when(provider.getWithThrowable()).thenReturn(graffiti);
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, provider);
when(keyManager.getValidatorByPublicKey(eq(publicKey))).thenReturn(Optional.of(validator));

Expand All @@ -146,6 +146,6 @@ private void checkGraffiti(final Optional<Bytes32> graffiti) throws Throwable {
new GetGraffiti.GraffitiResponse(publicKey, graffiti);
assertThat(request.getResponseCode()).isEqualTo(SC_OK);
assertThat(request.getResponseBody()).isEqualTo(expectedResponse);
verify(provider).getGraffitiWithThrowable();
verify(provider).getWithThrowable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SetGraffitiTest {
.build();

@Test
void shouldSuccessfullySetGraffiti() throws IOException {
void shouldSuccessfullySetGraffiti() throws IOException, GraffitiManagementException {
request.setRequestBody(graffiti);

final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
Expand All @@ -76,7 +76,7 @@ void shouldSuccessfullySetGraffiti() throws IOException {
}

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

Expand All @@ -95,7 +95,7 @@ void shouldReturnErrorWhenIssueSettingGraffiti() throws IOException {
}

@Test
void shouldThrowExceptionWhenInvalidGraffitiInput() throws IOException {
void shouldThrowExceptionWhenInvalidGraffitiInput() throws GraffitiManagementException {
final String invalidGraffiti = "This graffiti is a bit too long!!";
final String errorMessage =
String.format(
Expand Down

0 comments on commit 4ed37cc

Please sign in to comment.