Search in sources :

Example 1 with FuzzingResult

use of com.endava.cats.model.FuzzingResult in project cats by Endava.

the class CatsUtilTest method shouldReturnEmptyFuzzingResultWhenEmptyJson.

@Test
void shouldReturnEmptyFuzzingResultWhenEmptyJson() {
    CatsUtil catsUtil = new CatsUtil(new CatsDSLParser());
    FuzzingStrategy strategy = FuzzingStrategy.replace().withData("fuzzed");
    FuzzingResult result = catsUtil.replaceField("", "test", strategy);
    Assertions.assertThat(result.getFuzzedValue()).isEmpty();
    Assertions.assertThat(result.getJson()).isEmpty();
}
Also used : FuzzingStrategy(com.endava.cats.model.FuzzingStrategy) FuzzingResult(com.endava.cats.model.FuzzingResult) CatsDSLParser(com.endava.cats.dsl.CatsDSLParser) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with FuzzingResult

use of com.endava.cats.model.FuzzingResult in project cats by Endava.

the class CatsUtil method replaceField.

public FuzzingResult replaceField(String payload, String jsonPropertyForReplacement, FuzzingStrategy fuzzingStrategyToApply, boolean mergeFuzzing) {
    if (StringUtils.isNotBlank(payload)) {
        String jsonPropToGetValue = jsonPropertyForReplacement;
        if (JsonUtils.isJsonArray(payload)) {
            jsonPropToGetValue = JsonUtils.FIRST_ELEMENT_FROM_ROOT_ARRAY + jsonPropertyForReplacement;
            jsonPropertyForReplacement = JsonUtils.ALL_ELEMENTS_ROOT_ARRAY + jsonPropertyForReplacement;
        }
        DocumentContext context = JsonPath.parse(payload);
        Object oldValue = context.read(JsonUtils.sanitizeToJsonPath(jsonPropToGetValue));
        String valueToSet = fuzzingStrategyToApply.process(oldValue);
        if (mergeFuzzing) {
            valueToSet = FuzzingStrategy.mergeFuzzing(this.nullOrValueOf(oldValue), fuzzingStrategyToApply.getData());
        }
        context.set(JsonUtils.sanitizeToJsonPath(jsonPropertyForReplacement), valueToSet);
        return new FuzzingResult(context.jsonString(), valueToSet);
    }
    return FuzzingResult.empty();
}
Also used : FuzzingResult(com.endava.cats.model.FuzzingResult) DocumentContext(com.jayway.jsonpath.DocumentContext)

Example 3 with FuzzingResult

use of com.endava.cats.model.FuzzingResult in project cats by Endava.

the class BaseFieldsFuzzerTest method createFuzzingData.

@NotNull
private FuzzingData createFuzzingData() {
    FuzzingResult fuzzingResult = Mockito.mock(FuzzingResult.class);
    Mockito.when(fuzzingResult.getJson()).thenReturn("{}");
    FuzzingData data = Mockito.mock(FuzzingData.class);
    Set<String> fields = Collections.singleton("field");
    Map<String, Schema> schemaMap = new HashMap<>();
    schemaMap.put("field", new StringSchema());
    Mockito.when(data.getAllFieldsByHttpMethod()).thenReturn(fields);
    Mockito.when(data.getRequestPropertyTypes()).thenReturn(schemaMap);
    Mockito.when(data.getPayload()).thenReturn("{\"field\": 2}");
    CatsUtil mockCatsUtil = Mockito.mock(CatsUtil.class);
    Mockito.when(mockCatsUtil.replaceField(Mockito.eq("{\"field\": 2}"), Mockito.eq("field"), Mockito.any())).thenReturn(fuzzingResult);
    baseFieldsFuzzer = new MyBaseFieldsFuzzer(serviceCaller, testCaseListener, mockCatsUtil, filesArguments);
    Mockito.doNothing().when(testCaseListener).reportResult(Mockito.any(), Mockito.eq(data), Mockito.any(), Mockito.any());
    return data;
}
Also used : HashMap(java.util.HashMap) CatsUtil(com.endava.cats.util.CatsUtil) Schema(io.swagger.v3.oas.models.media.Schema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) FuzzingResult(com.endava.cats.model.FuzzingResult) FuzzingData(com.endava.cats.model.FuzzingData) StringSchema(io.swagger.v3.oas.models.media.StringSchema) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with FuzzingResult

use of com.endava.cats.model.FuzzingResult in project cats by Endava.

the class BaseFieldsFuzzer method process.

