MaterialHandlerService implements IMaterialHandlerService { @Override public String checkPipelineForTriggerMaterials(PipelineDefinition pipelineDefinition) { List<String> triggerMaterials = new ArrayList<>(); List<MaterialDefinition> materialDefinitions = (List<MaterialDefinition>) this.materialDefinitionService.getAllFromPipelineDefinition(pipelineDefinition.getId()).getEntity(); for (MaterialDefinition materialDefinition : materialDefinitions) { if (materialDefinition.isPollingForChanges()) { this.materialUpdater = MaterialUpdaterFactory.create(materialDefinition.getType()); String oldError = materialDefinition.getErrorMessage(); setDestinationOfGitMaterial(materialDefinition, pipelineDefinition.getName()); MaterialDefinition latestVersion = materialUpdater.getLatestMaterialVersion(materialDefinition); String newError = materialDefinition.getErrorMessage(); if (!oldError.equals(newError)) { ServiceResult result = this.materialDefinitionService.update(latestVersion); } if (!latestVersion.getErrorMessage().isEmpty()) { continue; } Material dbLatestVersion = (Material) this.materialService.getLatestMaterial(materialDefinition.getId(), pipelineDefinition.getId()).getEntity(); boolean areTheSame = false; if (dbLatestVersion != null) { areTheSame = this.materialUpdater.areMaterialsSameVersion(latestVersion, dbLatestVersion.getMaterialDefinition()); } if (!areTheSame) { triggerMaterials.add(materialDefinition.getName()); } } } return String.join(", ", triggerMaterials); } MaterialHandlerService();  MaterialHandlerService(IMaterialDefinitionService materialDefinitionService, IMaterialService materialService, IMaterialUpdater materialUpdater); @Override String checkPipelineForTriggerMaterials(PipelineDefinition pipelineDefinition); @Override Material updateMaterial(Material material, Pipeline pipeline);  }
@Test public void checkPipelineForTriggerMaterials_oneTriggerMaterial_correctMessage() { PipelineDefinition pipelineDefinition = new PipelineDefinition(); List<MaterialDefinition> materialDefinitions = new ArrayList<>(); GitMaterial gitMaterial = new GitMaterial(); gitMaterial.setName(MATERIAL_ONE); gitMaterial.setPollingForChanges(true); materialDefinitions.add(gitMaterial); ServiceResult serviceResult = new ServiceResult(); serviceResult.setEntity(materialDefinitions); Mockito.when(this.mockedMaterialDefinitionService.getAllFromPipelineDefinition(Mockito.anyString())).thenReturn(serviceResult); String expectedResult = MATERIAL_ONE; String actualResult = this.materialHandlerService.checkPipelineForTriggerMaterials(pipelineDefinition); Assert.assertEquals(expectedResult, actualResult); }
@Test public void checkPipelineForTriggerMaterials_noTriggerMaterial_noMessage() { PipelineDefinition pipelineDefinition = new PipelineDefinition(); List<MaterialDefinition> materialDefinitions = new ArrayList<>(); GitMaterial gitMaterial = new GitMaterial(); gitMaterial.setName(MATERIAL_ONE); gitMaterial.setPollingForChanges(true); materialDefinitions.add(gitMaterial); ServiceResult serviceResult = new ServiceResult(); serviceResult.setEntity(materialDefinitions); Mockito.when(this.mockedMaterialDefinitionService.getAllFromPipelineDefinition(Mockito.anyString())).thenReturn(serviceResult); Mockito.when(this.mockedMaterialUpdater.areMaterialsSameVersion(Mockito.any(MaterialDefinition.class), Mockito.any(MaterialDefinition.class))) .thenReturn(true); String actualResult = this.materialHandlerService.checkPipelineForTriggerMaterials(pipelineDefinition); Assert.assertTrue(actualResult.isEmpty()); }
@Test public void checkPipelineForTriggerMaterials_materialNotPolling_noMessage() { PipelineDefinition pipelineDefinition = new PipelineDefinition(); List<MaterialDefinition> materialDefinitions = new ArrayList<>(); GitMaterial gitMaterial = new GitMaterial(); gitMaterial.setName(MATERIAL_ONE); gitMaterial.setPollingForChanges(false); materialDefinitions.add(gitMaterial); ServiceResult serviceResult = new ServiceResult(); serviceResult.setEntity(materialDefinitions); Mockito.when(this.mockedMaterialDefinitionService.getAllFromPipelineDefinition(Mockito.anyString())).thenReturn(serviceResult); String actualResult = this.materialHandlerService.checkPipelineForTriggerMaterials(pipelineDefinition); Assert.assertTrue(actualResult.isEmpty()); }
@Test public void checkPipelineForTriggerMaterials_couldNotGetLatestMaterial_noMessage() { GitMaterial gitMaterial = Mockito.mock(GitMaterial.class); GitMaterial latestGitMaterial = Mockito.mock(GitMaterial.class); Mockito.when(latestGitMaterial.getErrorMessage()).thenReturn("errorMessage"); PipelineDefinition pipelineDefinition = new PipelineDefinition(); List<MaterialDefinition> materialDefinitions = new ArrayList<>(); ServiceResult updatedMaterialDefinition = new ServiceResult(); updatedMaterialDefinition.setMessage(""); updatedMaterialDefinition.setNotificationType(NotificationType.SUCCESS); updatedMaterialDefinition.setEntity(gitMaterial); ServiceResult serviceResult = new ServiceResult(); serviceResult.setEntity(materialDefinitions); Mockito.when(gitMaterial.getErrorMessage()).thenReturn("first").thenReturn("second"); Mockito.when(this.mockedMaterialDefinitionService.getAllFromPipelineDefinition(Mockito.anyString())).thenReturn(serviceResult); Mockito.when(this.mockedMaterialDefinitionService.update(Mockito.any(MaterialDefinition.class))).thenReturn(updatedMaterialDefinition); Mockito.when(this.mockedMaterialUpdater.getLatestMaterialVersion(Mockito.any(MaterialDefinition.class))).thenReturn(latestGitMaterial); Mockito.when(gitMaterial.isPollingForChanges()).thenReturn(true); String actualResult = this.materialHandlerService.checkPipelineForTriggerMaterials(pipelineDefinition); Assert.assertTrue(actualResult.isEmpty()); }
@Test public void checkPipelineForTriggerMaterials_noPreviousMaterialInDb_triggerMessage() { PipelineDefinition pipelineDefinition = new PipelineDefinition(); List<MaterialDefinition> materialDefinitions = new ArrayList<>(); GitMaterial gitMaterial = new GitMaterial(); gitMaterial.setName(MATERIAL_ONE); gitMaterial.setPollingForChanges(true); materialDefinitions.add(gitMaterial); ServiceResult serviceResult = new ServiceResult(); serviceResult.setEntity(materialDefinitions); Mockito.when(this.mockedMaterialDefinitionService.getAllFromPipelineDefinition(Mockito.anyString())).thenReturn(serviceResult); ServiceResult latestMaterialServiceResult = new ServiceResult(); latestMaterialServiceResult.setEntity(null); Mockito.when(this.mockedMaterialService.getLatestMaterial(Mockito.anyString(), Mockito.anyString())).thenReturn(latestMaterialServiceResult); String expectedResult = MATERIAL_ONE; String actualResult = this.materialHandlerService.checkPipelineForTriggerMaterials(pipelineDefinition); Assert.assertEquals(expectedResult, actualResult); }