Skip to content

Commit

Permalink
Tweaks exception and exception javadocs.
Browse files Browse the repository at this point in the history
Tweaks the error messages.
  • Loading branch information
bseeger committed Sep 25, 2023
1 parent fe4ca22 commit 1bbfd2b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -40,61 +41,63 @@ public SessionContinuityInterceptor(List<FlowConfiguration> flowConfigurations)
* @param handler chosen handler to execute, for type and/or instance evaluation
* @return Boolean True - allows the request to proceed to the ScreenController, False - stops the request from reaching the
* Screen Controller.
* @throws Exception
* @throws IOException - thrown in the event that an input or output exception occurs when this method does a redirect.
*/
@Override
public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler)
throws Exception {
String pathFormat = request.getRequestURI().contains("navigation") ? NAVIGATION_FLOW_PATH_FORMAT : FLOW_PATH_FORMAT;
Map<String, String> parsedUrl = new AntPathMatcher().extractUriTemplateVariables(pathFormat, request.getRequestURI());
throws IOException {
String pathFormat = request.getRequestURI().contains("navigation") ? NAVIGATION_FLOW_PATH_FORMAT : FLOW_PATH_FORMAT;
Map<String, String> parsedUrl = new AntPathMatcher().extractUriTemplateVariables(pathFormat, request.getRequestURI());

HttpSession session = request.getSession(false);
HttpSession session = request.getSession(false);

FlowConfiguration flowConfiguration = flowConfigurations.stream()
.filter(fc -> fc.getName().equals(parsedUrl.get("flow")))
.findFirst()
.orElse(null);
FlowConfiguration flowConfiguration = flowConfigurations.stream()
.filter(fc -> fc.getName().equals(parsedUrl.get("flow")))
.findFirst()
.orElse(null);

if (flowConfiguration == null) {
if (flowConfiguration == null) {
return true;
}

if (flowConfiguration.getLandmarks() == null) {
throw new LandmarkNotSetException(
"The SessionContinuityInterceptor is enabled, but no 'landmarks' section has been created in the application's form " +
"flows configuration file.");
}

if (flowConfiguration.getLandmarks().getFirstScreen() == null) {
throw new LandmarkNotSetException(
"The SessionContinuityInterceptor is enabled, but a 'firstScreen' page has not " +
"been identified in the 'landmarks' section in the application's form flows configuration file.");
}

String firstScreen = flowConfiguration.getLandmarks().getFirstScreen();

if (!flowConfiguration.getFlow().containsKey(firstScreen)) {
throw new LandmarkNotSetException(String.format(
"The form flows configuration file does not contain a screen with the name '%s'. " +
"Please make sure to correctly set the 'firstScreen' in the form flows configuration file 'landmarks' section.",
firstScreen));
}

if (session == null) {
if (parsedUrl.get("screen").equals(firstScreen)) {
return true;
}

if (flowConfiguration.getLandmarks() == null) {
throw new LandmarkNotSetException(
"You have enabled session continuity interception but have not created a landmark section in your applications flow configuration file.");
}

if (flowConfiguration.getLandmarks().getFirstScreen() == null) {
throw new LandmarkNotSetException(
"Please make sure to set a firstScreen under your flow configuration files landmark section.");
}

String firstScreen = flowConfiguration.getLandmarks().getFirstScreen();

if (!flowConfiguration.getFlow().containsKey(firstScreen)) {
throw new LandmarkNotSetException(String.format(
"Please make sure that you have correctly set the firstScreen under your flow configuration files landmark section. Your flow configuration file does not contain a screen with the name %s.",
firstScreen));
}


if (session == null) {
if(parsedUrl.get("screen").equals(firstScreen)){
return true;
}
log.error("No active session found for request to {}. Redirecting to landing page.", request.getRequestURI());
response.sendRedirect(REDIRECT_URL);
return false;
}

if (session.getAttribute("id") == null) {
log.error("A submission ID was not found in the session for request to {}. Redirecting to landing page.",
request.getRequestURI());
response.sendRedirect(REDIRECT_URL);
return false;
}

return true;
log.error("No active session found for request to {}. Redirecting to landing page.", request.getRequestURI());
response.sendRedirect(REDIRECT_URL);
return false;
}

if (session.getAttribute("id") == null) {
log.error("A submission ID was not found in the session for request to {}. Redirecting to landing page.",
request.getRequestURI());
response.sendRedirect(REDIRECT_URL);
return false;
}

return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static org.mockito.Mockito.inOrder;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import formflow.library.config.FlowConfiguration;
import formflow.library.config.LandmarkConfiguration;
import formflow.library.config.NextScreen;
Expand All @@ -29,10 +30,10 @@
@TestPropertySource(properties = {"form-flow.session-continuity-interceptor.enabled=true"})
@Import(SpyInterceptorConfig.class)
class InterceptorMockMvcTest extends AbstractMockMvcTest {

@Autowired
private LocaleChangeInterceptor localeChangeInterceptor;

@Autowired
private SessionContinuityInterceptor sessionContinuityInterceptor;

Expand All @@ -44,7 +45,7 @@ protected void setUp() throws Exception {
sessionContinuityInterceptor.flowConfigurations = List.of();
super.setUp();
}

@Test
void shouldRunTheDataRequiredInterceptorLast() throws Exception {
FlowConfiguration flowConfiguration = new FlowConfiguration();
Expand All @@ -67,7 +68,7 @@ void shouldRunTheDataRequiredInterceptorLast() throws Exception {
inOrder.verify(localeChangeInterceptor).preHandle(any(), any(), any());
inOrder.verify(sessionContinuityInterceptor).preHandle(any(), any(), any());
}

@Test
void shouldErrorIfLandmarkIsNotSet() throws Exception {
FlowConfiguration flowConfiguration = new FlowConfiguration();
Expand All @@ -78,7 +79,10 @@ void shouldErrorIfLandmarkIsNotSet() throws Exception {
.andExpect(result -> {
Exception resolvedException = result.getResolvedException();
assertTrue(resolvedException instanceof LandmarkNotSetException, "Expected RuntimeException to be thrown");
assertEquals("You have enabled session continuity interception but have not created a landmark section in your applications flow configuration file.", resolvedException.getMessage());

assertEquals(
"The SessionContinuityInterceptor is enabled, but no 'landmarks' section has been created in the application's form flows configuration file.",
resolvedException.getMessage());
});
}

Expand All @@ -95,7 +99,9 @@ void shouldErrorIfFirstScreenIsNotSet() throws Exception {
.andExpect(result -> {
Exception resolvedException = result.getResolvedException();
assertTrue(resolvedException instanceof LandmarkNotSetException, "Expected RuntimeException to be thrown");
assertEquals("Please make sure to set a firstScreen under your flow configuration files landmark section.", resolvedException.getMessage());
assertEquals(
"The SessionContinuityInterceptor is enabled, but a 'firstScreen' page has not been identified in the 'landmarks' section in the application's form flows configuration file.",
resolvedException.getMessage());
});
}

Expand All @@ -114,13 +120,15 @@ void shouldErrorIfFirstScreenDoesNotExistWithinFlowConfiguration() throws Except
landmarkConfiguration.setFirstScreen("nonExistentScreen");
flowConfiguration.setLandmarks(landmarkConfiguration);
sessionContinuityInterceptor.flowConfigurations = List.of(flowConfiguration);

mockMvc.perform(MockMvcRequestBuilders.get("/flow/testLandmarkFlow/first"))
.andExpect(status().is5xxServerError())
.andExpect(result -> {
Exception resolvedException = result.getResolvedException();
assertTrue(resolvedException instanceof LandmarkNotSetException, "Expected RuntimeException to be thrown");
assertEquals("Please make sure that you have correctly set the firstScreen under your flow configuration files landmark section. Your flow configuration file does not contain a screen with the name nonExistentScreen.", resolvedException.getMessage());
assertEquals(
"The form flows configuration file does not contain a screen with the name 'nonExistentScreen'. Please make sure to correctly set the 'firstScreen' in the form flows configuration file 'landmarks' section.",
resolvedException.getMessage());
});
}
}

0 comments on commit 1bbfd2b

Please sign in to comment.