use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.
the class FieldDetectorConnector method detect.
/**
* Send image to image field detector and retrieve the box and text
* @param imageFile
* @param resizeFactor
* @param urlPath
* @return
*/
private JSONObject detect(File imageFile, double resizeFactor, String urlPath) {
if (imageFile == null) {
throw new ScenarioException("Image file is null");
}
if (!imageFile.exists()) {
throw new ScenarioException(String.format("Image file %s not found", imageFile.getAbsolutePath()));
}
HttpResponse<JsonNode> fieldDefinition = Unirest.post(url + urlPath).field("resize", resizeFactor).field("image", imageFile).asJson();
JSONObject bodyObject = fieldDefinition.getBody().getObject();
if (fieldDefinition.getStatus() != 200) {
try {
throw new ScenarioException("Field detector returned error: " + bodyObject.get("error"));
} catch (NullPointerException e) {
throw new ScenarioException("Field detector returned error");
}
}
List<String> imageNames = new ArrayList<>(Arrays.asList(JSONObject.getNames(bodyObject)));
imageNames.remove("error");
if (imageNames.isEmpty()) {
throw new ScenarioException("Field detector did not return any information: " + bodyObject.get("error"));
}
return bodyObject.getJSONObject(imageNames.get(0));
}
use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.
the class Campaign method getAll.
/**
* Get list of all campaigns
* @return
*/
@SuppressWarnings("unchecked")
public static List<Campaign> getAll() {
try {
JSONObject json = getPagedJSonResponse(buildGetRequest(apiRootUrl + CAMPAIGNS_URL));
List<Campaign> campaigns = new ArrayList<>();
if (json.has(FIELD_EMBEDDED)) {
for (JSONObject folderJson : (List<JSONObject>) json.getJSONObject(FIELD_EMBEDDED).getJSONArray(FIELD_CAMPAIGNS).toList()) {
campaigns.add(Campaign.fromJson(folderJson));
}
}
return campaigns;
} catch (UnirestException e) {
throw new ScenarioException("Cannot get all campaigns", e);
}
}
use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.
the class Campaign method create.
public static Campaign create(Project project, String campaignName, CampaignFolder parentFolder) {
try {
JSONObject body = new JSONObject();
body.put(FIELD_TYPE, TYPE_CAMPAIGN);
body.put(FIELD_NAME, campaignName);
body.put("status", "PLANNED");
JSONObject parent = new JSONObject();
if (parentFolder == null) {
parent.put(FIELD_ID, project.id);
parent.put(FIELD_TYPE, "project");
} else {
parent.put(FIELD_ID, parentFolder.id);
parent.put(FIELD_TYPE, "campaign-folder");
}
body.put("parent", parent);
JSONObject json = getJSonResponse(buildPostRequest(apiRootUrl + CAMPAIGNS_URL).body(body));
return fromJson(json);
} catch (UnirestException e) {
throw new ScenarioException(String.format("Cannot create campaign %s", campaignName), e);
}
}
use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.
the class Campaign method getIterations.
/**
* get iterations for the current campaign
*/
@SuppressWarnings("unchecked")
public List<Iteration> getIterations() {
try {
JSONObject json = getPagedJSonResponse(buildGetRequest(url + String.format(ITERATIONS_URL, id)));
List<Iteration> iterations = new ArrayList<>();
if (json.has(FIELD_EMBEDDED)) {
for (JSONObject iterationJson : (List<JSONObject>) json.getJSONObject(FIELD_EMBEDDED).getJSONArray("iterations").toList()) {
iterations.add(Iteration.fromJson(iterationJson));
}
}
return iterations;
} catch (UnirestException e) {
throw new ScenarioException(String.format("Cannot get list of iterations for campaign %s", name));
}
}
use of com.seleniumtests.customexception.ScenarioException in project seleniumRobot by bhecquet.
the class CampaignFolder method fromJson.
public static CampaignFolder fromJson(JSONObject json) {
try {
Entity parent;
if (json.has(FIELD_PARENT)) {
if (TYPE_PROJECT.equals(json.getJSONObject(FIELD_PARENT).getString(FIELD_TYPE))) {
parent = Project.fromJson(json.getJSONObject(FIELD_PARENT));
} else if (TYPE_CAMPAIGN_FOLDER.equals(json.getJSONObject(FIELD_PARENT).getString(FIELD_TYPE))) {
parent = CampaignFolder.fromJson(json.getJSONObject(FIELD_PARENT));
} else {
parent = null;
}
} else {
parent = null;
}
Project project = null;
if (json.has(TYPE_PROJECT)) {
project = Project.fromJson(json.getJSONObject(TYPE_PROJECT));
}
return new CampaignFolder(json.getJSONObject("_links").getJSONObject("self").getString("href"), json.getInt(FIELD_ID), json.getString(FIELD_NAME), project, parent);
} catch (JSONException e) {
throw new ScenarioException(String.format("Cannot create CampaignFolder from JSON [%s] data: %s", json.toString(), e.getMessage()));
}
}
Aggregations