AMImageRequestHandler implements AMRequestHandler<AMImageProcessor> { @Override public Optional<AdaptiveMedia<AMImageProcessor>> handleRequest( HttpServletRequest httpServletRequest) throws IOException, ServletException { Optional<Tuple<FileVersion, AMImageAttributeMapping>> interpretedPathOptional = _interpretPath( httpServletRequest.getPathInfo()); return interpretedPathOptional.flatMap( tuple -> { Optional<AdaptiveMedia<AMImageProcessor>> adaptiveMediaOptional = _findAdaptiveMedia( tuple.first, tuple.second); adaptiveMediaOptional.ifPresent( adaptiveMedia -> _processAMImage( adaptiveMedia, tuple.first, tuple.second)); return adaptiveMediaOptional; }); }  @Override Optional<AdaptiveMedia<AMImageProcessor>> handleRequest(
			HttpServletRequest httpServletRequest);  }
@Test(expected = AMRuntimeException.class) public void testFinderFailsWithMediaProcessorException() throws Exception { AMImageConfigurationEntry amImageConfigurationEntry = _createAMImageConfigurationEntry( _fileVersion.getCompanyId(), 200, 500); HttpServletRequest httpServletRequest = _createRequestFor( _fileVersion, amImageConfigurationEntry); Mockito.when( _amImageFinder.getAdaptiveMediaStream(Mockito.any(Function.class)) ).thenThrow( AMException.class ); _amImageRequestHandler.handleRequest(httpServletRequest); }
@Test(expected = AMRuntimeException.class) public void testFinderFailsWithPortalException() throws Exception { AMImageConfigurationEntry getConfigurationEntryFilter = _createAMImageConfigurationEntry( _fileVersion.getCompanyId(), 200, 500); HttpServletRequest httpServletRequest = _createRequestFor( _fileVersion, getConfigurationEntryFilter); Mockito.when( _amImageFinder.getAdaptiveMediaStream(Mockito.any(Function.class)) ).thenThrow( PortalException.class ); _amImageRequestHandler.handleRequest(httpServletRequest); }
@Test public void testInvalidPath() throws Exception { Mockito.when( _pathInterpreter.interpretPath(Mockito.anyString()) ).thenReturn( Optional.empty() ); HttpServletRequest httpServletRequest = Mockito.mock( HttpServletRequest.class); Optional<AdaptiveMedia<AMImageProcessor>> adaptiveMediaOptional = _amImageRequestHandler.handleRequest(httpServletRequest); Assert.assertFalse(adaptiveMediaOptional.isPresent()); }
@Test(expected = NullPointerException.class) public void testNullRequest() throws Exception { _amImageRequestHandler.handleRequest(null); }
@Test public void testPathInterpreterFailure() throws Exception { Mockito.when( _pathInterpreter.interpretPath(Mockito.anyString()) ).thenThrow( AMRuntimeException.class ); HttpServletRequest httpServletRequest = Mockito.mock( HttpServletRequest.class); Optional<AdaptiveMedia<AMImageProcessor>> adaptiveMediaOptional = _amImageRequestHandler.handleRequest(httpServletRequest); Assert.assertFalse(adaptiveMediaOptional.isPresent()); }
@Test public void testReturnsTheClosestMatchByWidthIfNoExactMatchPresentAndRunsTheProcess() throws Exception { AMImageConfigurationEntry getConfigurationEntryFilter = _createAMImageConfigurationEntry( _fileVersion.getCompanyId(), 200, 500); AMImageConfigurationEntry closestAMImageConfigurationEntry = _createAMImageConfigurationEntry( _fileVersion.getCompanyId(), 201, 501); AMImageConfigurationEntry fartherAMImageConfigurationEntry = _createAMImageConfigurationEntry( _fileVersion.getCompanyId(), 301, 501); AMImageConfigurationEntry farthestAMImageConfigurationEntry = _createAMImageConfigurationEntry( _fileVersion.getCompanyId(), 401, 501); AdaptiveMedia<AMImageProcessor> closestAdaptiveMedia = _createAdaptiveMedia( _fileVersion, closestAMImageConfigurationEntry); AdaptiveMedia<AMImageProcessor> fartherAdaptiveMedia = _createAdaptiveMedia( _fileVersion, fartherAMImageConfigurationEntry); AdaptiveMedia<AMImageProcessor> farthestAdaptiveMedia = _createAdaptiveMedia( _fileVersion, farthestAMImageConfigurationEntry); _mockClosestMatch( _fileVersion, getConfigurationEntryFilter, Arrays.asList( farthestAdaptiveMedia, closestAdaptiveMedia, fartherAdaptiveMedia)); HttpServletRequest httpServletRequest = _createRequestFor( _fileVersion, getConfigurationEntryFilter); Assert.assertEquals( Optional.of(closestAdaptiveMedia), _amImageRequestHandler.handleRequest(httpServletRequest)); Mockito.verify( _amAsyncProcessor ).triggerProcess( _fileVersion, String.valueOf(_fileVersion.getFileVersionId()) ); }
@Test public void testReturnsTheExactMatchIfPresentAndDoesNotRunTheProcess() throws Exception { AMImageConfigurationEntry amImageConfigurationEntry = _createAMImageConfigurationEntry( _fileVersion.getCompanyId(), 200, 500); AdaptiveMedia<AMImageProcessor> adaptiveMedia = _createAdaptiveMedia( _fileVersion, amImageConfigurationEntry); _mockExactMatch(_fileVersion, amImageConfigurationEntry, adaptiveMedia); HttpServletRequest httpServletRequest = _createRequestFor( _fileVersion, amImageConfigurationEntry); Assert.assertEquals( Optional.of(adaptiveMedia), _amImageRequestHandler.handleRequest(httpServletRequest)); Mockito.verify( _amAsyncProcessor, Mockito.never() ).triggerProcess( _fileVersion, String.valueOf(_fileVersion.getFileVersionId()) ); }
@Test public void testReturnsTheRealImageIfThereAreNoAdaptiveMediasAndRunsTheProcess() throws Exception { AMImageConfigurationEntry amImageConfigurationEntry = _createAMImageConfigurationEntry( _fileVersion.getCompanyId(), 200, 500); HttpServletRequest httpServletRequest = _createRequestFor( _fileVersion, amImageConfigurationEntry); Mockito.when( _amImageFinder.getAdaptiveMediaStream(Mockito.any(Function.class)) ).thenAnswer( invocation -> Stream.empty() ); Optional<AdaptiveMedia<AMImageProcessor>> adaptiveMediaOptional = _amImageRequestHandler.handleRequest(httpServletRequest); Assert.assertTrue(adaptiveMediaOptional.isPresent()); AdaptiveMedia<AMImageProcessor> adaptiveMedia = adaptiveMediaOptional.get(); Assert.assertEquals( _fileVersion.getContentStream(false), adaptiveMedia.getInputStream()); Assert.assertEquals( Optional.of(_fileVersion.getFileName()), adaptiveMedia.getValueOptional( AMAttribute.getFileNameAMAttribute())); Assert.assertEquals( Optional.of(_fileVersion.getMimeType()), adaptiveMedia.getValueOptional( AMAttribute.getContentTypeAMAttribute())); Optional<Long> contentLength = adaptiveMedia.getValueOptional( AMAttribute.getContentLengthAMAttribute()); Assert.assertEquals(_fileVersion.getSize(), (long)contentLength.get()); Mockito.verify( _amAsyncProcessor ).triggerProcess( _fileVersion, String.valueOf(_fileVersion.getFileVersionId()) ); }