Can we customize mapping file names in Wiremock?

1k Views Asked by At

I am recording the application through Wiremock using JAVA DSL, Do we have the option to customize the mapping file names? instead of getting the filename which is generated from wiremock..

Example: searchpanel_arrivalairport_th-72f9b8b7-076f-4102-b6a8-aa38710fde1b.json (Generated form wiremock using java ) I am expecting the above file name with my desired naming convention like

seacrpanel_airport_LGW.json

2

There are 2 best solutions below

0
On

Custom filenames can be added by customizing StubMappingJsonRecorder.

I added CustomStubMappingJsonRecorder and override writeToMappingAndBodyFile method.

 if(fileName!=null && !fileName.equals("")){
      mappingFileName=fileName+"-mapping.json";
      bodyFileName=fileName+"-body.json";
    }else {
      mappingFileName = UniqueFilenameGenerator.generate(request.getUrl(), 
      "mapping", filed);
       bodyFileName = UniqueFilenameGenerator.generate(request.getUrl(), "body", 
         fileId, ContentTypes.determineFileExtension(request.getUrl(), 
         response.getHeaders().getContentTypeHeader(), body));
        }
0
On

There's no easy way to do this at the moment. It is however possible. As @santhiya-ps says you need to write your own implementation of RequestListener, probably using StubMappingJsonRecorder as a template.

You can't extend it and override writeToMappingAndBodyFile as that method is private, but that is the method you probably want to change.

import com.github.tomakehurst.wiremock.common.*;
import com.github.tomakehurst.wiremock.core.*;
import com.github.tomakehurst.wiremock.http.*;
import java.util.List;
import static com.github.tomakehurst.wiremock.core.WireMockApp.*;

class NameTemplateStubMappingJsonRecorder implements RequestListener {

  private final FileSource mappingsFileSource;
  private final FileSource filesFileSource;
  private final Admin admin;
  private final List<CaseInsensitiveKey> headersToMatch;
  private final IdGenerator idGenerator = new VeryShortIdGenerator();

  public NameTemplateStubMappingJsonRecorder(Admin admin) {
    this.mappingsFileSource = admin.getOptions().filesRoot().child(MAPPINGS_ROOT);
    this.filesFileSource = admin.getOptions().filesRoot().child(FILES_ROOT);
    this.admin = admin;
    this.headersToMatch = admin.getOptions().matchingHeaders();
  }

  @Override
  public void requestReceived(Request request, Response response) {
    // TODO copy StubMappingJsonRecorder changing as required...
  }
}

You can then register your RequestListener as so:

WireMockServer wireMockServer = new WireMockServer();

wireMockServer.addMockServiceRequestListener(
  new NameTemplateStubMappingJsonRecorder(wireMockServer)
);

wireMockServer.start();

So long as you still store the mapping files in the expected directory (stored in FileSource mappingsFileSource above, which will be ${rootDir}/mappings, where rootDir is configured as explained in Configuration - File Locations) they should be loaded successfully as all files with extension json in that dir are loaded as mappings.

It would be much easier if StubMappingJsonRecorder took a strategy for generating these names - it might be worth creating an issue on the WireMock repo asking for an easier way to do this. I'd suggest getting an agreement on a basic design before raising a PR though.