ArchetypeGeneratorService { @GET @Path("/status/{id}") @Produces(MediaType.APPLICATION_JSON) public GenerationTaskDescriptor getStatus(@Context UriInfo uriInfo, @PathParam("id") String id) throws ServerException { ArchetypeGenerator.GenerationTask task = archetypeGenerator.getTaskById(Long.valueOf(id)); final GenerationTaskDescriptor status = DtoFactory.getInstance().createDto(GenerationTaskDescriptor.class); if (!task.isDone()) { status.setStatus(GenerationTaskDescriptor.Status.IN_PROGRESS); } else if (task.getResult().isSuccessful()) { status.setStatus(GenerationTaskDescriptor.Status.SUCCESSFUL); final String downloadURL = uriInfo.getBaseUriBuilder() .path(getClass()).path(getClass(), "downloadGeneratedProject") .build(task.getId()).toString(); status.setDownloadUrl(downloadURL); } else { status.setStatus(GenerationTaskDescriptor.Status.FAILED); try { status.setReport(new String(Files.readAllBytes(task.getResult().getGenerationReport().toPath()))); } catch (IOException e) { LOG.error(e.getMessage(), e); } } return status; }  @Path("/generate") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) GenerationTaskDescriptor generate(@Context UriInfo uriInfo,
                                             @QueryParam("groupId") String groupId,
                                             @QueryParam("artifactId") String artifactId,
                                             @QueryParam("version") String version,
                                             MavenArchetype archetype); @GET @Path("/status/{id}") @Produces(MediaType.APPLICATION_JSON) GenerationTaskDescriptor getStatus(@Context UriInfo uriInfo, @PathParam("id") String id); @GET @Path("/download/{id}") Response downloadGeneratedProject(@PathParam("id") String id);  }
@Test public void testGetStatusWhenTaskIsNotDone() throws Exception { doReturn(false).when(taskMock).isDone(); doReturn(taskMock).when(archetypeGenerator).getTaskById(anyLong()); GenerationTaskDescriptor task = service.getStatus(uriInfo, String.valueOf(taskId)); Assert.assertEquals(GenerationTaskDescriptor.Status.IN_PROGRESS, task.getStatus()); }
@Test public void testGetStatusWhenTaskIsSuccessful() throws Exception { doReturn(true).when(taskMock).isDone(); GenerationResult generationResult = mock(GenerationResult.class); doReturn(true).when(generationResult).isSuccessful(); doReturn(generationResult).when(taskMock).getResult(); doReturn(taskMock).when(archetypeGenerator).getTaskById(anyLong()); GenerationTaskDescriptor task = service.getStatus(uriInfo, String.valueOf(taskId)); Assert.assertEquals(GenerationTaskDescriptor.Status.SUCCESSFUL, task.getStatus()); Assert.assertEquals("http: }
@Test public void testGetStatusWhenTaskIsFailed() throws Exception { final Path testLogFile = Paths.get(Thread.currentThread().getContextClassLoader().getResource("test.log").toURI()); doReturn(true).when(taskMock).isDone(); GenerationResult generationResult = mock(GenerationResult.class); doReturn(false).when(generationResult).isSuccessful(); doReturn(new File(testLogFile.toString())).when(generationResult).getGenerationReport(); doReturn(generationResult).when(taskMock).getResult(); doReturn(taskMock).when(archetypeGenerator).getTaskById(anyLong()); GenerationTaskDescriptor task = service.getStatus(uriInfo, String.valueOf(taskId)); Assert.assertEquals(GenerationTaskDescriptor.Status.FAILED, task.getStatus()); Assert.assertEquals(new String(Files.readAllBytes(testLogFile)), task.getReport()); }
@Test(expected = ServerException.class) public void testGetStatusWithInvalidTaskId() throws Exception { doThrow(ServerException.class).when(archetypeGenerator).getTaskById(anyLong()); service.getStatus(uriInfo, String.valueOf(taskId)); }