Search in sources :

Example 21 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletRendererImplTest method doRenderMarkupCachedContentExpirationMethodTest.

/**
 * Mimic workflow when cached portlet data using "expiration" method is available.
 *
 * @throws PortletContainerException
 * @throws IOException
 * @throws PortletException
 */
@Test
public void doRenderMarkupCachedContentExpirationMethodTest() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    TestingCacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult> cacheState = new TestingCacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult>();
    cacheState.setUseCachedData(true);
    CacheControl cacheControl = cacheState.getCacheControl();
    final PortletRenderResult portletResult = new PortletRenderResult("title", null, 0, 100);
    final String output = "<p>Some content</p>";
    CachedPortletData<PortletRenderResult> cachedPortletData = new CachedPortletData<PortletRenderResult>(portletResult, output, null, null, false, null, cacheControl.getExpirationTime());
    cacheState.setCachedPortletData(cachedPortletData);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletRenderState(request, portletWindowId)).thenReturn(cacheState);
    when(portalRequestInfo.getTargetedPortletWindowId()).thenReturn(portletWindowId);
    RenderPortletOutputHandler handler = new RenderPortletOutputHandler("UTF-8");
    portletRenderer.doRenderMarkup(portletWindowId, request, response, handler);
    Assert.assertEquals(output, handler.getOutput());
    verify(portletCacheControlService, times(1)).getPortletRenderState(request, portletWindowId);
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : CachedPortletData(org.apereo.portal.portlet.container.cache.CachedPortletData) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 22 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletRendererImplTest method doRenderMarkupCachedContentValidationNotExpiredMethodTest.

/**
 * Mimic workflow when data cached portlet data using "validation" method is available.
 *
 * @throws PortletContainerException
 * @throws IOException
 * @throws PortletException
 */
@Test
public void doRenderMarkupCachedContentValidationNotExpiredMethodTest() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    TestingCacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult> cacheState = new TestingCacheState<CachedPortletData<PortletRenderResult>, PortletRenderResult>();
    cacheState.setUseCachedData(true);
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(true);
    cacheControl.setETag("123456");
    cacheControl.setExpirationTime(300);
    final PortletRenderResult portletResult = new PortletRenderResult("title", null, 0, 100);
    final String output = "<p>Some content</p>";
    CachedPortletData<PortletRenderResult> cachedPortletData = new CachedPortletData<PortletRenderResult>(portletResult, output, null, null, false, cacheControl.getETag(), cacheControl.getExpirationTime());
    cacheState.setCachedPortletData(cachedPortletData);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletRenderState(request, portletWindowId)).thenReturn(cacheState);
    when(portalRequestInfo.getTargetedPortletWindowId()).thenReturn(portletWindowId);
    RenderPortletOutputHandler handler = new RenderPortletOutputHandler("UTF-8");
    portletRenderer.doRenderMarkup(portletWindowId, request, response, handler);
    Assert.assertEquals(output, handler.getOutput());
    verify(portletCacheControlService, times(1)).getPortletRenderState(request, portletWindowId);
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : CachedPortletData(org.apereo.portal.portlet.container.cache.CachedPortletData) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 23 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletRendererImplTest method doServeResourceUseBrowserContentTest.

/**
 * Same as {@link #doServeResourceCachedContentValidationMethodTest()}, but simulate browser
 * sending If-None-Match header that matches the etag. Verify no content returned and a 304
 * status code.
 *
 * @throws PortletException
 * @throws IOException
 * @throws PortletContainerException
 */
@Test
public void doServeResourceUseBrowserContentTest() 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.setUseBrowserData(true);
    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();
    Assert.assertEquals(0, response.getContentLength());
    Assert.assertEquals(304, response.getStatus());
    verify(portletCacheControlService, times(1)).getPortletResourceState(request, portletWindowId);
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : CachedPortletData(org.apereo.portal.portlet.container.cache.CachedPortletData) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CachedPortletResourceData(org.apereo.portal.portlet.container.cache.CachedPortletResourceData) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 24 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletRendererImplTest method doServeResourceMarkupCaptureNegativeExpirationTime.

/**
 * No cached data exists, but mock a {@link CacheControl} with a negative value for
 * expirationtime. Will trigger the portletContainer#doServeResource, capture the output, and
 * give to the portlet cachecontrol service.
 *
 * <p>negative value for cacheControl expiration time means "cache forever."
 *
 * @throws PortletException
 * @throws IOException
 * @throws PortletContainerException
 */
@Test
public void doServeResourceMarkupCaptureNegativeExpirationTime() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("application/octet-stream");
    TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(false);
    cacheControl.setExpirationTime(-1);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
    when(portletCacheControlService.shouldOutputBeCached(cacheControl)).thenReturn(true);
    ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
    portletRenderer.doServeResource(portletWindowId, request, response, handler);
    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, times(1)).shouldOutputBeCached(isA(CacheControl.class));
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : PortletHttpServletRequestWrapper(org.apereo.portal.utils.web.PortletHttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CachedPortletResourceData(org.apereo.portal.portlet.container.cache.CachedPortletResourceData) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 25 with CacheControl

use of javax.portlet.CacheControl in project uPortal by Jasig.

the class PortletRendererImplTest method doServeResourceCapture.

/**
 * No cached data exists, but mock a {@link CacheControl} that will trigger the
 * portletContainer#doServeResource, capture the output, and give to the portlet cachecontrol
 * service.
 *
 * @throws PortletException
 * @throws IOException
 * @throws PortletContainerException
 */
@Test
public void doServeResourceCapture() throws PortletException, IOException, PortletContainerException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    response.setContentType("application/octet-stream");
    TestingCacheState<CachedPortletResourceData<Long>, Long> cacheState = new TestingCacheState<CachedPortletResourceData<Long>, Long>();
    CacheControl cacheControl = cacheState.getCacheControl();
    cacheControl.setUseCachedContent(false);
    cacheControl.setExpirationTime(300);
    setupPortletExecutionMocks(request);
    when(portletCacheControlService.getPortletResourceState(request, portletWindowId)).thenReturn(cacheState);
    when(portletCacheControlService.shouldOutputBeCached(cacheControl)).thenReturn(true);
    ResourcePortletOutputHandler handler = new ResourcePortletOutputHandler(response);
    portletRenderer.doServeResource(portletWindowId, request, response, handler);
    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, times(1)).shouldOutputBeCached(isA(CacheControl.class));
    verifyNoMoreInteractions(portletContainer, portletCacheControlService);
}
Also used : PortletHttpServletRequestWrapper(org.apereo.portal.utils.web.PortletHttpServletRequestWrapper) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) CachedPortletResourceData(org.apereo.portal.portlet.container.cache.CachedPortletResourceData) CacheControl(javax.portlet.CacheControl) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

CacheControl (javax.portlet.CacheControl)26 Test (org.junit.Test)22 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)22 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)16 CachedPortletData (org.apereo.portal.portlet.container.cache.CachedPortletData)14 CachedPortletResourceData (org.apereo.portal.portlet.container.cache.CachedPortletResourceData)11 PortletHttpServletRequestWrapper (org.apereo.portal.utils.web.PortletHttpServletRequestWrapper)11 MockPortletDefinitionId (org.apereo.portal.mock.portlet.om.MockPortletDefinitionId)6 MockPortletWindowId (org.apereo.portal.mock.portlet.om.MockPortletWindowId)6 PortletRenderResult (org.apereo.portal.portlet.rendering.PortletRenderResult)5 IPortletWindow (org.apereo.portal.portlet.om.IPortletWindow)4 PortletHttpServletResponseWrapper (org.apereo.portal.utils.web.PortletHttpServletResponseWrapper)4 IOException (java.io.IOException)2 PortletException (javax.portlet.PortletException)2 PortletContainerException (org.apache.pluto.container.PortletContainerException)2 PortletDefinition (org.apache.pluto.container.om.portlet.PortletDefinition)2 PortletDispatchException (org.apereo.portal.portlet.PortletDispatchException)2 HeaderSettingCacheControl (org.apereo.portal.portlet.container.cache.HeaderSettingCacheControl)2 IPortletEntity (org.apereo.portal.portlet.om.IPortletEntity)2 List (java.util.List)1