Skip to content

Commit

Permalink
Use BeforeRequestSent event for all event listeners (#30)
Browse files Browse the repository at this point in the history
- use BeforeRequestSent for all event listeners
- removed waits + delays in various tests
- fixed bug for deletion on non-existant/empty context

<!-- Please describe your pull request here. -->

## References

- TODO

<!-- References to relevant GitHub issues and pull requests, esp.
upstream and downstream changes -->

## Submitter checklist

- [ ] The PR request is well described and justified, including the body
and the references
- [ ] The PR title represents the desired changelog entry
- [ ] The repository's code style is followed (see the contributing
guide)
- [ ] Test coverage that demonstrates that the change works as expected
- [ ] For new features, there's necessary documentation in this pull
request or in a subsequent PR to
[wiremock.org](https://github.com/wiremock/wiremock.org)

<!--
Put an `x` into the [ ] to show you have filled the information.
The template comes from
https://github.com/wiremock/.github/blob/main/.github/pull_request_template.md
You can override it by creating .github/pull_request_template.md in your
own repository
-->
  • Loading branch information
dirkbolte authored Aug 4, 2023
1 parent cd64b79 commit 162e415
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.wiremock.extensions.state.internal.ResponseTemplateModel;
import org.wiremock.extensions.state.internal.StateExtensionMixin;

import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -61,7 +60,7 @@ public boolean applyGlobally() {
return false;
}

public void afterComplete(ServeEvent serveEvent, Parameters parameters) {
public void beforeResponseSent(ServeEvent serveEvent, Parameters parameters) {
var model = Map.of(
"request", RequestTemplateModel.from(serveEvent.getRequest()),
"response", ResponseTemplateModel.from(serveEvent.getResponse())
Expand All @@ -76,9 +75,13 @@ public void afterComplete(ServeEvent serveEvent, Parameters parameters) {

private void handleListDeletion(DeleteStateParameters.ListParameters listConfig, String contextName, Map<String, Object> model) {
if (Boolean.TRUE.equals(listConfig.getDeleteFirst())) {
contextManager.createOrUpdateContextList(contextName, LinkedList::removeFirst);
contextManager.createOrUpdateContextList(contextName, maps -> {
if (!maps.isEmpty()) maps.removeFirst();
});
} else if (Boolean.TRUE.equals(listConfig.getDeleteLast())) {
contextManager.createOrUpdateContextList(contextName, LinkedList::removeLast);
contextManager.createOrUpdateContextList(contextName, maps -> {
if (!maps.isEmpty()) maps.removeLast();
});
} else if (StringUtils.isNotBlank(listConfig.getDeleteIndex())) {
try {
var index = Integer.parseInt(renderTemplate(model, listConfig.getDeleteIndex()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public RecordStateEventListener(ContextManager contextManager, TemplateEngine te
this.templateEngine = templateEngine;
}

public void afterComplete(ServeEvent serveEvent, Parameters parameters) {
public void beforeResponseSent(ServeEvent serveEvent, Parameters parameters) {
var model = Map.of(
"request", RequestTemplateModel.from(serveEvent.getRequest()),
"response", ResponseTemplateModel.from(serveEvent.getResponse())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ void test_unknownContext_noOtherContext_ok() throws URISyntaxException {
getRequest("state", context);

await()
.pollDelay(Duration.ofSeconds(1))
.pollInterval(Duration.ofMillis(10))
.atMost(Duration.ofSeconds(5)).untilAsserted(() -> assertThat(contextManager.getContext(context)).isEmpty());
}
Expand All @@ -231,11 +230,9 @@ void test_unknownContext_otherContext_ok() throws URISyntaxException {
getRequest("state", context);

await()
.pollDelay(Duration.ofSeconds(1))
.pollInterval(Duration.ofMillis(10))
.atMost(Duration.ofSeconds(5)).untilAsserted(() -> assertThat(contextManager.getContext(context)).isEmpty());
await()
.pollDelay(Duration.ofSeconds(1))
.pollInterval(Duration.ofMillis(10))
.atMost(Duration.ofSeconds(5)).untilAsserted(() -> assertThat(contextManager.getContext(otherContext)).isPresent());
}
Expand Down Expand Up @@ -290,19 +287,21 @@ void test_unknownContext_noOtherContext_ok() throws URISyntaxException {
getRequest("list/deleteFirst", context);

await()
.pollDelay(Duration.ofSeconds(1))
.pollInterval(Duration.ofMillis(10))
.atMost(Duration.ofSeconds(5)).untilAsserted(() -> assertThat(contextManager.getContext(context)).isEmpty());
.atMost(Duration.ofSeconds(5))
.untilAsserted(() ->
assertThat(contextManager.getContext(context))
.isPresent()
.hasValueSatisfying(it -> assertThat(it.getList()).isEmpty())
);
}

@Test
void test_deleteFirst_ok() throws URISyntaxException {
var contextName = RandomStringUtils.randomAlphabetic(5);

postRequest("list", contextName, "one");
assertList(contextName, list -> assertThat(list).hasSize(1));
postRequest("list", contextName, "two");
assertList(contextName, list -> assertThat(list).hasSize(2));

getRequest("list/deleteFirst", contextName);

Expand All @@ -320,9 +319,7 @@ void test_deleteLast_ok() throws URISyntaxException {
var contextName = RandomStringUtils.randomAlphabetic(5);

postRequest("list", contextName, "one");
assertList(contextName, list -> assertThat(list).hasSize(1));
postRequest("list", contextName, "two");
assertList(contextName, list -> assertThat(list).hasSize(2));

getRequest("list/deleteLast", contextName);

Expand All @@ -340,11 +337,8 @@ void test_deleteIndex_middle_ok() throws URISyntaxException {
var contextName = RandomStringUtils.randomAlphabetic(5);

postRequest("list", contextName, "one");
assertContextNumUpdates(contextName, 1);
postRequest("list", contextName, "two");
assertContextNumUpdates(contextName, 2);
postRequest("list", contextName, "three");
assertContextNumUpdates(contextName, 3);
assertList(contextName, list -> assertThat(list).hasSize(3));

getRequest("list/deleteIndex/1", contextName);
Expand All @@ -363,11 +357,8 @@ void test_deleteIndex_last_ok() throws URISyntaxException {
var contextName = RandomStringUtils.randomAlphabetic(5);

postRequest("list", contextName, "one");
assertContextNumUpdates(contextName, 1);
postRequest("list", contextName, "two");
assertContextNumUpdates(contextName, 2);
postRequest("list", contextName, "three");
assertContextNumUpdates(contextName, 3);
assertList(contextName, list -> assertThat(list).hasSize(3));

getRequest("list/deleteIndex/2", contextName);
Expand All @@ -386,11 +377,8 @@ void test_deleteWhere_middle_ok() throws URISyntaxException {
var contextName = RandomStringUtils.randomAlphabetic(5);

postRequest("list", contextName, "one");
assertContextNumUpdates(contextName, 1);
postRequest("list", contextName, "two");
assertContextNumUpdates(contextName, 2);
postRequest("list", contextName, "three");
assertContextNumUpdates(contextName, 3);
assertList(contextName, list -> assertThat(list).hasSize(3));

getRequest("list/deleteWhere/two", contextName);
Expand All @@ -409,11 +397,8 @@ void test_deleteWhere_last_ok() throws URISyntaxException {
var contextName = RandomStringUtils.randomAlphabetic(5);

postRequest("list", contextName, "one");
assertContextNumUpdates(contextName, 1);
postRequest("list", contextName, "two");
assertContextNumUpdates(contextName, 2);
postRequest("list", contextName, "three");
assertContextNumUpdates(contextName, 3);
assertList(contextName, list -> assertThat(list).hasSize(3));

getRequest("list/deleteWhere/three", contextName);
Expand All @@ -429,7 +414,6 @@ void test_deleteWhere_last_ok() throws URISyntaxException {

private void assertList(String contextName, Consumer<LinkedList<Map<String, String>>> consumer) {
await()
.pollDelay(Duration.ofMillis(10))
.pollInterval(Duration.ofMillis(10))
.atMost(ofSeconds(5))
.untilAsserted(() ->
Expand Down

0 comments on commit 162e415

Please sign in to comment.