Skip to content

Instantly share code, notes, and snippets.

@fabmars
Last active August 31, 2022 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fabmars/163f99eb67bb2fba38487df32e716b74 to your computer and use it in GitHub Desktop.
Save fabmars/163f99eb67bb2fba38487df32e716b74 to your computer and use it in GitHub Desktop.
RestAssuredBodyTestGenerator
package whatever.testing;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.response.Response;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class RestAssuredBodyTestGenerator {
private ObjectMapper objectMapper;
public RestAssuredBodyTestGenerator(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
public void printBodyTest(Response response) {
printBodyTest(response.body().asInputStream(), System.out);
}
public void printBodyTest(InputStream is, PrintStream ps) {
try {
Object o = objectMapper.readValue(is, Object.class);
ps.print(".body(");
printTestFromObject(true,"", o, ps);
ps.println(");");
} catch(IOException e) {
e.printStackTrace();
}
}
private static void printTestFromObject(boolean first, String prefix, Object o, PrintStream ps) {
if(o instanceof Map) {
printTestFromMap(first, prefix, (Map)o, ps);
} else if(o instanceof List) {
printTestFromList(first, prefix, (List)o, ps);
} else {
printTestFromValue(first, prefix, ps, null, o);
}
}
private static void printTestFromMap(boolean first, String prefix, Map<String, Object> map, PrintStream ps) {
for (Entry<String, Object> entry : map.entrySet()) {
String prop = entry.getKey();
printTestFromObject(first, toBodyProp(prefix, prop), entry.getValue(), ps);
first = false;
}
}
private static String toBodyProp(String prefix, String prop) {
StringBuffer sb = new StringBuffer();
boolean isPrefix = prefix != null && !prefix.isEmpty();
if(isPrefix) {
sb.append(prefix);
}
if(prop != null && !prop.isEmpty()) {
if(isPrefix) {
sb.append('.');
}
sb.append(prop);
}
return sb.toString();
}
private static void printTestFromList(boolean first, String prefix, List list, PrintStream ps) {
int size = list.size();
printTestFromValue(first, prefix, ps, "size()", size);
for (int i = 0; i < size; i++) {
printTestFromObject(false, prefix + '[' + i + ']', list.get(i), ps);
}
}
private static void printTestFromValue(boolean first, String prefix, PrintStream ps, String prop, Object value) {
if(!first) {
ps.println(",");
}
ps.print("\"");
ps.print(prefix);
if(prop != null) {
if(!prefix.isEmpty()) {
ps.print('.');
}
ps.print(prop);
}
if(value != null) {
ps.print("\", equalTo(");
ps.print(formatValue(value));
ps.print(")");
} else {
ps.print("\", nullValue()");
}
}
private static Object formatValue(Object value) {
if(value instanceof String) {
return "\"" + ((String) value).replaceAll("\"", "\\\"") + "\"";
} else if(value instanceof Double) {
return value + "f";
} else {
return value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment