14
14
package tech .pegasys .teku .validator .api ;
15
15
16
16
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 ;
17
20
18
21
import java .util .Optional ;
19
22
import org .apache .tuweni .bytes .Bytes32 ;
20
23
import org .junit .jupiter .api .Test ;
24
+ import tech .pegasys .teku .infrastructure .async .ExceptionThrowingSupplier ;
21
25
import tech .pegasys .teku .spec .TestSpecFactory ;
22
26
import tech .pegasys .teku .spec .util .DataStructureUtil ;
23
27
@@ -29,28 +33,77 @@ class UpdatableGraffitiProviderTest {
29
33
private UpdatableGraffitiProvider provider ;
30
34
31
35
@ Test
32
- void shouldGetStorageGraffitiWhenAvailable () {
36
+ void get_shouldGetStorageGraffitiWhenAvailable () {
33
37
provider = new UpdatableGraffitiProvider (() -> Optional .of (storageGraffiti ), Optional ::empty );
34
38
assertThat (provider .get ()).hasValue (storageGraffiti );
35
39
}
36
40
37
41
@ Test
38
- void shouldGetStorageGraffitiWhenBothAvailable () {
42
+ void get_shouldGetStorageGraffitiWhenBothAvailable () {
39
43
provider =
40
44
new UpdatableGraffitiProvider (
41
45
() -> Optional .of (storageGraffiti ), () -> Optional .of (defaultGraffiti ));
42
46
assertThat (provider .get ()).hasValue (storageGraffiti );
43
47
}
44
48
45
49
@ Test
46
- void shouldGetDefaultGraffitiWhenStorageEmpty () {
50
+ void get_shouldGetDefaultGraffitiWhenStorageEmpty () {
47
51
provider = new UpdatableGraffitiProvider (Optional ::empty , () -> Optional .of (defaultGraffiti ));
48
52
assertThat (provider .get ()).hasValue (defaultGraffiti );
49
53
}
50
54
51
55
@ Test
52
- void shouldBeEmptyWhenBothEmpty () {
56
+ void get_shouldBeEmptyWhenBothEmpty () {
53
57
provider = new UpdatableGraffitiProvider (Optional ::empty , Optional ::empty );
54
58
assertThat (provider .get ()).isEmpty ();
55
59
}
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
+ }
56
109
}
0 commit comments