com.github.tomakehurst.wiremock.recording.ScenarioProcessor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wiremock-standalone Show documentation
Show all versions of wiremock-standalone Show documentation
A web service test double for all occasions - standalone edition
/*
* Copyright (C) 2017-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.tomakehurst.wiremock.recording;
import com.github.tomakehurst.wiremock.common.Urls;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.stubbing.Scenario;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
public class ScenarioProcessor {
public void putRepeatedRequestsInScenarios(List stubMappings) {
Map> stubsGroupedByRequest =
stubMappings.stream()
.collect(
Collectors.groupingBy(
StubMapping::getRequest,
LinkedHashMap::new,
Collectors.toCollection(LinkedList::new)));
Map> groupsWithMoreThanOneStub =
stubsGroupedByRequest.entrySet().stream()
.filter(entry -> entry.getValue().size() > 1)
.collect(
Collectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(entry1, entry2) -> entry1,
LinkedHashMap::new));
int scenarioIndex = 0;
for (Map.Entry> entry :
groupsWithMoreThanOneStub.entrySet()) {
scenarioIndex++;
putStubsInScenario(scenarioIndex, List.copyOf(entry.getValue()));
}
}
private void putStubsInScenario(int scenarioIndex, List stubMappings) {
StubMapping firstScenario = stubMappings.get(0);
String scenarioName =
"scenario-"
+ scenarioIndex
+ "-"
+ Urls.urlToPathParts(URI.create(firstScenario.getRequest().getUrl()));
int count = 1;
for (StubMapping stub : stubMappings) {
stub.setScenarioName(scenarioName);
if (count == 1) {
stub.setRequiredScenarioState(Scenario.STARTED);
} else {
stub.setRequiredScenarioState(scenarioName + "-" + count);
}
if (count < stubMappings.size()) {
stub.setNewScenarioState(scenarioName + "-" + (count + 1));
}
count++;
}
}
}