MaterialHandlerService implements IMaterialHandlerService { @Override public Material updateMaterial(Material material, Pipeline pipeline) { this.materialUpdater = MaterialUpdaterFactory.create(material.getMaterialDefinition().getType()); String oldError = material.getMaterialDefinition().getErrorMessage(); setDestinationOfGitMaterial(material.getMaterialDefinition(), pipeline.getPipelineDefinitionName()); MaterialDefinition latestVersion = this.materialUpdater.getLatestMaterialVersion(material.getMaterialDefinition()); String newError = material.getMaterialDefinition().getErrorMessage(); if (!oldError.equals(newError)) { ServiceResult result = this.materialDefinitionService.update(latestVersion); } if (!material.getMaterialDefinition().getErrorMessage().isEmpty()) { return null; } Material dbLatestVersion = (Material) this.materialService.getLatestMaterial(material.getMaterialDefinition().getId(), pipeline.getPipelineDefinitionId()).getEntity(); boolean areTheSame = false; if (dbLatestVersion != null) { areTheSame = this.materialUpdater.areMaterialsSameVersion(latestVersion, dbLatestVersion.getMaterialDefinition()); } if (!areTheSame) { material.setChangeDate(LocalDateTime.now()); material.setMaterialDefinition(latestVersion); material.setUpdated(true); } return material; } MaterialHandlerService();  MaterialHandlerService(IMaterialDefinitionService materialDefinitionService, IMaterialService materialService, IMaterialUpdater materialUpdater); @Override String checkPipelineForTriggerMaterials(PipelineDefinition pipelineDefinition); @Override Material updateMaterial(Material material, Pipeline pipeline);  }
@Test public void updateMaterial_newVersion_materialUpdated() { Material material = new Material(); GitMaterial gitMaterial = new GitMaterial(); material.setMaterialDefinition(gitMaterial); Pipeline pipeline = new Pipeline(); pipeline.setPipelineDefinitionId(UUID.randomUUID().toString()); pipeline.setPipelineDefinitionName("testName"); Material actualResult = this.materialHandlerService.updateMaterial(material, pipeline); Assert.assertNotNull(actualResult.getChangeDate()); Assert.assertTrue(actualResult.isUpdated()); }
@Test public void updateMaterial_noNewVersion_materialNotUpdated() { Material material = new Material(); GitMaterial gitMaterial = new GitMaterial(); material.setMaterialDefinition(gitMaterial); Pipeline pipeline = new Pipeline(); pipeline.setPipelineDefinitionId(UUID.randomUUID().toString()); pipeline.setPipelineDefinitionName("testName"); Mockito.when(this.mockedMaterialUpdater.areMaterialsSameVersion(Mockito.any(MaterialDefinition.class), Mockito.any(MaterialDefinition.class))) .thenReturn(true); Material actualResult = this.materialHandlerService.updateMaterial(material, pipeline); Assert.assertFalse(actualResult.isUpdated()); }
@Test public void updateMaterial_couldNotGetLatestMaterial_null() { Material material = new Material(); GitMaterial gitMaterial = Mockito.mock(GitMaterial.class); Pipeline pipeline = new Pipeline(); pipeline.setPipelineDefinitionId(UUID.randomUUID().toString()); pipeline.setPipelineDefinitionName("testName"); material.setMaterialDefinition(gitMaterial); ServiceResult updatedServiceResult = new ServiceResult(); updatedServiceResult.setNotificationType(NotificationType.ERROR); updatedServiceResult.setMessage(""); updatedServiceResult.setEntity(gitMaterial); Mockito.when(gitMaterial.getErrorMessage()).thenReturn("not empty").thenReturn("new message"); Mockito.when(this.mockedMaterialUpdater.getLatestMaterialVersion(Mockito.any(MaterialDefinition.class))) .thenReturn(null); Mockito.when(this.mockedMaterialDefinitionService.update(Mockito.any(MaterialDefinition.class))).thenReturn(updatedServiceResult); Material actualResult = this.materialHandlerService.updateMaterial(material, pipeline); Assert.assertNull(actualResult); }
@Test public void updateMaterial_noPreviousMaterialInDb_materialUpdated() { Material material = new Material(); GitMaterial gitMaterial = new GitMaterial(); material.setMaterialDefinition(gitMaterial); Pipeline pipeline = new Pipeline(); pipeline.setPipelineDefinitionId(UUID.randomUUID().toString()); pipeline.setPipelineDefinitionName("testName"); ServiceResult latestMaterialServiceResult = new ServiceResult(); latestMaterialServiceResult.setEntity(null); Mockito.when(this.mockedMaterialService.getLatestMaterial(Mockito.anyString(), Mockito.anyString())).thenReturn(latestMaterialServiceResult); Material actualResult = this.materialHandlerService.updateMaterial(material, pipeline); Assert.assertNotNull(actualResult.getChangeDate()); Assert.assertTrue(actualResult.isUpdated()); }