13
13
14
14
package tech .pegasys .teku .validator .client .restapi .apis ;
15
15
16
+ import static org .assertj .core .api .Assertions .assertThat ;
17
+ import static org .mockito .ArgumentMatchers .any ;
18
+ import static org .mockito .Mockito .mock ;
19
+ import static org .mockito .Mockito .when ;
16
20
import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_BAD_REQUEST ;
17
21
import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_FORBIDDEN ;
18
22
import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_INTERNAL_SERVER_ERROR ;
19
- import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_NOT_IMPLEMENTED ;
23
+ import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_NOT_FOUND ;
20
24
import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_NO_CONTENT ;
21
25
import static tech .pegasys .teku .infrastructure .http .HttpStatusCodes .SC_UNAUTHORIZED ;
22
26
import static tech .pegasys .teku .infrastructure .restapi .MetadataTestUtil .verifyMetadataEmptyResponse ;
23
27
import static tech .pegasys .teku .infrastructure .restapi .MetadataTestUtil .verifyMetadataErrorResponse ;
28
+ import static tech .pegasys .teku .spec .generator .signatures .NoOpLocalSigner .NO_OP_SIGNER ;
24
29
25
30
import com .fasterxml .jackson .core .JsonProcessingException ;
31
+ import java .io .IOException ;
32
+ import java .util .Optional ;
26
33
import org .junit .jupiter .api .Test ;
34
+ import tech .pegasys .teku .bls .BLSPublicKey ;
35
+ import tech .pegasys .teku .infrastructure .http .HttpErrorResponse ;
36
+ import tech .pegasys .teku .infrastructure .restapi .StubRestApiRequest ;
37
+ import tech .pegasys .teku .spec .TestSpecFactory ;
38
+ import tech .pegasys .teku .spec .util .DataStructureUtil ;
39
+ import tech .pegasys .teku .validator .api .GraffitiManager ;
40
+ import tech .pegasys .teku .validator .client .OwnedKeyManager ;
41
+ import tech .pegasys .teku .validator .client .Validator ;
27
42
28
43
class DeleteGraffitiTest {
29
- private final DeleteGraffiti handler = new DeleteGraffiti ();
44
+ private final DataStructureUtil dataStructureUtil =
45
+ new DataStructureUtil (TestSpecFactory .createDefault ());
46
+
47
+ private final OwnedKeyManager keyManager = mock (OwnedKeyManager .class );
48
+ private final GraffitiManager graffitiManager = mock (GraffitiManager .class );
49
+ private final DeleteGraffiti handler = new DeleteGraffiti (keyManager , graffitiManager );
50
+
51
+ private final BLSPublicKey publicKey = dataStructureUtil .randomPublicKey ();
52
+
53
+ private final StubRestApiRequest request =
54
+ StubRestApiRequest .builder ()
55
+ .metadata (handler .getMetadata ())
56
+ .pathParameter ("pubkey" , publicKey .toHexString ())
57
+ .build ();
58
+
59
+ @ Test
60
+ void shouldSuccessfullyDeleteGraffiti () throws JsonProcessingException {
61
+ final Validator validator = new Validator (publicKey , NO_OP_SIGNER , Optional ::empty );
62
+ when (keyManager .getValidatorByPublicKey (any ())).thenReturn (Optional .of (validator ));
63
+ when (graffitiManager .deleteGraffiti (any ())).thenReturn (Optional .empty ());
64
+
65
+ handler .handleRequest (request );
66
+
67
+ assertThat (request .getResponseCode ()).isEqualTo (SC_NO_CONTENT );
68
+ assertThat (request .getResponseBody ()).isNull ();
69
+ }
70
+
71
+ @ Test
72
+ void shouldReturnErrorWhenIssueDeleting () throws JsonProcessingException {
73
+ final Validator validator = new Validator (publicKey , NO_OP_SIGNER , Optional ::empty );
74
+ when (keyManager .getValidatorByPublicKey (any ())).thenReturn (Optional .of (validator ));
75
+ when (graffitiManager .deleteGraffiti (any ())).thenReturn (Optional .of ("Error deleting graffiti" ));
76
+
77
+ handler .handleRequest (request );
78
+
79
+ assertThat (request .getResponseCode ()).isEqualTo (SC_INTERNAL_SERVER_ERROR );
80
+ assertThat (request .getResponseBody ())
81
+ .isEqualTo (new HttpErrorResponse (SC_INTERNAL_SERVER_ERROR , "Error deleting graffiti" ));
82
+ }
83
+
84
+ @ Test
85
+ void shouldRespondNotFoundWhenNoValidator () throws IOException {
86
+ when (keyManager .getValidatorByPublicKey (any ())).thenReturn (Optional .empty ());
87
+
88
+ handler .handleRequest (request );
89
+
90
+ assertThat (request .getResponseCode ()).isEqualTo (SC_NOT_FOUND );
91
+ }
30
92
31
93
@ Test
32
94
void metadata_shouldHandle204 () {
@@ -52,9 +114,4 @@ void metadata_shouldHandle403() throws JsonProcessingException {
52
114
void metadata_shouldHandle500 () throws JsonProcessingException {
53
115
verifyMetadataErrorResponse (handler , SC_INTERNAL_SERVER_ERROR );
54
116
}
55
-
56
- @ Test
57
- void metadata_shouldHandle501 () throws JsonProcessingException {
58
- verifyMetadataErrorResponse (handler , SC_NOT_IMPLEMENTED );
59
- }
60
117
}
0 commit comments