UploadArtifactExecutor extends TaskExecutor { @Override public Task executeTask(Task task, StringBuilder report, WorkInfo workInfo) { UploadArtifactTask taskDefinition = (UploadArtifactTask) task.getTaskDefinition(); this.updateTask(task, TaskStatus.PASSED, LocalDateTime.now(), null); String uploadingMessage = String.format("Start uploading artifact source: %s ", taskDefinition.getSource()); LOGGER.debug(uploadingMessage); ReportAppender.appendInfoMessage(uploadingMessage, report); String pathToFile = AgentConfiguration.getInstallInfo().getAgentSandbox() + File.separator + "Pipelines" + File.separator + workInfo.getPipelineDefinitionName() + File.separator + taskDefinition.getSource(); String fullPath = this.fileManagementService.pathCombine(pathToFile); String rootPath = this.fileManagementService.getRootPath(fullPath); String wildCardPattern = this.fileManagementService.getPattern(rootPath, fullPath); if (rootPath.isEmpty()) { return this.nullProcessing(report, task, String.format("%s is Non-existent source.", taskDefinition.getSource())); } List<File> files = this.fileManagementService.getFiles(fullPath, wildCardPattern); if (files.size() == 0) { return this.nullProcessing(report, task, String.format("Error in getting files in %s", fullPath)); } File zipFile = this.fileManagementService.generateUniqueFile(AgentConfiguration.getInstallInfo().getAgentTempDirectoryPath(), "zip"); String errorMessage = this.fileManagementService.zipFiles(zipFile.getPath(), files, rootPath, true); if (errorMessage != null) { zipFile.delete(); return this.nullProcessing(report, task, "Error occurred in zipping files!"); } String executionFolder = String.valueOf(workInfo.getPipelineExecutionID()); UploadArtifactInfo uploadArtifactInfo = new UploadArtifactInfo(zipFile, taskDefinition.getDestination()); String folderPath = String.format(ConfigConstants.SERVER_CREATE_ARTIFACT_API_ADDRESS, workInfo.getPipelineDefinitionName(), executionFolder); AgentConfiguration.getInstallInfo().setCreateArtifactApiAddress(String.format("%s/%s", AgentConfiguration.getInstallInfo().getServerAddress(), folderPath)); String destination = null; try { destination = URLEncoder.encode(taskDefinition.getDestination(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } String requestSource = this.fileManagementService.urlCombine(AgentConfiguration.getInstallInfo().getCreateArtifactApiAddress()) + "/upload-artifact" + "?destination=" + destination; WebResource webResource = this.restClient.resource(requestSource); InputStream targetStream = null; try { targetStream = FileUtils.openInputStream(zipFile); } catch (IOException e) { e.printStackTrace(); } ClientResponse response = webResource .type(MediaType.MULTIPART_FORM_DATA_TYPE) .post(ClientResponse.class, targetStream); if (response.getStatus() != 200) { zipFile.delete(); return this.nullProcessing(report, task, String.format("Error occurred in server response! Returned status code: %s", response.getStatus())); } zipFile.delete(); this.updateTask(task, TaskStatus.PASSED, null, LocalDateTime.now()); return task; } UploadArtifactExecutor();  UploadArtifactExecutor(Client client, IFileManagementService fileManagementService); @Override Task executeTask(Task task, StringBuilder report, WorkInfo workInfo);  }
@Test public void executeTask_emptyRootPath_taskFailed() { Mockito.when(this.mockedFileManagementService.pathCombine(Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getRootPath(Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getPattern(Mockito.anyString(), Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getFiles(Mockito.anyString(), Mockito.anyString())).thenReturn(null); Mockito.when(this.mockedFileManagementService.generateUniqueFile(Mockito.anyString(), Mockito.anyString())).thenReturn(this.mockedFile); Mockito.when(this.mockedFileManagementService.zipFiles("pathToFile", this.mockedFileList, "rootPath", false)).thenReturn(null); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("my/path"); Mockito.when(this.mockedClient.resource("my/path")).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("multipart/form-data").post(ClientResponse.class, this.mockedFile)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Task resultTask = this.uploadArtifactExecutor.executeTask(this.correctUploadArtifactTask, this.report, this.workInfo); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.correctUploadArtifactTask.getTaskDefinition(), resultTask.getTaskDefinition()); Assert.assertEquals(this.correctUploadArtifactTask.getTaskDefinition().getType(), resultTask.getTaskDefinition().getType()); }
@Ignore @Test public void executeTask_artifactUploadedSuccessfully_taskPassed() { Mockito.when(this.mockedFileManagementService.pathCombine(Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getRootPath(Mockito.anyString())).thenReturn("rootPath"); Mockito.when(this.mockedFileManagementService.getPattern(Mockito.anyString(), Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getFiles(Mockito.anyString(), Mockito.anyString())).thenReturn(this.mockedFileList); Mockito.when(this.mockedFileManagementService.generateUniqueFile(Mockito.anyString(), Mockito.anyString())).thenReturn(this.mockedFile); Mockito.when(this.mockedFileManagementService.zipFiles("pathToFile", this.mockedFileList, "rootPath", false)).thenReturn(null); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("my/path"); UploadArtifactInfo uploadArtifactInfo = new UploadArtifactInfo(this.mockedFile, this.uploadArtifactTaskDefinition.getDestination()); String uploadArtifactInfoAsString = this.jsonConverter.toJson(uploadArtifactInfo); Mockito.when(this.mockedClient.resource("my/path/upload-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("multipart/form-data").post(ClientResponse.class, uploadArtifactInfoAsString)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Task resultTask = this.uploadArtifactExecutor.executeTask(this.correctUploadArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).zipFiles(Mockito.anyObject(), Mockito.any(this.mockedFileList.getClass()), Mockito.anyString(), Mockito.anyBoolean()); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).getFiles(Mockito.anyString(), Mockito.anyString()); Assert.assertEquals(TaskStatus.PASSED, resultTask.getStatus()); }
@Test public void executeTask_failedToZipFiles_taskFailed() { Mockito.when(this.mockedFileManagementService.pathCombine(Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getRootPath(Mockito.anyString())).thenReturn("rootPath"); Mockito.when(this.mockedFileManagementService.getPattern(Mockito.anyString(), Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getFiles(Mockito.anyString(), Mockito.anyString())).thenReturn(this.mockedFileList); Mockito.when(this.mockedFileManagementService.generateUniqueFile(Mockito.anyString(), Mockito.anyString())).thenReturn(this.mockedFile); Mockito.when(this.mockedFileManagementService.zipFiles("pathToFile", this.mockedFileList, "rootPath", true)).thenReturn("Error in zipFiles method!"); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("my/path"); Mockito.when(this.mockedClient.resource("my/path/upload-artifact")).thenReturn(this.mockedResource); Mockito.when(this.mockedResource.type(Mockito.anyString())).thenReturn(this.mockedBuilder); Mockito.when(this.mockedResource.accept(Mockito.anyString())).thenReturn(this.mockedBuilder); UploadArtifactInfo uploadArtifactInfo = new UploadArtifactInfo(this.mockedFile, this.uploadArtifactTaskDefinition.getDestination()); String uploadArtifactInfoAsString = this.jsonConverter.toJson(uploadArtifactInfo); Mockito.when(this.mockedResource.type("multipart/form-data").post(ClientResponse.class, uploadArtifactInfoAsString)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Task resultTask = this.uploadArtifactExecutor.executeTask(this.correctUploadArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).zipFiles(Mockito.anyObject(), Mockito.any(this.mockedFileList.getClass()), Mockito.anyString(), Mockito.anyBoolean()); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.correctUploadArtifactTask.getTaskDefinition(), resultTask.getTaskDefinition()); Assert.assertEquals(this.correctUploadArtifactTask.getTaskDefinition().getType(), resultTask.getTaskDefinition().getType()); }
@Ignore @Test public void executeTask_responseNotOk_taskFailed() { Mockito.when(this.mockedFileManagementService.pathCombine(Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getRootPath(Mockito.anyString())).thenReturn("rootPath"); Mockito.when(this.mockedFileManagementService.getPattern(Mockito.anyString(), Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getFiles(Mockito.anyString(), Mockito.anyString())).thenReturn(this.mockedFileList); Mockito.when(this.mockedFileManagementService.generateUniqueFile(Mockito.anyString(), Mockito.anyString())).thenReturn(this.mockedFile); Mockito.when(this.mockedFileManagementService.zipFiles("pathToFile", this.mockedFileList, "rootPath", false)).thenReturn(null); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("my/path"); UploadArtifactInfo uploadArtifactInfo = new UploadArtifactInfo(this.mockedFile, this.uploadArtifactTaskDefinition.getDestination()); String uploadArtifactInfoAsString = this.jsonConverter.toJson(uploadArtifactInfo); Mockito.when(this.mockedClient.resource("my/path/upload-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, uploadArtifactInfoAsString)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(400); Task resultTask = this.uploadArtifactExecutor.executeTask(this.correctUploadArtifactTask, this.report, this.workInfo); Mockito.verify(this.mockedFileManagementService, Mockito.times(1)).zipFiles(Mockito.anyObject(), Mockito.any(this.mockedFileList.getClass()), Mockito.anyString(), Mockito.anyBoolean()); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.correctUploadArtifactTask.getTaskDefinition(), resultTask.getTaskDefinition()); Assert.assertEquals(this.correctUploadArtifactTask.getTaskDefinition().getType(), resultTask.getTaskDefinition().getType()); }
@Test public void executeTask_noFilesToUpload_taskFailed() { Mockito.when(this.mockedFileManagementService.pathCombine(Mockito.anyString())).thenReturn("full\\path"); Mockito.when(this.mockedFileManagementService.getRootPath(Mockito.anyString())).thenReturn("rootPath"); Mockito.when(this.mockedFileManagementService.getPattern(Mockito.anyString(), Mockito.anyString())).thenReturn(""); Mockito.when(this.mockedFileManagementService.getFiles(Mockito.anyString(), Mockito.anyString())).thenReturn(new ArrayList<>()); Mockito.when(this.mockedFileManagementService.generateUniqueFile(Mockito.anyString(), Mockito.anyString())).thenReturn(this.mockedFile); Mockito.when(this.mockedFileManagementService.zipFiles("pathToFile", this.mockedFileList, "rootPath", false)).thenReturn(null); Mockito.when(this.mockedFileManagementService.urlCombine(Mockito.anyString())).thenReturn("my/path"); Mockito.when(this.mockedClient.resource("my/path")).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("multipart/form-data").post(ClientResponse.class, this.mockedFile)).thenReturn(this.mockedResponse); Mockito.when(this.mockedResponse.getStatus()).thenReturn(200); Task resultTask = this.uploadArtifactExecutor.executeTask(this.correctUploadArtifactTask, this.report, this.workInfo); Assert.assertEquals(TaskStatus.FAILED, resultTask.getStatus()); Assert.assertEquals(this.correctUploadArtifactTask.getTaskDefinition(), resultTask.getTaskDefinition()); Assert.assertEquals(this.correctUploadArtifactTask.getTaskDefinition().getType(), resultTask.getTaskDefinition().getType()); }