Detail extends BaseHstComponent { @Override public void doBeforeRender(final HstRequest request, final HstResponse response) throws HstComponentException { HippoBean doc = getContentBean(request); if (doc == null) { log.warn("Did not find a content bean for relative content path '{}' for pathInfo '{}'", request.getRequestContext().getResolvedSiteMapItem().getRelativeContentPath(), request.getRequestContext().getResolvedSiteMapItem().getPathInfo()); response.setStatus(404); return; } request.setAttribute("document",doc); }  @Override void doBeforeRender(final HstRequest request, final HstResponse response); static final Logger log; }
@Test public void doBeforeRender_FoundBean() throws Exception { HippoBean detailBean = new MockHippoBean(); ServletContext servletContext = createMock(ServletContext.class); ComponentConfiguration componentConfiguration = new MockComponentConfiguration(); ObjectConverter objectConverter = createMock(ObjectConverter.class); HstRequest request = new MockHstRequest(); Session session = createMock(Session.class); HstResponse response = createMock(HstResponse.class); HstRequestContext requestContext = new MockHstRequestContext(); ((MockHstRequestContext) requestContext).setSession(session); ResolvedMount resolvedMount = createMock(ResolvedMount.class); Mount mount = createMock(Mount.class); ((MockHstRequestContext) requestContext).setResolvedMount(resolvedMount); ResolvedSiteMapItem resolvedSiteMapItem = new MockResolvedSiteMapItem(); ((MockResolvedSiteMapItem) resolvedSiteMapItem).setRelativeContentPath("common/detail"); ((MockHstRequestContext) requestContext).setResolvedSiteMapItem(resolvedSiteMapItem); ((MockHstRequest) request).setRequestContext(requestContext); expect(resolvedMount.getMount()).andReturn(mount); expect(mount.getContentPath()).andReturn("/hst:hst/hst:sites/mysite-live/hst:content"); expect(servletContext.getAttribute("org.hippoecm.hst.component.support.bean.BaseHstComponent.objectConverter")).andReturn(objectConverter).anyTimes(); expect(objectConverter.getObject(session, "/hst:hst/hst:sites/mysite-live/hst:content/common/detail")).andReturn(detailBean); replay(servletContext, objectConverter, resolvedMount, mount, response, session); Detail detail = new Detail(); detail.init(servletContext, componentConfiguration); detail.doBeforeRender(request, response); verify(servletContext, objectConverter, resolvedMount, mount, response, session); assertEquals(detailBean, request.getAttribute("document")); }
@Test public void doBeforeRender_MissingBean() throws Exception { ServletContext servletContext = createMock(ServletContext.class); ComponentConfiguration componentConfiguration = new MockComponentConfiguration(); ObjectConverter objectConverter = createMock(ObjectConverter.class); HstRequest request = new MockHstRequest(); Session session = createMock(Session.class); HstResponse response = createMock(HstResponse.class); HstRequestContext requestContext = new MockHstRequestContext(); ((MockHstRequestContext) requestContext).setSession(session); ResolvedMount resolvedMount = createMock(ResolvedMount.class); Mount mount = createMock(Mount.class); ((MockHstRequestContext) requestContext).setResolvedMount(resolvedMount); ResolvedSiteMapItem resolvedSiteMapItem = new MockResolvedSiteMapItem(); ((MockResolvedSiteMapItem) resolvedSiteMapItem).setRelativeContentPath("common/detail"); ((MockHstRequestContext) requestContext).setResolvedSiteMapItem(resolvedSiteMapItem); ((MockHstRequest) request).setRequestContext(requestContext); expect(resolvedMount.getMount()).andReturn(mount); expect(mount.getContentPath()).andReturn("/hst:hst/hst:sites/mysite-live/hst:content"); expect(servletContext.getAttribute("org.hippoecm.hst.component.support.bean.BaseHstComponent.objectConverter")).andReturn(objectConverter).anyTimes(); expect(objectConverter.getObject(session, "/hst:hst/hst:sites/mysite-live/hst:content/common/detail")).andReturn(null); response.setStatus(HstResponse.SC_NOT_FOUND); expectLastCall(); replay(servletContext, objectConverter, resolvedMount, mount, response, session); Detail detail = new Detail(); detail.init(servletContext, componentConfiguration); detail.doBeforeRender(request, response); verify(servletContext, objectConverter, resolvedMount, mount, response, session); assertNull(request.getAttribute("document")); }