com.sdl.selenium.web.Transform Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Testy Show documentation
Show all versions of Testy Show documentation
Automated Acceptance Testing. Selenium and Selenium WebDriver test framework for web applications.
(optimized for dynamic html, ExtJS, Bootstrap, complex UI, simple web applications/sites)
package com.sdl.selenium.web;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public interface Transform {
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES, true);
/**
* add in V class this: @JsonInclude(JsonInclude.Include.NON_NULL)
*/
@SneakyThrows
default List transformToObjectList(V type, List> actualListOfList) {
String json = mapper.writeValueAsString(type);
JsonNode jsonNode = mapper.readValue(json, JsonNode.class);
String jsonPretty = jsonNode.toPrettyString();
List jsonList = new ArrayList<>(Arrays.asList(jsonPretty.split("\\n")));
jsonList.removeIf(o -> o.startsWith("{") || o.endsWith("}"));
List actualList = actualListOfList.stream().map(i -> {
V actualObject = null;
try {
List result = new ArrayList<>();
result.add("{");
for (int j = 0; j < i.size(); j++) {
String replace = i.get(j).replace("\n", " ");
String oldValue = jsonList.get(j);
if (oldValue.contains(":")) {
String tmp = oldValue.replaceAll(":([^{}]*)\"", ":\"" + replace + "\"");
result.add(tmp);
} else {
result.add(oldValue);
}
}
result.add("}");
String jsonActual = String.join("", result);
actualObject = (V) mapper.readValue(jsonActual, type.getClass());
} catch (Exception e1) {
e1.printStackTrace();
}
return actualObject;
}).collect(Collectors.toList());
return actualList;
}
default List transformToObjectList(Class type, List> actualListOfList) {
Class> newClazz;
int size = actualListOfList.get(0).size();
Constructor> constructor = null;
try {
newClazz = Class.forName(type.getTypeName());
Constructor>[] constructors = newClazz.getConstructors();
for (Constructor> c : constructors) {
int parameterCount = c.getParameterCount();
if (size == parameterCount) {
constructor = c;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
final Constructor finalConstructor = (Constructor) constructor;
return actualListOfList.stream().map(t -> {
try {
return finalConstructor.newInstance(t.toArray());
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}).collect(Collectors.toList());
}
/**
* add in V class this: @JsonInclude(JsonInclude.Include.NON_NULL)
*/
@SneakyThrows
default V transformToObject(V type, List actualList) {
String json = mapper.writeValueAsString(type);
JsonNode jsonNode = mapper.readValue(json, JsonNode.class);
String jsonPretty = jsonNode.toPrettyString();
List jsonList = new ArrayList<>(Arrays.asList(jsonPretty.split("\\n")));
jsonList.removeIf(o -> o.startsWith("{") || o.endsWith("}"));
V actualObject = null;
try {
List result = new ArrayList<>();
result.add("{");
for (int j = 0; j < actualList.size(); j++) {
String replace = actualList.get(j).replace("\n", " ");
String oldValue = jsonList.get(j);
if (oldValue.contains(":")) {
String tmp = oldValue.replaceAll(":([^{}]*)\"", ":\"" + replace + "\"");
result.add(tmp);
} else {
result.add(oldValue);
}
}
result.add("}");
String jsonActual = String.join("", result);
actualObject = (V) mapper.readValue(jsonActual, type.getClass());
} catch (Exception e1) {
e1.printStackTrace();
}
return actualObject;
}
default V transformToObject(Class type, List cellsText) {
int fieldsCount;
Constructor constructor = null;
try {
Class> newClazz = Class.forName(type.getTypeName());
fieldsCount = cellsText.size();
Constructor[] constructors = newClazz.getConstructors();
for (Constructor c : constructors) {
if (fieldsCount == c.getParameterCount()) {
constructor = c;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Constructor constructorTemp = (Constructor) constructor;
return constructorTemp.newInstance(cellsText.toArray(new Object[0]));
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy