Search in sources :

Example 6 with PayloadGenerator

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);
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) Schema(io.swagger.v3.oas.models.media.Schema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIParser(io.swagger.parser.OpenAPIParser) PayloadGenerator(com.endava.cats.model.generator.PayloadGenerator) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 7 with PayloadGenerator

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);
}
Also used : StringSchema(io.swagger.v3.oas.models.media.StringSchema) PayloadGenerator(com.endava.cats.model.generator.PayloadGenerator) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with PayloadGenerator

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;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) PayloadGenerator(com.endava.cats.model.generator.PayloadGenerator) TreeMap(java.util.TreeMap)

Example 9 with PayloadGenerator

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;
}
Also used : ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) PayloadGenerator(com.endava.cats.model.generator.PayloadGenerator)

Example 10 with PayloadGenerator

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");
}
Also used : PayloadGenerator(com.endava.cats.model.generator.PayloadGenerator) Map(java.util.Map) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

PayloadGenerator (com.endava.cats.model.generator.PayloadGenerator)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)11 StringSchema (io.swagger.v3.oas.models.media.StringSchema)8 QuarkusTest (io.quarkus.test.junit.QuarkusTest)6 Test (org.junit.jupiter.api.Test)6 CsvSource (org.junit.jupiter.params.provider.CsvSource)5 Map (java.util.Map)3 OpenAPIParser (io.swagger.parser.OpenAPIParser)1 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)1 Schema (io.swagger.v3.oas.models.media.Schema)1 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1