Skip to content

Commit 4ed37cc

Browse files
committed
Fix testing
1 parent 9693c3e commit 4ed37cc

File tree

7 files changed

+69
-14
lines changed

7 files changed

+69
-14
lines changed

validator/api/src/main/java/tech/pegasys/teku/validator/api/UpdatableGraffitiProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private Optional<Bytes32> getFromStorage() {
4141
}
4242
}
4343

44-
public Optional<Bytes32> getGraffitiWithThrowable() throws Throwable {
44+
public Optional<Bytes32> getWithThrowable() throws Throwable {
4545
return storageProvider.get().or(defaultProvider::get);
4646
}
4747
}

validator/api/src/test/java/tech/pegasys/teku/validator/api/GraffitiManagerTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ void setGraffiti_shouldReturnErrorMessageWhenUnableToWriteFile(@TempDir final Pa
9090
assertThat(file.setWritable(false)).isTrue();
9191

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

9697
@Test

validator/api/src/test/java/tech/pegasys/teku/validator/api/UpdatableGraffitiProviderTest.java

+57-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
package tech.pegasys.teku.validator.api;
1515

1616
import static org.assertj.core.api.Assertions.assertThat;
17+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
18+
import static org.mockito.Mockito.mock;
19+
import static org.mockito.Mockito.when;
1720

1821
import java.util.Optional;
1922
import org.apache.tuweni.bytes.Bytes32;
2023
import org.junit.jupiter.api.Test;
24+
import tech.pegasys.teku.infrastructure.async.ExceptionThrowingSupplier;
2125
import tech.pegasys.teku.spec.TestSpecFactory;
2226
import tech.pegasys.teku.spec.util.DataStructureUtil;
2327

@@ -29,28 +33,77 @@ class UpdatableGraffitiProviderTest {
2933
private UpdatableGraffitiProvider provider;
3034

3135
@Test
32-
void shouldGetStorageGraffitiWhenAvailable() {
36+
void get_shouldGetStorageGraffitiWhenAvailable() {
3337
provider = new UpdatableGraffitiProvider(() -> Optional.of(storageGraffiti), Optional::empty);
3438
assertThat(provider.get()).hasValue(storageGraffiti);
3539
}
3640

3741
@Test
38-
void shouldGetStorageGraffitiWhenBothAvailable() {
42+
void get_shouldGetStorageGraffitiWhenBothAvailable() {
3943
provider =
4044
new UpdatableGraffitiProvider(
4145
() -> Optional.of(storageGraffiti), () -> Optional.of(defaultGraffiti));
4246
assertThat(provider.get()).hasValue(storageGraffiti);
4347
}
4448

4549
@Test
46-
void shouldGetDefaultGraffitiWhenStorageEmpty() {
50+
void get_shouldGetDefaultGraffitiWhenStorageEmpty() {
4751
provider = new UpdatableGraffitiProvider(Optional::empty, () -> Optional.of(defaultGraffiti));
4852
assertThat(provider.get()).hasValue(defaultGraffiti);
4953
}
5054

5155
@Test
52-
void shouldBeEmptyWhenBothEmpty() {
56+
void get_shouldBeEmptyWhenBothEmpty() {
5357
provider = new UpdatableGraffitiProvider(Optional::empty, Optional::empty);
5458
assertThat(provider.get()).isEmpty();
5559
}
60+
61+
@Test
62+
@SuppressWarnings("unchecked")
63+
public void get_shouldDelegateToDefaultProviderWhenStorageProviderFails() throws Throwable {
64+
final ExceptionThrowingSupplier<Optional<Bytes32>> storageProvider =
65+
mock(ExceptionThrowingSupplier.class);
66+
when(storageProvider.get()).thenThrow(new RuntimeException("Error"));
67+
68+
provider = new UpdatableGraffitiProvider(storageProvider, () -> Optional.of(defaultGraffiti));
69+
assertThat(provider.get()).hasValue(defaultGraffiti);
70+
}
71+
72+
@Test
73+
void getWithThrowable_shouldGetStorageGraffitiWhenAvailable() {
74+
provider = new UpdatableGraffitiProvider(() -> Optional.of(storageGraffiti), Optional::empty);
75+
assertThat(provider.get()).hasValue(storageGraffiti);
76+
}
77+
78+
@Test
79+
void getWithThrowable_shouldGetStorageGraffitiWhenBothAvailable() {
80+
provider =
81+
new UpdatableGraffitiProvider(
82+
() -> Optional.of(storageGraffiti), () -> Optional.of(defaultGraffiti));
83+
assertThat(provider.get()).hasValue(storageGraffiti);
84+
}
85+
86+
@Test
87+
void getWithThrowable_shouldGetDefaultGraffitiWhenStorageEmpty() {
88+
provider = new UpdatableGraffitiProvider(Optional::empty, () -> Optional.of(defaultGraffiti));
89+
assertThat(provider.get()).hasValue(defaultGraffiti);
90+
}
91+
92+
@Test
93+
void getWithThrowable_shouldBeEmptyWhenBothEmpty() {
94+
provider = new UpdatableGraffitiProvider(Optional::empty, Optional::empty);
95+
assertThat(provider.get()).isEmpty();
96+
}
97+
98+
@Test
99+
@SuppressWarnings("unchecked")
100+
public void getWithThrowable_shouldThrowExceptionWhenStorageProviderFails() throws Throwable {
101+
final RuntimeException exception = new RuntimeException("Error");
102+
final ExceptionThrowingSupplier<Optional<Bytes32>> storageProvider =
103+
mock(ExceptionThrowingSupplier.class);
104+
when(storageProvider.get()).thenThrow(exception);
105+
106+
provider = new UpdatableGraffitiProvider(storageProvider, () -> Optional.of(defaultGraffiti));
107+
assertThatThrownBy(() -> provider.getWithThrowable()).isEqualTo(exception);
108+
}
56109
}

validator/client/src/main/java/tech/pegasys/teku/validator/client/restapi/apis/GetGraffiti.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void handleRequest(final RestApiRequest request) throws JsonProcessingExc
9393
try {
9494
final UpdatableGraffitiProvider provider =
9595
(UpdatableGraffitiProvider) maybeValidator.get().getGraffitiProvider();
96-
request.respondOk(new GraffitiResponse(publicKey, provider.getGraffitiWithThrowable()));
96+
request.respondOk(new GraffitiResponse(publicKey, provider.getWithThrowable()));
9797
} catch (Throwable e) {
9898
request.respondError(SC_INTERNAL_SERVER_ERROR, e.getMessage());
9999
}

validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/DeleteGraffitiTest.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class DeleteGraffitiTest {
6060
.build();
6161

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

@@ -72,7 +72,8 @@ void shouldSuccessfullyDeleteGraffiti() throws IOException {
7272
}
7373

7474
@Test
75-
void shouldReturnErrorWhenIssueDeletingGraffiti() throws IOException {
75+
void shouldReturnErrorWhenIssueDeletingGraffiti()
76+
throws IOException, GraffitiManagementException {
7677
final String errorMessage = "Unable to delete graffiti for validator " + publicKey;
7778
final Validator validator = new Validator(publicKey, NO_OP_SIGNER, Optional::empty);
7879
when(keyManager.getValidatorByPublicKey(any())).thenReturn(Optional.of(validator));

validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/GetGraffitiTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void metadata_shouldHandle500() throws JsonProcessingException {
136136

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

@@ -146,6 +146,6 @@ private void checkGraffiti(final Optional<Bytes32> graffiti) throws Throwable {
146146
new GetGraffiti.GraffitiResponse(publicKey, graffiti);
147147
assertThat(request.getResponseCode()).isEqualTo(SC_OK);
148148
assertThat(request.getResponseBody()).isEqualTo(expectedResponse);
149-
verify(provider).getGraffitiWithThrowable();
149+
verify(provider).getWithThrowable();
150150
}
151151
}

validator/client/src/test/java/tech/pegasys/teku/validator/client/restapi/apis/SetGraffitiTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class SetGraffitiTest {
6262
.build();
6363

6464
@Test
65-
void shouldSuccessfullySetGraffiti() throws IOException {
65+
void shouldSuccessfullySetGraffiti() throws IOException, GraffitiManagementException {
6666
request.setRequestBody(graffiti);
6767

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

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

@@ -95,7 +95,7 @@ void shouldReturnErrorWhenIssueSettingGraffiti() throws IOException {
9595
}
9696

9797
@Test
98-
void shouldThrowExceptionWhenInvalidGraffitiInput() throws IOException {
98+
void shouldThrowExceptionWhenInvalidGraffitiInput() throws GraffitiManagementException {
9999
final String invalidGraffiti = "This graffiti is a bit too long!!";
100100
final String errorMessage =
101101
String.format(

0 commit comments

Comments
 (0)