### Question:
JarUploadHandler extends
		AbstractRestHandler<RestfulGateway, FileUpload, JarUploadResponseBody, EmptyMessageParameters> { @Override protected CompletableFuture<JarUploadResponseBody> handleRequest( @Nonnull final HandlerRequest<FileUpload, EmptyMessageParameters> request, @Nonnull final RestfulGateway gateway) throws RestHandlerException { final FileUpload fileUpload = request.getRequestBody(); return CompletableFuture.supplyAsync(() -> { if (!fileUpload.getPath().getFileName().toString().endsWith(".jar")) { deleteUploadedFile(fileUpload); throw new CompletionException(new RestHandlerException( "Only Jar files are allowed.", HttpResponseStatus.BAD_REQUEST)); } else { final Path destination = jarDir.resolve(fileUpload.getPath().getFileName()); try { Files.move(fileUpload.getPath(), destination); } catch (IOException e) { deleteUploadedFile(fileUpload); throw new CompletionException(new RestHandlerException( String.format("Could not move uploaded jar file [%s] to [%s].", fileUpload.getPath(), destination), HttpResponseStatus.INTERNAL_SERVER_ERROR, e)); } return new JarUploadResponseBody(fileUpload.getPath() .normalize() .toString()); } }, executor); } JarUploadHandler(
			final CompletableFuture<String> localRestAddress,
			final GatewayRetriever<? extends RestfulGateway> leaderRetriever,
			final Time timeout,
			final Map<String, String> responseHeaders,
			final MessageHeaders<FileUpload, JarUploadResponseBody, EmptyMessageParameters> messageHeaders,
			final Path jarDir,
			final Executor executor);   }### Answer:
@Test public void testRejectNonJarFiles() throws Exception { final Path uploadedFile = Files.createFile(jarDir.resolve("katrin.png")); final HandlerRequest<FileUpload, EmptyMessageParameters> request = createRequest(uploadedFile); try { jarUploadHandler.handleRequest(request, mockDispatcherGateway).get(); fail("Expected exception not thrown."); } catch (final ExecutionException e) { final Throwable throwable = ExceptionUtils.stripCompletionException(e.getCause()); assertThat(throwable, instanceOf(RestHandlerException.class)); final RestHandlerException restHandlerException = (RestHandlerException) throwable; assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.BAD_REQUEST)); } }
@Test public void testUploadJar() throws Exception { final Path uploadedFile = Files.createFile(jarDir.resolve("Kafka010Example.jar")); final HandlerRequest<FileUpload, EmptyMessageParameters> request = createRequest(uploadedFile); final JarUploadResponseBody jarUploadResponseBody = jarUploadHandler.handleRequest(request, mockDispatcherGateway).get(); assertThat(jarUploadResponseBody.getStatus(), equalTo(JarUploadResponseBody.UploadStatus.success)); assertThat(jarUploadResponseBody.getFilename(), equalTo(uploadedFile.normalize().toString())); }
@Test public void testFailedUpload() throws Exception { final Path uploadedFile = jarDir.resolve("Kafka010Example.jar"); final HandlerRequest<FileUpload, EmptyMessageParameters> request = createRequest(uploadedFile); try { jarUploadHandler.handleRequest(request, mockDispatcherGateway).get(); fail("Expected exception not thrown."); } 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("Could not move uploaded jar file")); assertThat(restHandlerException.getHttpResponseStatus(), equalTo(HttpResponseStatus.INTERNAL_SERVER_ERROR)); } }