use of javax.portlet.CacheControl in project uPortal by Jasig.
the class PortletRendererImplTest method doServeResourceCachedContentReplayHeadersTest.
/**
* Verify headers stored in cache are replayed on the response for cached doServeResource
* content.
*
* @throws PortletContainerException
* @throws IOException
* @throws PortletException
*/
@Test
public void doServeResourceCachedContentReplayHeadersTest() throws PortletException, IOException, PortletContainerException {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
cacheState.setUseCachedData(true);
CacheControl cacheControl = cacheState.getCacheControl();
cacheControl.setUseCachedContent(true);
cacheControl.setExpirationTime(300);
final String output = "{ \"hello\": \"world\" }";
final Map<String, List<Serializable>> headers = ImmutableMap.<String, List<Serializable>>of("header1", Arrays.<Serializable>asList("value1"), "header2", Arrays.<Serializable>asList("value2", "value3"));
final CachedPortletData<Long> cachedPortletData = new CachedPortletData<Long>(1000l, output, null, "application/json", false, cacheControl.getETag(), cacheControl.getExpirationTime());
final CachedPortletResourceData<Long> cachedPortletResourceData = new CachedPortletResourceData<Long>(cachedPortletData, headers, null, null, null, null);
cacheState.setCachedPortletData(cachedPortletResourceData);
setupPortletExecutionMocks(request);
when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
portletRenderer.doServeResource(portletWindowId, request, response, handler);
byte[] fromResponse = response.getContentAsByteArray();
assertArrayEquals(output.getBytes(), fromResponse);
Assert.assertEquals("value1", response.getHeader("header1"));
Assert.assertEquals(Arrays.asList(new String[] { "value2", "value3" }), response.getHeaders("header2"));
verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
use of javax.portlet.CacheControl in project uPortal by Jasig.
the class PortletRendererImplTest method doServeResourceCachedContentValidationMethodIfNoneMatchInvalidTest.
/**
* Same as {@link #doServeResourceCachedContentValidationMethodTest()}, but simulate browser
* sending If-None-Match header with mismatched etag. Response is 200 with content and new etag
*
* @throws PortletException
* @throws IOException
* @throws PortletContainerException
*/
@Test
public void doServeResourceCachedContentValidationMethodIfNoneMatchInvalidTest() throws PortletException, IOException, PortletContainerException {
MockHttpServletRequest request = new MockHttpServletRequest();
request.addHeader("If-None-Match", "123456");
MockHttpServletResponse response = new MockHttpServletResponse();
TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
cacheState.setUseCachedData(true);
CacheControl cacheControl = cacheState.getCacheControl();
cacheControl.setUseCachedContent(true);
cacheControl.setExpirationTime(300);
cacheControl.setETag("123457");
final String output = "{ \"hello\": \"world\" }";
final CachedPortletData<Long> cachedPortletData = new CachedPortletData<Long>(1000l, output, null, "application/json", false, cacheControl.getETag(), cacheControl.getExpirationTime());
final CachedPortletResourceData<Long> cachedPortletResourceData = new CachedPortletResourceData<Long>(cachedPortletData, Collections.EMPTY_MAP, null, null, null, null);
cacheState.setCachedPortletData(cachedPortletResourceData);
setupPortletExecutionMocks(request);
when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
portletRenderer.doServeResource(portletWindowId, request, response, handler);
byte[] fromResponse = response.getContentAsByteArray();
assertArrayEquals(output.getBytes(), fromResponse);
Assert.assertEquals(200, response.getStatus());
Assert.assertEquals("123457", response.getHeader("ETag"));
verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
use of javax.portlet.CacheControl in project uPortal by Jasig.
the class PortletRendererImplTest method doServeResourceCachedContentValidationMethodTest.
/**
* Mimic workflow when cached portlet data using "validation" method is available.
*
* @throws PortletContainerException
* @throws IOException
* @throws PortletException
*/
@Test
public void doServeResourceCachedContentValidationMethodTest() throws PortletException, IOException, PortletContainerException {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
CacheControl cacheControl = cacheState.getCacheControl();
cacheControl.setUseCachedContent(true);
cacheControl.setExpirationTime(300);
cacheControl.setETag("123456");
final String output = "{ \"hello\": \"world\" }";
final CachedPortletData<Long> cachedPortletData = new CachedPortletData<Long>(1000l, output, null, "application/json", false, cacheControl.getETag(), cacheControl.getExpirationTime());
final CachedPortletResourceData<Long> cachedPortletResourceData = new CachedPortletResourceData<Long>(cachedPortletData, Collections.EMPTY_MAP, null, null, null, null);
cacheState.setCachedPortletData(cachedPortletResourceData);
setupPortletExecutionMocks(request);
when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
portletRenderer.doServeResource(portletWindowId, request, response, handler);
byte[] fromResponse = response.getContentAsByteArray();
assertArrayEquals(output.getBytes(), fromResponse);
verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
verify(portletCacheControlService, times(1)).getCacheSizeThreshold();
verify(portletContainer, times(1)).doServeResource(eq(plutoPortletWindow), isA(PortletHttpServletRequestWrapper.class), isA(PortletResourceHttpServletResponseWrapper.class));
verify(portletCacheControlService).cachePortletResourceOutput(eq(portletWindowId), isA(PortletHttpServletRequestWrapper.class), eq(cacheState), eq(cachedPortletResourceData));
verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
use of javax.portlet.CacheControl in project uPortal by Jasig.
the class PortletRendererImplTest method doRenderMarkupCapture.
/**
* No cached data exists, but mock a {@link CacheControl} that will trigger the
* portletContainer#doRender, capture the output, and give to the portlet cachecontrol service.
*
* @throws PortletException
* @throws IOException
* @throws PortletContainerException
*/
@Test
public void doRenderMarkupCapture() throws PortletException, IOException, PortletContainerException {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
CacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult> cacheState = new TestingCacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult>();
CacheControl cacheControl = cacheState.getCacheControl();
cacheControl.setUseCachedContent(false);
cacheControl.setExpirationTime(300);
setupPortletExecutionMocks(request);
when(portletCacheControlService.getPortletRenderState(request, portletWindowId)).thenReturn(cacheState);
when(portletCacheControlService.shouldOutputBeCached(cacheControl)).thenReturn(true);
when(portalRequestInfo.getTargetedPortletWindowId()).thenReturn(portletWindowId);
RenderPortletOutputHandler handler = new RenderPortletOutputHandler("UTF-8");
portletRenderer.doRenderMarkup(portletWindowId, request, response, handler);
verify(portletContainer, times(1)).doRender(eq(plutoPortletWindow), isA(PortletHttpServletRequestWrapper.class), isA(PortletHttpServletResponseWrapper.class));
verify(portletCacheControlService, times(1)).getPortletRenderState(request, portletWindowId);
verify(portletCacheControlService, times(1)).getCacheSizeThreshold();
verify(portletCacheControlService, times(1)).shouldOutputBeCached(cacheControl);
verify(portletCacheControlService, times(1)).cachePortletRenderOutput(eq(portletWindowId), isA(PortletHttpServletRequestWrapper.class), eq(cacheState), isA(CachedPortletData.class));
verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
use of javax.portlet.CacheControl in project uPortal by Jasig.
the class PortletCacheControlServiceImplTest method testGetCacheControlDefault.
@Test
public void testGetCacheControlDefault() {
MockHttpServletRequest httpRequest = new MockHttpServletRequest();
MockPortletWindowId portletWindowId = new MockPortletWindowId("123");
MockPortletDefinitionId portletDefinitionId = new MockPortletDefinitionId(789);
when(portletDescriptor.getCacheScope()).thenReturn(null);
final IPortletWindowRegistry portletWindowRegistry = mock(IPortletWindowRegistry.class);
final IPortletWindow portletWindow = mock(IPortletWindow.class);
final IPortletEntity portletEntity = mock(IPortletEntity.class);
when(portletWindowRegistry.getPortletWindow(httpRequest, portletWindowId)).thenReturn(portletWindow);
when(portletWindow.getPortletEntity()).thenReturn(portletEntity);
when(portletWindow.getWindowState()).thenReturn(WindowState.NORMAL);
when(portletWindow.getPortletMode()).thenReturn(PortletMode.VIEW);
when(portletEntity.getPortletDefinitionId()).thenReturn(portletDefinitionId);
when(portletDefinitionRegistry.getParentPortletDescriptor(portletDefinitionId)).thenReturn(portletDescriptor);
final CacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult> cacheState = cacheControlService.getPortletRenderState(httpRequest, portletWindowId);
final CacheControl cacheControl = cacheState.getCacheControl();
assertFalse(cacheControl.isPublicScope());
assertNull(cacheControl.getETag());
}
Aggregations