DefaultSysProvisioningBatchService extends AbstractReadWriteDtoService<SysProvisioningBatchDto, SysProvisioningBatch, EmptyFilter> implements SysProvisioningBatchService { @Override @Transactional public SysProvisioningBatchDto findBatch(UUID systemEntity) { List<SysProvisioningBatch> batches = repository.findAllBySystemEntity_IdOrderByCreatedAsc(systemEntity); if (batches.isEmpty()) { return null; } if (batches.size() == 1) { return toDto(batches.get(0)); } SysProvisioningBatch firstBatch = batches.get(0); for (int index = 1; index < batches.size(); index ++) { SysProvisioningBatch oldBatch = batches.get(index); repository.mergeBatch(oldBatch, firstBatch); deleteById(oldBatch.getId()); } return toDto(firstBatch); } @Autowired  DefaultSysProvisioningBatchService(SysProvisioningBatchRepository repository); @Override ZonedDateTime calculateNextAttempt(SysProvisioningOperationDto operation); @Override @Transactional(readOnly = true) Page<SysProvisioningBatchDto> findBatchesToProcess(Boolean virtualSystem, Pageable pageable); @Override @Transactional(readOnly = true) Page<SysProvisioningBatchDto> findBatchesToRetry(ZonedDateTime date, Pageable pageable); @Override @Transactional SysProvisioningBatchDto findBatch(UUID systemEntity);  }
@Test public void testMergeBatches() { SysSystemDto system = getHelper().createSystem(TestResource.TABLE_NAME); SysSystemEntityDto systemEntity = getHelper().createSystemEntity(system); SysProvisioningBatchDto batchOne = new SysProvisioningBatchDto(); batchOne.setId(UUID.randomUUID()); batchOne.setSystemEntity(systemEntity.getId()); batchOne = batchService.save(batchOne); SysProvisioningOperationDto operationOne = createOperation(systemEntity, batchOne); SysProvisioningBatchDto batchTwo = new SysProvisioningBatchDto(); batchTwo.setId(UUID.randomUUID()); batchTwo.setSystemEntity(systemEntity.getId()); batchTwo = batchService.save(batchTwo); SysProvisioningOperationDto operationTwo = createOperation(systemEntity, batchTwo); Assert.assertNotEquals(batchOne.getId(), batchTwo.getId()); SysProvisioningBatchDto mergedBatch = batchService.findBatch(systemEntity.getId()); Assert.assertEquals(batchOne.getId(), mergedBatch.getId()); Assert.assertNull(batchService.get(batchTwo)); operationOne = operationService.get(operationOne.getId()); operationTwo = operationService.get(operationTwo.getId()); Assert.assertEquals(batchOne.getId(), operationOne.getBatch()); Assert.assertEquals(batchOne.getId(), operationTwo.getBatch()); }