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();
}
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();
}
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;
}
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(""));
}
}
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");
}
Aggregations