### Question:
PostAggregateAndProof implements Handler { @OpenApi( path = ROUTE, method = HttpMethod.POST, summary = "Verifies given aggregate and proof and publishes it on appropriate gossipsub topic.", tags = {TAG_VALIDATOR}, requestBody = @OpenApiRequestBody(content = {@OpenApiContent(from = SignedAggregateAndProof.class)}), description = "Aggregates all attestations matching given attestation data root and slot.", responses = { @OpenApiResponse(status = RES_OK, description = "Successfully processed attestation."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Invalid parameter supplied."), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node internal error.") }) @Override public void handle(Context ctx) throws Exception { try { final SignedAggregateAndProof signedAggregateAndProof = jsonProvider.jsonToObject(ctx.body(), SignedAggregateAndProof.class); provider.sendAggregateAndProof(signedAggregateAndProof); ctx.status(SC_OK); } catch (final JsonMappingException e) { ctx.result(jsonProvider.objectToJSON(new BadRequest(e.getMessage()))); ctx.status(SC_BAD_REQUEST); } catch (Exception e) { ctx.status(SC_INTERNAL_SERVER_ERROR); } } PostAggregateAndProof(
      final ValidatorDataProvider provider, final JsonProvider jsonProvider); @OpenApi( path = ROUTE, method = HttpMethod.POST, summary = "Verifies given aggregate and proof and publishes it on appropriate gossipsub topic.", tags = {TAG_VALIDATOR}, requestBody = @OpenApiRequestBody(content = {@OpenApiContent(from = SignedAggregateAndProof.class)}), description = "Aggregates all attestations matching given attestation data root and slot.", responses = { @OpenApiResponse(status = RES_OK, description = "Successfully processed attestation."), @OpenApiResponse(status = RES_BAD_REQUEST, description = "Invalid parameter supplied."), @OpenApiResponse(status = RES_INTERNAL_ERROR, description = "Beacon node internal error.") }) @Override void handle(Context ctx); static final String ROUTE; }### Answer:
@Test public void shouldReturnBadRequestWhenRequestBodyIsInvalid() throws Exception { when(context.body()).thenReturn("{\"foo\": \"bar\"}"); handler.handle(context); verify(context).status(SC_BAD_REQUEST); }
@Test public void shouldReturnServerErrorWhenFutureHasUnmappedException() throws Exception { final SignedAggregateAndProof signedAggregateAndProof = dataStructureUtil.randomSignedAggregateAndProof(); final tech.pegasys.teku.api.schema.SignedAggregateAndProof schemaSignedAggregateAndProof = new tech.pegasys.teku.api.schema.SignedAggregateAndProof(signedAggregateAndProof); when(context.body()).thenReturn(jsonProvider.objectToJSON(schemaSignedAggregateAndProof)); doThrow(new RuntimeException()).when(provider).sendAggregateAndProof(any()); handler.handle(context); verify(context).status(SC_INTERNAL_SERVER_ERROR); }
@Test public void shouldReturnSuccessWhenSendAggregateAndProofSucceeds() throws Exception { final SignedAggregateAndProof signedAggregateAndProof = dataStructureUtil.randomSignedAggregateAndProof(); final tech.pegasys.teku.api.schema.SignedAggregateAndProof schemaSignedAggregateAndProof = new tech.pegasys.teku.api.schema.SignedAggregateAndProof(signedAggregateAndProof); String signedAggregateAndProofAsJson = jsonProvider.objectToJSON(schemaSignedAggregateAndProof); when(context.body()).thenReturn(signedAggregateAndProofAsJson); handler.handle(context); ArgumentCaptor<tech.pegasys.teku.api.schema.SignedAggregateAndProof> captor = ArgumentCaptor.forClass(tech.pegasys.teku.api.schema.SignedAggregateAndProof.class); verify(provider).sendAggregateAndProof(captor.capture()); verify(context).status(SC_OK); assertThat(jsonProvider.objectToJSON(captor.getValue())) .isEqualTo(signedAggregateAndProofAsJson); }