How to use putToken method of org.cerberus.service.xray.impl.XRayService class

Best Cerberus-source code snippet using org.cerberus.service.xray.impl.XRayService.putToken

Source:XRayService.java Github

copy

Full Screen

...94 LOG.error(ex, ex);95 }96 return null;97 }98 private void putToken(String system, String origin, String value) {99 try {100 JSONObject entry = new JSONObject();101 entry.put("key", "TOKEN-" + origin + "#" + system);102 entry.put("value", value);103 entry.put("created", "now");104 cacheEntry.put("TOKEN-" + origin + "#" + system, entry);105 } catch (JSONException ex) {106 LOG.error(ex, ex);107 }108 }109 @Override110 public JSONArray getAllCacheEntries() {111 JSONArray arrayResult = new JSONArray();112 for (Map.Entry<String, JSONObject> entry : cacheEntry.entrySet()) {113 String key = entry.getKey();114 JSONObject val = entry.getValue();115 arrayResult.put(val);116 }117 return arrayResult;118 }119 @Override120 public void purgeAllCacheEntries() {121 cacheEntry.clear();122 LOG.info("All XRay config cache entries purged.");123 }124 @Override125 @Async126 public void createXRayTestExecution(TestCaseExecution execution) {127 try {128 Tag currentTag = new Tag();129 if ((TestCase.TESTCASE_ORIGIN_JIRAXRAYCLOUD.equalsIgnoreCase(execution.getTestCaseObj().getOrigine()))130 || TestCase.TESTCASE_ORIGIN_JIRAXRAYDC.equalsIgnoreCase(execution.getTestCaseObj().getOrigine())) {131 getXRayAuthenticationToken(execution.getTestCaseObj().getOrigine(), execution.getSystem());132 JSONObject xRayRequest = new JSONObject();133 LOG.debug("Calling JIRA XRay TestExecution creation.");134 if (!StringUtil.isNullOrEmpty(execution.getTag())) {135 currentTag = tagService.convert(tagService.readByKey(execution.getTag()));136 if ((currentTag != null)) {137 xRayRequest = xRayGenerationService.generateCreateTestExecution(currentTag, execution);138 String xRayUrl = XRAYCLOUD_TESTEXECUTIONCREATION_URL;139 if (TestCase.TESTCASE_ORIGIN_JIRAXRAYDC.equalsIgnoreCase(execution.getTestCaseObj().getOrigine())) {140 xRayUrl = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_xraydc_url, execution.getSystem(), "");141 xRayUrl += "/import/execution";142 }143 CloseableHttpClient httpclient = null;144 HttpClientBuilder httpclientBuilder;145 if (proxyService.useProxy(xRayUrl, "")) {146 String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", "", DEFAULT_PROXY_HOST);147 int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", "", DEFAULT_PROXY_PORT);148 HttpHost proxyHostObject = new HttpHost(proxyHost, proxyPort);149 if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", "", DEFAULT_PROXYAUTHENT_ACTIVATE)) {150 String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", "", DEFAULT_PROXYAUTHENT_USER);151 String proxyPassword = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", "", DEFAULT_PROXYAUTHENT_PASSWORD);152 CredentialsProvider credsProvider = new BasicCredentialsProvider();153 credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));154 LOG.debug("Activating Proxy With Authentification.");155 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject)156 .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())157 .setDefaultCredentialsProvider(credsProvider);158 } else {159 LOG.debug("Activating Proxy (No Authentification).");160 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject);161 }162 } else {163 httpclientBuilder = HttpClientBuilder.create();164 }165 boolean acceptUnsignedSsl = parameterService.getParameterBooleanByKey("cerberus_accept_unsigned_ssl_certificate", "", true);166 if (acceptUnsignedSsl) {167 // authorize non valide certificat ssl168 SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {169 @Override170 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {171 return true;172 }173 }).build();174 httpclientBuilder175 .setSSLContext(sslContext)176 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);177 }178 httpclient = httpclientBuilder.build();179 HttpPost post = new HttpPost(xRayUrl);180 StringEntity entity = new StringEntity(xRayRequest.toString());181 post.setEntity(entity);182 post.setHeader("Accept", "application/json");183 post.setHeader("Content-type", "application/json");184 post.setHeader("Authorization", "Bearer " + getToken(execution.getSystem(), execution.getTestCaseObj().getOrigine()));185 LOG.debug("Bearer " + getToken(execution.getSystem(), execution.getTestCaseObj().getOrigine()));186 HttpResponse response = httpclient.execute(post);187 int rc = response.getStatusLine().getStatusCode();188 if (rc >= 200 && rc < 300) {189 LOG.debug("XRay Test Execution request http return code : " + rc);190 String responseString = EntityUtils.toString(response.getEntity());191 LOG.debug("Response : {}", responseString);192 JSONObject xRayResponse = new JSONObject(responseString);193 String xrayURL = "";194 if (xRayResponse.has("key")) {195 currentTag.setXRayTestExecution(xRayResponse.getString("key"));196 if (xRayResponse.has("self")) {197 xrayURL = xRayResponse.getString("self");198 URL xrURL = new URL(xrayURL);199 currentTag.setXRayURL(xrURL.getProtocol() + "://" + xrURL.getHost());200 }201 tagService.updateXRayTestExecution(currentTag.getTag(), currentTag);202 }203 LOG.debug("Setting new XRay TestExecution '{}' to tag '{}'", xRayResponse.getString("key"), currentTag.getTag());204 } else {205 LOG.warn("XRay Test Execution request http return code : " + rc);206 String responseString = EntityUtils.toString(response.getEntity());207 LOG.debug("Response : {}", responseString);208 LOG.warn("Message sent to " + xRayUrl + ":");209 LOG.warn(xRayRequest.toString(1));210 }211 }212 }213 }214 } catch (Exception ex) {215 LOG.error(ex, ex);216 }217 }218 private void getXRayAuthenticationToken(String origin, String system) throws Exception {219 String xRayUrl = XRAYCLOUD_AUTHENT_URL;220 if (getToken(system, origin) == null) {221 if (TestCase.TESTCASE_ORIGIN_JIRAXRAYCLOUD.equals(origin)) {222 LOG.debug("Getting new XRay Token.");223 String clientID = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_xraycloud_clientid, system, "");224 String clientSecret = parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_xraycloud_clientsecret, system, "");225 if (StringUtil.isNullOrEmpty(clientID) || StringUtil.isNullOrEmpty(clientSecret)) {226 throw new CerberusException(new MessageGeneral(MessageGeneralEnum.NO_DATA_FOUND));227 }228 JSONObject authenMessage = xRayGenerationService.generateAuthenticationRequest(clientID, clientSecret);229 // curl -H "Content-Type: application/json" -X POST --data '{ "client_id": "E5A6F0FC4A8941C88CF4D1CAACFFAA81","client_secret": "2625a68b1953e66fff5b64642f6a9f59c6885db83fb3a9f9a73b34170513ad3f" }' https://xray.cloud.getxray.app/api/v2/authenticate230 CloseableHttpClient httpclient = null;231 HttpClientBuilder httpclientBuilder;232 if (proxyService.useProxy(xRayUrl, "")) {233 String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", "", DEFAULT_PROXY_HOST);234 int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", "", DEFAULT_PROXY_PORT);235 HttpHost proxyHostObject = new HttpHost(proxyHost, proxyPort);236 if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", "", DEFAULT_PROXYAUTHENT_ACTIVATE)) {237 String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", "", DEFAULT_PROXYAUTHENT_USER);238 String proxyPassword = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", "", DEFAULT_PROXYAUTHENT_PASSWORD);239 CredentialsProvider credsProvider = new BasicCredentialsProvider();240 credsProvider.setCredentials(new AuthScope(proxyHost, proxyPort), new UsernamePasswordCredentials(proxyUser, proxyPassword));241 LOG.debug("Activating Proxy With Authentification.");242 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject)243 .setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())244 .setDefaultCredentialsProvider(credsProvider);245 } else {246 LOG.debug("Activating Proxy (No Authentification).");247 httpclientBuilder = HttpClientBuilder.create().setProxy(proxyHostObject);248 }249 } else {250 httpclientBuilder = HttpClientBuilder.create();251 }252 boolean acceptUnsignedSsl = parameterService.getParameterBooleanByKey("cerberus_accept_unsigned_ssl_certificate", "", true);253 if (acceptUnsignedSsl) {254 // authorize non valide certificat ssl255 SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy() {256 @Override257 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {258 return true;259 }260 }).build();261 httpclientBuilder262 .setSSLContext(sslContext)263 .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);264 }265 httpclient = httpclientBuilder.build();266 HttpPost post = new HttpPost(xRayUrl);267 StringEntity entity = new StringEntity(authenMessage.toString());268 post.setEntity(entity);269 post.setHeader("Accept", "application/json");270 post.setHeader("Content-type", "application/json");271 HttpResponse response = httpclient.execute(post);272 int rc = response.getStatusLine().getStatusCode();273 if (rc >= 200 && rc < 300) {274 LOG.debug("XRay Authent request http return code : " + rc);275 String responseString = EntityUtils.toString(response.getEntity());276 LOG.debug("Response : {}", responseString);277 // Token is surounded with " (double quotes. We need to remove them.278 putToken(system, origin, responseString.substring(0, responseString.length() - 1).substring(1));279 LOG.debug("Setting new XRay Cloud Token : {}", getToken(system, origin));280 } else {281 LOG.warn("XRay Authent request http return code : " + rc);282 LOG.warn("Message sent to " + xRayUrl + ":");283 LOG.warn(authenMessage.toString(1));284 }285 } else if (TestCase.TESTCASE_ORIGIN_JIRAXRAYDC.equals(origin)) {286 putToken(system, origin, parameterService.getParameterStringByKey(Parameter.VALUE_cerberus_xraydc_token, system, ""));287 LOG.debug("Setting new XRay DC Token : {}", getToken(system, origin));288 }289 } else {290 LOG.debug("Token in cache : {}", getToken(system, origin));291 }292 }293}...

Full Screen

Full Screen

putToken

Using AI Code Generation

copy

Full Screen

1import org.cerberus.service.xray.impl.XRayService2def xrayService = new XRayService()3xrayService.putToken("put your token here")4import org.cerberus.service.xray.impl.XRayService5def xrayService = new XRayService()6xrayService.putToken("put your token here")7import org.cerberus.service.xray.impl.XRayService8def xrayService = new XRayService()9xrayService.putToken("put your token here")10import org.cerberus.service.xray.impl.XRayService11def xrayService = new XRayService()12xrayService.putToken("put your token here")13import org.cerberus.service.xray.impl.XRayService14def xrayService = new XRayService()15xrayService.putToken("put your token here")16import org.cerberus.service.xray.impl.XRayService17def xrayService = new XRayService()18xrayService.putToken("put your token here")19import org.cerberus.service.xray.impl.XRayService20def xrayService = new XRayService()21xrayService.putToken("put your token here")22import org.cerberus.service.xray.impl.XRayService23def xrayService = new XRayService()24xrayService.putToken("put your token here")25import org.cerberus.service.xray.impl.XRayService26def xrayService = new XRayService()27xrayService.putToken("put your token here")

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Cerberus-source automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful