package com.appthwack.appthwack;
import java.util.List;
import java.util.HashMap;
import java.io.File;
import java.lang.Math;
import com.appthwack.appthwack.AppThwackResult;
import com.appthwack.appthwack.AppThwackProject;
import com.appthwack.appthwack.AppThwackException;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.ClientResponse;
import javax.ws.rs.core.MediaType;
/**
* Represents an instance of a test run on AppThwack.
* @author ahawker
*
*/
@JsonAutoDetect
@JsonIgnoreProperties(ignoreUnknown=true)
public class AppThwackRun {
private static final int MinimumPollingInterval = 5 * 1000;
private static final int DefaultPollingInterval = 30 * 1000;
@JsonProperty("run_id")
public Integer id;
@JsonIgnore
private WebResource root;
@JsonIgnore
private AppThwackProject project;
public AppThwackRun() {
}
public AppThwackRun(Integer id) {
this.id = id;
}
/**
* Create a new AppThwackRun for the given project and run ids.
* @param projectId Id of project which performed the run.
* @param runId Id of test run.
* @param root HTTP resource root.
*/
public AppThwackRun(AppThwackProject project, Integer runId, WebResource root) {
this.project = project;
this.id = runId;
this.root = root;
}
/**
* Returns URL to this AppThwackRun instance visible on the site.
* @return
*/
public String getWebUrl() {
return String.format("%s/run/%d", project.getWebUrl(), id);
}
/**
* Returns true if the run has completed and results are ready to download.
* @return
*/
public boolean isCompleted() {
AppThwackResult.ResultSummary summary = getResultsSummary();
return summary.status.equalsIgnoreCase("completed")
&& summary.reportFile != null
&& !summary.reportFile.isEmpty();
}
/**
* Blocks until the current run has completed, checking
* status with the default polling interval.
*/
public AppThwackResult waitForCompleted() {
return waitForCompleted(DefaultPollingInterval);
}
/**
* Blocks until the current run has completed, checking
* status with the given polling interval.
* @param ms status polling interval in milliseconds
*/
public AppThwackResult waitForCompleted(int ms) {
ms = Math.max(MinimumPollingInterval, ms);
while (true) {
if (isCompleted()) {
return getResults();
}
try {
Thread.sleep(ms);
}
catch (InterruptedException ex) {
break;
}
}
return null;
}
/**
* Returns the status string of the current run.
* @return Status of the run.
*/
public String getStatus() {
HashMap<String, String> map = root
.path("run")
.path(Integer.toString(project.id))
.path(Integer.toString(id))
.path("status")
.get(new GenericType<HashMap<String, String>>(){});
return map.get("status");
}
/**
* Returns the results generated by this run.
* @return result highlights
*/
public AppThwackResult getResults() {
AppThwackResult result = root
.path("run")
.path(Integer.toString(project.id))
.path(Integer.toString(id))
.get(AppThwackResult.class);
result.setRun(this);
return result;
}
/**
* Returns the results summary generated by the run.
* @return
*/
public AppThwackResult.ResultSummary getResultsSummary() {
AppThwackResult.ResultSummary summary = root
.path("run")
.path(Integer.toString(project.id))
.path(Integer.toString(id))
.path("summary")
.get(AppThwackResult.ResultSummary.class);
summary.setRun(this);
return summary;
}
/**
* Returns temporary file object for downloaded results.
* @return saved local copy of results
*/
public File downloadResults() {
return root.path("run")
.path(Integer.toString(project.id))
.path(Integer.toString(id))
.queryParam("format", "archive")
.get(File.class);
}
public void setRoot(WebResource root) {
this.root = root;
}
public AppThwackProject getProject() {
return project;
}
public void setProject(AppThwackProject project) {
this.project = project;
}
@Override
public String toString() {
return String.format("AppThwackRun (%s)", getWebUrl());
}
}