/** * Creates a new mock request context with the provided flow execution context. To add request parameters to this * request, use the {@link #putRequestParameter(String, String)} method. * @param flowExecutionContext the flow execution context */ public MockRequestContext(FlowExecutionContext flowExecutionContext) { this.flowExecutionContext = flowExecutionContext; this.externalContext = new MockExternalContext(); this.messageContext = new DefaultMessageContext(); }
/** * Sets the id of the event that should be signaled by this context. For use when resuming a flow. This method * depends on a MockViewFactory being configured for parsing the event id on a resume operation. * @param eventId the id of the event to signal */ public void setEventId(String eventId) { putRequestParameter("_eventId", eventId); }
/** * Puts a multi-valued MultipartFile request parameter into the mock parameter map. * * @param parameterName the parameter name * @param parameterValue the parameter value */ public void putRequestParameter(String parameterName, List<MultipartFile> parameterValue) { getMockRequestParameterMap().put(parameterName, parameterValue); }
public boolean isRedirectRequested() { return getFlowExecutionRedirectRequested() || getFlowDefinitionRedirectRequested() || getExternalRedirectRequested(); }
private void assertResponseAllowed() throws IllegalStateException { if (!isResponseAllowed()) { if (getFlowExecutionRedirectRequested()) { throw new IllegalStateException( "A response is not allowed because a redirect has already been requested on this ExternalContext"); } if (getFlowDefinitionRedirectRequested()) { throw new IllegalStateException( "A response is not allowed because a flowRedirect has already been requested on this ExternalContext"); } if (getExternalRedirectRequested()) { throw new IllegalStateException( "A response is not allowed because an externalRedirect has already been requested on this ExternalContext"); } if (responseComplete) { throw new IllegalStateException( "A response is not allowed because one has already been completed on this ExternalContext"); } else { throw new IllegalStateException("A response is not allowed"); } } }
public void testSelectValidResult() { startFlow(null, new MockExternalContext()); MockExternalContext context = new MockExternalContext(); context.putRequestParameter("firstName", "Keith"); context.putRequestParameter("lastName", "Donald"); context.setEventId("search"); resumeFlow(context); context = new MockExternalContext(); context.setEventId("select"); context.putRequestParameter("id", "1"); resumeFlow(context); assertCurrentStateEquals("displayResults"); }
public void testFlowWithSubflow() { MockExternalContext context = new MockExternalContext(); flowExecution.start(null, context); context.setEventId("managed"); flowExecution.resume(context); context.setEventId("finish"); flowExecution.resume(context); }
public void testEnterViewStateWithAlwaysRedirectOnPause() { Flow flow = new Flow("myFlow"); StubViewFactory viewFactory = new StubViewFactory(); ViewState state = new ViewState(flow, "viewState", viewFactory); MockRequestControlContext context = new MockRequestControlContext(flow); context.getFlashScope().put("foo", "bar"); context.setAlwaysRedirectOnPause(true); state.enter(context); assertFalse("Render called", context.getFlowScope().contains("renderCalled")); assertTrue(context.getMockExternalContext().getFlowExecutionRedirectRequested()); assertTrue(context.getFlashScope().contains("foo")); }
public void testCustomSessionKey() { conversationManager.setSessionKey("foo"); MockExternalContext context = new MockExternalContext(); ExternalContextHolder.setExternalContext(context); conversationManager.beginConversation(new ConversationParameters("test", "test", "test")); assertNotNull(context.getSessionMap().get("foo")); }
public void testEmbeddedModeOverridesRedirectInSameState() { Flow flow = new Flow("myFlow"); StubViewFactory viewFactory = new StubViewFactory(); ViewState state = new ViewState(flow, "viewState", viewFactory); Transition t = new Transition(on("submit"), null); state.getTransitionSet().add(t); MockRequestControlContext context = new MockRequestControlContext(flow); context.getMockExternalContext().setAjaxRequest(true); context.setEmbeddedMode(); context.setAlwaysRedirectOnPause(true); context.setRedirectInSameState(true); state.enter(context); assertFalse(context.getMockExternalContext().getFlowExecutionRedirectRequested()); }
public void testEnterViewStateRenderNotAllowed() { Flow flow = new Flow("myFlow"); StubViewFactory viewFactory = new StubViewFactory(); ViewState state = new ViewState(flow, "viewState", viewFactory); MockRequestControlContext context = new MockRequestControlContext(flow); context.getMockExternalContext().setResponseAllowed(false); context.getFlashScope().put("foo", "bar"); state.enter(context); assertFalse("Render called", context.getFlowScope().contains("renderCalled")); assertTrue(context.getExternalContext().isResponseComplete()); assertTrue(context.getMockExternalContext().getFlowExecutionRedirectRequested()); assertTrue(context.getFlashScope().contains("foo")); }
/** * Assert that the response written to the mock context equals the response provided. * @param response the expected response * @param context the mock external context that was written to */ protected void assertResponseWrittenEquals(String response, MockExternalContext context) { assertEquals(response, context.getMockResponseWriter().getBuffer().toString()); }
public void testEnterViewStateRedirectInPopup() { Flow flow = new Flow("myFlow"); StubViewFactory viewFactory = new StubViewFactory(); ViewState state = new ViewState(flow, "viewState", viewFactory); state.setRedirect(true); state.setPopup(true); MockRequestControlContext context = new MockRequestControlContext(flow); context.getFlashScope().put("foo", "bar"); state.enter(context); assertFalse("Render called", context.getFlowScope().contains("renderCalled")); assertTrue(context.getMockExternalContext().getFlowExecutionRedirectRequested()); assertTrue(context.getMockExternalContext().getRedirectInPopup()); assertTrue(context.getFlashScope().contains("foo")); }
public void requestRedirectInPopup() throws IllegalStateException { if (isRedirectRequested()) { redirectInPopup = true; } else { throw new IllegalStateException( "Only call requestRedirectInPopup after a redirect has been requested by calling requestFlowExecutionRedirect, requestFlowDefinitionRedirect, or requestExternalRedirect"); } }
public Writer getResponseWriter() { assertResponseAllowed(); return responseWriter; }
public void testNewSearch() { startFlow(null, new MockExternalContext()); MockExternalContext context = new MockExternalContext(); context.putRequestParameter("firstName", "Keith"); context.putRequestParameter("lastName", "Donald"); context.setEventId("search"); resumeFlow(context); context = new MockExternalContext(); context.setEventId("newSearch"); resumeFlow(context); assertCurrentStateEquals("enterCriteria"); assertResponseWrittenEquals("searchCriteria", context); }
private void assertResponseAllowed() throws IllegalStateException { if (!isResponseAllowed()) { if (getFlowExecutionRedirectRequested()) { throw new IllegalStateException( "A response is not allowed because a redirect has already been requested on this ExternalContext"); } if (getFlowDefinitionRedirectRequested()) { throw new IllegalStateException( "A response is not allowed because a flowRedirect has already been requested on this ExternalContext"); } if (getExternalRedirectRequested()) { throw new IllegalStateException( "A response is not allowed because an externalRedirect has already been requested on this ExternalContext"); } if (responseComplete) { throw new IllegalStateException( "A response is not allowed because one has already been completed on this ExternalContext"); } else { throw new IllegalStateException("A response is not allowed"); } } }
public void testManagedFlowWithUnmanagedSubflow() { MockExternalContext context = new MockExternalContext(); flowExecution.start(null, context); context.setEventId("notmanaged"); flowExecution.resume(context); }
public void testEnterViewStateWithLocalRedirect() { Flow flow = new Flow("myFlow"); StubViewFactory viewFactory = new StubViewFactory(); ViewState state = new ViewState(flow, "viewState", viewFactory); state.setRedirect(true); MockRequestControlContext context = new MockRequestControlContext(flow); context.getFlashScope().put("foo", "bar"); state.enter(context); assertFalse("Render called", context.getFlowScope().contains("renderCalled")); assertTrue(context.getMockExternalContext().getFlowExecutionRedirectRequested()); assertTrue(context.getFlashScope().contains("foo")); }
public boolean isRedirectRequested() { return getFlowExecutionRedirectRequested() || getFlowDefinitionRedirectRequested() || getExternalRedirectRequested(); }