Skip to content

Commit

Permalink
change API submit test logs
Browse files Browse the repository at this point in the history
  • Loading branch information
phuthanhta committed Jan 18, 2018
1 parent fb2d0bd commit c522a79
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 36 deletions.
105 changes: 98 additions & 7 deletions src/main/java/com/qasymphony/ci/plugin/AutomationTestService.java
Expand Up @@ -8,7 +8,11 @@
import com.qasymphony.ci.plugin.utils.HttpClientUtils;
import com.qasymphony.ci.plugin.utils.JsonUtils;
import com.qasymphony.ci.plugin.utils.ResponseEntity;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -17,30 +21,44 @@
* @author anpham
*/
public class AutomationTestService {
private static final String AUTO_TEST_LOG_ENDPOINT = "%s/api/v3/projects/%s/test-runs/%s/auto-test-logs/ci/%s";
private static final String AUTO_TEST_LOG_ENDPOINT = "%s/api/v3.1/projects/%s/test-runs/%s/auto-test-logs?type=automation";
private static final String API_SUBMIT_TASK_STATUS = "%s/api/v3/projects/queue-processing/%s";

public static ResponseEntity push(String buildNumber, String buildPath, List<AutomationTestResult> testResults, Configuration configuration, Map<String, String> headers)
public static ResponseEntity push(String buildNumber, String buildPath, List<AutomationTestResult> testResults, Configuration configuration, String accessToken)
throws SubmittedException {

if (testResults.size() <= 0)
return null;

for (int i = 0; i < testResults.size(); i++) {
AutomationTestResult result = testResults.get(i);
result.setBuildNumber(buildNumber);
result.setBuildURL(buildPath);
}
AutomationTestResultWrapper wrapper = new AutomationTestResultWrapper();

wrapper.setBuildNumber(buildNumber);
Long moduleId = configuration.getModuleId();
if (0 < moduleId) {
wrapper.setParent_module(moduleId);
}
wrapper.setBuildPath(buildPath);
wrapper.setTestResults(testResults);

wrapper.setTest_logs(testResults);
Long testSuiteId = prepareTestSuite(configuration, accessToken);
if (-1 == testSuiteId) {
throw new SubmittedException("Could not find test suite to submit test logs");
}
wrapper.setTest_suite(testSuiteId);
Map<String, String> headers = OauthProvider.buildHeaders(accessToken, null);
/**
* using {@link String#format(Locale, String, Object...)} instead {@link java.text.MessageFormat#format(String, Object...)}
* to avoid unexpected formatted link. see: QTE-2798 for more details.
*/
String url = String.format(AUTO_TEST_LOG_ENDPOINT, configuration.getUrl(), configuration.getProjectId(), 0, configuration.getId());
String url = String.format(AUTO_TEST_LOG_ENDPOINT, configuration.getUrl(), configuration.getProjectId(), 0);

ResponseEntity responseEntity = null;
try {
responseEntity = HttpClientUtils.post(url, headers, JsonUtils.toJson(wrapper));
String data = JsonUtils.toJson(wrapper);
responseEntity = HttpClientUtils.post(url, headers, data);
} catch (ClientRequestException e) {
throw new SubmittedException(e.getMessage(), null == responseEntity ? 0 : responseEntity.getStatusCode());
}
Expand All @@ -59,4 +77,77 @@ public static ResponseEntity getTaskStatus(Configuration configuration, long tas

return responseEntity;
}

private static Long createNewTestSuite(Configuration configuration, String accessToken, Long parentId, String parentType, String testSuiteName) {
try {
Object createResult = ConfigService.createTestSuite(configuration.getUrl(), accessToken, configuration.getProjectId(), parentId, parentType, testSuiteName);
JSONObject retObj = JSONObject.fromObject(createResult);
return retObj.getLong("id");
} catch (Exception ex) {
ex.printStackTrace();
}
return -1L;
}

private static Long findTestSuiteUnderContainerByName(Configuration configuration, String accessToken, Long parentId, String parentType, String testSuiteName) {
Object testSuites = ConfigService.getTestSuiteChildren(configuration.getUrl(), accessToken, configuration.getProjectId(), parentId, parentType);
JSONArray suites = JSONArray.fromObject(testSuites);
Long foundTestSuiteId = -1L;
if (null != suites && 0 < suites.size()) {
for (int i = 0; i < suites.size(); i++) {
String name = suites.getJSONObject(i).getString("name");
if (0 == testSuiteName.compareTo(name)) {
foundTestSuiteId = suites.getJSONObject(i).getLong("id");
break;
}
}
}
return foundTestSuiteId;
}

private static Long prepareTestSuite(Configuration configuration, String accessToken) {
Long projectId = configuration.getProjectId();
Date now = new Date();
SimpleDateFormat ft = new SimpleDateFormat("MM-dd-yyyy");
String testSuiteName = String.format("%s %s", configuration.getJenkinsProjectName(), ft.format(now));
if (!configuration.isSubmitToContainer()) {
// submit to release
// create/get testsuite under create configuration.getReleaseId()
Long foundTestSuiteId = findTestSuiteUnderContainerByName(configuration, accessToken, configuration.getReleaseId(), "release", testSuiteName);
if (-1L != foundTestSuiteId) {
return foundTestSuiteId;
}
// create new test suite
return createNewTestSuite(configuration, accessToken, configuration.getReleaseId(), "release", testSuiteName );
} else {
JSONObject json = configuration.getContainerJSONObject();
if (null != json) {
JSONArray containerPath = json.getJSONArray("containerPath");
if (null != containerPath) {
int pathSize = containerPath.size();
if (0 < pathSize) {
JSONObject jsonNode = containerPath.getJSONObject(pathSize - 1);
Long nodeId = jsonNode.getLong("nodeId");
String nodeType = jsonNode.getString("nodeType");
switch (nodeType) {
case "release":
case "test-cycle":
if (!configuration.isCreateNewTestSuiteEveryBuild()) {
Long foundTestSuite = findTestSuiteUnderContainerByName(configuration, accessToken, nodeId, nodeType, testSuiteName);
if (-1L != foundTestSuite) {
return foundTestSuite;
}
}
return createNewTestSuite(configuration, accessToken, nodeId, nodeType, testSuiteName);
case "test-suite":
return nodeId;
}
}
}

}

}
return -1L;
}
}
58 changes: 40 additions & 18 deletions src/main/java/com/qasymphony/ci/plugin/ConfigService.java
Expand Up @@ -383,24 +383,46 @@ public static Object getTestCycleChildren(String qTestUrl, String accessToken, L
}
}

public static Object getTestSuiteChildren(String qTestUrl, String accessToken, Long projectId, Long parentId, String parentType) {
String url = String.format("%s/api/v3/projects/%s/test-suites?", qTestUrl, projectId);
if (null != parentId) {
url += "parentId=" + parentId;
if (null != parentType) {
url += "&parentType=" + parentType;
}
}
try {
ResponseEntity responseEntity = HttpClientUtils.get(url, OauthProvider.buildHeaders(accessToken, null));
if (HttpStatus.SC_OK != responseEntity.getStatusCode()) {
return null;
}
return responseEntity.getBody();
} catch (ClientRequestException e) {
LOG.log(Level.WARNING, "Cannot get test-suites from: " + qTestUrl + "," + e.getMessage());
return null;
}
public static Object getTestSuiteChildren(String qTestUrl, String accessToken, Long projectId, Long parentId, String parentType) {
String url = String.format("%s/api/v3/projects/%s/test-suites?", qTestUrl, projectId);
if (null != parentId) {
url += "parentId=" + parentId;
if (null != parentType) {
url += "&parentType=" + parentType;
}
}
try {
ResponseEntity responseEntity = HttpClientUtils.get(url, OauthProvider.buildHeaders(accessToken, null));
if (HttpStatus.SC_OK != responseEntity.getStatusCode()) {
return null;
}
return responseEntity.getBody();
} catch (ClientRequestException e) {
LOG.log(Level.WARNING, "Cannot get test-suites from: " + qTestUrl + "," + e.getMessage());
return null;
}
}

public static Object createTestSuite(String qTestUrl, String accessToken, Long projectId, Long parentId, String parentType, String testSuiteName) {
String url = String.format("%s/api/v3/projects/%s/test-suites?", qTestUrl, projectId);
if (null != parentId) {
url += "parentId=" + parentId;
if (null != parentType) {
url += "&parentType=" + parentType;
}
}
JSONObject json = new JSONObject();
json.put("name", testSuiteName);

try {
ResponseEntity responseEntity = HttpClientUtils.post(url, OauthProvider.buildHeaders(accessToken, null), JsonUtils.toJson(json));
if (HttpStatus.SC_OK != responseEntity.getStatusCode()) {
return null;
}
return responseEntity.getBody();
} catch (ClientRequestException e) {
LOG.log(Level.WARNING, "Cannot create test-suites from: " + qTestUrl + "," + e.getMessage());
return null;
}
}
}
Expand Up @@ -11,6 +11,12 @@
* @author anpham
*/
public class AutomationTestResult {
@JsonProperty("build_number")
private String buildNumber;

@JsonProperty("build_url")
private String buildURL;

@JsonProperty("exe_start_date")
private Date executedStartDate;
@JsonProperty("exe_end_date")
Expand Down Expand Up @@ -101,6 +107,22 @@ public List<AutomationAttachment> addAttachment(AutomationAttachment attachment)
attachments.add(attachment);
return attachments;
}
public String getBuildNumber() {
return buildNumber;
}

