FetchMaterialExecutor extends TaskExecutor { @Override public Task executeTask(Task task, StringBuilder report, WorkInfo workInfo) { FetchMaterialTask fetchMaterialTask = (FetchMaterialTask) task.getTaskDefinition(); this.updateTask(task, TaskStatus.PASSED, LocalDateTime.now(), null); String fetchingMessage = String.format("Fetching Material %s", fetchMaterialTask.getMaterialName()); LOGGER.debug(fetchingMessage); ReportAppender.appendInfoMessage(fetchingMessage, report); String materialPath = Paths.get( AgentConfiguration.getInstallInfo().getAgentPipelinesDir(), fetchMaterialTask.getPipelineName(), fetchMaterialTask.getDestination()) .toString(); String errorMessage = this.fileManagementService.deleteDirectoryRecursively(materialPath); if (errorMessage != null) { return this.nullProcessing(report, task, String.format("Unable to clean directory %s", materialPath)); } errorMessage = this.materialService.fetchMaterial(fetchMaterialTask); if (errorMessage == null) { this.updateTask(task, TaskStatus.PASSED, null, LocalDateTime.now()); String fetchedMessage = String.format("Material fetched at %s", materialPath); LOGGER.debug(fetchedMessage); ReportAppender.appendInfoMessage(fetchedMessage, report); } else { this.nullProcessing(report, task, errorMessage); } return task; } FetchMaterialExecutor(IMaterialService materialService);  FetchMaterialExecutor(IMaterialService materialService, IFileManagementService fileManagementService); @Override Task executeTask(Task task, StringBuilder report, WorkInfo workInfo);  }
@Test public void executeTask_validTask_taskPassed() { StringBuilder report = new StringBuilder(); TaskStatus expectedStatus = TaskStatus.PASSED; Task actualResult = this.fetchMaterialExecutor.executeTask(this.correctFetchMaterialTask, report, this.workInfo); Assert.assertEquals(actualResult.getStatus(), expectedStatus); Mockito.verify(this.mockedGitService, Mockito.times(1)).fetchMaterial((FetchMaterialTask) this.correctFetchMaterialTask.getTaskDefinition()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).deleteDirectoryRecursively(this.correctDirectoryPath); }
@Test public void executeTask_directoryCleanFailed_taskFailed() { StringBuilder report = new StringBuilder(); Task actualResult = this.fetchMaterialExecutor.executeTask(this.incorrectFetchMaterialTask, report, this.workInfo); Assert.assertEquals(actualResult.getStatus(), TaskStatus.FAILED); Mockito.verify(this.mockedGitService, Mockito.times(0)).fetchMaterial((FetchMaterialTask) this.incorrectFetchMaterialTask.getTaskDefinition()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).deleteDirectoryRecursively(this.wrongDirectoryPath); }
@Test public void executeTask_fetchMaterialFailed_taskFailed() { StringBuilder report = new StringBuilder(); Task actualResult = this.fetchMaterialExecutor.executeTask(this.secondIncorrectFetchMaterialTask, report, this.workInfo); Assert.assertEquals(actualResult.getStatus(), TaskStatus.FAILED); Mockito.verify(this.mockedGitService, Mockito.times(1)).fetchMaterial((FetchMaterialTask) this.secondIncorrectFetchMaterialTask.getTaskDefinition()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).deleteDirectoryRecursively("Pipelines"); }