Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import com.jamesmurty.utils.XMLBuilder2;
import io.restassured.response.ResponseBody;
import javax.xml.transform.OutputKeys;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static io.restassured.RestAssured.given;
import static org.codehaus.groovy.runtime.InvokerHelper.asList;
public class GGRConfigGenerator {
public static void main(String[] args) {
Map<String, Map<String, List<String>>> ggrBrowsers = collectInfoFromSelenoidInstances(args);
Map<String, Map<String, List<String>>> ggrBrowsersWithDefault = defineDefaultVersions(ggrBrowsers);
XMLBuilder2 resultXML = generateGgrXml(ggrBrowsersWithDefault);
printOutXML(resultXML);
}
private static void printOutXML(XMLBuilder2 resultXML) {
Properties xmlProperties = new Properties();
xmlProperties.put(OutputKeys.METHOD, "xml");
xmlProperties.put(OutputKeys.INDENT, "yes");
xmlProperties.put("{http://xml.apache.org/xslt}indent-amount", "2");
xmlProperties.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
System.out.println("Generated xml config file:");
System.out.println(resultXML.asString(xmlProperties));
}
private static Map<String,Map<String,List<String>>> defineDefaultVersions(Map<String, Map<String, List<String>>> ggrBrowsers) {
for(Map.Entry<String, Map<String, List<String>>> browser : ggrBrowsers.entrySet()){
double latest = browser.getValue().entrySet().stream()
.mapToDouble(entry -> Double.parseDouble(entry.getKey()))
.max()
.getAsDouble();
ggrBrowsers.get(browser.getKey()).put("defaultVersion", asList(Double.toString(latest)));
}
return ggrBrowsers;
}
private static XMLBuilder2 generateGgrXml(Map<String, Map<String, List<String>>> ggrBrowsers) {
XMLBuilder2 mainBuilder = XMLBuilder2.create("qa:browsers")
.attribute("xmlns:qa", "urn:config.gridrouter.qatools.ru");
for(Map.Entry<String, Map<String, List<String>>> ggrBrowser : ggrBrowsers.entrySet()){
XMLBuilder2 browserBuilder = mainBuilder
.element("browser")
.attribute("name", ggrBrowser.getKey())
.attribute("defaultVersion", ggrBrowser.getValue().get("defaultVersion").get(0));
for(Map.Entry<String, List<String>> ggrBrowserVersion : ggrBrowser.getValue().entrySet()){
if(ggrBrowserVersion.getKey().equalsIgnoreCase("defaultVersion"))
continue;
XMLBuilder2 versionBuilder = browserBuilder
.element("version")
.attribute("number", ggrBrowserVersion.getKey())
.element("region")
.attribute("name", "1");
for(String ggrHost : ggrBrowserVersion.getValue()){
versionBuilder
.element("host")
.attribute("name", ggrHost.split(":")[0])
.attribute("port", ggrHost.split(":")[1])
.attribute("count", "1")
.up();
}
versionBuilder.up();
}
browserBuilder.up();
}
return mainBuilder.up();
}
private static Map<String, Map<String, List<String>>> collectInfoFromSelenoidInstances(String[] hosts){
Map<String, Map<String, List<String>>> ggrBrowsers = new HashMap<>();
for (String host : hosts) {
ResponseBody body = given().get(String.format("http://%s/status", host)).thenReturn().body();
Map<String, Map<String, Object>> selenoidBrowsers = body.jsonPath().getMap("browsers");
for (Map.Entry<String, Map<String, Object>> selenoidBrowser : selenoidBrowsers.entrySet()) {
if (ggrBrowsers.containsKey(selenoidBrowser.getKey())) {
Map<String, List<String>> ggrVersions = ggrBrowsers.get(selenoidBrowser.getKey());
for (Map.Entry<String, Object> version : selenoidBrowser.getValue().entrySet()) {
if (ggrVersions.containsKey(version.getKey())) {
ggrVersions.get(version.getKey()).add(host);
} else {
ggrVersions.put(version.getKey(), asList(host));
}
}
}else{
ggrBrowsers.put(selenoidBrowser.getKey(), new HashMap());
for(Map.Entry<String, Object> version : selenoidBrowser.getValue().entrySet()){
ggrBrowsers.get(selenoidBrowser.getKey()).put(version.getKey(), asList(host));
}
}
}
}
return ggrBrowsers;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment