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

Commit

Permalink
Merge pull request #367 from sbtqa/fix-jsonCleaner
Browse files Browse the repository at this point in the history
Refactor json cleaner from regex to parser
  • Loading branch information
clicman authored Dec 3, 2021
2 parents e75c1be + 7523577 commit 161a521
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package ru.sbtqa.tag.api.utils;

import com.google.common.reflect.TypeToken;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.GsonBuilder;
import org.apache.commons.lang3.reflect.FieldUtils;
import ru.sbtqa.tag.api.EndpointEntry;
import ru.sbtqa.tag.qautils.errors.AutotestError;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -110,8 +116,68 @@ public static String removeOptionals(String jsonString, Map<String, Object> para
}

public static String removeEmptyObjects(String jsonString) {
String regex = "\"[\\w]+\"[\\s\\r\\n]*:[\\s\\r\\n]*\\{[\\s\\r\\n]*}[\\s\\r\\n,]*";
return jsonString.replaceAll(regex, "");
Type objectType = new TypeToken<Map<String, Object>>() {
}.getType();
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));
}

}

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> 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();
if (value == null
|| (value instanceof String && ((String) value).isEmpty())
|| (value instanceof Map && ((Map<?, ?>) value).isEmpty())
|| (value instanceof ArrayList && ((ArrayList<?>) value).isEmpty())) {
it.remove();
} else if (value instanceof Map) {
value = jsonObjectCleaner((Map<String, Object>) value);
if (((Map<?, ?>) value).isEmpty()) {
it.remove();
}
} else if (value instanceof ArrayList) {
value = jsonArrayCleaner((ArrayList<?>) value);
if (((ArrayList<?>) value).isEmpty()) {
it.remove();
}
}
entry.setValue(value);
}
return jsonData;
}

/**
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}"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@
"minusOne": {

},
"zero": { },
"zero": {
"task": [
{

}
]
},
"first": "${first}",
"second": { },
"emptyArray": [],
"task": [
{


}
],
"third": {


Expand Down

0 comments on commit 161a521

Please sign in to comment.