AMImageProcessorImpl implements AMImageProcessor { @Override public void process(FileVersion fileVersion) throws PortalException { if (!_amImageValidator.isProcessingSupported(fileVersion)) { return; } Iterable<AMImageConfigurationEntry> amImageConfigurationEntries = _amImageConfigurationHelper.getAMImageConfigurationEntries( fileVersion.getCompanyId()); for (AMImageConfigurationEntry amImageConfigurationEntry : amImageConfigurationEntries) { process(fileVersion, amImageConfigurationEntry.getUUID()); } }  @Override void cleanUp(FileVersion fileVersion); @Override void process(FileVersion fileVersion); @Override void process(FileVersion fileVersion, String configurationEntryUuid);  }
@Test public void testProcessConfigurationWhenAMImageEntryAlreadyExists() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString()) ).thenReturn( Optional.of( new AMImageConfigurationEntryImpl( RandomTestUtil.randomString(), RandomTestUtil.randomString(), Collections.emptyMap())) ); Mockito.when( _amImageEntryLocalService.fetchAMImageEntry( Mockito.anyString(), Mockito.anyLong()) ).thenReturn( Mockito.mock(AMImageEntry.class) ); Mockito.when( _fileVersion.getFileEntry() ).thenReturn( _fileEntry ); Mockito.when( _fileEntry.isCheckedOut() ).thenReturn( false ); _amImageProcessorImpl.process( _fileVersion, RandomTestUtil.randomString()); Mockito.verify( _amImageScaler, Mockito.never() ).scaleImage( Mockito.any(FileVersion.class), Mockito.any(AMImageConfigurationEntry.class) ); }
@Test public void testProcessConfigurationWhenFileEntryIsCheckedOut() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString()) ).thenReturn( Optional.of( new AMImageConfigurationEntryImpl( RandomTestUtil.randomString(), RandomTestUtil.randomString(), Collections.emptyMap())) ); Mockito.when( _amImageEntryLocalService.fetchAMImageEntry( Mockito.anyString(), Mockito.anyLong()) ).thenReturn( Mockito.mock(AMImageEntry.class) ); Mockito.when( _fileVersion.getFileEntry() ).thenReturn( _fileEntry ); Mockito.when( _fileEntry.isCheckedOut() ).thenReturn( true ); Mockito.when( _amImageScalerTracker.getAMImageScaler(Mockito.anyString()) ).thenReturn( _amImageScaler ); Mockito.when( _amImageScaler.scaleImage( Mockito.any(FileVersion.class), Mockito.any(AMImageConfigurationEntry.class)) ).thenReturn( new AMImageScaledImageImpl(new byte[100], 100, 100) ); _amImageProcessorImpl.process( _fileVersion, RandomTestUtil.randomString()); Mockito.verify( _amImageEntryLocalService ).deleteAMImageEntry( Mockito.anyLong() ); Mockito.verify( _amImageScaler ).scaleImage( Mockito.any(FileVersion.class), Mockito.any(AMImageConfigurationEntry.class) ); }
@Test public void testProcessConfigurationWhenNoAMImageScalerAvailable() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); AMImageConfigurationEntry amImageConfigurationEntry = new AMImageConfigurationEntryImpl( RandomTestUtil.randomString(), RandomTestUtil.randomString(), Collections.emptyMap()); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntries( Mockito.anyLong()) ).thenReturn( Collections.singleton(amImageConfigurationEntry) ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString()) ).thenReturn( Optional.of(amImageConfigurationEntry) ); Mockito.when( _amImageScalerTracker.getAMImageScaler(Mockito.anyString()) ).thenReturn( null ); _amImageProcessorImpl.process( _fileVersion, RandomTestUtil.randomString()); Mockito.verify( _amImageEntryLocalService, Mockito.never() ).addAMImageEntry( Mockito.any(AMImageConfigurationEntry.class), Mockito.any(FileVersion.class), Mockito.anyInt(), Mockito.anyInt(), Mockito.any(InputStream.class), Mockito.anyLong() ); }
@Test public void testProcessConfigurationWhenNoConfigurationEntry() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString()) ).thenReturn( Optional.empty() ); _amImageProcessorImpl.process( _fileVersion, RandomTestUtil.randomString()); Mockito.verify( _amImageEntryLocalService, Mockito.never() ).fetchAMImageEntry( Mockito.anyString(), Mockito.anyLong() ); }
@Test public void testProcessConfigurationWhenNotSupported() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported( Mockito.any(FileVersion.class)) ).thenReturn( false ); _amImageProcessorImpl.process( _fileVersion, RandomTestUtil.randomString()); Mockito.verify( _amImageConfigurationHelper, Mockito.never() ).getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString() ); }
@Test(expected = DuplicateAMImageEntryException.class) public void testProcessDuplicateAMImageEntryExceptionInImageService() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); AMImageConfigurationEntry amImageConfigurationEntry = new AMImageConfigurationEntryImpl( RandomTestUtil.randomString(), RandomTestUtil.randomString(), Collections.emptyMap()); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntries( Mockito.anyLong()) ).thenReturn( Collections.singleton(amImageConfigurationEntry) ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString()) ).thenReturn( Optional.of(amImageConfigurationEntry) ); Mockito.when( _amImageScalerTracker.getAMImageScaler(Mockito.anyString()) ).thenReturn( _amImageScaler ); Mockito.when( _amImageScaler.scaleImage(_fileVersion, amImageConfigurationEntry) ).thenReturn( new AMImageScaledImageImpl(new byte[100], 150, 200) ); Mockito.doThrow( DuplicateAMImageEntryException.class ).when( _amImageEntryLocalService ).addAMImageEntry( Mockito.any(AMImageConfigurationEntry.class), Mockito.any(FileVersion.class), Mockito.eq(150), Mockito.eq(200), Mockito.any(InputStream.class), Mockito.eq(100L) ); _amImageProcessorImpl.process(_fileVersion); }
@Test public void testProcessFileVersion() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); AMImageConfigurationEntry amImageConfigurationEntry = new AMImageConfigurationEntryImpl( RandomTestUtil.randomString(), RandomTestUtil.randomString(), Collections.emptyMap()); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntries( Mockito.anyLong()) ).thenReturn( Collections.singleton(amImageConfigurationEntry) ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString()) ).thenReturn( Optional.of(amImageConfigurationEntry) ); Mockito.when( _amImageScalerTracker.getAMImageScaler(Mockito.anyString()) ).thenReturn( _amImageScaler ); Mockito.when( _amImageScaler.scaleImage(_fileVersion, amImageConfigurationEntry) ).thenReturn( new AMImageScaledImageImpl(new byte[100], 150, 200) ); _amImageProcessorImpl.process(_fileVersion); Mockito.verify( _amImageScaler ).scaleImage( _fileVersion, amImageConfigurationEntry ); Mockito.verify( _amImageEntryLocalService ).addAMImageEntry( Mockito.any(AMImageConfigurationEntry.class), Mockito.any(FileVersion.class), Mockito.eq(150), Mockito.eq(200), Mockito.any(InputStream.class), Mockito.eq(100L) ); }
@Test(expected = AMRuntimeException.InvalidConfiguration.class) public void testProcessInvalidConfigurationException() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); Mockito.doThrow( AMRuntimeException.InvalidConfiguration.class ).when( _amImageConfigurationHelper ).getAMImageConfigurationEntries( Mockito.anyLong() ); _amImageProcessorImpl.process(_fileVersion); }
@Test(expected = AMRuntimeException.IOException.class) public void testProcessIOExceptionInImageProcessor() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); AMImageConfigurationEntry amImageConfigurationEntry = new AMImageConfigurationEntryImpl( RandomTestUtil.randomString(), RandomTestUtil.randomString(), Collections.emptyMap()); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntries( Mockito.anyLong()) ).thenReturn( Collections.singleton(amImageConfigurationEntry) ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString()) ).thenReturn( Optional.of(amImageConfigurationEntry) ); Mockito.when( _amImageScalerTracker.getAMImageScaler(Mockito.anyString()) ).thenReturn( _amImageScaler ); Mockito.doThrow( AMRuntimeException.IOException.class ).when( _amImageScaler ).scaleImage( _fileVersion, amImageConfigurationEntry ); _amImageProcessorImpl.process(_fileVersion); }
@Test(expected = AMRuntimeException.IOException.class) public void testProcessIOExceptionInStorage() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported(_fileVersion) ).thenReturn( true ); AMImageConfigurationEntry amImageConfigurationEntry = new AMImageConfigurationEntryImpl( RandomTestUtil.randomString(), RandomTestUtil.randomString(), Collections.emptyMap()); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntries( Mockito.anyLong()) ).thenReturn( Collections.singleton(amImageConfigurationEntry) ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntry( Mockito.anyLong(), Mockito.anyString()) ).thenReturn( Optional.of(amImageConfigurationEntry) ); Mockito.when( _amImageScalerTracker.getAMImageScaler(Mockito.anyString()) ).thenReturn( _amImageScaler ); Mockito.when( _amImageScaler.scaleImage(_fileVersion, amImageConfigurationEntry) ).thenReturn( new AMImageScaledImageImpl(new byte[100], 150, 200) ); Mockito.doThrow( AMRuntimeException.IOException.class ).when( _amImageEntryLocalService ).addAMImageEntry( Mockito.any(AMImageConfigurationEntry.class), Mockito.any(FileVersion.class), Mockito.eq(150), Mockito.eq(200), Mockito.any(InputStream.class), Mockito.eq(100L) ); _amImageProcessorImpl.process(_fileVersion); }
@Test public void testProcessWhenNoConfigurationEntries() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported( Mockito.any(FileVersion.class)) ).thenReturn( true ); Mockito.when( _amImageConfigurationHelper.getAMImageConfigurationEntries( Mockito.anyLong()) ).thenReturn( Collections.emptyList() ); _amImageProcessorImpl.process(_fileVersion); Mockito.verify( _amImageScaler, Mockito.never() ).scaleImage( Mockito.any(FileVersion.class), Mockito.any(AMImageConfigurationEntry.class) ); Mockito.verify( _amImageEntryLocalService, Mockito.never() ).addAMImageEntry( Mockito.any(AMImageConfigurationEntry.class), Mockito.any(FileVersion.class), Mockito.anyInt(), Mockito.anyInt(), Mockito.any(InputStream.class), Mockito.anyLong() ); }
@Test public void testProcessWhenNotSupported() throws Exception { Mockito.when( _amImageValidator.isProcessingSupported( Mockito.any(FileVersion.class)) ).thenReturn( false ); _amImageProcessorImpl.process(_fileVersion); Mockito.verify( _amImageConfigurationHelper, Mockito.never() ).getAMImageConfigurationEntries( Mockito.anyLong() ); }