use of com.qasymphony.ci.plugin.utils.ResponseEntity in project jenkin-qtest-plugin by QASymphony.
the class OauthProvider method getAccessToken.
public static String getAccessToken(String url, String apiKey, String secretKey) throws OAuthException {
StringBuilder sb = new StringBuilder().append(url).append("/oauth/token?grant_type=refresh_token").append("&refresh_token=").append(HttpClientUtils.encode(apiKey));
Map<String, String> headers = new HashMap<>();
headers.put(Constants.HEADER_AUTH, secretKey);
try {
ResponseEntity entity = HttpClientUtils.post(sb.toString(), headers, null);
if (HttpStatus.SC_OK != entity.getStatusCode()) {
throw new OAuthException(entity.getBody(), entity.getStatusCode());
}
JsonNode node = JsonUtils.readTree(entity.getBody());
if (null == node) {
throw new OAuthException("Cannot get access token from: " + entity.getBody(), entity.getStatusCode());
}
return JsonUtils.getText(node, "access_token");
} catch (Exception e) {
throw new OAuthException(e.getMessage(), e);
}
}
use of com.qasymphony.ci.plugin.utils.ResponseEntity in project jenkin-qtest-plugin by QASymphony.
the class JunitQtestSubmitterImpl method getTaskResponse.
private AutomationTestResponse getTaskResponse(JunitSubmitterRequest request, SubmittedTask task, Map<String, String> headers) throws SubmittedException {
ResponseEntity responseEntity;
try {
// get task status
responseEntity = AutomationTestService.getTaskStatus(request.getqTestURL(), task.getId(), headers);
} catch (ClientRequestException e) {
LoggerUtils.formatError(request.getListener().getLogger(), "Cannot get response of taskId: %s, error: %s", task.getId(), e.getMessage());
throw new SubmittedException(e.getMessage(), -1);
}
LOG.info(String.format("project:%s, status:%s, body:%s", request.getJenkinsProjectName(), null == responseEntity ? -1 : responseEntity.getStatusCode(), null == responseEntity ? "" : responseEntity.getBody()));
if ((null == responseEntity) || (responseEntity.getStatusCode() != HttpStatus.SC_OK)) {
throw new SubmittedException(ConfigService.getErrorMessage(responseEntity.getBody()), responseEntity.getStatusCode());
}
return new AutomationTestResponse(responseEntity.getBody());
}
use of com.qasymphony.ci.plugin.utils.ResponseEntity in project jenkin-qtest-plugin by QASymphony.
the class JunitQtestSubmitterImpl method submit.
@Override
public JunitSubmitterResult submit(JunitSubmitterRequest request) throws Exception {
String accessToken = OauthProvider.getAccessToken(request.getqTestURL(), request.getApiKey());
if (StringUtils.isEmpty(accessToken))
throw new SubmittedException(String.format("Cannot get access token from: %s, API key is: %s", request.getqTestURL(), request.getApiKey()));
ResponseEntity responseEntity = AutomationTestService.push(request.getBuildNumber(), request.getBuildPath(), request.getTestResults(), request, accessToken);
AutomationTestResponse response = null;
if (responseEntity.getStatusCode() == HttpStatus.SC_CREATED) {
// receive task response
SubmittedTask task = JsonUtils.fromJson(responseEntity.getBody(), SubmittedTask.class);
if (task == null || task.getId() <= 0)
throw new SubmittedException(responseEntity.getBody(), responseEntity.getStatusCode());
response = getSubmitLogResponse(request, task);
} else {
// if cannot passed validation from qTest
throw new SubmittedException(ConfigService.getErrorMessage(responseEntity.getBody()), responseEntity.getStatusCode());
}
Boolean nullResponse = (null == response);
Boolean isSubmitSuccess = ((!nullResponse && response.getTestSuiteId() > 0) ? true : false);
JunitSubmitterResult result = new JunitSubmitterResult().setNumberOfTestResult(request.getTestResults().size()).setTestSuiteId(nullResponse ? null : response.getTestSuiteId()).setTestSuiteName(nullResponse ? "" : response.getTestSuiteName()).setNumberOfTestLog(nullResponse ? 0 : response.getTotalTestLogs()).setSubmittedStatus(isSubmitSuccess ? JunitSubmitterResult.STATUS_SUCCESS : JunitSubmitterResult.STATUS_FAILED);
return result;
}
use of com.qasymphony.ci.plugin.utils.ResponseEntity in project jenkin-qtest-plugin by QASymphony.
the class AutomationTestService method push.
public static ResponseEntity push(String buildNumber, String buildPath, List<AutomationTestResult> testResults, JunitSubmitterRequest request, String accessToken) throws SubmittedException {
if (testResults.size() <= 0)
return null;
String url;
AutomationTestResultWrapper wrapper = new AutomationTestResultWrapper();
wrapper.setBuildNumber(buildNumber);
wrapper.setBuildPath(buildPath);
wrapper.setSkipCreatingAutomationModule(true);
Long moduleId = request.getModuleID();
if (null != moduleId && 0 < moduleId) {
wrapper.setParent_module(moduleId);
}
if (request.getSubmitToExistingContainer()) {
String fullURL = request.getJenkinsServerURL();
if (!fullURL.endsWith("/")) {
fullURL += "/";
}
fullURL += buildPath;
for (int i = 0; i < testResults.size(); i++) {
AutomationTestResult result = testResults.get(i);
result.setBuildNumber(buildNumber);
result.setBuildURL(fullURL);
}
url = String.format(AUTO_TEST_LOG_ENDPOINT_V3_1, request.getqTestURL(), request.getProjectID(), 0);
Long testSuiteId = prepareTestSuite(request, accessToken);
if (-1 == testSuiteId) {
throw new SubmittedException("Could not find or create test suite to submit test logs", -1);
}
wrapper.setTest_suite(testSuiteId);
wrapper.setTest_logs(testResults);
} else {
/**
* 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.
*/
url = String.format(AUTO_TEST_LOG_ENDPOINT_V3, request.getqTestURL(), request.getProjectID(), 0, request.getConfigurationID());
wrapper.setTestResults(testResults);
}
Map<String, String> headers = OauthProvider.buildHeaders(accessToken, null);
ResponseEntity responseEntity = null;
try {
String data = JsonUtils.toJson(wrapper);
responseEntity = HttpClientUtils.post(url, headers, data);
} catch (ClientRequestException e) {
throw new SubmittedException(e.getMessage(), null == responseEntity ? 0 : responseEntity.getStatusCode());
}
return responseEntity;
}
use of com.qasymphony.ci.plugin.utils.ResponseEntity in project jenkin-qtest-plugin by QASymphony.
the class AutomationTestService method getTaskStatus.
public static ResponseEntity getTaskStatus(String qTestURL, long taskId, Map<String, String> headers) throws ClientRequestException {
String url = String.format(API_SUBMIT_TASK_STATUS, qTestURL, taskId);
ResponseEntity responseEntity = null;
try {
responseEntity = HttpClientUtils.get(url, headers);
} catch (ClientRequestException e) {
throw e;
}
return responseEntity;
}
Aggregations