protected void process(FuzzingData data, String fuzzedField, FuzzingStrategy fuzzingStrategy) {
    FuzzingConstraints fuzzingConstraints = this.createFuzzingConstraints(data, fuzzingStrategy, fuzzedField);
    testCaseListener.addScenario(logger, "Send [{}] in request fields: field [{}], value [{}], is required [{}]", this.typeOfDataSentToTheService(), fuzzedField, fuzzingStrategy.truncatedValue(), fuzzingConstraints.getRequiredString());
    if (this.isFuzzingPossible(data, fuzzedField, fuzzingStrategy)) {
        FuzzingResult fuzzingResult = catsUtil.replaceField(data.getPayload(), fuzzedField, fuzzingStrategy);
        boolean isFuzzedValueMatchingPattern = this.isFuzzedValueMatchingPattern(fuzzingResult.getFuzzedValue(), data, fuzzedField);
        ServiceData serviceData = ServiceData.builder().relativePath(data.getPath()).headers(data.getHeaders()).payload(fuzzingResult.getJson()).httpMethod(data.getMethod()).fuzzedField(fuzzedField).queryParams(data.getQueryParams()).build();
        CatsResponse response = serviceCaller.call(serviceData);
        ResponseCodeFamily expectedResponseCodeBasedOnConstraints = this.getExpectedResponseCodeBasedOnConstraints(isFuzzedValueMatchingPattern, fuzzingConstraints);
        testCaseListener.addExpectedResult(logger, "Should return [{}]", expectedResponseCodeBasedOnConstraints.asString());
        testCaseListener.reportResult(logger, data, response, expectedResponseCodeBasedOnConstraints);
    } else {
        FuzzingStrategy strategy = this.createSkipStrategy(fuzzingStrategy);
        testCaseListener.skipTest(logger, strategy.process(""));
    }
}
Also used : CatsResponse(com.endava.cats.model.CatsResponse) ResponseCodeFamily(com.endava.cats.http.ResponseCodeFamily) FuzzingStrategy(com.endava.cats.model.FuzzingStrategy) FuzzingResult(com.endava.cats.model.FuzzingResult) FuzzingConstraints(com.endava.cats.model.FuzzingConstraints) ServiceData(com.endava.cats.io.ServiceData)

Example 5 with FuzzingResult

use of com.endava.cats.model.FuzzingResult in project cats by Endava.

the class CatsUtilTest method givenAPayloadAndAFuzzingStrategy_whenReplacingTheFuzzedValue_thenThePayloadIsProperlyFuzzed.

@ParameterizedTest
@CsvSource(value = { "{\"field\":\"value\", \"anotherField\":\"otherValue\"}|field", "{\"field\": 2, \"anotherField\":\"otherValue\"}|field", "[{\"field\": 2, \"anotherField\":\"otherValue\"},{\"field\": 2, \"anotherField\":\"otherValue\"}]|field", "{\"field\": {\"subField\":\"value\"}, \"anotherField\":\"otherValue\"}|field#subField", "{\"field\": [{\"subField\":\"value\"},{\"subField\":\"value\"}], \"anotherField\":\"otherValue\"}|field[*]#subField" }, delimiter = '|')
void givenAPayloadAndAFuzzingStrategy_whenReplacingTheFuzzedValue_thenThePayloadIsProperlyFuzzed(String json, String path) {
    CatsUtil catsUtil = new CatsUtil(new CatsDSLParser());
    FuzzingStrategy strategy = FuzzingStrategy.replace().withData("fuzzed");
    FuzzingResult result = catsUtil.replaceField(json, path, strategy);
    Assertions.assertThat(result.getFuzzedValue()).isEqualTo("fuzzed");
    Assertions.assertThat(result.getJson()).contains("fuzzed");
}
Also used : FuzzingStrategy(com.endava.cats.model.FuzzingStrategy) FuzzingResult(com.endava.cats.model.FuzzingResult) CatsDSLParser(com.endava.cats.dsl.CatsDSLParser) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

FuzzingResult (com.endava.cats.model.FuzzingResult)6 FuzzingStrategy (com.endava.cats.model.FuzzingStrategy)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 CatsDSLParser (com.endava.cats.dsl.CatsDSLParser)2 FuzzingData (com.endava.cats.model.FuzzingData)2 CatsUtil (com.endava.cats.util.CatsUtil)2 Schema (io.swagger.v3.oas.models.media.Schema)2 StringSchema (io.swagger.v3.oas.models.media.StringSchema)2 HashMap (java.util.HashMap)2 CsvSource (org.junit.jupiter.params.provider.CsvSource)2 ResponseCodeFamily (com.endava.cats.http.ResponseCodeFamily)1 ServiceData (com.endava.cats.io.ServiceData)1 CatsResponse (com.endava.cats.model.CatsResponse)1 FuzzingConstraints (com.endava.cats.model.FuzzingConstraints)1 DocumentContext (com.jayway.jsonpath.DocumentContext)1 QuarkusTest (io.quarkus.test.junit.QuarkusTest)1 NotNull (org.jetbrains.annotations.NotNull)1 Test (org.junit.jupiter.api.Test)1