Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testrail 7.2 api changes #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.nullin</groupId>
<artifactId>testrail-integration</artifactId>
<version>2.3.5-SNAPHSOT</version>
<version>2.3.5-SNAPSHOT</version>
<packaging>pom</packaging>

<name>TestRail Integration</name>
Expand Down Expand Up @@ -60,6 +60,19 @@
<target>1.7</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
2 changes: 1 addition & 1 deletion testrail-connector/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>com.nullin</groupId>
<artifactId>testrail-integration</artifactId>
<version>2.3.5-SNAPHSOT</version>
<version>2.3.5-SNAPSHOT</version>
</parent>

<artifactId>testrail-connector</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.util.Set;
import java.util.logging.Logger;

import org.apache.http.HttpException;

import com.nullin.testrail.client.ClientException;
import com.nullin.testrail.client.TestRailClient;
import com.nullin.testrail.dto.Case;
Expand Down Expand Up @@ -171,58 +173,77 @@ public void reportResult(String automationId, Map<String, Object> properties) {
if (!enabled) {
return; //do nothing
}

int retryCount = 5;

while (retryCount > 0) {
ResultStatus resultStatus = (ResultStatus)properties.get(KEY_STATUS);
Throwable throwable = (Throwable)properties.get(KEY_THROWABLE);
String elapsed = (String)properties.get(KEY_ELAPSED);
String screenshotUrl = (String)properties.get(KEY_SCREENSHOT_URL);
Map<String, String> moreInfo = (Map<String, String>)properties.get(KEY_MORE_INFO);

try {
Integer caseId = caseIdLookupMap.get(automationId);
if (caseId == null) {
logger.severe("Didn't find case id for test with automation id " + automationId);
return; //nothing more to do
}

StringBuilder comment = new StringBuilder("More Info (if any):\n");
if (moreInfo != null && !moreInfo.isEmpty()) {
for (Map.Entry<String, String> entry: moreInfo.entrySet()) {
comment.append("- ").append(entry.getKey()).append(" : ")
.append('`').append(entry.getValue()).append("`\n");
}
} else {
comment.append("- `none`\n");
}
comment.append("\n");
if (screenshotUrl != null && !screenshotUrl.isEmpty()) {
comment.append("![](").append(screenshotUrl).append(")\n\n");
}
if (resultStatus.equals(ResultStatus.SKIP)) {
comment.append("Test skipped because of configuration method failure. " +
"Related config error (if captured): \n\n");
comment.append(getStackTraceAsString(throwable));
}
if (resultStatus.equals(ResultStatus.FAIL)) {
comment.append("Test failed with following exception (if captured): \n\n");
comment.append(getStackTraceAsString(throwable));
}

//add the result
Map<String, Object> body = new HashMap<String, Object>();
body.put("status_id", getStatus(resultStatus));
body.put("comment", new String(comment.toString().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
body.put("elapsed", elapsed);

Integer runId = testToRunIdMap.get(automationId + config);
if (runId == null) {
throw new IllegalArgumentException("Unable to find run id for test with automation id "
+ automationId + " and configuration set as " + config);
}
client.addResultForCase(runId, caseId, body);

retryCount = 0;
} catch(Exception ex) {
if (ex.getMessage().contains("Received status code 409 with content")) {
logger.warning("Maintenance mode, sleep 1 second and retry: " + ex.getMessage());
retryCount--;

ResultStatus resultStatus = (ResultStatus)properties.get(KEY_STATUS);
Throwable throwable = (Throwable)properties.get(KEY_THROWABLE);
String elapsed = (String)properties.get(KEY_ELAPSED);
String screenshotUrl = (String)properties.get(KEY_SCREENSHOT_URL);
Map<String, String> moreInfo = (Map<String, String>)properties.get(KEY_MORE_INFO);

try {
Integer caseId = caseIdLookupMap.get(automationId);
if (caseId == null) {
logger.severe("Didn't find case id for test with automation id " + automationId);
return; //nothing more to do
}

StringBuilder comment = new StringBuilder("More Info (if any):\n");
if (moreInfo != null && !moreInfo.isEmpty()) {
for (Map.Entry<String, String> entry: moreInfo.entrySet()) {
comment.append("- ").append(entry.getKey()).append(" : ")
.append('`').append(entry.getValue()).append("`\n");
}
} else {
comment.append("- `none`\n");
}
comment.append("\n");
if (screenshotUrl != null && !screenshotUrl.isEmpty()) {
comment.append("![](").append(screenshotUrl).append(")\n\n");
}
if (resultStatus.equals(ResultStatus.SKIP)) {
comment.append("Test skipped because of configuration method failure. " +
"Related config error (if captured): \n\n");
comment.append(getStackTraceAsString(throwable));
}
if (resultStatus.equals(ResultStatus.FAIL)) {
comment.append("Test failed with following exception (if captured): \n\n");
comment.append(getStackTraceAsString(throwable));
}

//add the result
Map<String, Object> body = new HashMap<String, Object>();
body.put("status_id", getStatus(resultStatus));
body.put("comment", new String(comment.toString().getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8));
body.put("elapsed", elapsed);

Integer runId = testToRunIdMap.get(automationId + config);
if (runId == null) {
throw new IllegalArgumentException("Unable to find run id for test with automation id "
+ automationId + " and configuration set as " + config);
}
client.addResultForCase(runId, caseId, body);
} catch(Exception ex) {
//only log and do nothing else
logger.severe("Ran into exception " + ex.getMessage());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}

} else {
//only log and do nothing else
logger.severe("Ran into exception " + ex.getMessage());

retryCount = 0;
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package com.nullin.testrail.client;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.binary.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.nullin.testrail.dto.Case;
import com.nullin.testrail.dto.CaseList;
import com.nullin.testrail.dto.Milestone;
import com.nullin.testrail.dto.Plan;
import com.nullin.testrail.dto.PlanEntry;
Expand All @@ -19,6 +23,7 @@
import com.nullin.testrail.dto.Section;
import com.nullin.testrail.dto.Suite;
import com.nullin.testrail.dto.Test;
import com.nullin.testrail.dto.TestList;

/**
* TestRail Client for endpoints described at
Expand Down Expand Up @@ -156,7 +161,23 @@ public Test getTest(int testId) throws IOException, ClientException {
}

public List<Test> getTests(int runId) throws IOException, ClientException {
return objectMapper.readValue(client.invokeHttpGet("get_tests/" + runId), new TypeReference<List<Test>>(){});
List<Test> result = new ArrayList<>();
String url = "get_tests/" + runId;

while (url != null) {
TestList testList = objectMapper.readValue(client.invokeHttpGet(url), TestList.class);

if (testList != null && testList._links.next != null) {

result.addAll(testList.tests);

url = testList._links.next.replace("/api/v2", "");
} else {
break;
}
}

return result;
}

/*
Expand Down Expand Up @@ -193,7 +214,21 @@ public List<Case> getCases(int projectId, int suiteId, int sectionId, Map<String
url += "&" + entry.getKey() + "=" + entry.getValue();
}
}
return objectMapper.readValue(client.invokeHttpGet(url), new TypeReference<List<Case>>(){});

List<Case> result = new ArrayList<>();
while (url != null) {
CaseList caseList = objectMapper.readValue(client.invokeHttpGet(url), CaseList.class);

if (caseList != null && caseList._links.next != null) {
result.addAll(caseList.cases);

url = caseList._links.next.replace("/api/v2", "");
} else {
break;
}
}

return result;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nullin.testrail.dto;

import java.util.List;

public class CaseList extends ResultList {
public List<Case> cases;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.nullin.testrail.dto;

public class Links {
public String next;
public String prev;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.nullin.testrail.dto;

public class ResultList {
public int offset;
public int limit;
public int size;
public Links _links;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nullin.testrail.dto;

import java.util.List;

public class TestList extends ResultList {
public List<Test> tests;
}