Skip to content

Commit

Permalink
fix #32: if wiremockFilesDir contains other files than mapping files …
Browse files Browse the repository at this point in the history
…it fails
  • Loading branch information
PeterPaul-Perfana committed Jun 29, 2023
1 parent 44d193a commit 034347f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Properties:
* `wiremockFilesDir` the directory where to find the wiremock files
* `wiremockUrl` the wiremock urls, comma separated, including api endpoint, e.g. `http://localhost:9999/__admin/mappings` or `http://localhost:9999/__admin/settings`
* `useProxy` on port 8888, for example to use with fiddler
* `continueOnUploadError` if true, continue uploading other files if an upload error occurs, default is true

Custom events:
* `wiremock-change-mappings` use to change delay of wiremock mapping file at specific time
Expand All @@ -15,6 +16,8 @@ Custom events:
Use the correct type of file for each event. For import use the exported file of wiremock studio
that contains multiple mappings.

All files should

Example wiremock response with a dynamic delay:

```json
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/io/perfana/event/wiremock/WiremockClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void uploadFileWithReplacements(String fileContents, Map<String, String> replace
HttpPost httpPost = new HttpPost(uriBuilder.build());
String replaced = injectReplacements(fileContents, replacements);

logger.debug("About to send to " + uriPath + ": " + replaced);
logger.debug("About to send to " + uriPath + ": " + reduceLength(replaced, 2048));

StringEntity data = new StringEntity(replaced, CHARSET_UTF8);

Expand All @@ -118,6 +118,13 @@ void uploadFileWithReplacements(String fileContents, Map<String, String> replace
}
}

private static String reduceLength(String text, int maxLength) {
if (text.length() > maxLength) {
return text.substring(0, maxLength) + "...";
}
return text;
}

private String injectReplacements(String fileContents, Map<String, String> replacements) {
return replacements.entrySet().stream()
.map(token -> (Function<String, String>) s -> replaceAllTokensInString(s, token))
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/io/perfana/event/wiremock/WiremockEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -75,15 +74,29 @@ private void importAllWiremockFiles(WiremockClient client, File[] files, Map<Str
.peek(file -> logger.info("check " + file))
.filter(file -> !file.isDirectory())
.filter(File::canRead)
.filter(file -> file.getName().endsWith(".json"))
.peek(file -> logger.info("import " + file))
.map(this::readContents)
.filter(Objects::nonNull)
.forEach(fileContents -> client.uploadFileWithReplacements(fileContents, replacements, uriPath));
.forEach(fileContents -> uploadWithTryCatch(client, replacements, uriPath, fileContents));
}

private void uploadWithTryCatch(WiremockClient client, Map<String, String> replacements, String uriPath, String fileContents) {
try {
client.uploadFileWithReplacements(fileContents, replacements, uriPath);
} catch (Exception e) {
if (eventContext.isContinueOnUploadError()) {
logger.error("Error uploading file: " + e.getMessage());
} else {
logger.error("Error uploading file: " + e.getMessage());
throw e;
}
}
}

private String readContents(File file) {
try {
return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
return Files.readString(file.toPath());
} catch (IOException e) {
logger.error("reading file: " + file);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class WiremockEventConfig extends EventConfig {
private String wiremockFilesDir;
private String wiremockUrl;
private boolean useProxy = false;
private boolean continueOnUploadError = true;

public void setWiremockFilesDir(String wiremockFilesDir) {
this.wiremockFilesDir = wiremockFilesDir;
Expand All @@ -34,9 +35,13 @@ public void setUseProxy(boolean useProxy) {
this.useProxy = useProxy;
}

public void setContinueOnUploadError(boolean continueOnUploadError) {
this.continueOnUploadError = continueOnUploadError;
}

@Override
public WiremockEventContext toContext() {
return new WiremockEventContext(super.toContext(), wiremockFilesDir, wiremockUrl, useProxy);
return new WiremockEventContext(super.toContext(), wiremockFilesDir, wiremockUrl, useProxy, continueOnUploadError);
}

@Override
Expand All @@ -45,6 +50,7 @@ public String toString() {
"wiremockFilesDir='" + wiremockFilesDir + '\'' +
", wiremockUrl='" + wiremockUrl + '\'' +
", useProxy=" + useProxy +
", continueOnUploadError=" + continueOnUploadError +
"} " + super.toString();
}
}
35 changes: 33 additions & 2 deletions src/main/java/io/perfana/event/wiremock/WiremockEventContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ public class WiremockEventContext extends EventContext {
private final String wiremockFilesDir;
private final String wiremockUrl;
private final boolean useProxy;
private final boolean continueOnUploadError;

protected WiremockEventContext(EventContext context, String wiremockFilesDir, String wiremockUrl, boolean useProxy) {
super(context, WiremockEventFactory.class.getName(), false);
protected WiremockEventContext(EventContext context, String wiremockFilesDir, String wiremockUrl, boolean useProxy, boolean continueOnUploadError) {
super(context, WiremockEventFactory.class.getName());
this.wiremockFilesDir = wiremockFilesDir;
this.wiremockUrl = wiremockUrl;
this.useProxy = useProxy;
this.continueOnUploadError = continueOnUploadError;
}

public String getWiremockFilesDir() {
Expand All @@ -41,12 +43,41 @@ public boolean isUseProxy() {
return useProxy;
}

public boolean isContinueOnUploadError() {
return continueOnUploadError;
}

@Override
public String toString() {
return "WiremockEventConfig{" +
"wiremockFilesDir='" + wiremockFilesDir + '\'' +
", wiremockUrl='" + wiremockUrl + '\'' +
", useProxy=" + useProxy +
", continueOnUploadError=" + continueOnUploadError +
"} " + super.toString();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;

WiremockEventContext that = (WiremockEventContext) o;

if (useProxy != that.useProxy) return false;
if (continueOnUploadError != that.continueOnUploadError) return false;
if (!wiremockFilesDir.equals(that.wiremockFilesDir)) return false;
return wiremockUrl.equals(that.wiremockUrl);
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + wiremockFilesDir.hashCode();
result = 31 * result + wiremockUrl.hashCode();
result = 31 * result + (useProxy ? 1 : 0);
result = 31 * result + (continueOnUploadError ? 1 : 0);
return result;
}
}

0 comments on commit 034347f

Please sign in to comment.