### Question:
GetChainHead implements Handler { @OpenApi( path = ROUTE, method = HttpMethod.GET, summary = "Get information about the chain head.", tags = {TAG_BEACON}, description = "Returns information about the head of the beacon chain including the finalized and " + "justified information.", responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = BeaconChainHead.class)), @OpenApiResponse(status = RES_NO_CONTENT, description = NO_CONTENT_PRE_GENESIS), @OpenApiResponse(status = RES_INTERNAL_ERROR) }) @Override public void handle(final Context ctx) throws JsonProcessingException { ctx.header(Header.CACHE_CONTROL, CACHE_NONE); final Optional<BeaconChainHead> beaconChainHead = provider.getHeadState(); if (beaconChainHead.isPresent()) { ctx.result(jsonProvider.objectToJSON(beaconChainHead.get())); } else { ctx.status(SC_NO_CONTENT); } } GetChainHead(final ChainDataProvider provider, final JsonProvider jsonProvider); @OpenApi( path = ROUTE, method = HttpMethod.GET, summary = "Get information about the chain head.", tags = {TAG_BEACON}, description = "Returns information about the head of the beacon chain including the finalized and " + "justified information.", responses = { @OpenApiResponse(status = RES_OK, content = @OpenApiContent(from = BeaconChainHead.class)), @OpenApiResponse(status = RES_NO_CONTENT, description = NO_CONTENT_PRE_GENESIS), @OpenApiResponse(status = RES_INTERNAL_ERROR) }) @Override void handle(final Context ctx); static final String ROUTE; }### Answer:
@Test public void shouldReturnBeaconChainHeadResponse() throws Exception { final GetChainHead handler = new GetChainHead(provider, jsonProvider); final BeaconChainHead beaconChainHead = new BeaconChainHead(blockAndState); final String expected = jsonProvider.objectToJSON(beaconChainHead); when(provider.getHeadState()).thenReturn(Optional.of(beaconChainHead)); handler.handle(context); verify(context).result(expected); }
@Test public void shouldReturnNoContentWhenStateIsNull() throws Exception { final GetChainHead handler = new GetChainHead(provider, jsonProvider); when(provider.getHeadState()).thenReturn(Optional.empty()); handler.handle(context); verify(context).status(SC_NO_CONTENT); }