Untitled
unknown
plain_text
8 months ago
17 kB
6
Indexable
json.class:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package sth.json;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.testng.asserts.SoftAssert;
import sth.json.path.JSONPath;
public interface JSON {
int ASCENDING = 1;
int DESCENDING = -1;
<T> T get(String var1);
<T> T get(int var1);
JSON set(String var1, Object var2);
JSON set(int var1, Object var2);
JSON append(Object var1);
boolean hasField(String var1);
<T> boolean isType(String var1, Class<T> var2);
Class getType(String var1);
Set<String> fields();
int size();
boolean isArray();
default byte[] serialize() {
return JacksonHelper.writeValueAsBytes(this);
}
default JSON copy() {
return parse(this.serialize());
}
default String toString(int tab) {
return JacksonHelper.toString(this, tab);
}
default <T> List<T> toList() {
JSONException.throwIf(!this.isArray(), "Can't turn json object into list");
List<T> list = new ArrayList(this.size());
for(int i = 0; i < this.size(); ++i) {
list.add(this.get(i));
}
return list;
}
default <T> List<T> all(String jsonPathTemplate, Object... values) {
return this.<T>all(Utils.template(jsonPathTemplate, values));
}
default <T> List<T> all(String jsonPath) {
return (List)JSONPath.find(this, jsonPath).collect(Collectors.toCollection(LinkedList::new));
}
default <T> Set<T> unique(String jsonPathTemplate, Object... values) {
return this.<T>unique(Utils.template(jsonPathTemplate, values));
}
default <T> Set<T> unique(String jsonPath) {
return (Set)JSONPath.find(this, jsonPath).collect(Collectors.toCollection(LinkedHashSet::new));
}
default <T> T one(String jsonPathTemplate, Object... values) {
return (T)this.one(Utils.template(jsonPathTemplate, values));
}
default <T> T one(String jsonPath) {
return (T)JSONPath.find(this, jsonPath).limit(1L).filter(Objects::nonNull).findFirst().orElse((Object)null);
}
default boolean exists(String jsonPathTemplate, Object... values) {
return this.exists(Utils.template(jsonPathTemplate, values));
}
default boolean exists(String jsonPath) {
return this.one(jsonPath) != null;
}
default <T> void forEach(BiConsumer<String, T> consumer) {
JSONException.throwIf(this.isArray(), "Can't iterate over key/value pairs of array");
this.fields().forEach((field) -> consumer.accept(field, this.get(field)));
}
default <T> void forEach(Consumer<T> consumer) {
JSONException.throwIf(!this.isArray(), "Can't iterate over indexes of object");
for(int i = 0; i < this.size(); ++i) {
consumer.accept(this.get(i));
}
}
default Stream<KeyValue> stream() {
return this.isArray() ? IntStream.range(0, this.size()).mapToObj((i) -> new KeyValue(i, this.get(i))) : this.fields().stream().map((key) -> new KeyValue(key, this.get(key)));
}
default Stream<KeyValue> recursiveStream() {
return this.stream().flatMap((keyValue) -> {
Object value = keyValue.value();
return value instanceof JSON ? Stream.concat(Stream.of(keyValue), ((JSON)value).recursiveStream()) : Stream.of(keyValue);
});
}
default JSON sortByField(String field) {
return this.sortByField(field, 1);
}
JSON sortByField(String var1, int var2);
static JSON parse(String json) {
return (JSON)JacksonHelper.readValue(json, JSON.class);
}
static JSON valueOf(String json) {
return parse(json);
}
static JSON parse(byte[] arr) {
return (JSON)JacksonHelper.readValue(arr, JSON.class);
}
static JSON parse(InputStream is) {
return (JSON)JacksonHelper.readValue(is, JSON.class);
}
static JSONBuilder buildObject() {
return JSONBuilder.object();
}
static JSONBuilder buildArray() {
return JSONBuilder.array();
}
static boolean matchListByFields(JSON a, JSON b, String orderByField, List<String> comparisonFieldPaths, SoftAssert softAssert) {
List<JSON> jsonListA = a.<JSON>toList();
List<JSON> jsonListB = b.<JSON>toList();
for(JSON json : jsonListA) {
if (!json.hasField(orderByField)) {
softAssert.assertTrue(json.hasField(orderByField), "JSON a member: " + json.toString(3) + " has no field " + orderByField);
return false;
}
boolean foundOrderByFieldMatch = false;
for(JSON comparisonJson : jsonListB) {
if (!comparisonJson.hasField(orderByField)) {
softAssert.assertTrue(comparisonJson.hasField(orderByField), "JSON b member: " + comparisonJson.toString(3) + " has no field " + orderByField);
return false;
}
if (json.get(orderByField).equals(comparisonJson.get(orderByField))) {
foundOrderByFieldMatch = true;
if (!matchByFields(json, comparisonJson, comparisonFieldPaths, softAssert)) {
return false;
}
break;
}
}
if (!foundOrderByFieldMatch) {
softAssert.assertTrue(foundOrderByFieldMatch, "No field " + orderByField + " match found in JSON b for " + json.get(orderByField));
return false;
}
}
return true;
}
static boolean matchListByFields(JSON a, JSON b, String orderByField, List<String> comparisonFieldPaths) {
SoftAssert sa = new SoftAssert();
return matchListByFields(a, b, orderByField, comparisonFieldPaths, sa);
}
static boolean matchByFields(JSON a, JSON b, List<String> comparisonFieldPaths, SoftAssert softAssert) {
boolean failure = false;
if (a.isArray() || b.isArray()) {
softAssert.assertFalse(a.isArray(), "JSON: " + a.toString(3) + " is an array");
softAssert.assertFalse(b.isArray(), "JSON: " + b.toString(3) + " is an array");
failure = true;
}
for(String field : comparisonFieldPaths) {
if (!field.isEmpty()) {
String jsonPath;
if (field.charAt(0) == '.') {
jsonPath = field;
} else {
jsonPath = "." + field;
}
if (a.exists(jsonPath) && b.exists(jsonPath) && !a.one(jsonPath).equals(b.one(jsonPath))) {
softAssert.assertTrue(a.one(jsonPath).equals(b.one(jsonPath)), "Values at " + jsonPath + " do not match\nA: " + a.one(jsonPath) + "\nB: " + b.one(jsonPath));
failure = true;
}
}
}
if (failure) {
return false;
} else {
return true;
}
}
static boolean matchByFields(JSON a, JSON b, List<String> comparisonFieldPaths) {
SoftAssert sa = new SoftAssert();
return matchByFields(a, b, comparisonFieldPaths, sa);
}
/** @deprecated */
@Deprecated
static List<JSON> sortJSONArrayToList(JSON jsonArray, String fieldName) {
return jsonArray.sortByField(fieldName).<JSON>toList();
}
}
ipvsresponse.java:
// Copyright 2019-2020, Charter Communications, All rights reserved.
package sth.ipvs.rest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import sth.http.ServiceResponse;
import sth.http.client.Request;
import sth.http.client.Response;
import sth.http.common.HttpStatus;
import sth.json.JSON;
import sth.xml.XML;
/**
* A wrapper around the Request/Response dialog between HTTP client and server. For specific
* use cases further methods could be added (for example to deserialize proprietary formats).
*/
public class IPVSResponse implements ServiceResponse {
private Request request;
private Response response;
protected Logger log = LoggerFactory.getLogger(getClass());
public IPVSResponse(Request request, Response response) {
this.request = request;
this.response = response;
}
/**
* Get the {@link Request} object that was executed to create the {@link Response}
*/
public Request request() {
return request;
}
/**
* Get the underlying {@link Response} object
*/
public Response response() {
return response;
}
/**
* True if the response status code is in the success range (200-299)
*/
public boolean isSuccess() {
return response.status().isSuccess();
}
/**
* Assert that the response status code was in the success range (200-299)
*/
public IPVSResponse assertSuccess() {
Assert.assertTrue(isSuccess(), "request to " + request.uri() + " failed:\n" + response);
return this;
}
/**
* True if the response status code is in the error range (>=400)
*/
public boolean isError() {
return response.status().isError();
}
/**
* Parse the response body to a JSON object
*/
public JSON json() {
return response.as(JSON.class);
}
/**
* Parse the response body to an XML object
*/
public XML xml() {
return response.as(XML.class);
}
/**
* Get the response body as a String
*/
public String responseString() {
return response.as(String.class);
}
/**
* Get HTTP status from response
*/
public HttpStatus status() {
return response.status();
}
/**
* Get the HTTP status code of the response
*/
public int statusCode() {
return response.status().code();
}
/**
* Get the request URI that was executed
*/
public String uri() {
return request.uri().toString();
}
/**
* Get a response header
*/
public String header(String headerName) {
return response.header(headerName);
}
@Override
public String toString() {
String contentType = response.header("content-type");
if (contentType.contains("json")) {
return json().toString(3);
} else if (contentType.contains("xml")) {
return xml().toString(3);
} else {
return responseString();
}
}
/**
* getError(JSON json, String errorField)
* If there are errors in a json body
* return the error msg portion of the body.
*
* @param json
* @param errorField name to check for errors.
* @return errorStr
*/
private String getError(JSON json, String errorField) {
String errorStr = "";
JSON errorMsgs = null;
log.debug("getError(): errorField={}, json={}", errorField, json);
if (json != null) {
if (json.hasField(errorField)) {
errorMsgs = json.get(errorField);
if (errorMsgs.size() > 0) {
errorStr = errorMsgs.toString();
log.debug("getError(): Found errorField={}, json={}", errorField, json);
}
}
}
return errorStr;
}
/**
* getErrorMsg(JSON json)
* Return the error or errors or errorMessages or ? msg portion of the body.
*
* @param json
* @return errorStr
*/
public String getErrorMsg(JSON json) {
String errorStr = "";
errorStr = getError(json, "error");
if (errorStr == "") {
errorStr = getError(json, "errors");
} else if (json.toString().contains("errorMessages")) {
errorStr = json.toString();
}
return errorStr;
}
}
templateresponse.java:
// Copyright 2019-2020, Charter Communications, All rights reserved.
package sth.template.rest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.Assert;
import sth.http.ServiceResponse;
import sth.http.client.Request;
import sth.http.client.Response;
import sth.http.common.HttpStatus;
import sth.json.JSON;
import sth.xml.XML;
/**
* A wrapper around the Request/Response dialog between HTTP client and server. For specific
* use cases further methods could be added (for example to deserialize proprietary formats).
*/
public class TemplateResponse implements ServiceResponse {
private Request request;
private Response response;
protected Logger log = LoggerFactory.getLogger(getClass());
public TemplateResponse(Request request, Response response) {
this.request = request;
this.response = response;
}
/**
* Get the {@link Request} object that was executed to create the {@link Response}
*/
public Request request() {
return request;
}
/**
* Get the underlying {@link Response} object
*/
public Response response() {
return response;
}
/**
* True if the response status code is in the success range (200-299)
*/
public boolean isSuccess() {
return response.status().isSuccess();
}
/**
* Assert that the response status code was in the success range (200-299)
*/
public TemplateResponse assertSuccess() {
Assert.assertTrue(isSuccess(), "request to " + request.uri() + " failed:\n" + response);
return this;
}
/**
* True if the response status code is in the error range (>=400)
*/
public boolean isError() {
return response.status().isError();
}
/**
* Parse the response body to a JSON object
*/
public JSON json() {
return response.as(JSON.class);
}
/**
* Parse the response body to an XML object
*/
public XML xml() {
return response.as(XML.class);
}
/**
* Get the response body as a String
*/
public String responseString() {
return response.as(String.class);
}
/**
* Get HTTP status from response
*/
public HttpStatus status() {
return response.status();
}
/**
* Get the HTTP status code of the response
*/
public int statusCode() {
return response.status().code();
}
/**
* Get the request URI that was executed
*/
public String uri() {
return request.uri().toString();
}
/**
* Get a response header
*/
public String header(String headerName) {
return response.header(headerName);
}
@Override
public String toString() {
String contentType = response.header("content-type");
if (contentType.contains("json")) {
return json().toString(3);
} else if (contentType.contains("xml")) {
return xml().toString(3);
} else {
return responseString();
}
}
/**
* getError(JSON json, String errorField)
* If there are errors in a json body
* return the error msg portion of the body.
*
* @param json
* @param errorField name to check for errors.
* @return errorStr
*/
private String getError(JSON json, String errorField) {
String errorStr = "";
JSON errorMsgs = null;
log.debug("getError(): errorField={}, json={}", errorField, json);
if (json != null) {
if (json.hasField(errorField)) {
errorMsgs = json.get(errorField);
if (errorMsgs.size() > 0) {
errorStr = errorMsgs.toString();
log.debug("getError(): Found errorField={}, json={}", errorField, json);
}
}
}
return errorStr;
}
/**
* getErrorMsg(JSON json)
* Return the error or errors or errorMessages or ??? msg portion of the body.
*
* @param json
* @return errorStr
*/
public String getErrorMsg(JSON json) {
String errorStr = "";
errorStr = getError(json, "error");
if (errorStr == "") {
errorStr = getError(json, "errors");
} else if (json.toString().contains("errorMessages")) {
errorStr = json.toString();
}
return errorStr;
}
}
Editor is loading...
Leave a Comment