Skip to content

Commit

Permalink
Added ability to remove serve events by stub metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
tomakehurst committed Sep 10, 2019
1 parent 5b6a366 commit e0c18ef
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ public FindServeEventsResult removeServeEventsMatching(RequestPattern requestPat
return wireMockApp.removeServeEventsMatching(requestPattern);
}

@Override
public FindServeEventsResult removeServeEventsForStubsMatchingMetadata(StringValuePattern metadataPattern) {
return wireMockApp.removeServeEventsForStubsMatchingMetadata(metadataPattern);
}

@Override
public void updateGlobalSettings(GlobalSettings newSettings) {
wireMockApp.updateGlobalSettings(newSettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private void initDefaultRoutes(Router router) {
router.add(GET, "/requests/{id}", GetServedStubTask.class);
router.add(DELETE, "/requests/{id}", RemoveServeEventTask.class);
router.add(POST, "/requests/remove", RemoveServeEventsByRequestPatternTask.class);
router.add(POST, "/requests/remove-by-metadata", RemoveServeEventsByStubMetadataTask.class);

router.add(POST, "/recordings/snapshot", SnapshotTask.class);
router.add(POST, "/recordings/start", StartRecordingTask.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.tomakehurst.wiremock.admin;

import com.github.tomakehurst.wiremock.admin.model.PathParams;
import com.github.tomakehurst.wiremock.common.Json;
import com.github.tomakehurst.wiremock.core.Admin;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.ResponseDefinition;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.github.tomakehurst.wiremock.verification.FindServeEventsResult;

public class RemoveServeEventsByStubMetadataTask implements AdminTask {

@Override
public ResponseDefinition execute(Admin admin, Request request, PathParams pathParams) {
StringValuePattern metadataPattern = Json.read(request.getBodyAsString(), StringValuePattern.class);
FindServeEventsResult findServeEventsResult = admin.removeServeEventsForStubsMatchingMetadata(metadataPattern);
return ResponseDefinition.okForJson(findServeEventsResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ public FindServeEventsResult removeServeEventsMatching(RequestPattern requestPat
return Json.read(body, FindServeEventsResult.class);
}

@Override
public FindServeEventsResult removeServeEventsForStubsMatchingMetadata(StringValuePattern metadataPattern) {
String body = postJsonAssertOkAndReturnBody(
urlFor(RemoveServeEventsByStubMetadataTask.class),
Json.write(metadataPattern));
return Json.read(body, FindServeEventsResult.class);
}

@Override
public FindNearMissesResult findNearMissesForUnmatchedRequests() {
String body = getJsonAssertOkAndReturnBody(urlFor(FindNearMissesForUnmatchedTask.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,14 @@ public static List<ServeEvent> removeServeEvents(RequestPatternBuilder requestPa
return defaultInstance.get().removeEvents(requestPatternBuilder);
}

public static List<ServeEvent> removeEventsByStubMetadata(StringValuePattern pattern) {
return defaultInstance.get().removeEventsByMetadata(pattern);
}

public List<ServeEvent> removeEventsByMetadata(StringValuePattern pattern) {
return admin.removeServeEventsForStubsMatchingMetadata(pattern).getServeEvents();
}

public static RequestPatternBuilder getRequestedFor(UrlPattern urlPattern) {
return new RequestPatternBuilder(RequestMethod.GET, urlPattern);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/github/tomakehurst/wiremock/core/Admin.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import com.github.tomakehurst.wiremock.recording.RecordSpecBuilder;
import com.github.tomakehurst.wiremock.recording.RecordingStatusResult;
import com.github.tomakehurst.wiremock.recording.SnapshotRecordResult;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.github.tomakehurst.wiremock.stubbing.StubImport;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.github.tomakehurst.wiremock.verification.*;

import java.util.List;
import java.util.UUID;

public interface Admin {
Expand All @@ -52,6 +54,7 @@ public interface Admin {

void removeServeEvent(UUID eventId);
FindServeEventsResult removeServeEventsMatching(RequestPattern requestPattern);
FindServeEventsResult removeServeEventsForStubsMatchingMetadata(StringValuePattern pattern);

FindNearMissesResult findTopNearMissesFor(LoggedRequest loggedRequest);
FindNearMissesResult findTopNearMissesFor(RequestPattern requestPattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ public FindServeEventsResult removeServeEventsMatching(RequestPattern requestPat
return new FindServeEventsResult(requestJournal.removeEventsMatching(requestPattern));
}

@Override
public FindServeEventsResult removeServeEventsForStubsMatchingMetadata(StringValuePattern metadataPattern) {
return new FindServeEventsResult(requestJournal.removeServeEventsForStubsMatchingMetadata(metadataPattern));
}

@Override
public FindNearMissesResult findNearMissesForUnmatchedRequests() {
ImmutableList.Builder<NearMiss> listBuilder = ImmutableList.builder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.tomakehurst.wiremock.verification;

import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.google.common.base.Optional;

Expand Down Expand Up @@ -60,4 +61,9 @@ public void removeEvent(UUID eventId) {
public List<ServeEvent> removeEventsMatching(RequestPattern requestPattern) {
throw new RequestJournalDisabledException();
}

@Override
public List<ServeEvent> removeServeEventsForStubsMatchingMetadata(StringValuePattern metadataPattern) {
throw new RequestJournalDisabledException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
*/
package com.github.tomakehurst.wiremock.verification;

import com.github.tomakehurst.wiremock.common.Json;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.github.tomakehurst.wiremock.stubbing.StubMapping;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
Expand Down Expand Up @@ -63,22 +66,27 @@ public void requestReceived(ServeEvent serveEvent) {

@Override
public void removeEvent(final UUID eventId) {
Iterable<ServeEvent> toDelete = filter(serveEvents, new Predicate<ServeEvent>() {
removeServeEvents(new Predicate<ServeEvent>() {
@Override
public boolean apply(ServeEvent input) {
return input.getId().equals(eventId);
}
});

for (ServeEvent event: toDelete) {
serveEvents.remove(event);
}
}

@Override
public List<ServeEvent> removeEventsMatching(RequestPattern requestPattern) {
return removeServeEvents(withRequstMatching(requestPattern));
}

@Override
public List<ServeEvent> removeServeEventsForStubsMatchingMetadata(StringValuePattern metadataPattern) {
return removeServeEvents(withStubMetadataMatching(metadataPattern));
}

private List<ServeEvent> removeServeEvents(Predicate<ServeEvent> predicate) {
List<ServeEvent> toDelete = FluentIterable.from(serveEvents)
.filter(withRequstMatching(requestPattern))
.filter(predicate)
.toList();

for (ServeEvent event: toDelete) {
Expand Down Expand Up @@ -124,4 +132,19 @@ private void removeOldEntries() {
}
}

private static Predicate<ServeEvent> withStubMetadataMatching(final StringValuePattern metadataPattern) {
return new Predicate<ServeEvent>() {
@Override
public boolean apply(ServeEvent serveEvent) {
StubMapping stub = serveEvent.getStubMapping();
if (stub != null) {
String metadataJson = Json.write(stub.getMetadata());
return metadataPattern.match(metadataJson).isExactMatch();
}

return false;
}
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.tomakehurst.wiremock.verification;

import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.matching.StringValuePattern;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.google.common.base.Optional;

Expand All @@ -36,4 +37,5 @@ public interface RequestJournal {

void removeEvent(UUID eventId);
List<ServeEvent> removeEventsMatching(RequestPattern requestPattern);
List<ServeEvent> removeServeEventsForStubsMatchingMetadata(StringValuePattern metadataPattern);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,28 @@
import com.github.tomakehurst.wiremock.http.HttpHeaders;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.*;
import com.github.tomakehurst.wiremock.matching.MatchResult;
import com.github.tomakehurst.wiremock.matching.RequestMatcher;
import com.github.tomakehurst.wiremock.matching.RequestMatcherExtension;
import com.github.tomakehurst.wiremock.matching.ValueMatcher;
import com.github.tomakehurst.wiremock.stubbing.ServeEvent;
import com.github.tomakehurst.wiremock.testsupport.WireMatchers;
import com.github.tomakehurst.wiremock.testsupport.WireMockTestClient;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import com.github.tomakehurst.wiremock.verification.RequestJournalDisabledException;
import com.google.common.base.Optional;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

import javax.swing.text.html.parser.Entity;
import java.util.List;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.lessThan;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.common.Metadata.metadata;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.github.tomakehurst.wiremock.matching.RequestPatternBuilder.forCustomMatcher;
Expand All @@ -52,7 +53,6 @@
import static org.apache.http.entity.ContentType.TEXT_PLAIN;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

@RunWith(Enclosed.class)
Expand Down Expand Up @@ -729,6 +729,24 @@ public void returnsEmptyListWhenNoEventsMatchedForRemoval() {
assertThat(serveEvents.size(), is(3));
}

@Test
public void removesEventsAssociatedWithStubsMatchingMetadata() {
stubFor(get("/with-metadata")
.withMetadata(metadata()
.list("tags", "delete-me")
));
stubFor(get("/without-metadata"));

testClient.get("/with-metadata");
testClient.get("/without-metadata");

removeEventsByStubMetadata(matchingJsonPath("$.tags[0]", equalTo("delete-me")));

List<ServeEvent> serveEvents = getAllServeEvents();
assertThat(serveEvents.size(), is(1));
assertThat(serveEvents.get(0).getRequest().getUrl(), is("/without-metadata"));
}

}

public static class JournalDisabled {
Expand Down

0 comments on commit e0c18ef

Please sign in to comment.