X-Git-Url: https://gerrit.akraino.org/r/gitweb?p=validation.git;a=blobdiff_plain;f=ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fakraino%2Fvalidation%2Fui%2Fclient%2Fnexus%2FNexusExecutorClient.java;fp=ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fakraino%2Fvalidation%2Fui%2Fclient%2Fnexus%2FNexusExecutorClient.java;h=30206bf371527b8d7a6db760e3c558f4deca93f4;hp=7ee7239950f9e70f2548c69cf86b34a3853f1df8;hb=2eba847ebb6acb2686be08eb1cdafc1b12071e7d;hpb=f86b9715d156238532fcb0bf464bd72e9cf7ce96 diff --git a/ui/src/main/java/org/akraino/validation/ui/client/nexus/NexusExecutorClient.java b/ui/src/main/java/org/akraino/validation/ui/client/nexus/NexusExecutorClient.java index 7ee7239..30206bf 100644 --- a/ui/src/main/java/org/akraino/validation/ui/client/nexus/NexusExecutorClient.java +++ b/ui/src/main/java/org/akraino/validation/ui/client/nexus/NexusExecutorClient.java @@ -22,20 +22,24 @@ import java.net.Proxy; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; -import java.security.cert.X509Certificate; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Date; +import java.util.HashSet; import java.util.List; -import java.util.Map; +import java.util.Locale; -import javax.net.ssl.HostnameVerifier; -import javax.net.ssl.HttpsURLConnection; -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLSession; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; +import javax.annotation.Nonnull; import org.akraino.validation.ui.client.nexus.resources.RobotTestResult; -import org.akraino.validation.ui.client.nexus.resources.WrapperRobotTestResult; +import org.akraino.validation.ui.client.nexus.resources.Status; +import org.akraino.validation.ui.client.nexus.resources.TestInfoYaml; +import org.akraino.validation.ui.client.nexus.resources.ValidationNexusTestResult; +import org.akraino.validation.ui.client.nexus.resources.WRobotNexusTestResult; import org.akraino.validation.ui.data.BlueprintLayer; import org.apache.commons.httpclient.HttpException; import org.json.JSONObject; @@ -44,12 +48,16 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.onboarding.util.PortalApiProperties; +import org.onap.portalsdk.core.web.support.UserUtils; +import org.springframework.stereotype.Service; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.ClientHandlerException; import com.sun.jersey.api.client.ClientResponse; @@ -58,21 +66,19 @@ import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.json.JSONConfiguration; -import com.sun.jersey.client.urlconnection.HTTPSProperties; import com.sun.jersey.client.urlconnection.HttpURLConnectionFactory; import com.sun.jersey.client.urlconnection.URLConnectionClientHandler; +@Service public final class NexusExecutorClient { private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(NexusExecutorClient.class); private final Client client; private final String baseurl; - private final HostnameVerifier hostnameVerifier; - private final TrustManager[] trustAll; - public NexusExecutorClient(String newBaseurl) { - this.baseurl = newBaseurl; + public NexusExecutorClient() { + this.baseurl = PortalApiProperties.getProperty("nexus_url"); ClientConfig clientConfig = new DefaultClientConfig(); clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); this.client = new Client(new URLConnectionClientHandler(new HttpURLConnectionFactory() { @@ -81,10 +87,10 @@ public final class NexusExecutorClient { @Override public HttpURLConnection getHttpURLConnection(URL url) throws IOException { try { - String proxyIp = - System.getenv("NEXUS_PROXY").substring(0, System.getenv("NEXUS_PROXY").lastIndexOf(":")); - String proxyPort = - System.getenv("NEXUS_PROXY").substring(System.getenv("NEXUS_PROXY").lastIndexOf(":") + 1); + String proxyIp = System.getenv("NEXUS_PROXY").substring(0, + System.getenv("NEXUS_PROXY").lastIndexOf(":")); + String proxyPort = System.getenv("NEXUS_PROXY") + .substring(System.getenv("NEXUS_PROXY").lastIndexOf(":") + 1); proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyIp, Integer.parseInt(proxyPort))); return (HttpURLConnection) url.openConnection(proxy); } catch (Exception ex) { @@ -92,42 +98,289 @@ public final class NexusExecutorClient { } } }), clientConfig); - // Create all-trusting host name verifier - hostnameVerifier = new HostnameVerifier() { - @Override - public boolean verify(String hostname, SSLSession session) { - return true; + } + + public String getBaseUrl() { + return this.baseurl; + } + + public List getResource(String endpoint) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException, ParseException { + List resources = new ArrayList(); + String nexusUrl = this.baseurl; + if (endpoint != null) { + nexusUrl = this.baseurl + "/" + endpoint; + } + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get nexus resource: " + nexusUrl); + WebResource webResource = this.client.resource(nexusUrl + "/"); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); + ClientResponse response = webResource.get(ClientResponse.class); + if (response.getStatus() != 200) { + throw new HttpException("Could not retrieve nexus resource " + nexusUrl + ". HTTP error code : " + + response.getStatus() + " and message: " + response.getEntity(String.class)); + } + Document document = Jsoup.parse(response.getEntity(String.class)); + List elements = document.getElementsByTag("body").get(0).getElementsByTag("table").get(0) + .getElementsByTag("tbody").get(0).getElementsByTag("tr"); + for (int i = 2; i < elements.size(); i++) { + String resource = elements.get(i).getElementsByTag("td").get(0).getElementsByTag("a").get(0).text(); + resource = resource.substring(0, resource.length() - 1); + resources.add(resource); + } + return resources; + } + + public List getResource(@Nonnull String endpoint1, @Nonnull String endpoint2) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException, ParseException { + String endpoint = endpoint1 + "/" + endpoint2; + return this.getResource(endpoint); + } + + public List getResource(@Nonnull String endpoint1, @Nonnull String endpoint2, @Nonnull String endpoint3) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException, ParseException { + String endpoint = endpoint1 + "/" + endpoint2 + "/" + endpoint3; + return this.getResource(endpoint); + } + + public ValidationNexusTestResult getResult(@Nonnull String name, @Nonnull String version, @Nonnull String siloText, + @Nonnull String timestamp) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException, ParseException, NullPointerException { + String nexusUrl = this.baseurl + "/" + siloText + "/" + name + "/" + version; + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get validation nexus test result"); + WebResource webResource = this.client.resource(nexusUrl + "/"); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); + ClientResponse response = webResource.get(ClientResponse.class); + if (response.getStatus() != 200) { + throw new HttpException("Could not retrieve validation nexus test result. HTTP error code : " + + response.getStatus() + " and message: " + response.getEntity(String.class)); + } + Document document = Jsoup.parse(response.getEntity(String.class)); + List elements = document.getElementsByTag("body").get(0).getElementsByTag("table").get(0) + .getElementsByTag("tbody").get(0).getElementsByTag("tr"); + Element element = findElementByTimestamp(elements.subList(2, elements.size()), timestamp); + if (element == null) { + return null; + } + ValidationNexusTestResult vNexusResult = new ValidationNexusTestResult(); + vNexusResult.setBlueprintName(name); + vNexusResult.setSilo(siloText); + vNexusResult.setVersion(version); + vNexusResult.setTimestamp(timestamp); + String lastModified = element.getElementsByTag("td").get(1).text(); + vNexusResult.setDateOfStorage(lastModified); + TestInfoYaml testInfo = getTestInfo(webResource.getURI().toString() + timestamp); + if (testInfo != null) { + if (testInfo.gettest_info().getLayer().equals("all")) { + vNexusResult.setAllLayers(true); + } else { + vNexusResult.setAllLayers(false); } - }; - // Create a trust manager that does not validate certificate chains - trustAll = new TrustManager[] {new X509TrustManager() { - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; // Not relevant. + vNexusResult.setOptional(testInfo.gettest_info().getOptional()); + } + List wTestResults = getWRobotTestResults(name, version, siloText, timestamp); + vNexusResult.setwRobotNexusTestResults(wTestResults); + vNexusResult.setResult(determineResult(wTestResults)); + return vNexusResult; + } + + public List getResults(@Nonnull String name, @Nonnull String version, + @Nonnull String siloText, int noOfLastElements) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException, ParseException { + String nexusUrl = this.baseurl + "/" + siloText + "/" + name + "/" + version; + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get validation Nexus test results"); + WebResource webResource = this.client.resource(nexusUrl + "/"); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); + ClientResponse response = webResource.get(ClientResponse.class); + if (response.getStatus() != 200) { + throw new HttpException("Could not retrieve validation Nexus test results. HTTP error code : " + + response.getStatus() + " and message: " + response.getEntity(String.class)); + } + List vNexusResults = new ArrayList(); + Document document = Jsoup.parse(response.getEntity(String.class)); + List elements = document.getElementsByTag("body").get(0).getElementsByTag("table").get(0) + .getElementsByTag("tbody").get(0).getElementsByTag("tr"); + elements = findLastElementsByDate(elements.subList(2, elements.size()), noOfLastElements); + for (int i = 0; i < elements.size(); i++) { + try { + String timestamp = elements.get(i).getElementsByTag("td").get(0).getElementsByTag("a").get(0).text(); + timestamp = timestamp.substring(0, timestamp.length() - 1); + ValidationNexusTestResult vNexusResult = this.getResult(name, version, siloText, timestamp); + vNexusResults.add(vNexusResult); + } catch (HttpException ex) { + LOGGER.warn(EELFLoggerDelegate.auditLogger, "Exception occured while retrieving timestamp results"); + continue; } + } + return vNexusResults; + } - @Override - public void checkClientTrusted(X509Certificate[] certs, String authType) { - // Do nothing. Just allow them all. + public List getResults(@Nonnull String name, @Nonnull String version, + @Nonnull String siloText, @Nonnull Date date) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException, ParseException, NullPointerException { + String nexusUrl = this.baseurl + "/" + siloText + "/" + name + "/" + version; + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get validation Nexus results based on date"); + WebResource webResource = this.client.resource(nexusUrl + "/"); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); + ClientResponse response = webResource.get(ClientResponse.class); + if (response.getStatus() != 200) { + throw new HttpException("Could not retrieve validation Nexus results based on date. HTTP error code : " + + response.getStatus() + " and message: " + response.getEntity(String.class)); + } + List vNexusResults = new ArrayList(); + Document document = Jsoup.parse(response.getEntity(String.class)); + List elements = document.getElementsByTag("body").get(0).getElementsByTag("table").get(0) + .getElementsByTag("tbody").get(0).getElementsByTag("tr"); + elements = findElementsBasedOnDate(elements.subList(2, elements.size()), date); + for (int i = 0; i < elements.size(); i++) { + try { + String timestamp = elements.get(i).getElementsByTag("td").get(0).getElementsByTag("a").get(0).text(); + timestamp = timestamp.substring(0, timestamp.length() - 1); + ValidationNexusTestResult vNexusResult = this.getResult(name, version, siloText, timestamp); + vNexusResults.add(vNexusResult); + } catch (HttpException ex) { + LOGGER.warn(EELFLoggerDelegate.auditLogger, "Exception occured while retrieving timestamp results"); + continue; } + } + return vNexusResults; + } + public ValidationNexusTestResult getLastResultBasedOnOutcome(@Nonnull String name, @Nonnull String version, + @Nonnull String siloText, List layers, Boolean optional, boolean outcome) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException, ParseException, NullPointerException { + String nexusUrl = this.baseurl + "/" + siloText + "/" + name + "/" + version; + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get last result based on outcome"); + WebResource webResource = this.client.resource(nexusUrl + "/"); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); + ClientResponse response = webResource.get(ClientResponse.class); + if (response.getStatus() != 200) { + throw new HttpException("Could not retrieve last result based on outcome from Nexus. HTTP error code : " + + response.getStatus() + " and message: " + response.getEntity(String.class)); + } + Document document = Jsoup.parse(response.getEntity(String.class)); + List elements = document.getElementsByTag("body").get(0).getElementsByTag("table").get(0) + .getElementsByTag("tbody").get(0).getElementsByTag("tr"); + elements = elements.subList(2, elements.size()); + if (elements.size() < 1) { + return null; + } + DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); + Collections.sort(elements, new Comparator() { @Override - public void checkServerTrusted(X509Certificate[] certs, String authType) { - // Do nothing. Just allow them all. + public int compare(Element element1, Element element2) { + try { + return dateFormat.parse(element2.getElementsByTag("td").get(1).text()) + .compareTo(dateFormat.parse(element1.getElementsByTag("td").get(1).text())); + } catch (ParseException e) { + LOGGER.error(EELFLoggerDelegate.errorLogger, + "Error when parsing date. " + UserUtils.getStackTrace(e)); + return 0; + } + } + }); + for (Element element : elements) { + try { + String elementTimestamp = element.getElementsByTag("td").get(0).getElementsByTag("a").get(0).text(); + elementTimestamp = elementTimestamp.substring(0, elementTimestamp.length() - 1); + ValidationNexusTestResult vNexusResult = this.getResult(name, version, siloText, elementTimestamp); + if (vNexusResult.getResult() != outcome) { + continue; + } + if (optional != null && vNexusResult.getOptional() != optional) { + continue; + } + if (layers != null) { + List storedLayers = new ArrayList(); + for (WRobotNexusTestResult wRobot : vNexusResult.getwRobotNexusTestResults()) { + storedLayers.add(wRobot.getBlueprintLayer()); + } + if (!new HashSet<>(storedLayers).equals(new HashSet<>(layers))) { + continue; + } + } + return vNexusResult; + } catch (HttpException ex) { + LOGGER.warn(EELFLoggerDelegate.auditLogger, + "Error when trying to retrieve results. " + UserUtils.getStackTrace(ex)); + continue; } - }}; + } + return null; } - public String getBaseUrl() { - return this.baseurl; + public ValidationNexusTestResult getLastResultBasedOnOutcome(@Nonnull String name, @Nonnull String version, + @Nonnull String siloText, Boolean allLayers, Boolean optional, boolean outcome) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException, ParseException, NullPointerException { + String nexusUrl = this.baseurl + "/" + siloText + "/" + name + "/" + version; + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get last result based on outcome"); + WebResource webResource = this.client.resource(nexusUrl + "/"); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); + ClientResponse response = webResource.get(ClientResponse.class); + if (response.getStatus() != 200) { + throw new HttpException("Could not retrieve last result based on outcome from Nexus. HTTP error code : " + + response.getStatus() + " and message: " + response.getEntity(String.class)); + } + Document document = Jsoup.parse(response.getEntity(String.class)); + List elements = document.getElementsByTag("body").get(0).getElementsByTag("table").get(0) + .getElementsByTag("tbody").get(0).getElementsByTag("tr"); + elements = elements.subList(2, elements.size()); + if (elements.size() < 1) { + return null; + } + DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); + Collections.sort(elements, new Comparator() { + @Override + public int compare(Element element1, Element element2) { + try { + return dateFormat.parse(element2.getElementsByTag("td").get(1).text()) + .compareTo(dateFormat.parse(element1.getElementsByTag("td").get(1).text())); + } catch (ParseException e) { + LOGGER.error(EELFLoggerDelegate.errorLogger, + "Error when parsing date. " + UserUtils.getStackTrace(e)); + return 0; + } + } + }); + for (Element element : elements) { + try { + String elementTimestamp = element.getElementsByTag("td").get(0).getElementsByTag("a").get(0).text(); + elementTimestamp = elementTimestamp.substring(0, elementTimestamp.length() - 1); + ValidationNexusTestResult vNexusResult = this.getResult(name, version, siloText, elementTimestamp); + if (vNexusResult.getResult() != outcome) { + continue; + } + if (optional != null && vNexusResult.getOptional() != optional) { + continue; + } + if (allLayers != null && vNexusResult.getAllLayers() != allLayers) { + continue; + } + return vNexusResult; + } catch (HttpException ex) { + LOGGER.warn(EELFLoggerDelegate.auditLogger, + "Error when trying to retrieve results. " + UserUtils.getStackTrace(ex)); + continue; + } + } + return null; } - public List getRobotTestResults() throws ClientHandlerException, UniformInterfaceException, - JsonParseException, JsonMappingException, IOException, KeyManagementException, NoSuchAlgorithmException { - List listOfwrappers = new ArrayList(); + public List getWRobotTestResults(@Nonnull String name, @Nonnull String version, + @Nonnull String siloText, @Nonnull String timestamp) + throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, + IOException, KeyManagementException, NoSuchAlgorithmException { + String nexusUrl = this.baseurl + "/" + siloText + "/" + name + "/" + version + "/" + timestamp + "/results"; + List listOfwrappers = new ArrayList(); LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get the blueprint layers"); - setProperties(); - WebResource webResource = this.client.resource(this.baseurl + "/"); + WebResource webResource = this.client.resource(nexusUrl + "/"); LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); ClientResponse response = webResource.get(ClientResponse.class); if (response.getStatus() != 200) { @@ -135,31 +388,38 @@ public final class NexusExecutorClient { + response.getStatus() + " and message: " + response.getEntity(String.class)); } Document document = Jsoup.parse(response.getEntity(String.class)); - List elements = - document.getElementsByTag("body").get(0).getElementsByTag("table").get(0).getElementsByTag("tr"); + List elements = document.getElementsByTag("body").get(0).getElementsByTag("table").get(0) + .getElementsByTag("tr"); for (int i = 2; i < elements.size(); i++) { - String layer = elements.get(i).getElementsByTag("td").get(0).getElementsByTag("a").get(0).text(); - layer = layer.substring(0, layer.length() - 1); - List robotTestResults = getResultsOfALayer(this.baseurl + "/" + layer); - WrapperRobotTestResult wrapper = new WrapperRobotTestResult(); - wrapper.setBlueprintLayer(BlueprintLayer.valueOf(layer.substring(0, 1).toUpperCase() + layer.substring(1))); - wrapper.setRobotTestResults(robotTestResults); - listOfwrappers.add(wrapper); + try { + String layer = elements.get(i).getElementsByTag("td").get(0).getElementsByTag("a").get(0).text(); + layer = layer.substring(0, layer.length() - 1); + if (layer.contains("test")) { + continue; + } + List robotTestResults = getRobotTestResults(nexusUrl + "/" + layer); + WRobotNexusTestResult wrapper = new WRobotNexusTestResult(); + wrapper.setBlueprintLayer(BlueprintLayer.valueOf(layer)); + wrapper.setRobotTestResults(robotTestResults); + listOfwrappers.add(wrapper); + } catch (HttpException | IllegalArgumentException ex) { + LOGGER.warn(EELFLoggerDelegate.auditLogger, "Exception occured while retrieving robot results"); + continue; + } } return listOfwrappers; } - private List getResultsOfALayer(String resultsUrl) + private List getRobotTestResults(String resultsUrl) throws ClientHandlerException, UniformInterfaceException, JsonParseException, JsonMappingException, IOException, KeyManagementException, NoSuchAlgorithmException { - List robotTestResults = new ArrayList(); - LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get Robot Test Results"); - setProperties(); + List rTestResults = new ArrayList(); + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get test suites results"); WebResource webResource = this.client.resource(resultsUrl + "/"); LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); ClientResponse response = webResource.get(ClientResponse.class); if (response.getStatus() != 200) { - throw new HttpException("Could not retrieve robot test results from Nexus. HTTP error code : " + throw new HttpException("Could not retrieve test suites results from Nexus. HTTP error code : " + response.getStatus() + " and message: " + response.getEntity(String.class)); } Document document = Jsoup.parse(response.getEntity(String.class)); @@ -172,7 +432,7 @@ public final class NexusExecutorClient { LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); response = webResource.get(ClientResponse.class); if (response.getStatus() != 200) { - throw new HttpException("Could not retrieve robot test result from Nexus. HTTP error code : " + throw new HttpException("Could not retrieve test suite result from Nexus. HTTP error code : " + response.getStatus() + " and message: " + response.getEntity(String.class)); } String result = response.getEntity(String.class); @@ -183,21 +443,86 @@ public final class NexusExecutorClient { mapper.setSerializationInclusion(Include.NON_NULL); RobotTestResult robotTestResult = mapper.readValue(xmlJSONObj.toString(), RobotTestResult.class); robotTestResult.setName(testSuiteName); - robotTestResults.add(robotTestResult); + rTestResults.add(robotTestResult); + } + return rTestResults; + } + + private boolean determineResult(List wTestResults) { + boolean result = true; + for (WRobotNexusTestResult wTestResult : wTestResults) { + for (RobotTestResult robotTestResult : wTestResult.getRobotTestResults()) { + for (Status status : robotTestResult.getRobot().getStatistics().getTotal().getStat()) { + if (status.getContent().trim().equals("All Tests") && status.getFail() > 0) { + result = false; + } + } + } + } + return result; + } + + private List findLastElementsByDate(List elements, int noOfLastElements) { + if (elements.size() <= noOfLastElements) { + return elements; } - return robotTestResults; + DateFormat dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); + Collections.sort(elements, new Comparator() { + @Override + public int compare(Element element1, Element element2) { + try { + return dateFormat.parse(element2.getElementsByTag("td").get(1).text()) + .compareTo(dateFormat.parse(element1.getElementsByTag("td").get(1).text())); + } catch (ParseException e) { + LOGGER.error(EELFLoggerDelegate.errorLogger, + "Error when parsing date. " + UserUtils.getStackTrace(e)); + return 0; + } + } + }); + return elements.subList(0, noOfLastElements); } - private void setProperties() throws NoSuchAlgorithmException, KeyManagementException { - SSLContext sslContext = SSLContext.getInstance("SSL"); - sslContext.init(null, this.trustAll, new java.security.SecureRandom()); - HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); - // Install the all-trusting host verifier - HttpsURLConnection.setDefaultHostnameVerifier(this.hostnameVerifier); - DefaultClientConfig config = new DefaultClientConfig(); - Map properties = config.getProperties(); - HTTPSProperties httpsProperties = new HTTPSProperties((str, sslSession) -> true, sslContext); - properties.put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES, httpsProperties); + private Element findElementByTimestamp(List elements, String timestamp) { + for (Element element : elements) { + String elementTimestamp = element.getElementsByTag("td").get(0).getElementsByTag("a").get(0).text(); + elementTimestamp = elementTimestamp.substring(0, elementTimestamp.length() - 1); + if (elementTimestamp.equals(timestamp)) { + return element; + } + } + return null; } + private List findElementsBasedOnDate(List elements, Date date) throws ParseException { + DateFormat formatTime = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); + DateFormat formatNoTime = new SimpleDateFormat("EEE MMM dd zzz yyyy", Locale.US); + List desiredElements = new ArrayList(); + for (Element element : elements) { + String lastModified = element.getElementsByTag("td").get(1).text(); + if (formatNoTime.format(formatTime.parse(lastModified)).compareTo(formatNoTime.format(date)) == 0) { + desiredElements.add(element); + } + } + return desiredElements; + } + + private TestInfoYaml getTestInfo(String timestampUrl) throws JsonParseException, JsonMappingException, IOException { + LOGGER.info(EELFLoggerDelegate.applicationLogger, "Trying to get test info"); + WebResource webResource = this.client.resource(timestampUrl + "/results/test_info.yaml"); + LOGGER.debug(EELFLoggerDelegate.debugLogger, "Request URI of get: " + webResource.getURI().toString()); + ClientResponse response = webResource.get(ClientResponse.class); + if (response.getStatus() != 200) { + return null; + } + String testInfo = response.getEntity(String.class); + ObjectMapper yamlReader = new ObjectMapper(new YAMLFactory()); + Object obj = yamlReader.readValue(testInfo, Object.class); + ObjectMapper mapper = new ObjectMapper(); + mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + mapper.setSerializationInclusion(Include.NON_NULL); + ObjectMapper jsonWriter = new ObjectMapper(); + return mapper.readValue(jsonWriter.writeValueAsString(obj), TestInfoYaml.class); + } }