ApplicationVersionServiceImpl implements ApplicationVersionService { @Override @Transactional(REQUIRES_NEW) public Stack saveStack(final String applicationId, final String applicationVersion) { if (applicationId == null && applicationVersion == null) { return new Stack( null, null ); } ApplicationEntity application = null; VersionEntity version = null; if (applicationVersion != null) { version = versionRepository.findByName(applicationVersion); if (version == null) { version = versionRepository.save(new VersionEntity(applicationVersion)); } } if (applicationId != null) { application = applicationRepository.findByName(applicationId); if (application == null) { application = new ApplicationEntity(applicationId); } if (version != null) { if (!application.getVersionEntities().contains(version)) { application.getVersionEntities().add(version); } } application = applicationRepository.save(application); } return new Stack(application, version); }  @Override @Transactional(REQUIRES_NEW) Stack saveStack(final String applicationId, final String applicationVersion);  }
@Test public void testSaveStack() throws Exception { when(applicationRepositoryMock.findByName(anyString())).thenReturn(null); when(applicationRepositoryMock.save(any(ApplicationEntity.class))).thenReturn(applicationEntity); when(versionRepositoryMock.save(any(VersionEntity.class))).thenReturn(versionEntity); final Stack stack = applicationVersionService.saveStack(MY_APP_1, SNAPSHOT); assertThat(stack.getApplicationEntity().getName()).isEqualTo(MY_APP_1); assertThat(stack.getVersionEntity().getName()).isEqualTo(SNAPSHOT); verify(versionRepositoryMock).findByName(anyString()); verify(versionRepositoryMock).save(any(VersionEntity.class)); verify(applicationRepositoryMock).findByName(anyString()); verify(applicationRepositoryMock).save(any(ApplicationEntity.class)); }
@Test public void testVersionAdding() throws Exception { final VersionEntity version1 = new VersionEntity("1.0"); final VersionEntity version2 = new VersionEntity("2.0"); final ApplicationEntity application = new ApplicationEntity("WowApp"); application.setVersionEntities(newArrayList(version1,version2)); when(applicationRepositoryMock.findByName(anyString())).thenReturn(application); when(applicationRepositoryMock.save(any(ApplicationEntity.class))).thenReturn(application); when(versionRepositoryMock.save(any(VersionEntity.class))).thenReturn(versionEntity); final Stack stack = applicationVersionService.saveStack(application.getName(), SNAPSHOT); assertThat(stack.getApplicationEntity().getVersionEntities().size()).isEqualTo(3); verify(versionRepositoryMock).findByName(anyString()); verify(versionRepositoryMock).save(any(VersionEntity.class)); verify(applicationRepositoryMock).findByName(anyString()); verify(applicationRepositoryMock).save(any(ApplicationEntity.class)); }
@Test public void testNullVersion() throws Exception { when(applicationRepositoryMock.findByName(anyString())).thenReturn(null); when(applicationRepositoryMock.save(any(ApplicationEntity.class))).thenReturn(applicationEntity); final Stack stack = applicationVersionService.saveStack(MY_APP_1, null); assertThat(stack.getApplicationEntity().getName()).isEqualTo(MY_APP_1); assertThat(stack.getVersionEntity()).isEqualTo(null); verify(applicationRepositoryMock).findByName(anyString()); verify(applicationRepositoryMock).save(any(ApplicationEntity.class)); }