-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests with Spybeans for interceptors
- Loading branch information
1 parent
e232343
commit b379213
Showing
6 changed files
with
165 additions
and
6 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
src/main/java/formflow/library/exceptions/LandmarkNotSetException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package formflow.library.exceptions; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) | ||
public class LandmarkNotSetException extends RuntimeException { | ||
public LandmarkNotSetException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/test/java/formflow/library/interceptors/InterceptorOrderMockMvcTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package formflow.library.interceptors; | ||
|
||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.ArgumentMatchers.any; | ||
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; | ||
import formflow.library.config.ScreenNavigationConfiguration; | ||
import formflow.library.exceptions.LandmarkNotSetException; | ||
import formflow.library.utilities.AbstractMockMvcTest; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InOrder; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; | ||
|
||
@SpringBootTest(properties = {"form-flow.path=flows-config/test-landmark-flow.yaml"}) | ||
@TestPropertySource(properties = {"form-flow.session-continuity-interceptor.enabled=true"}) | ||
@Import(SpyInterceptorConfig.class) | ||
class InterceptorOrderMockMvcTest extends AbstractMockMvcTest { | ||
|
||
@Autowired | ||
private LocaleChangeInterceptor localeChangeInterceptor; | ||
|
||
@Autowired | ||
private DataRequiredInterceptor dataRequiredInterceptor; | ||
|
||
@Test | ||
void shouldRunTheDataRequiredInterceptorLast() throws Exception { | ||
mockMvc.perform(get("/flow/testLandmarkFlow/first?lang=es")) | ||
.andExpect(status().isOk()); | ||
|
||
InOrder inOrder = inOrder(localeChangeInterceptor, dataRequiredInterceptor); | ||
inOrder.verify(localeChangeInterceptor).preHandle(any(), any(), any()); | ||
inOrder.verify(dataRequiredInterceptor).preHandle(any(), any(), any()); | ||
} | ||
|
||
@Test | ||
void shouldErrorIfLandmarkIsNotSet() throws Exception { | ||
FlowConfiguration flowConfiguration = new FlowConfiguration(); | ||
flowConfiguration.setName("testLandmarkFlow"); | ||
dataRequiredInterceptor.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("You have enabled session continuity interception but have not created a landmark section in your applications flow configuration file.", resolvedException.getMessage()); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldErrorIfFirstScreenIsNotSet() throws Exception { | ||
FlowConfiguration flowConfiguration = new FlowConfiguration(); | ||
flowConfiguration.setName("testLandmarkFlow"); | ||
LandmarkConfiguration landmarkConfiguration = new LandmarkConfiguration(); | ||
landmarkConfiguration.setFirstScreen(null); | ||
flowConfiguration.setLandmarks(landmarkConfiguration); | ||
dataRequiredInterceptor.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 to set a firstScreen under your flow configuration files landmark section.", resolvedException.getMessage()); | ||
}); | ||
} | ||
|
||
@Test | ||
void shouldErrorIfFirstScreenDoesNotExistWithinFlowConfiguration() throws Exception { | ||
FlowConfiguration flowConfiguration = new FlowConfiguration(); | ||
ScreenNavigationConfiguration screenNavigationConfiguration = new ScreenNavigationConfiguration(); | ||
NextScreen nextScreen = new NextScreen(); | ||
nextScreen.setName("first"); | ||
screenNavigationConfiguration.setNextScreens(List.of(nextScreen)); | ||
HashMap<String, ScreenNavigationConfiguration> screenNavigationConfigurations = new HashMap<>(); | ||
screenNavigationConfigurations.put("first", screenNavigationConfiguration); | ||
flowConfiguration.setFlow(screenNavigationConfigurations); | ||
flowConfiguration.setName("testLandmarkFlow"); | ||
LandmarkConfiguration landmarkConfiguration = new LandmarkConfiguration(); | ||
landmarkConfiguration.setFirstScreen("nonExistentScreen"); | ||
flowConfiguration.setLandmarks(landmarkConfiguration); | ||
dataRequiredInterceptor.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()); | ||
}); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/java/formflow/library/interceptors/SpyInterceptorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package formflow.library.interceptors; | ||
|
||
import formflow.library.config.FlowConfiguration; | ||
import java.util.List; | ||
import org.mockito.Mockito; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; | ||
|
||
@TestConfiguration | ||
public class SpyInterceptorConfig implements WebMvcConfigurer { | ||
|
||
@Autowired | ||
private List<FlowConfiguration> flowConfigurations; | ||
|
||
@Bean | ||
@Primary | ||
public LocaleChangeInterceptor localeChangeInterceptor() { | ||
return Mockito.spy(new LocaleChangeInterceptor()); | ||
} | ||
|
||
@Bean | ||
@Primary // Ensure this bean takes precedence over the real one | ||
public DataRequiredInterceptor dataRequiredInterceptor() { | ||
return Mockito.spy(new DataRequiredInterceptor(flowConfigurations)); | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(localeChangeInterceptor()); | ||
registry.addInterceptor(dataRequiredInterceptor()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters