Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Add top level arrays support
Browse files Browse the repository at this point in the history
  • Loading branch information
clicman committed Dec 3, 2021
1 parent b148159 commit 7523577
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -117,14 +116,46 @@ public static String removeOptionals(String jsonString, Map<String, Object> para
}

public static String removeEmptyObjects(String jsonString) {
Type type = new TypeToken<Map<String, Object>>() {
Type objectType = new TypeToken<Map<String, Object>>() {
}.getType();
Map<String, Object> data = new Gson().fromJson(jsonString, type);
Type arrayType = new TypeToken<ArrayList<?>>() {
}.getType();
boolean isJsonArray = jsonString.trim().startsWith("[");

if (isJsonArray) {
ArrayList<?> array = new Gson().fromJson(jsonString, arrayType);
return new GsonBuilder().setPrettyPrinting().create().toJson(jsonArrayCleaner(array));
} else {
Map<String, Object> data = new Gson().fromJson(jsonString, objectType);
return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObjectCleaner(data));
}

}

return new GsonBuilder().setPrettyPrinting().create().toJson(jsonCleaner(data));
private static ArrayList<?> jsonArrayCleaner(ArrayList<?> jsonArray) {
for (Iterator<?> it = jsonArray.iterator(); it.hasNext(); ) {
Object item = it.next();
if (item == null
|| (item instanceof String && ((String) item).isEmpty())
|| (item instanceof Map && ((Map<?, ?>) item).isEmpty())
|| (item instanceof ArrayList && ((ArrayList<?>) item).isEmpty())) {
it.remove();
} else if (item instanceof ArrayList) {
item = jsonArrayCleaner((ArrayList<?>) item);
if (((ArrayList<?>) item).isEmpty()) {
it.remove();
}
} else if (item instanceof Map) {
item = jsonObjectCleaner((Map<String, Object>) item);
if (((Map<?, ?>) item).isEmpty()) {
it.remove();
}
}
}
return jsonArray;
}

private static Map<String, Object> jsonCleaner(Map<String, Object> jsonData) {
private static Map<String, Object> jsonObjectCleaner(Map<String, Object> jsonData) {
for (Iterator<Map.Entry<String, Object>> it = jsonData.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, Object> entry = it.next();
Object value = entry.getValue();
Expand All @@ -134,29 +165,14 @@ private static Map<String, Object> jsonCleaner(Map<String, Object> jsonData) {
|| (value instanceof ArrayList && ((ArrayList<?>) value).isEmpty())) {
it.remove();
} else if (value instanceof Map) {
value = jsonCleaner((Map<String, Object>) value);
value = jsonObjectCleaner((Map<String, Object>) value);
if (((Map<?, ?>) value).isEmpty()) {
it.remove();
}
} else if (value instanceof ArrayList) {
List<?> list = ((ArrayList<?>) value).stream().filter(item -> {
if (item == null
|| (item instanceof String && ((String) item).isEmpty())
|| (item instanceof Map && ((Map<?, ?>) item).isEmpty())
|| (item instanceof ArrayList && ((ArrayList<?>) item).isEmpty())) {
return false;
} else if (item instanceof Map) {
Map<String, Object> parsedArrayItem = jsonCleaner((Map<String, Object>) item);
if (((Map<?, ?>) parsedArrayItem).isEmpty()) {
return false;
}
}
return true;
}).collect(Collectors.toList());
if (list.isEmpty()) {
value = jsonArrayCleaner((ArrayList<?>) value);
if (((ArrayList<?>) value).isEmpty()) {
it.remove();
} else {
value = list;
}
}
entry.setValue(value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ru.sbtqa.tag.api.endpoints;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.util.JSON;
import jakarta.ws.rs.Consumes;
Expand Down Expand Up @@ -205,4 +206,14 @@ public Response typedArrays(String request) {
result.setResult(response.toJson());
return Response.ok(result).build();
}

@POST
@Path("arrays-as-is")
@Produces(MediaType.APPLICATION_JSON)
public Response arrayResponse(String request) {
SimpleResult result = new SimpleResult();
BasicDBList response = (BasicDBList) JSON.parse(request);
result.setResult(response.toString());
return Response.ok(result).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.sbtqa.tag.api.entries.template;

import ru.sbtqa.tag.api.EndpointEntry;
import ru.sbtqa.tag.api.annotation.Body;
import ru.sbtqa.tag.api.annotation.Validation;
import ru.sbtqa.tag.pagefactory.Rest;
import ru.sbtqa.tag.pagefactory.annotations.rest.Endpoint;

import static org.hamcrest.Matchers.equalTo;

@Endpoint(method = Rest.POST, path = "client/arrays-as-is", title = "empty arrays", template = "templates/EmptyArrays.json", shouldRemoveEmptyObjects = true)
public class EmptyArraysEntry extends EndpointEntry {

@Body(name = "first")
private String first;

@Validation(title = "result")
public void validate() {
String expected = "[ \"1\" , \"2\" , { \"milk\" : true , \"first\" : \"parameter\"}]";
getResponse().body("result", equalTo(expected));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ Feature: Test template placeholder replacing
* user sends request for "empty objects" with parameters
| first | parameter |
* system returns "result"

@empty-objects
Scenario: Test template placeholder replacing with empty arrays
* user sends request for "empty arrays" with parameters
| first | parameter |
* system returns "result"
13 changes: 13 additions & 0 deletions plugins/rest-plugin/src/test/resources/templates/EmptyArrays.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
"1",
"2",
{},
[{},{}],

{
"id": null,
"user": "",
"milk": true,
"first": "${first}"
}
]

0 comments on commit 7523577

Please sign in to comment.