public void setBuildNumber(String buildNumber) {
this.buildNumber = buildNumber;
}

public String getBuildURL() {
return buildURL;
}

public void setBuildURL(String buildURL) {
this.buildURL = buildURL;
}


/**
* Add testLog and resolve status of testResult
Expand Down
Expand Up @@ -10,7 +10,44 @@ public class AutomationTestResultWrapper {
private String buildNumber;
private String buildPath;

private List<AutomationTestResult> testResults;
private Long test_suite;

public Long getParent_module() {
return parent_module;
}

public void setParent_module(Long parent_module) {
this.parent_module = parent_module;
}

private Long parent_module;
private List<AutomationTestResult> test_logs;

public Long getTest_suite() {
return test_suite;
}

public void setTest_suite(Long test_suite) {
this.test_suite = test_suite;
}

public List<AutomationTestResult> getTest_logs() {
return test_logs;
}

public void setTest_logs(List<AutomationTestResult> test_logs) {
this.test_logs = test_logs;
}

public String getExecution_date() {
return execution_date;
}

public void setExecution_date(String execution_date) {
this.execution_date = execution_date;
}

private String execution_date;

public String getBuildNumber() {
return buildNumber;
Expand All @@ -28,12 +65,6 @@ public void setBuildPath(String buildPath) {
this.buildPath = buildPath;
}

public List<AutomationTestResult> getTestResults() {
return testResults;
}

public void setTestResults(List<AutomationTestResult> testResults) {
this.testResults = testResults;
}

}
12 changes: 11 additions & 1 deletion src/main/java/com/qasymphony/ci/plugin/model/Configuration.java
Expand Up @@ -317,9 +317,19 @@ public String getFakeContainerName() {
} catch (Exception ex) {
ex.printStackTrace();
}

return "";
}

public JSONObject getContainerJSONObject() {
try {
JSONObject json = JSONObject.fromObject(this.containerJSONSetting);
JSONArray containerPath = JSONArray.fromObject(json.getString("containerPath"));
json.put("containerPath", containerPath);
return json;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}

@Extension
Expand Down
Expand Up @@ -41,7 +41,7 @@ public class JunitQtestSubmitterImpl implements JunitSubmitter {
request.getConfiguration().getUrl(), request.getConfiguration().getAppSecretKey()));

ResponseEntity responseEntity = AutomationTestService.push(request.getBuildNumber(), request.getBuildPath(),
request.getTestResults(), request.getConfiguration(), OauthProvider.buildHeaders(accessToken, null));
request.getTestResults(), request.getConfiguration(), accessToken);
AutomationTestResponse response = null;
if (responseEntity.getStatusCode() == HttpStatus.SC_CREATED) {
//receive task response
Expand Down
4 changes: 2 additions & 2 deletions src/main/webapp/js/app.js
Expand Up @@ -3,7 +3,7 @@ var currentSelectedNodeId = -1;
var currentJSONContainer = {
selectedContainer: {
name: "",
daily_create_test_suite: false
daily_create_test_suite: true
},
containerPath: []
};
Expand Down Expand Up @@ -204,7 +204,7 @@ function loadProjectData() {
currentJSONContainer = {
selectedContainer: {
name: "",
daily_create_test_suite: false
daily_create_test_suite: true
},
containerPath: []
};
Expand Down

0 comments on commit c522a79

Please sign in to comment.