### Question:
JarDeleteHandler extends AbstractRestHandler<RestfulGateway, EmptyRequestBody, EmptyResponseBody, JarDeleteMessageParameters> { @Override protected CompletableFuture<EmptyResponseBody> handleRequest( @Nonnull final HandlerRequest<EmptyRequestBody, JarDeleteMessageParameters> request, @Nonnull final RestfulGateway gateway) throws RestHandlerException { final String jarId = request.getPathParameter(JarIdPathParameter.class); return CompletableFuture.supplyAsync(() -> { final Path jarToDelete = jarDir.resolve(jarId); if (!Files.exists(jarToDelete)) { throw new CompletionException(new RestHandlerException( String.format("File %s does not exist in %s.", jarId, jarDir), HttpResponseStatus.BAD_REQUEST)); } else { try { Files.delete(jarToDelete); return EmptyResponseBody.getInstance(); } catch (final IOException e) { throw new CompletionException(new RestHandlerException( String.format("Failed to delete jar %s.", jarToDelete), HttpResponseStatus.INTERNAL_SERVER_ERROR, e)); } } }, executor); } JarDeleteHandler(
			final GatewayRetriever<? extends RestfulGateway> leaderRetriever,
			final Time timeout,
			final Map<String, String> responseHeaders,
			final MessageHeaders<EmptyRequestBody, EmptyResponseBody, JarDeleteMessageParameters> messageHeaders,
			final Path jarDir,
			final Executor executor);   }### Answer:
@Test public void testDeleteUnknownJar() throws Exception { final HandlerRequest<EmptyRequestBody, JarDeleteMessageParameters> request = createRequest("doesnotexist.jar"); try { jarDeleteHandler.handleRequest(request, restfulGateway).get(); } catch (final ExecutionException e) { final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause()); assertThat(throwable, instanceOf(RestHandlerException.class)); final RestHandlerException restHandlerException = (RestHandlerException) throwable; assertThat(restHandlerException.getMessage(), containsString("File doesnotexist.jar does not exist in")); assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST)); } }
@Test public void testDeleteJarById() throws Exception { assertThat(Files.exists(jarDir.resolve(TEST_JAR_NAME)), equalTo(true)); final HandlerRequest<EmptyRequestBody, JarDeleteMessageParameters> request = createRequest(TEST_JAR_NAME); jarDeleteHandler.handleRequest(request, restfulGateway).get(); assertThat(Files.exists(jarDir.resolve(TEST_JAR_NAME)), equalTo(false)); }
@Test public void testDeleteUnknownJar() throws Exception { final HandlerRequest<EmptyRequestBody, JarDeleteMessageParameters> request = createRequest("doesnotexist.jar"); try { jarDeleteHandler.handleRequest(request, restfulGateway).get(); } catch (final ExecutionException e) { final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause()); assertThat(throwable, instanceOf(RestHandlerException.class)); final RestHandlerException restHandlerException = (RestHandlerException) throwable; assertThat(restHandlerException.getMessage(), containsString("File doesnotexist.jar does not exist in")); assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST)); return; } fail("The test should have failed by now."); }
@Test public void testFailedDelete() throws Exception { makeJarDirReadOnly(); final HandlerRequest<EmptyRequestBody, JarDeleteMessageParameters> request = createRequest(TEST_JAR_NAME); try { jarDeleteHandler.handleRequest(request, restfulGateway).get(); } catch (final ExecutionException e) { final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause()); assertThat(throwable, instanceOf(RestHandlerException.class)); final RestHandlerException restHandlerException = (RestHandlerException) throwable; assertThat(restHandlerException.getMessage(), containsString("Failed to delete jar")); assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.INTERNAL_SERVER_ERROR)); } }