1818import static com .github .tomakehurst .wiremock .matching .RequestPatternBuilder .newRequestPattern ;
1919import static org .hamcrest .MatcherAssert .assertThat ;
2020import static org .hamcrest .Matchers .*;
21+ import static org .junit .jupiter .api .Assertions .assertTrue ;
22+ import static org .junit .jupiter .api .Assertions .assertFalse ;
2123
2224import com .github .tomakehurst .wiremock .client .WireMock ;
2325import com .github .tomakehurst .wiremock .http .ResponseDefinition ;
2426import com .github .tomakehurst .wiremock .matching .MatchResult ;
2527import com .github .tomakehurst .wiremock .stubbing .StubMapping ;
2628import com .google .common .collect .ImmutableList ;
2729import java .util .List ;
30+
2831import org .junit .jupiter .api .Test ;
2932
3033public class SnapshotStubMappingPostProcessorTest {
31- private static final List <StubMapping > TEST_STUB_MAPPINGS =
34+ // NOTE: testStubMappings is not deeply immutable, as StubMappings are mutable, and to preserve hermeticity must be
35+ // an instance rather than a class variable.
36+ private final List <StubMapping > testStubMappings =
3237 ImmutableList .of (
3338 WireMock .get ("/foo" ).build (), WireMock .get ("/bar" ).build (), WireMock .get ("/foo" ).build ());
3439
3540 @ Test
36- public void processFiltersRepeatedRequestsWhenNotRecordingScenarios () {
41+ public void process_notRecordingScenarios_filtersRepeatedRequests () {
3742 final List <StubMapping > actual =
3843 new SnapshotStubMappingPostProcessor (false , noopTransformerRunner (), null , null )
39- .process (TEST_STUB_MAPPINGS );
44+ .process (testStubMappings );
4045
4146 assertThat (actual , hasSize (2 ));
4247 assertThat (actual .get (0 ).getRequest ().getUrl (), equalTo ("/foo" ));
4348 assertThat (actual .get (1 ).getRequest ().getUrl (), equalTo ("/bar" ));
4449 }
4550
4651 @ Test
47- public void processRunsTransformers () {
52+ public void process_withTransformer () {
4853 SnapshotStubMappingTransformerRunner transformerRunner =
4954 new SnapshotStubMappingTransformerRunner (null ) {
5055 @ Override
@@ -58,21 +63,48 @@ public StubMapping apply(StubMapping stubMapping) {
5863
5964 final List <StubMapping > actual =
6065 new SnapshotStubMappingPostProcessor (false , transformerRunner , null , null )
61- .process (TEST_STUB_MAPPINGS );
66+ .process (testStubMappings );
6267
6368 assertThat (actual , hasSize (2 ));
6469 assertThat (actual .get (0 ).getRequest ().getUrl (), equalTo ("/foo/transformed" ));
6570 assertThat (actual .get (1 ).getRequest ().getUrl (), equalTo ("/bar/transformed" ));
6671 }
6772
73+ @ Test
74+ public void process_withShouldRecordRepeatsAsScenariosAndTransformer_runsTransformerBeforeScenarioProcessor () {
75+ SnapshotStubMappingTransformerRunner transformerRunner =
76+ new SnapshotStubMappingTransformerRunner (null ) {
77+ @ Override
78+ public StubMapping apply (StubMapping stubMapping ) {
79+ // Return StubMapping with "/transformed" at the end of the original URL
80+ String url = stubMapping .getRequest ().getUrl ();
81+ return new StubMapping (
82+ newRequestPattern ().withUrl (url + "/transformed" ).build (), ResponseDefinition .ok ());
83+ }
84+ };
85+
86+ final List <StubMapping > actual =
87+ new SnapshotStubMappingPostProcessor (true , transformerRunner , null , null )
88+ .process (testStubMappings );
89+
90+ assertThat (actual , hasSize (3 ));
91+ assertThat (actual .get (0 ).getRequest ().getUrl (), equalTo ("/foo/transformed" ));
92+ assertThat (actual .get (1 ).getRequest ().getUrl (), equalTo ("/bar/transformed" ));
93+ assertThat (actual .get (2 ).getRequest ().getUrl (), equalTo ("/foo/transformed" ));
94+
95+ assertTrue (actual .get (0 ).isInScenario ());
96+ assertFalse (actual .get (1 ).isInScenario ());
97+ assertTrue (actual .get (2 ).isInScenario ());
98+ }
99+
68100 @ Test
69- public void processExtractsBodiesWhenMatched () {
101+ public void process_withBodyExtractMatcherAndBodyExtractor_extractsBodiesWhenMatched () {
70102 final ResponseDefinitionBodyMatcher bodyMatcher =
71103 new ResponseDefinitionBodyMatcher (0 , 0 ) {
72104 @ Override
73105 public MatchResult match (ResponseDefinition responseDefinition ) {
74106 // Only match the second stub mapping
75- return responseDefinition == TEST_STUB_MAPPINGS .get (1 ).getResponse ()
107+ return responseDefinition == testStubMappings .get (1 ).getResponse ()
76108 ? MatchResult .exactMatch ()
77109 : MatchResult .noMatch ();
78110 }
@@ -89,7 +121,7 @@ public void extractInPlace(StubMapping stubMapping) {
89121 final List <StubMapping > actual =
90122 new SnapshotStubMappingPostProcessor (
91123 false , noopTransformerRunner (), bodyMatcher , bodyExtractor )
92- .process (TEST_STUB_MAPPINGS );
124+ .process (testStubMappings );
93125
94126 assertThat (actual , hasSize (2 ));
95127 // Should've only modified second stub mapping
0 commit comments