use of com.endava.cats.model.generator.PayloadGenerator in project cats by Endava.
the class PayloadGeneratorTest method setupPayloadGenerator.
private PayloadGenerator setupPayloadGenerator() throws IOException {
OpenAPIParser openAPIV3Parser = new OpenAPIParser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);
OpenAPI openAPI = openAPIV3Parser.readContents(new String(Files.readAllBytes(Paths.get("src/test/resources/petstore.yml"))), null, options).getOpenAPI();
Map<String, Schema> schemas = OpenApiUtils.getSchemas(openAPI, "application/json");
globalContext.getSchemaMap().putAll(schemas);
return new PayloadGenerator(globalContext, true);
}
use of com.endava.cats.model.generator.PayloadGenerator in project cats by Endava.
the class PayloadGeneratorTest method shouldRecognizeUrlFromPropertyFormat.
@ParameterizedTest
@CsvSource({ "url,true", "uri,true", "addressurl,false", "addressuri,false", "not,false" })
void shouldRecognizeUrlFromPropertyFormat(String property, boolean expected) {
StringSchema schema = new StringSchema();
schema.setFormat(property);
PayloadGenerator generator = new PayloadGenerator(globalContext, true);
boolean isIp = generator.isURI(schema, "other");
Assertions.assertThat(isIp).isEqualTo(expected);
}
use of com.endava.cats.model.generator.PayloadGenerator in project cats by Endava.
the class FuzzingDataFactory method getResponsePayloads.
/**
* We need to get JSON structural samples for each response code documented into the contract. This includes ONE_OF or ANY_OF combinations.
*
* @param operation the current OpenAPI operation
* @param responseCodes the list of response codes associated to the current Operation
* @return a list if response payloads associated to each response code
*/
private Map<String, List<String>> getResponsePayloads(Operation operation, Set<String> responseCodes) {
Map<String, List<String>> responses = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
PayloadGenerator generator = new PayloadGenerator(globalContext, processingArguments.isUseExamples());
for (String responseCode : responseCodes) {
String responseSchemaRef = this.extractResponseSchemaRef(operation, responseCode);
if (responseSchemaRef != null) {
String respSchemaName = this.getSchemaName(responseSchemaRef);
List<String> samples = this.generateSample(respSchemaName, generator);
responses.put(responseCode, samples);
} else {
responses.put(responseCode, Collections.emptyList());
}
}
return responses;
}
use of com.endava.cats.model.generator.PayloadGenerator in project cats by Endava.
the class FuzzingDataFactory method getRequestPayloadsSamples.
private List<String> getRequestPayloadsSamples(MediaType mediaType, String reqSchemaName) {
PayloadGenerator generator = new PayloadGenerator(globalContext, processingArguments.isUseExamples());
List<String> result = this.generateSample(reqSchemaName, generator);
if (mediaType != null && mediaType.getSchema() instanceof ArraySchema) {
/*when dealing with ArraySchemas we make sure we have 2 elements in the array*/
result = result.stream().map(payload -> "[" + payload + "," + payload + "]").collect(Collectors.toList());
}
return result;
}
use of com.endava.cats.model.generator.PayloadGenerator in project cats by Endava.
the class PayloadGeneratorTest method shouldGenerateOneOfWhenOneOfInRoot.
@Test
void shouldGenerateOneOfWhenOneOfInRoot() throws Exception {
PayloadGenerator generator = setupPayloadGenerator();
List<Map<String, String>> example = generator.generate(null, "PetType");
String exampleJson = example.get(0).get("example");
Assertions.assertThat(exampleJson).contains("PetTypeONE_OF#/components/schemas/Husky").contains("PetTypeONE_OF#/components/schemas/Labrador");
}
Aggregations