FetchArtifactExecutor extends TaskExecutor { @Override public Task executeTask(Task task, StringBuilder report, WorkInfo workInfo) { FetchArtifactTask taskDefinition = (FetchArtifactTask) task.getTaskDefinition(); super.updateTask(task, TaskStatus.PASSED, LocalDateTime.now(), null); if ((taskDefinition.getDesignatedPipelineExecutionId() == null) || (taskDefinition.getDesignatedPipelineExecutionId().isEmpty())) { return this.nullProcessing(report, task, "Error occurred in getting pipeline execution ID!"); } if ((taskDefinition.getDesignatedPipelineDefinitionName() == null) || (taskDefinition.getDesignatedPipelineDefinitionName().isEmpty())) { return this.nullProcessing(report, task, "Error occurred in getting pipeline name!"); } String fetchingMessage = String.format("Start fetching artifact source: " + taskDefinition.getDesignatedPipelineDefinitionName() + File.separator + taskDefinition.getDesignatedPipelineExecutionId() + File.separator + taskDefinition.getSource()); LOGGER.debug(fetchingMessage); ReportAppender.appendInfoMessage(fetchingMessage, report); String folderPath = String.format(ConfigConstants.SERVER_CREATE_ARTIFACT_API_ADDRESS, workInfo.getPipelineDefinitionName(), workInfo.getPipelineExecutionID()); AgentConfiguration.getInstallInfo().setCreateArtifactApiAddress(String.format("%s/%s", AgentConfiguration.getInstallInfo().getServerAddress(), folderPath)); String requestSource = this.fileManagementService.urlCombine(AgentConfiguration.getInstallInfo().getCreateArtifactApiAddress()) + "/fetch-artifact"; WebResource webResource = this.restClient.resource(requestSource); String source = taskDefinition.getDesignatedPipelineDefinitionName() + File.separator + taskDefinition.getDesignatedPipelineExecutionId() + File.separator + taskDefinition.getSource(); ClientResponse response = webResource.type("application/json").post(ClientResponse.class, source); if ((response.getStatus() != 200)) { return this.nullProcessing(report, task, String.format("Could not get resource. TaskStatus code %s", response.getStatus())); } if (response.getEntityInputStream() == null) { return this.nullProcessing(report, task, "Could not get resource. Input stream is null"); } String filePath = Paths.get(AgentConfiguration.getInstallInfo().getAgentTempDirectoryPath(), UUID.randomUUID() + ".zip").toString(); File fetchArtifactDir = new File(filePath); this.fileManagementService.generateDirectory(fetchArtifactDir); String errorMessage; errorMessage = this.fileManagementService.initiateFile(fetchArtifactDir, response.getEntityInputStream(), filePath); if (errorMessage != null) { return this.nullProcessing(report, task, "Error occurred in creating the artifact!"); } String destination; if (taskDefinition.getDestination() != null) { destination = String.valueOf(Paths.get(AgentConfiguration.getInstallInfo().getAgentPipelinesDir() + File.separator + taskDefinition.getDesignatedPipelineDefinitionName(), taskDefinition.getDestination())); } else { destination = String.valueOf(Paths.get(AgentConfiguration.getInstallInfo().getAgentPipelinesDir() + File.separator + taskDefinition.getDesignatedPipelineDefinitionName())); } errorMessage = this.fileManagementService.unzipFile(filePath, destination); filePath = Paths.get(AgentConfiguration.getInstallInfo().getAgentTempDirectoryPath()).toString(); String deleteMessage = this.fileManagementService.deleteFilesInDirectory(filePath); if (errorMessage != null) { return this.nullProcessing(report, task, "Error occurred in unzipping files!"); } if (deleteMessage != null) { return this.nullProcessing(report, task, "Error occurred in deleting files!"); } super.updateTask(task, TaskStatus.PASSED, null, LocalDateTime.now()); String fetchedMessage = String.format("Saved artifact to %s after verifying the integrity of its contents.", destination); LOGGER.debug(fetchedMessage); ReportAppender.appendInfoMessage(fetchedMessage, report); return task; } FetchArtifactExecutor();  FetchArtifactExecutor(Client client, IFileManagementService fileManagementService); @Override Task executeTask(Task task, StringBuilder report, WorkInfo workInfo);  }
@Test public void executeTask_artifactFetchedSuccessfully_taskPassed() { Mockito.when(this.mockedFileManagementService.unzipFile(Mockito.anyString(), Mockito.anyString())).thenReturn(null); Mockito.when(this.mockedFileManagementService.initiateFile(Mockito.any(File.class), Mockito.any(InputStream.class), Mockito.anyString())).thenReturn(null); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("sourceForAPI"); Mockito.when(this.mockedClient.resource("sourceForAPI/fetch-artifact")).thenReturn(this.mockedResource); Mockito.when(this.mockedResource.type(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.accept(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.type("application/json").post(ClientResponse.class, this.source)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Mockito.when(this.mockedResponse.getEntityInputStream()).thenReturn(this.mockedInputStream); Task resultTask = this.fetchArtifactExecutor.executeTask(this.correctFetchArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).unzipFile(Mockito.anyString(), Mockito.anyString()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).initiateFile(Mockito.any(File.class), Mockito.any(InputStream.class), Mockito.anyString()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).urlCombine(Mockito.anyString()); Mockito.verify(this.mockedResponse, Mockito.times(1)).getStatus(); Assert.assertEquals(TaskStatus.PASSED, resultTask.getStatus()); Assert.assertEquals(this.fetchArtifactTaskDefinition.getName(), resultTask.getTaskDefinition().getName()); }
@Test public void executeTask_failedToDeleteFiles_taskFailed() { Mockito.when(this.mockedFileManagementService.unzipFile(Mockito.anyString(), Mockito.anyString())).thenReturn(null); Mockito.when(this.mockedFileManagementService.initiateFile(Mockito.any(File.class), Mockito.any(InputStream.class), Mockito.anyString())).thenReturn(null); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("sourceForAPI"); Mockito.when(this.mockedClient.resource("sourceForAPI/fetch-artifact")).thenReturn(this.mockedResource); Mockito.when(this.mockedResource.type(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.accept(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.type("application/json").post(ClientResponse.class, this.source)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Mockito.when(this.mockedResponse.getEntityInputStream()).thenReturn(this.mockedInputStream); Mockito.when(this.mockedFileManagementService.deleteFilesInDirectory(Mockito.anyString())).thenReturn("Error"); Task resultTask = this.fetchArtifactExecutor.executeTask(this.correctFetchArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).unzipFile(Mockito.anyString(), Mockito.anyString()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).initiateFile(Mockito.any(File.class), Mockito.any(InputStream.class), Mockito.anyString()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).urlCombine(Mockito.anyString()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).deleteFilesInDirectory(Mockito.anyString()); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.fetchArtifactTaskDefinition.getName(), resultTask.getTaskDefinition().getName()); }
@Test public void executeTask_failedToUnzipFiles_taskFailed() { Mockito.when(this.mockedFileManagementService.initiateFile(Mockito.any(File.class), Mockito.any(InputStream.class), Mockito.anyString())).thenReturn(null); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("sourceForAPI"); Mockito.when(this.mockedClient.resource("sourceForAPI/fetch-artifact")).thenReturn(this.mockedResource); Mockito.when(this.mockedResource.type(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.accept(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.type("application/json").post(ClientResponse.class, this.source)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Mockito.when(this.mockedResponse.getEntityInputStream()).thenReturn(this.mockedInputStream); Mockito.when(this.mockedFileManagementService.unzipFile(Mockito.anyString(), Mockito.anyString())).thenReturn("Error"); Task resultTask = this.fetchArtifactExecutor.executeTask(this.correctFetchArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).unzipFile(Mockito.anyString(), Mockito.anyString()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).initiateFile(Mockito.any(File.class), Mockito.any(InputStream.class), Mockito.anyString()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).urlCombine(Mockito.anyString()); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.fetchArtifactTaskDefinition.getName(), resultTask.getTaskDefinition().getName()); }
@Test public void executeTask_failedToCreateFiles_taskFailed() { Mockito.when(this.mockedFileManagementService.initiateFile(Mockito.any(File.class), Mockito.any(InputStream.class), Mockito.anyString())).thenReturn("Error"); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("sourceForAPI"); Mockito.when(this.mockedClient.resource("sourceForAPI/fetch-artifact")).thenReturn(this.mockedResource); Mockito.when(this.mockedResource.type(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.accept(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.type("application/json").post(ClientResponse.class, this.source)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Mockito.when(this.mockedResponse.getEntityInputStream()).thenReturn(this.mockedInputStream); Task resultTask = this.fetchArtifactExecutor.executeTask(this.correctFetchArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).initiateFile(Mockito.any(File.class), Mockito.any(InputStream.class), Mockito.anyString()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).urlCombine(Mockito.anyString()); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.fetchArtifactTaskDefinition.getName(), resultTask.getTaskDefinition().getName()); }
@Test public void executeTask_responseNotOk_taskFailed() { Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("sourceForAPI"); Mockito.when(this.mockedClient.resource("sourceForAPI/fetch-artifact")).thenReturn(this.mockedResource); Mockito.when(this.mockedResource.type(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.accept(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.type("application/json").post(ClientResponse.class, this.source)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(400); Mockito.when(this.mockedResponse.getEntityInputStream()).thenReturn(this.mockedInputStream); Task resultTask = this.fetchArtifactExecutor.executeTask(this.correctFetchArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).urlCombine(Mockito.anyString()); Assert.assertEquals(400, this.mockedResponse.getStatus()); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.fetchArtifactTaskDefinition.getName(), resultTask.getTaskDefinition().getName()); }
@Test public void executeTask_inputStreamNull_taskFailed() { Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("sourceForAPI"); Mockito.when(this.mockedClient.resource("sourceForAPI/fetch-artifact")).thenReturn(this.mockedResource); Mockito.when(this.mockedResource.type(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.accept(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.type("application/json").post(ClientResponse.class, this.source)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Mockito.when(this.mockedResponse.getEntityInputStream()).thenReturn(null); Task resultTask = this.fetchArtifactExecutor.executeTask(this.correctFetchArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).urlCombine(Mockito.anyString()); Assert.assertEquals(200, this.mockedResponse.getStatus()); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.fetchArtifactTaskDefinition.getName(), resultTask.getTaskDefinition().getName()); }