Skip to content

Commit

Permalink
Fix for #75 - preserve types when readinf from cats config files
Browse files Browse the repository at this point in the history
  • Loading branch information
en-milie committed Sep 12, 2023
1 parent 68ca800 commit 123fa8f
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 88 deletions.
36 changes: 12 additions & 24 deletions src/main/java/com/endava/cats/args/FilesArguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Singleton
public class FilesArguments {
private static final String ALL = "all";
private final PrettyLogger log = PrettyLoggerFactory.getLogger(this.getClass());
private Map<String, Map<String, String>> headers;
private Map<String, Map<String, String>> queryParams;
private Map<String, Map<String, String>> refData;
private Map<String, Map<String, Object>> headers;
private Map<String, Map<String, Object>> queryParams;
private Map<String, Map<String, Object>> refData;
private Map<String, Map<String, Object>> customFuzzerDetails = new HashMap<>();
private Map<String, Map<String, Object>> securityFuzzerDetails = new HashMap<>();

Expand All @@ -49,7 +48,7 @@ public class FilesArguments {
description = "Specifies the headers that will be passed along with the request. When supplied it will be applied to ALL paths. For per-path control use the `--headers` arg that requires a file.")
@Setter
@Getter
Map<String, String> headersMap;
Map<String, Object> headersMap;

@CommandLine.Option(names = {"--queryParams"},
description = "Specifies additional query parameters that will be passed along with request. This can be used to pass non-documented query params")
Expand Down Expand Up @@ -137,7 +136,7 @@ public void loadHeaders() throws IOException {
/*Merge headers from file with the ones supplied using the -H argument*/
if (headersMap != null) {
headers.merge(ALL, headersMap, (stringStringMap, stringStringMap2) -> {
Map<String, String> mergedMap = new HashMap<>(stringStringMap);
Map<String, Object> mergedMap = new HashMap<>(stringStringMap);
mergedMap.putAll(stringStringMap2);
return mergedMap;
});
Expand Down Expand Up @@ -178,7 +177,7 @@ public String replacePathWithUrlParams(String startingUrl) {
*
* @return a Map representation of the --headers file with paths being the Map keys
*/
public Map<String, String> getHeaders(String path) {
public Map<String, Object> getHeaders(String path) {
return getPathAndAll(headers, path);
}

Expand All @@ -190,7 +189,7 @@ public Map<String, String> getHeaders(String path) {
* @param currentPath the current API path
* @return a Map with the supplied --refData
*/
public Map<String, String> getRefData(String currentPath) {
public Map<String, Object> getRefData(String currentPath) {
return getPathAndAll(refData, currentPath);
}

Expand All @@ -201,7 +200,7 @@ public Map<String, String> getRefData(String currentPath) {
* @param path the given path
* @return a key-value map with all additional query params
*/
public Map<String, String> getAdditionalQueryParamsForPath(String path) {
public Map<String, Object> getAdditionalQueryParamsForPath(String path) {
return getPathAndAll(queryParams, path);
}

Expand All @@ -224,20 +223,19 @@ public Map<String, Map<String, Object>> getSecurityFuzzerDetails() {
}


static Map<String, String> getPathAndAll(Map<String, Map<String, String>> collection, String path) {
static Map<String, Object> getPathAndAll(Map<String, Map<String, Object>> collection, String path) {
return collection.entrySet().stream()
.filter(entry -> entry.getKey().equalsIgnoreCase(path) || entry.getKey().equalsIgnoreCase(ALL))
.map(Map.Entry::getValue).collect(HashMap::new, Map::putAll, Map::putAll);
}


private Map<String, Map<String, String>> loadFileAsMapOfMapsOfStrings(File file, String fileType) throws IOException {
Map<String, Map<String, String>> fromFile = new HashMap<>();
private Map<String, Map<String, Object>> loadFileAsMapOfMapsOfStrings(File file, String fileType) throws IOException {
Map<String, Map<String, Object>> fromFile = new HashMap<>();
if (file == null) {
log.debug("No {} file provided!", fileType);
} else {
log.config("{} file supplied {}", fileType, file.getAbsolutePath());
fromFile = this.loadYamlFileToMap(file.getAbsolutePath());
fromFile = this.parseYaml(file.getAbsolutePath());
log.debug("{} file loaded successfully: {}", fileType, fromFile);
}
return fromFile;
Expand All @@ -257,14 +255,4 @@ public Map<String, Map<String, Object>> parseYaml(String yaml) throws IOExceptio
}
return result;
}

public Map<String, Map<String, String>> loadYamlFileToMap(String filePath) throws IOException {
Map<String, Map<String, String>> result = new HashMap<>();
Map<String, Map<String, Object>> headersAsObject = this.parseYaml(filePath);
for (Map.Entry<String, Map<String, Object>> entry : headersAsObject.entrySet()) {
result.put(entry.getKey(), entry.getValue().entrySet()
.stream().collect(Collectors.toMap(Map.Entry::getKey, en -> String.valueOf(en.getValue()))));
}
return result;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/endava/cats/command/CatsCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
defaultValueProvider = CommandLine.PropertiesDefaultProvider.class,
exitCodeOnInvalidInput = 191,
exitCodeOnExecutionException = 192,
resourceBundle = "version",
// resourceBundle = "version",
subcommands = {
AutoComplete.GenerateCompletion.class,
CommandLine.HelpCommand.class,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/endava/cats/command/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class RunCommand implements Runnable, CommandLine.IExitCodeGenerator {

@CommandLine.Option(names = {"-H"},
description = "Specifies the headers that will be passed along with the request. When supplied it will be applied to ALL paths. For per-path control use the `--headers` arg that requires a file.")
Map<String, String> headersMap;
Map<String, Object> headersMap;

@CommandLine.Option(names = {"--createRefData"},
description = "This is only applicable when enabling the @|bold FunctionalFuzzer |@. It will instruct the @|bold FunctionalFuzzer|@ to create a @|bold,underline --refData|@ file " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void execute(FieldsIteratorExecutorContext context) {
Set<String> allFields = context.getFuzzingData().getAllFieldsByHttpMethod();
context.getLogger().debug("All fields: {}", allFields);
List<String> fieldsToBeRemoved = filesArguments.getRefData(context.getFuzzingData().getPath()).entrySet()
.stream().filter(entry -> entry.getValue().equalsIgnoreCase(CATS_REMOVE_FIELD)).map(Map.Entry::getKey).toList();
.stream().filter(entry -> String.valueOf(entry.getValue()).equalsIgnoreCase(CATS_REMOVE_FIELD)).map(Map.Entry::getKey).toList();
context.getLogger().config("The following fields marked as [{}] in refData will not be fuzzed: {}", CATS_REMOVE_FIELD, fieldsToBeRemoved);

fieldsToBeRemoved.forEach(allFields::remove);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.endava.cats.generator.simple.UnicodeGenerator;
import com.endava.cats.report.TestCaseListener;
import com.endava.cats.util.ConsoleUtils;
import com.endava.cats.util.WordUtils;
import io.github.ludovicianul.prettylogger.PrettyLogger;
import io.github.ludovicianul.prettylogger.PrettyLoggerFactory;

Expand Down Expand Up @@ -80,6 +81,7 @@ private List<String> replacePathVariables(String path) {
for (String variable : variables) {
String variableName = variable.substring(1, variable.length() - 1);
String value = Optional.ofNullable(filesArguments.getRefData(path).get(variableName))
.map(WordUtils::nullOrValueOf)
.orElse(filesArguments.getUrlParamsList()
.stream()
.filter(param -> param.startsWith(variableName + ":"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void fuzz(FuzzingData data) {
logger.debug("All fields {}", allFields);

List<String> fieldsToBeRemoved = filesArguments.getRefData(data.getPath()).entrySet()
.stream().filter(entry -> entry.getValue().equalsIgnoreCase(CATS_REMOVE_FIELD)).map(Map.Entry::getKey).toList();
.stream().filter(entry -> String.valueOf(entry.getValue()).equalsIgnoreCase(CATS_REMOVE_FIELD)).map(Map.Entry::getKey).toList();
logger.config("The following fields marked as [{}] in refData will not be fuzzed: {}", CATS_REMOVE_FIELD, fieldsToBeRemoved);

fieldsToBeRemoved.forEach(allFields::remove);
Expand Down
Loading

0 comments on commit 123fa8f

Please sign in to comment.