How to configure WireMock to load/save StubMappings to AWS S3?

1.4k Views Asked by At

I am trying to use WireMock's Record and Playback feature but with the difference that I want all the Stub Mappings to be saved to S3 and then load it back. I wrote a custom S3MappingSource that implements MappingsSource:

public class S3MappingSource implements MappingsSource {
  @Override
  public void save(StubMapping stubMapping) {
    storeToS3(stubMapping.getId().toString(), StubMapping.buildJsonStringFor(stubMapping));
  }

  @Override
  public void loadMappingsInto(StubMappings stubMappings) {
    var s3Objects = getAllFromS3();
    s3Objects.stream()
        .map(value -> StubMapping.buildFrom(value.getValueBytes().toStringUtf8()))
        .filter(Objects::nonNull)
        .forEach(stubMappings::addMapping);
  }

  @Override
  public void remove(StubMapping stubMapping) {
    throw new IllegalStateException("Not allowed");
  }

  @Override
  public void removeAll() {
    throw new IllegalStateException("Not allowed");
  }
}

Then I start the Wiremock server like this:

new WireMockServer(
    WireMockConfiguration.wireMockConfig()
        .port(mockConfiguration.getPort())
        .mappingSource(new S3MappingSource());

Even though mappings are getting stored in S3, and they are being read back, and everything is working right now, I don't feel that this is the right way. What is the recommended right way to do this?

0

There are 0 best solutions below