Skip to content

Commit c92afd0

Browse files
committed
chore: Add new utility methods for json parsing
1 parent cc37719 commit c92afd0

File tree

4 files changed

+84
-10
lines changed

4 files changed

+84
-10
lines changed

src/main/java/com/endava/cats/util/JsonUtils.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.endava.cats.model.ann.ExcludeTestCaseStrategy;
44
import com.fasterxml.jackson.annotation.JsonInclude;
55
import com.fasterxml.jackson.core.JsonGenerator;
6+
import com.fasterxml.jackson.core.JsonProcessingException;
7+
import com.fasterxml.jackson.core.type.TypeReference;
68
import com.fasterxml.jackson.databind.JsonNode;
79
import com.fasterxml.jackson.databind.ObjectMapper;
810
import com.fasterxml.jackson.databind.module.SimpleModule;
@@ -571,6 +573,39 @@ public static String insertCharactersInFieldKey(String json, String currentField
571573
return replaceNewElement(inputJsonWithRemovedKey, pathToKey, newFieldKey, currentFieldValue);
572574
}
573575

576+
/**
577+
* Parses a JSON string into a Map using the custom depth mapper.
578+
*
579+
* @param json the JSON string to parse
580+
* @return a Map representation of the JSON
581+
*/
582+
public static Map<String, Object> parseJsonToMap(String json) {
583+
if (json == null || json.isBlank()) {
584+
return Map.of();
585+
}
586+
587+
try {
588+
return getCustomDepthMapper().readValue(json, new TypeReference<>() {
589+
});
590+
} catch (IOException e) {
591+
throw new IllegalArgumentException("Invalid JSON input: " + json, e);
592+
}
593+
}
594+
595+
/**
596+
* Converts a Map to a JSON string using the custom depth mapper.
597+
*
598+
* @param map the Map to convert
599+
* @return the JSON string representation of the Map
600+
*/
601+
public static String toJsonString(Map<String, Object> map) {
602+
try {
603+
return getCustomDepthMapper().writeValueAsString(map);
604+
} catch (JsonProcessingException e) {
605+
throw new RuntimeException("Failed to convert map to JSON string", e);
606+
}
607+
}
608+
574609
private static void traverseJson(JsonElement element, String prefix, List<String> fields) {
575610
if (element.isJsonObject()) {
576611
JsonObject jsonObject = element.getAsJsonObject();

src/test/java/com/endava/cats/args/FilterArgumentsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void shouldIncludeAllFuzzers() {
7878
List<String> fuzzers = filterArguments.getFirstPhaseFuzzersForPath();
7979

8080
Assertions.assertThat(fuzzers).contains("LeadingControlCharsInHeadersFuzzer", "LeadingWhitespacesInHeadersFuzzer", "LeadingMultiCodePointEmojisInFieldsTrimValidateFuzzer"
81-
, "RemoveFieldsFuzzer", "CheckSecurityHeadersFuzzer").hasSize(144);
81+
, "RemoveFieldsFuzzer", "CheckSecurityHeadersFuzzer").hasSize(148);
8282
}
8383

8484
@Test
@@ -145,7 +145,7 @@ void shouldReturnGetAndDeleteWhenNotHttpMethodSupplied() {
145145

146146
@Test
147147
void shouldReturnAllRegisteredFuzzers() {
148-
Assertions.assertThat(filterArguments.getAllRegisteredFuzzers()).hasSize(149);
148+
Assertions.assertThat(filterArguments.getAllRegisteredFuzzers()).hasSize(153);
149149
}
150150

151151
@Test
@@ -368,7 +368,7 @@ void shouldHaveDefaultHttpMethods() {
368368

369369
@Test
370370
void shouldReturnFuzzersAsClasses() {
371-
Assertions.assertThat(filterArguments.getFirstPhaseFuzzersAsFuzzers()).hasSize(100);
371+
Assertions.assertThat(filterArguments.getFirstPhaseFuzzersAsFuzzers()).hasSize(104);
372372
}
373373

374374
@Test

src/test/resources/headers.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
all:
1+
All:
22
header: value
33
catsFuzzedHeader: cats
44
auth-header:

src/test/resources/petstore_empty_body.json

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,40 @@
1313
}
1414
],
1515
"paths": {
16+
"/pets-array": {
17+
"post": {
18+
"summary": "Create pets",
19+
"operationId": "createMorePets",
20+
"tags": [
21+
"pets"
22+
],
23+
"requestBody": {
24+
"content": {
25+
"application/json": {
26+
"schema": {
27+
"$ref": "#/components/schemas/PetsArray"
28+
}
29+
}
30+
},
31+
"required": true
32+
},
33+
"responses": {
34+
"201": {
35+
"description": "Null response"
36+
},
37+
"default": {
38+
"description": "unexpected error",
39+
"content": {
40+
"application/json": {
41+
"schema": {
42+
"$ref": "#/components/schemas/PetsArray"
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
},
1650
"/pets": {
1751
"get": {
1852
"summary": "List all pets",
@@ -67,12 +101,12 @@
67101
"tags": [
68102
"pets"
69103
],
70-
"requestBody": {
71-
"content": {
72-
"application/json": {}
73-
},
74-
"required": true
75-
},
104+
"requestBody": {
105+
"content": {
106+
"application/json": {}
107+
},
108+
"required": true
109+
},
76110
"responses": {
77111
"201": {
78112
"description": "Null response"
@@ -172,6 +206,11 @@
172206
},
173207
"components": {
174208
"schemas": {
209+
"PetsArray": {
210+
"example": [],
211+
"maxItems": 0,
212+
"type": "array"
213+
},
175214
"Pet": {
176215
"type": "object",
177216
"required": [

0 commit comments

Comments
 (0)