Skip to content

Commit 61e3954

Browse files
authored
fix: http mock redefinitions always working (#122)
Circular map for registry is now properly handled by removing and adding back redefinitions of existing mocks to keep clientId in registry
1 parent 4587644 commit 61e3954

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

mockfaster/src/main/java/com/decathlon/tzatziki/utils/MockFaster.java

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.apache.commons.lang3.tuple.Pair;
1616
import org.jetbrains.annotations.NotNull;
1717
import org.junit.Assert;
18+
import org.mockserver.closurecallback.websocketregistry.LocalCallbackRegistry;
1819
import org.mockserver.collections.CircularPriorityQueue;
1920
import org.mockserver.integration.ClientAndServer;
2021
import org.mockserver.matchers.*;
@@ -37,7 +38,6 @@
3738
import java.util.regex.Pattern;
3839

3940
import static com.decathlon.tzatziki.utils.Fields.getValue;
40-
import static com.decathlon.tzatziki.utils.Mapper.toYaml;
4141
import static com.decathlon.tzatziki.utils.Unchecked.unchecked;
4242
import static java.util.function.Function.identity;
4343
import static java.util.stream.Collectors.*;
@@ -78,29 +78,6 @@ protected HttpState initialize() throws ConcurrentException {
7878
public static synchronized void add_mock(HttpRequest httpRequest, ExpectationResponseCallback callback, Comparison comparison) {
7979
HttpState httpState = unchecked(HTTP_STATE::get);
8080
CircularPriorityQueue<String, HttpRequestMatcher, SortableExpectationId> expectationsQueue = Fields.getValue(httpState.getRequestMatchers(), "httpRequestMatchers");
81-
httpState.getRequestMatchers().retrieveActiveExpectations(httpRequest)
82-
// we are redefining an old mock with a matches, let's see if we have old callbacks still in 404
83-
.stream()
84-
.filter(expectation -> {
85-
Pair<Expectation[], UpdatableExpectationResponseCallback> updatableExpectationResponseCallbackPair = MOCKS.get(expectation.getHttpRequest().toString());
86-
if (updatableExpectationResponseCallbackPair == null) {
87-
log.error("""
88-
couldn't find the httpRequest in the mocks, this shouldn't happen!
89-
90-
httpRequest: {}
91-
92-
MOCKS keys: {}
93-
""", toYaml(httpRequest.toString()), toYaml(MOCKS.keySet()));
94-
return false;
95-
}
96-
return updatableExpectationResponseCallbackPair.getValue().callback.equals(NOT_FOUND);
97-
})
98-
.forEach(expectation -> {
99-
expectationsQueue.remove(expectationsQueue.getByKey(expectation.getId()).orElseThrow(() ->
100-
new IllegalStateException("couldn't find the old expectation in the queue for removal")));
101-
MOCKS.remove(expectation.getHttpRequest().toString());
102-
log.debug("removing expectation {}", expectation.getHttpRequest());
103-
});
10481

10582
AtomicBoolean isNew = new AtomicBoolean(false);
10683
latestPriority++;
@@ -127,7 +104,14 @@ public static synchronized void add_mock(HttpRequest httpRequest, ExpectationRes
127104
// update the priority of the expectation
128105
.map(expectation -> expectation.withPriority(latestPriority))
129106
// re-add the expectations, this will resort the CircularPriorityQueue
130-
.forEach(expectation -> httpState.getRequestMatchers().add(expectation, MockServerMatcherNotifier.Cause.API));
107+
.forEach(expectation -> {
108+
httpState.getRequestMatchers().add(expectation, MockServerMatcherNotifier.Cause.API);
109+
110+
String clientId = expectation.getHttpResponseObjectCallback().getClientId();
111+
Map<String, ExpectationResponseCallback> responseCallbackRegistry = LocalCallbackRegistry.responseCallbackRegistry();
112+
responseCallbackRegistry.remove(clientId);
113+
responseCallbackRegistry.put(clientId, expectationWithCallback.getValue());
114+
});
131115
}
132116

133117
if (httpRequest.getPath() instanceof NottableSchemaString uriSchema) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.decathlon.tzatziki.steps;
2+
3+
import io.cucumber.java.Before;
4+
import lombok.RequiredArgsConstructor;
5+
6+
@RequiredArgsConstructor
7+
public class LimitWebsocketsSteps {
8+
@Before(order = -2)
9+
public void before() {
10+
System.setProperty("mockserver.maxWebSocketExpectations", "200");
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
11
package com.decathlon.tzatziki.steps;
22

3+
import com.decathlon.tzatziki.utils.Comparison;
4+
import com.decathlon.tzatziki.utils.Guard;
35
import com.decathlon.tzatziki.utils.Interaction;
46
import io.cucumber.java.Before;
7+
import io.cucumber.java.en.Given;
8+
import lombok.RequiredArgsConstructor;
59

10+
import java.util.stream.IntStream;
11+
12+
@RequiredArgsConstructor
613
public class LocalSteps {
14+
private final HttpSteps httpSteps;
715

816
static {
917
Interaction.printResponses = true;
1018
}
1119

1220
@Before
1321
public void before() {}
22+
23+
@Given("^we add (\\d+)-(\\d+) mocks for id endpoint$")
24+
public void mockIdEndpointAsSeveralMocks(int startId, int endId) {
25+
IntStream.range(startId, endId + 1).forEach(idx -> httpSteps.url_is_mocked_as(Guard.always(), "http://backend/" + idx, Comparison.CONTAINS, """
26+
request:
27+
method: GET
28+
response:
29+
headers:
30+
Content-Type: application/json
31+
body:
32+
payload: Hello %d
33+
""".formatted(idx)));
34+
}
1435
}

tzatziki-http/src/test/resources/com/decathlon/tzatziki/steps/http.feature

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1203,4 +1203,16 @@ Feature: to interact with an http service and setup mocks
12031203
"""
12041204
<?xml version="1.0" encoding="utf-8"?><ns:user xmlns:ns="http://www.namespace.com">bob</ns:user>
12051205
"""
1206-
Then we received a status OK_200
1206+
Then we received a status OK_200
1207+
1208+
Scenario Template: Exceed max amount of expectation
1209+
Given we add 1-1 mocks for id endpoint
1210+
Given we add <mocksRange> mocks for id endpoint
1211+
Then getting on "http://backend/1" returns:
1212+
"""
1213+
Hello 1
1214+
"""
1215+
Examples:
1216+
| mocksRange |
1217+
| 2-150 |
1218+
| 151-250 |

0 commit comments

Comments
 (0)