Best Cerberus-source code snippet using org.cerberus.service.xray.impl.XRayService.getToken
Source:XRayService.java
...89 private static final String DEFAULT_PROXYAUTHENT_PASSWORD = "squid";90 private static final String XRAYCLOUD_AUTHENT_URL = "https://xray.cloud.getxray.app/api/v2/authenticate";91 private static final String XRAYCLOUD_TESTEXECUTIONCREATION_URL = "https://xray.cloud.getxray.app/api/v2/import/execution";92 private static final String XRAYDC_TESTEXECUTIONCREATION_URLPATH = "/rest/raven/2.0/api/import/execution";93 private String getToken(String system, String origin) {94 try {95 if (cacheEntry.get("TOKEN-" + origin + "#" + system) != null) {96 return cacheEntry.get("TOKEN-" + origin + "#" + system).getString("value");97 }98 } catch (JSONException ex) {99 LOG.error(ex, ex);100 }101 return null;102 }103 private void putToken(String system, String origin, String value) {104 try {105 JSONObject entry = new JSONObject();106 entry.put("key", "TOKEN-" + origin + "#" + system);107 entry.put("value", value);108 entry.put("created", "now");109 cacheEntry.put("TOKEN-" + origin + "#" + system, entry);110 } catch (JSONException ex) {111 LOG.error(ex, ex);112 }113 }114 @Override115 public JSONArray getAllCacheEntries() {116 JSONArray arrayResult = new JSONArray();117 for (Map.Entry<String, JSONObject> entry : cacheEntry.entrySet()) {118 String key = entry.getKey();119 JSONObject val = entry.getValue();120 arrayResult.put(val);121 }122 return arrayResult;123 }124 @Override125 public void purgeAllCacheEntries() {126 cacheEntry.clear();127 LOG.info("All XRay config cache entries purged.");128 }129 @Override130 @Async131 public void createXRayTestExecution(TestCaseExecution execution) {132 try {133 Tag currentTag = new Tag();134 if ((TestCase.TESTCASE_ORIGIN_JIRAXRAYCLOUD.equalsIgnoreCase(execution.getTestCaseObj().getOrigine()))135 || TestCase.TESTCASE_ORIGIN_JIRAXRAYDC.equalsIgnoreCase(execution.getTestCaseObj().getOrigine())) {136 getXRayAuthenticationToken(execution.getTestCaseObj().getOrigine(), execution.getSystem());137 JSONObject xRayRequest = new JSONObject();138 LOG.debug("Calling JIRA XRay TestExecution creation. {}", execution.getId());139 if (!StringUtil.isNullOrEmpty(execution.getTag())) {140 currentTag = tagService.convert(tagService.readByKey(execution.getTag()));141 if ((currentTag != null)) {142 int lock = 0;143 // We lock the tag updating it to PENDING when empty.144 if (StringUtil.isNullOrEmpty(currentTag.getXRayTestExecution())) {145 lock = tagService.lockXRayTestExecution(currentTag.getTag(), currentTag);146 LOG.debug("Lock attempt : {}", lock);147 }148 if (lock == 0) {149 int maxIteration = 0;150 // We wait that JIRA provide the Epic and Cerberus update it.151 while ("PENDING".equals(currentTag.getXRayTestExecution()) && maxIteration++ < 20) {152 LOG.debug("Loop Until Tag is no longuer PENDING {}/20 - {}", maxIteration, execution.getId());153 currentTag = tagService.convert(tagService.readByKey(execution.getTag()));154 Thread.sleep(5000);155 }156 }157 xRayRequest = xRayGenerationService.generateCreateTestExecution(currentTag, execution);158 String xRayUrl = XRAYCLOUD_TESTEXECUTIONCREATION_URL;159 if (TestCase.TESTCASE_ORIGIN_JIRAXRAYDC.equalsIgnoreCase(execution.getTestCaseObj().getOrigine())) {160 xRayUrl = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_xraydc_url, execution.getSystem(), "");161 xRayUrl += XRAYDC_TESTEXECUTIONCREATION_URLPATH;162 }163 CloseableHttpClient httpclient = null;164 HttpClientBuilder httpclientBuilder;165 if (proxyService.useProxy(xRayUrl, "")) {166 String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", "", DEFAULT_PROXY_HOST);167 int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", "", DEFAULT_PROXY_PORT);168 HttpHost proxyHostObject = new HttpHost(proxyHost, proxyPort);169 if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", "", DEFAULT_PROXYAUTHENT_ACTIVATE)) {170 String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", "", DEFAULT_PROXYAUTHENT_USER);171 String proxyPassword = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", "", DEFAULT_PROXYAUTHENT_PASSWORD);172 CredentialsProvider credsProvider = new BasicCredentialsProvider();173 credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));174 LOG.debug("Activating Proxy With Authentification.");175 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject)176 .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())177 .setDefaultCredentialsProvider(credsProvider);178 } else {179 LOG.debug("Activating Proxy (No Authentification).");180 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject);181 }182 } else {183 httpclientBuilder = HttpClientBuilder.create();184 }185 boolean acceptUnsignedSsl = parameterService.getParameterBooleanByKey("cerberus_accept_unsigned_ssl_certificate", "", true);186 if (acceptUnsignedSsl) {187 // authorize non valide certificat ssl188 SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {189 @Override190 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {191 return true;192 }193 }).build();194 httpclientBuilder195 .setSSLContext(sslContext)196 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);197 }198 httpclient = httpclientBuilder.build();199 HttpPost post = new HttpPost(xRayUrl);200 StringEntity entity = new StringEntity(xRayRequest.toString());201 post.setEntity(entity);202 post.setHeader("Accept", "application/json");203 post.setHeader("Content-type", "application/json");204 post.setHeader("Authorization", "Bearer " + getToken(execution.getSystem(), execution.getTestCaseObj().getOrigine()));205 LOG.debug("Bearer " + getToken(execution.getSystem(), execution.getTestCaseObj().getOrigine()));206 try {207 HttpResponse response = httpclient.execute(post);208 int rc = response.getStatusLine().getStatusCode();209 if (rc >= 200 && rc < 300) {210 LOG.debug("XRay Test Execution request http return code : " + rc);211 String responseString = EntityUtils.toString(response.getEntity());212 LOG.debug("Response : {}", responseString);213 JSONObject xRayResponse = new JSONObject(responseString);214 String xrayURL = "";215 String xrayTestExecution = "";216 if (xRayResponse.has("key")) {217 xrayTestExecution = xRayResponse.getString("key");218 if (xRayResponse.has("self")) {219 URL xrURL = new URL(xRayResponse.getString("self"));220 xrayURL = xrURL.getProtocol() + "://" + xrURL.getHost();221 }222 if (!xrayURL.equals(currentTag.getXRayURL()) || !xrayTestExecution.equals(currentTag.getXRayTestExecution())) {223 // We avoid updating is the data did not change.224 currentTag.setXRayURL(xrayURL);225 currentTag.setXRayTestExecution(xrayTestExecution);226 tagService.updateXRayTestExecution(currentTag.getTag(), currentTag);227 }228 }229 LOG.debug("Setting new XRay TestExecution '{}' to tag '{}'", xRayResponse.getString("key"), currentTag.getTag());230 } else {231 LOG.warn("XRay Test Execution request http return code : " + rc);232 logEventService.createForPrivateCalls("XRAY", "APICALL", "Xray Execution creation request to '" + xRayUrl + "' failed with http return code : " + rc + ".");233 String responseString = EntityUtils.toString(response.getEntity());234 LOG.warn("Message sent to " + xRayUrl + " :");235 LOG.warn(xRayRequest.toString(1));236 LOG.warn("Response : {}", responseString);237 }238 } catch (IOException e) {239 logEventService.createForPrivateCalls("XRAY", "APICALL", "Xray Execution creation request to '" + xRayUrl + "' failed : " + e.toString() + ".");240 }241 }242 }243 }244 } catch (Exception ex) {245 LOG.error(ex, ex);246 }247 }248 private void getXRayAuthenticationToken(String origin, String system) throws Exception {249 String xRayUrl = XRAYCLOUD_AUTHENT_URL;250 if (getToken(system, origin) == null) {251 if (TestCase.TESTCASE_ORIGIN_JIRAXRAYCLOUD.equals(origin)) {252 LOG.debug("Getting new XRay Token.");253 String clientID = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_xraycloud_clientid, system, "");254 String clientSecret = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_xraycloud_clientsecret, system, "");255 if (StringUtil.isNullOrEmpty(clientID) || StringUtil.isNullOrEmpty(clientSecret)) {256 throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));257 }258 JSONObject authenMessage = xRayGenerationService.generateAuthenticationRequest(clientID, clientSecret);259 // curl -H "Content-Type: application/json" -X POST --data '{ "client_id": "E5A6F0FC4A8941C88CF4D1CAACFFAA81","client_secret": "2625a68b1953e66fff5b64642f6a9f59c6885db83fb3a9f9a73b34170513ad3f" }' https://xray.cloud.getxray.app/api/v2/authenticate260 CloseableHttpClient httpclient = null;261 HttpClientBuilder httpclientBuilder;262 if (proxyService.useProxy(xRayUrl, "")) {263 String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", "", DEFAULT_PROXY_HOST);264 int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", "", DEFAULT_PROXY_PORT);265 HttpHost proxyHostObject = new HttpHost(proxyHost, proxyPort);266 if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", "", DEFAULT_PROXYAUTHENT_ACTIVATE)) {267 String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", "", DEFAULT_PROXYAUTHENT_USER);268 String proxyPassword = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", "", DEFAULT_PROXYAUTHENT_PASSWORD);269 CredentialsProvider credsProvider = new BasicCredentialsProvider();270 credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));271 LOG.debug("Activating Proxy With Authentification.");272 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject)273 .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())274 .setDefaultCredentialsProvider(credsProvider);275 } else {276 LOG.debug("Activating Proxy (No Authentification).");277 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject);278 }279 } else {280 httpclientBuilder = HttpClientBuilder.create();281 }282 boolean acceptUnsignedSsl = parameterService.getParameterBooleanByKey("cerberus_accept_unsigned_ssl_certificate", "", true);283 if (acceptUnsignedSsl) {284 // authorize non valide certificat ssl285 SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {286 @Override287 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {288 return true;289 }290 }).build();291 httpclientBuilder292 .setSSLContext(sslContext)293 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);294 }295 httpclient = httpclientBuilder.build();296 HttpPost post = new HttpPost(xRayUrl);297 StringEntity entity = new StringEntity(authenMessage.toString());298 post.setEntity(entity);299 post.setHeader("Accept", "application/json");300 post.setHeader("Content-type", "application/json");301 try {302 HttpResponse response = httpclient.execute(post);303 int rc = response.getStatusLine().getStatusCode();304 if (rc >= 200 && rc < 300) {305 LOG.debug("XRay Authent request http return code : " + rc);306 String responseString = EntityUtils.toString(response.getEntity());307 LOG.debug("Response : {}", responseString);308 // Token is surounded with " (double quotes. We need to remove them.309 putToken(system, origin, responseString.substring(0, responseString.length() - 1).substring(1));310 LOG.debug("Setting new XRay Cloud Token : {}", getToken(system, origin));311 } else {312 logEventService.createForPrivateCalls("XRAY", "APICALL", "Xray Authent request to '" + xRayUrl + "' failed with http return code : " + rc + ".");313 LOG.warn("XRay Authent request http return code : " + rc);314 LOG.warn("Message sent to " + xRayUrl + ":");315 LOG.debug(authenMessage.toString(1));316 }317 } catch (Exception e) {318 logEventService.createForPrivateCalls("XRAY", "APICALL", "Xray Authent request to '" + xRayUrl + "' failed : " + e.toString() + ".");319 }320 } else if (TestCase.TESTCASE_ORIGIN_JIRAXRAYDC.equals(origin)) {321 putToken(system, origin, parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_xraydc_token, system, ""));322 LOG.debug("Setting new XRay DC Token : {}", getToken(system, origin));323 }324 } else {325 LOG.debug("Token in cache : {}", getToken(system, origin));326 }327 }328}...
getToken
Using AI Code Generation
1org.cerberus.service.xray.impl.XRayService xrayService = new org.cerberus.service.xray.impl.XRayService();2String token = xrayService.getToken();3org.cerberus.service.xray.impl.XRayService xrayService = new org.cerberus.service.xray.impl.XRayService();4String issue = xrayService.getIssue("issueKey");5org.cerberus.service.xray.impl.XRayService xrayService = new org.cerberus.service.xray.impl.XRayService();6String issues = xrayService.getIssues("projectKey");7org.cerberus.service.xray.impl.XRayService xrayService = new org.cerberus.service.xray.impl.XRayService();8String issue = xrayService.createIssue("projectKey", "summary", "description", "issueType", "priority", "labels", "components", "environment", "dueDate");9org.cerberus.service.xray.impl.XRayService xrayService = new org.cerberus.service.xray.impl.XRayService();10String issue = xrayService.updateIssue("issueKey", "summary", "description", "issueType", "priority", "labels", "components", "environment", "dueDate");11org.cerberus.service.xray.impl.XRayService xrayService = new org.cerberus.service.xray.impl.XRayService();12String issue = xrayService.deleteIssue("issueKey");
getToken
Using AI Code Generation
1import groovy.json.JsonSlurper2import groovy.json.JsonOutput3import groovy.json.JsonBuilder4import org.cerberus.service.xray.impl.XRayService5import java.util.logging.Logger6import java.util.logging.Level7import java.util.regex.Pattern8import java.util.regex.Matcher9import java.util.regex.PatternSyntaxException10import java.util.regex.MatchResult11import java.util.regex.Pattern12import java.util.regex.Matcher13import java.util.regex.PatternSyntaxException14import java.util.regex.MatchResult
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!