ApplicationLifecycleServiceImpl implements ApplicationLifecycleService { @Override @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 100, maxDelay = 500), include = {ObjectOptimisticLockingFailureException.class, OptimisticLockException.class, DataIntegrityViolationException.class}) @Transactional(REQUIRES_NEW) public LifecycleEntity saveLifecycle(final ApplicationEntity applicationEntity, final VersionEntity versionEntity, final LifecycleEntity lifecycleToSave) { Assert.notNull(applicationEntity, "applicationEntity must not be null"); Assert.notNull(versionEntity, "versionEntity must not be null"); Assert.notNull(lifecycleToSave, "lifecycleToSave must not be null"); ApplicationEntity applicationByName = applicationRepository.findByName(applicationEntity.getName()); VersionEntity versionByName = versionRepository.findByName(versionEntity.getName()); if (applicationByName == null) { applicationByName = applicationRepository.save(applicationEntity); } if (versionByName == null) { versionByName = versionRepository.save(versionEntity); } if (!applicationByName.getVersionEntities().contains(versionByName)) { applicationByName.getVersionEntities().add(versionByName); applicationByName = applicationRepository.save(applicationByName); } lifecycleToSave.setApplicationEntity(applicationByName); lifecycleToSave.setVersionEntity(versionByName); return lifecycleRepository.save(lifecycleToSave); }  @Override @Retryable(maxAttempts = 10, backoff = @Backoff(delay = 100, maxDelay = 500), include = {ObjectOptimisticLockingFailureException.class, OptimisticLockException.class, DataIntegrityViolationException.class}) @Transactional(REQUIRES_NEW) LifecycleEntity saveLifecycle(final ApplicationEntity applicationEntity, final VersionEntity versionEntity,
            final LifecycleEntity lifecycleToSave); @Override LifecycleEntity saveInstanceLogLifecycle(final String instanceId, final DateTime instanceBootTime,
            final String userdataPath, final String region, final String logData, final String accountId); @Override ApplicationEntity findAppByInstanceIds(final String accountId, final String region, final Collection<String> instanceIds); @Override Page<LifecycleEntity> findByApplicationNameAndVersion(final String name, final String version, final Pageable pageable);  }
@Test public void testSaveLifecycleWithNewAppAndVersion() throws Exception { final LifecycleEntity lifecycle = new LifecycleEntity(); lifecycle.setInstanceId(INSTANCE_ID); lifecycle.setAccountId(ACCOUNT_ID); lifecycle.setRegion(REGION); final LifecycleEntity savedLifecycle = applicationLifecycleServiceImpl.saveLifecycle( application, version, lifecycle); verify(mockApplicationRepository).findByName(eq(applicationId)); verify(mockVersionRepository).findByName(eq(versionId)); verify(mockApplicationRepository, times(2)).save(eq(application)); verify(mockVersionRepository).save(eq(version)); verify(mockLifecycleRepository).save(same(lifecycle)); Assertions.assertThat(savedLifecycle).hasApplicationEntity(application); Assertions.assertThat(savedLifecycle).hasVersionEntity(version); Assertions.assertThat(savedLifecycle.getApplicationEntity()).hasOnlyVersionEntities(version); }
@Test public void testSaveExistingLifecycle() throws Exception { final ApplicationEntity existingApp = new ApplicationEntity(applicationId); final VersionEntity existingVersion = new VersionEntity(versionId); existingApp.getVersionEntities().add(existingVersion); when(mockApplicationRepository.findByName(eq(applicationId))).thenReturn(existingApp); when(mockVersionRepository.findByName(eq(versionId))).thenReturn(existingVersion); final LifecycleEntity lifecycle = new LifecycleEntity(); lifecycle.setInstanceId(INSTANCE_ID); lifecycle.setAccountId(ACCOUNT_ID); lifecycle.setRegion(REGION); final LifecycleEntity savedLifecycle = applicationLifecycleServiceImpl.saveLifecycle( application, version, lifecycle); verify(mockApplicationRepository).findByName(eq(applicationId)); verify(mockVersionRepository).findByName(eq(versionId)); verify(mockLifecycleRepository).save(lifecycle); Assertions.assertThat(savedLifecycle).isEqualTo(lifecycle); }