### Question:
ArtifactoryPromotionSyncServiceImpl implements ArtifactoryPromotionSyncService, JobOrchestratorSupplier, ConfigurationServiceListener<ArtifactoryConfiguration> { protected void syncBuild(Branch branch, String artifactoryBuildName, String buildName, ArtifactoryClient client, JobRunListener listener) { Optional<Build> buildOpt = structureService.findBuildByName( branch.getProject().getName(), branch.getName(), buildName ); if (buildOpt.isPresent()) { String log = String.format("Sync branch %s/%s for Artifactory build %s", branch.getProject().getName(), branch.getName(), buildName); logger.debug("[artifactory-sync] {}", log); listener.message(log); JsonNode buildInfo = client.getBuildInfo(artifactoryBuildName, buildName); List<ArtifactoryStatus> statuses = client.getStatuses(buildInfo); for (ArtifactoryStatus artifactoryStatus : statuses) { String statusName = artifactoryStatus.getName(); Optional<PromotionLevel> promotionLevelOpt = structureService.findPromotionLevelByName( branch.getProject().getName(), branch.getName(), statusName ); if (promotionLevelOpt.isPresent()) { Optional<PromotionRun> runOpt = structureService.getLastPromotionRunForBuildAndPromotionLevel(buildOpt.get(), promotionLevelOpt.get()); if (!runOpt.isPresent()) { logger.info("[artifactory-sync] Promote {}/{}/{} to {}", branch.getProject().getName(), branch.getName(), buildName, statusName); structureService.newPromotionRun( PromotionRun.of( buildOpt.get(), promotionLevelOpt.get(), Signature.of(artifactoryStatus.getUser()).withTime(artifactoryStatus.getTimestamp()), "Promoted from Artifactory" ) ); } } } } } @Autowired  ArtifactoryPromotionSyncServiceImpl(StructureService structureService, PropertyService propertyService, ArtifactoryClientFactory artifactoryClientFactory, ArtifactoryConfigurationService configurationService, ArtifactoryConfProperties artifactoryConfProperties, SecurityService securityService); @Override Stream<JobRegistration> collectJobRegistrations(); JobRegistration scheduleArtifactoryBuildSync(Branch branch);  }### Answer:
@Test public void syncBuild_new_promotion() { when(structureService.getLastPromotionRunForBuildAndPromotionLevel(build, promotionLevel)).thenReturn( Optional.of( PromotionRun.of( build, promotionLevel, Signature.of("test"), "Promotion" ) ) ); service.syncBuild(branch, "1.0.0", "1.0.0", artifactoryClient, System.out::println); verify(structureService, times(0)).newPromotionRun(any()); }
@Test public void syncBuild_existing_promotion() { when(structureService.getLastPromotionRunForBuildAndPromotionLevel(build, promotionLevel)).thenReturn( Optional.empty() ); service.syncBuild(branch, "1.0.0", "1.0.0", artifactoryClient, System.out::println); verify(structureService, times(1)).newPromotionRun(any()); }