Skip to content

Commit

Permalink
Add more debug logs when creating payload examples
Browse files Browse the repository at this point in the history
  • Loading branch information
en-milie committed Apr 1, 2022
1 parent 6d3f387 commit b5cb414
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public List<Map<String, String>> generate(List<String> mediaTypes, String modelN
}

private <T> Object resolvePropertyToExample(String propertyName, String mediaType, Schema<T> property) {
LOGGER.trace("Resolving example for property {}...", property);
if (property.getExample() != null && canUseExamples(property)) {
LOGGER.trace("Example set in swagger spec, returning example: '{}'", property.getExample());
return property.getExample();
Expand Down Expand Up @@ -170,7 +169,7 @@ private <T> Object getExampleForObjectSchema(Schema<T> property) {
}

private Object getExampleFromStringSchema(String propertyName, Schema<String> property) {
LOGGER.trace("String property");
LOGGER.trace("String property {}", propertyName);

String defaultValue = property.getDefault();
if (StringUtils.isNotBlank(defaultValue)) {
Expand Down Expand Up @@ -296,6 +295,7 @@ private Object resolveModelToExample(String name, String mediaType, Schema schem
LOGGER.trace("Resolving model '{}' to example", name);

if (schema.getProperties() != null) {
LOGGER.trace("Schema properties not null {}: {}", name, schema.getProperties().keySet());
this.processSchemaProperties(name, mediaType, schema, values);
}
if (schema instanceof ComposedSchema) {
Expand All @@ -308,7 +308,7 @@ private Object resolveModelToExample(String name, String mediaType, Schema schem
}

private void processSchemaProperties(String name, String mediaType, Schema schema, Map<String, Object> values) {
LOGGER.trace("Creating example from model values");
LOGGER.trace("Creating example from model values {}", name);
if (schema.getDiscriminator() != null) {
globalContext.getDiscriminators().add(currentProperty + "#" + schema.getDiscriminator().getPropertyName());
}
Expand Down Expand Up @@ -339,6 +339,7 @@ private void parseFromInnerSchema(String name, String mediaType, Schema schema,
values.put(propertyName.toString(), innerSchema.getEnum().stream().filter(value -> name.contains(value.toString())).findFirst().orElse(""));
globalContext.getRequestDataTypes().put(currentProperty, innerSchema);
} else {
LOGGER.trace("Resolving {}", propertyName);
Object example = this.resolvePropertyToExample(propertyName.toString(), mediaType, innerSchema);
values.put(propertyName.toString(), example);
globalContext.getRequestDataTypes().put(currentProperty, innerSchema);
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/com/endava/cats/args/FilterArgumentsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ void setup() {
@ParameterizedTest
@CsvSource({"checkHeaders,CheckSecurityHeadersFuzzer,RemoveFieldsFuzzer",
"checkFields,RemoveFieldsFuzzer,CheckSecurityHeadersFuzzer",
"checkHttp,HappyFuzzer,CheckSecurityHeadersFuzzer",
"checkContract,TopLevelElementsContractInfoFuzzer,CheckSecurityHeadersFuzzer"})
"checkHttp,HappyFuzzer,CheckSecurityHeadersFuzzer"})
void shouldReturnCheckHeadersFuzzers(String argument, String matching, String notMatching) {
ReflectionTestUtils.setField(checkArguments, argument, true);

Expand All @@ -58,7 +57,8 @@ void shouldReturnCheckHeadersFuzzers(String argument, String matching, String no
@ParameterizedTest
@CsvSource({"includeControlChars,LeadingControlCharsInHeadersFuzzer,LeadingWhitespacesInHeadersFuzzer",
"includeEmojis,LeadingMultiCodePointEmojisInFieldsTrimValidateFuzzer,LeadingControlCharsInHeadersFuzzer",
"includeWhitespaces,LeadingWhitespacesInFieldsTrimValidateFuzzer,LeadingControlCharsInHeadersFuzzer"})
"includeWhitespaces,LeadingWhitespacesInFieldsTrimValidateFuzzer,LeadingControlCharsInHeadersFuzzer",
"includeContract,TopLevelElementsContractInfoFuzzer,LeadingWhitespacesInHeadersFuzzer"})
void shouldIncludeLengthyFuzzers(String argument, String matching, String notMatching) {
ReflectionTestUtils.setField(checkArguments, argument, true);

Expand All @@ -72,6 +72,7 @@ void shouldIncludeAllFuzzers() {
ReflectionTestUtils.setField(checkArguments, "includeControlChars", true);
ReflectionTestUtils.setField(checkArguments, "includeEmojis", true);
ReflectionTestUtils.setField(checkArguments, "includeWhitespaces", true);
ReflectionTestUtils.setField(checkArguments, "includeContract", true);
List<String> fuzzers = filterArguments.getFuzzersForPath();

Assertions.assertThat(fuzzers).contains("LeadingControlCharsInHeadersFuzzer", "LeadingWhitespacesInHeadersFuzzer", "LeadingMultiCodePointEmojisInFieldsTrimValidateFuzzer"
Expand All @@ -82,7 +83,7 @@ void shouldIncludeAllFuzzers() {
void shouldReturnAllFuzzersWhenNoCheckSupplied() {
List<String> fuzzers = filterArguments.getFuzzersForPath();

Assertions.assertThat(fuzzers).contains("TopLevelElementsContractInfoFuzzer", "CheckSecurityHeadersFuzzer", "HappyFuzzer", "RemoveFieldsFuzzer");
Assertions.assertThat(fuzzers).contains("CheckSecurityHeadersFuzzer", "HappyFuzzer", "RemoveFieldsFuzzer");
}

@Test
Expand Down Expand Up @@ -110,7 +111,7 @@ void shouldRemoveSkippedFuzzers() {
ReflectionTestUtils.setField(filterArguments, "skipFuzzers", List.of("VeryLarge", "SecurityHeaders", "Jumbo"));
List<String> fuzzers = filterArguments.getFuzzersForPath();

Assertions.assertThat(fuzzers).contains("TopLevelElementsContractInfoFuzzer", "HappyFuzzer", "RemoveFieldsFuzzer")
Assertions.assertThat(fuzzers).contains("HappyFuzzer", "RemoveFieldsFuzzer")
.doesNotContain("CheckSecurityHeadersFuzzer", "VeryLargeValuesInFieldsFuzzer", "Jumbo");

}
Expand Down

0 comments on commit b5cb414

Please sign in to comment.