我正在尝试使用 WireMock 的记录和播放功能,但不同之处在于我希望将所有存根映射保存到 S3,然后将其加载回来。我编写了一个实现 MappingsSource 的自定义 S3MappingSource:
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");
}
}
然后我像这样启动 Wiremock 服务器:
new WireMockServer(
WireMockConfiguration.wireMockConfig()
.port(mockConfiguration.getPort())
.mappingSource(new S3MappingSource());
尽管映射被存储在 S3 中,并且正在被回读,并且现在一切正常,但我认为这不是正确的方法。推荐的正确方法是什么?