Skip to content

Commit

Permalink
chore(jira): Clean service log
Browse files Browse the repository at this point in the history
  • Loading branch information
boddissattva authored and KarimGl committed Feb 27, 2024
1 parent b633330 commit a40bc2c
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package com.chutneytesting.jira.api;

import com.chutneytesting.jira.domain.JiraRepository;
import com.chutneytesting.jira.domain.JiraTargetConfiguration;
import com.chutneytesting.jira.domain.JiraServerConfiguration;
import com.chutneytesting.jira.domain.JiraXrayService;
import com.chutneytesting.jira.xrayapi.XrayTestExecTest;
import java.util.ArrayList;
Expand Down Expand Up @@ -167,22 +167,22 @@ public void removeForCampaign(@PathVariable String campaignId) {
@PreAuthorize("hasAuthority('ADMIN_ACCESS')")
@GetMapping(path = BASE_CONFIGURATION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
public JiraConfigurationDto getConfiguration() {
JiraTargetConfiguration jiraTargetConfiguration = jiraRepository.loadServerConfiguration();
JiraServerConfiguration jiraServerConfiguration = jiraRepository.loadServerConfiguration();
return ImmutableJiraConfigurationDto.builder()
.url(jiraTargetConfiguration.url())
.username(jiraTargetConfiguration.username())
.password(jiraTargetConfiguration.password())
.urlProxy(jiraTargetConfiguration.urlProxy())
.userProxy(jiraTargetConfiguration.userProxy())
.passwordProxy(jiraTargetConfiguration.passwordProxy())
.url(jiraServerConfiguration.url())
.username(jiraServerConfiguration.username())
.password(jiraServerConfiguration.password())
.urlProxy(jiraServerConfiguration.urlProxy())
.userProxy(jiraServerConfiguration.userProxy())
.passwordProxy(jiraServerConfiguration.passwordProxy())
.build();
}

@PreAuthorize("hasAuthority('SCENARIO_READ') or hasAuthority('CAMPAIGN_READ')")
@GetMapping(path = BASE_CONFIGURATION_URL + "/url", produces = MediaType.TEXT_PLAIN_VALUE)
public String getConfigurationUrl() {
JiraTargetConfiguration jiraTargetConfiguration = jiraRepository.loadServerConfiguration();
return jiraTargetConfiguration.url();
JiraServerConfiguration jiraServerConfiguration = jiraRepository.loadServerConfiguration();
return jiraServerConfiguration.url();
}

@PreAuthorize("hasAuthority('ADMIN_ACCESS')")
Expand All @@ -191,7 +191,7 @@ public String getConfigurationUrl() {
produces = MediaType.APPLICATION_JSON_VALUE)
public void saveConfiguration(@RequestBody JiraConfigurationDto jiraConfigurationDto) {
jiraRepository.saveServerConfiguration(
new JiraTargetConfiguration(
new JiraServerConfiguration(
jiraConfigurationDto.url(),
jiraConfigurationDto.username(),
jiraConfigurationDto.password(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface JiraRepository {

void removeForCampaignExecution(String campaignExecutionId);

JiraTargetConfiguration loadServerConfiguration();
JiraServerConfiguration loadServerConfiguration();

void saveServerConfiguration(JiraTargetConfiguration jiraTargetConfiguration);
void saveServerConfiguration(JiraServerConfiguration jiraServerConfiguration);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import org.apache.commons.lang3.StringUtils;

public record JiraTargetConfiguration(
public record JiraServerConfiguration(
String url, String username, String password,
String urlProxy, String userProxy, String passwordProxy
) {
public JiraServerConfiguration() {
this(null, null, null, null, null, null);
}

public boolean isValid() {
return isNotBlank(url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

public interface JiraXrayClientFactory {

JiraXrayApi create(JiraTargetConfiguration jiraTargetConfiguration);
JiraXrayApi create(JiraServerConfiguration jiraServerConfiguration);

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ public class JiraXrayService {

private final JiraRepository jiraRepository;
private final JiraXrayClientFactory jiraXrayImplFactory;
private JiraServerConfiguration jiraServerConfiguration;

public JiraXrayService(JiraRepository jiraRepository, JiraXrayClientFactory jiraXrayImplFactory) {
this.jiraRepository = jiraRepository;
this.jiraXrayImplFactory = jiraXrayImplFactory;
loadJiraServerConfiguration();
}

public void updateTestExecution(Long campaignId, Long campaignExecutionId, String scenarioId, ReportForJira report) {
Expand Down Expand Up @@ -95,17 +97,17 @@ public void updateScenarioStatus(String testExecId, String chutneyId, String exe

List<XrayTestExecTest> testExecutionScenarios = getTestExecutionScenarios(testExecId);
Optional<XrayTestExecTest> foundTest = testExecutionScenarios.stream().filter(test -> scenarioJiraId.equals(test.getKey())).findFirst();
foundTest.ifPresent(xrayTestExecTest -> jiraXrayApi.updateStatusByTestRunId(xrayTestExecTest.getId(), executionStatus));
foundTest.ifPresent(xrayTestExecTest -> {
LOGGER.info("Update xray test execution {} status to {}", testExecId, executionStatus);
jiraXrayApi.updateStatusByTestRunId(xrayTestExecTest.getId(), executionStatus);
});
}

private JiraXrayApi createHttpJiraXrayImpl() {
JiraTargetConfiguration jiraTargetConfiguration = jiraRepository.loadServerConfiguration();
if (!jiraTargetConfiguration.isValid()) {
LOGGER.error("Unable to create xray http service, jira url is undefined");
if (!loadJiraServerConfiguration()) {
throw new NoJiraConfigurationException();
} else {
return jiraXrayImplFactory.create(jiraTargetConfiguration);
}
return jiraXrayImplFactory.create(jiraServerConfiguration);
}

private List<String> getErrors(ReportForJira report) {
Expand Down Expand Up @@ -150,4 +152,18 @@ private String formatEvidenceFilename(String parentStep, String stepName) {
+ (parentStep.trim().isEmpty() ? "" : "_")
+ stepName.trim().replace(" ", "-");
}

private boolean loadJiraServerConfiguration() {
JiraServerConfiguration currentConfiguration = jiraRepository.loadServerConfiguration();
boolean hasConfigurationChanged = !currentConfiguration.equals(this.jiraServerConfiguration);
if (hasConfigurationChanged) {
LOGGER.info("Jira server configuration has changed");
this.jiraServerConfiguration = currentConfiguration;
}
boolean isConfigurationValid = this.jiraServerConfiguration.isValid();
if (hasConfigurationChanged && !isConfigurationValid) {
LOGGER.warn("Jira server configuration is not valid");
}
return isConfigurationValid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClient;
import com.atlassian.jira.rest.client.internal.async.DisposableHttpClient;
import com.chutneytesting.jira.domain.JiraTargetConfiguration;
import com.chutneytesting.jira.domain.JiraServerConfiguration;
import com.chutneytesting.jira.domain.JiraXrayApi;
import com.chutneytesting.jira.domain.exception.NoJiraConfigurationException;
import com.chutneytesting.jira.infra.atlassian.httpclient.api.factory.HttpClientOptions;
Expand Down Expand Up @@ -75,18 +75,18 @@ public class HttpJiraXrayImpl implements JiraXrayApi {

private static final int MS_TIMEOUT = 10 * 1000; // 10 s

private final JiraTargetConfiguration jiraTargetConfiguration;
private final JiraServerConfiguration jiraServerConfiguration;

public HttpJiraXrayImpl(JiraTargetConfiguration jiraTargetConfiguration) {
this.jiraTargetConfiguration = jiraTargetConfiguration;
if (!jiraTargetConfiguration.isValid()) {
public HttpJiraXrayImpl(JiraServerConfiguration jiraServerConfiguration) {
this.jiraServerConfiguration = jiraServerConfiguration;
if (!jiraServerConfiguration.isValid()) {
throw new NoJiraConfigurationException();
}
}

@Override
public void updateRequest(Xray xray) {
String updateUri = jiraTargetConfiguration.url() + "/rest/raven/1.0/import/execution";
String updateUri = jiraServerConfiguration.url() + "/rest/raven/1.0/import/execution";

RestTemplate restTemplate = buildRestTemplate();

Expand All @@ -107,7 +107,7 @@ public void updateRequest(Xray xray) {
public List<XrayTestExecTest> getTestExecutionScenarios(String xrayId) {
List<XrayTestExecTest> tests = new ArrayList<>();

String uriTemplate = jiraTargetConfiguration.url() + "/rest/raven/1.0/api/%s/%s/test";
String uriTemplate = jiraServerConfiguration.url() + "/rest/raven/1.0/api/%s/%s/test";
String uri = String.format(uriTemplate, isTestPlan(xrayId) ? "testplan" : "testexec", xrayId);

RestTemplate restTemplate = buildRestTemplate();
Expand All @@ -127,7 +127,7 @@ public List<XrayTestExecTest> getTestExecutionScenarios(String xrayId) {

@Override
public void updateStatusByTestRunId(String testRuntId, String executionStatus) {
String uriTemplate = jiraTargetConfiguration.url() + "/rest/raven/1.0/api/testrun/%s/status?status=%s";
String uriTemplate = jiraServerConfiguration.url() + "/rest/raven/1.0/api/testrun/%s/status?status=%s";
String uri = String.format(uriTemplate, testRuntId, executionStatus);

RestTemplate restTemplate = buildRestTemplate();
Expand All @@ -140,7 +140,7 @@ public void updateStatusByTestRunId(String testRuntId, String executionStatus) {

@Override
public void associateTestExecutionFromTestPlan(String testPlanId, String testExecutionId) {
String uriTemplate = jiraTargetConfiguration.url() + "/rest/raven/1.0/api/testplan/%s/testexecution";
String uriTemplate = jiraServerConfiguration.url() + "/rest/raven/1.0/api/testplan/%s/testexecution";
String uri = String.format(uriTemplate, testPlanId);

RestTemplate restTemplate = buildRestTemplate();
Expand Down Expand Up @@ -192,7 +192,7 @@ private Issue getIssue(String issueKey) {
}

private JiraIssueType getIssueTypeByName(String issueTypeName) {
String uri = jiraTargetConfiguration.url() + "/rest/api/latest/issuetype";
String uri = jiraServerConfiguration.url() + "/rest/api/latest/issuetype";
Optional<JiraIssueType> issueTypeOptional = Optional.empty();

RestTemplate restTemplate = buildRestTemplate();
Expand Down Expand Up @@ -222,21 +222,21 @@ private RestTemplate buildRestTemplate() {
}

private HttpClient buildHttpClient() throws URISyntaxException {
HttpHost httpHost = HttpHost.create(new URI(jiraTargetConfiguration.url()));
HttpHost proxyHttpHost = HttpHost.create(new URI(jiraTargetConfiguration.urlProxy()));
HttpHost httpHost = HttpHost.create(new URI(jiraServerConfiguration.url()));
HttpHost proxyHttpHost = HttpHost.create(new URI(jiraServerConfiguration.urlProxy()));

HttpClientBuilder httpClientBuilder = HttpClients.custom()
.setConnectionManager(buildConnectionManager())
.setDefaultCredentialsProvider(getBasicCredentialsProvider(httpHost, proxyHttpHost));

var defaultHeaders = new ArrayList<BasicHeader>();
String authorization = basicAuthHeaderEncodedValue(jiraTargetConfiguration.username(), jiraTargetConfiguration.password());
String authorization = basicAuthHeaderEncodedValue(jiraServerConfiguration.username(), jiraServerConfiguration.password());
defaultHeaders.add(new BasicHeader(HttpHeaders.AUTHORIZATION, authorization));

if (jiraTargetConfiguration.hasProxy()) {
if (jiraServerConfiguration.hasProxy()) {
httpClientBuilder.setProxy(proxyHttpHost);
if (jiraTargetConfiguration.hasProxyWithAuth()) {
String proxyAuthorization = basicAuthHeaderEncodedValue(jiraTargetConfiguration.userProxy(), jiraTargetConfiguration.passwordProxy());
if (jiraServerConfiguration.hasProxyWithAuth()) {
String proxyAuthorization = basicAuthHeaderEncodedValue(jiraServerConfiguration.userProxy(), jiraServerConfiguration.passwordProxy());
defaultHeaders.add(new BasicHeader(HttpHeaders.PROXY_AUTHORIZATION, proxyAuthorization));
}
}
Expand Down Expand Up @@ -273,26 +273,26 @@ private BasicCredentialsProvider getBasicCredentialsProvider(HttpHost httpHost,
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(
new AuthScope(httpHost),
new UsernamePasswordCredentials(jiraTargetConfiguration.username(), jiraTargetConfiguration.password().toCharArray())
new UsernamePasswordCredentials(jiraServerConfiguration.username(), jiraServerConfiguration.password().toCharArray())
);
if (jiraTargetConfiguration.hasProxyWithAuth()) {
if (jiraServerConfiguration.hasProxyWithAuth()) {
credentialsProvider.setCredentials(
new AuthScope(proxyHttpHost),
new UsernamePasswordCredentials(jiraTargetConfiguration.userProxy(), jiraTargetConfiguration.passwordProxy().toCharArray())
new UsernamePasswordCredentials(jiraServerConfiguration.userProxy(), jiraServerConfiguration.passwordProxy().toCharArray())
);
}
return credentialsProvider;
}

private JiraRestClient getJiraRestClient() throws URISyntaxException {
URI serverUri = URI.create(jiraTargetConfiguration.url());
URI serverUri = URI.create(jiraServerConfiguration.url());
return new AsynchronousJiraRestClient(
serverUri,
buildJiraHttpClient(
serverUri,
new BasicHttpAuthenticationHandler(
jiraTargetConfiguration.username(), jiraTargetConfiguration.password(),
jiraTargetConfiguration.userProxy(), jiraTargetConfiguration.passwordProxy()
jiraServerConfiguration.username(), jiraServerConfiguration.password(),
jiraServerConfiguration.userProxy(), jiraServerConfiguration.passwordProxy()
)
)
);
Expand All @@ -305,8 +305,8 @@ private DisposableHttpClient buildJiraHttpClient(
final HttpClientOptions options = new HttpClientOptions();
options.setTrustSelfSignedCertificates(true);

if (jiraTargetConfiguration.hasProxy()) {
HttpHost proxyHttpHost = HttpHost.create(new URI(jiraTargetConfiguration.urlProxy()));
if (jiraServerConfiguration.hasProxy()) {
HttpHost proxyHttpHost = HttpHost.create(new URI(jiraServerConfiguration.urlProxy()));
options.setProxyOptions(
ProxyOptions.ProxyOptionsBuilder.create()
.withProxy(Scheme.valueOf(serverUri.getScheme().toUpperCase()), proxyHttpHost)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import static com.chutneytesting.tools.file.FileUtils.initFolder;

import com.chutneytesting.jira.domain.JiraRepository;
import com.chutneytesting.jira.domain.JiraTargetConfiguration;
import com.chutneytesting.jira.domain.JiraServerConfiguration;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -117,20 +117,20 @@ public void removeForCampaignExecution(String campaignExecutionId) {
}

@Override
public JiraTargetConfiguration loadServerConfiguration() {
public JiraServerConfiguration loadServerConfiguration() {
JiraTargetConfigurationDto dto = doLoadServerConfiguration();
return new JiraTargetConfiguration(dto.url, dto.username, dto.password, dto.urlProxy, dto.userProxy, dto.passwordProxy);
return new JiraServerConfiguration(dto.url, dto.username, dto.password, dto.urlProxy, dto.userProxy, dto.passwordProxy);
}

@Override
public void saveServerConfiguration(JiraTargetConfiguration jiraTargetConfiguration) {
public void saveServerConfiguration(JiraServerConfiguration jiraServerConfiguration) {
JiraTargetConfigurationDto jiraTargetConfigurationDto = new JiraTargetConfigurationDto(
jiraTargetConfiguration.url(),
jiraTargetConfiguration.username(),
jiraTargetConfiguration.password(),
jiraTargetConfiguration.urlProxy(),
jiraTargetConfiguration.userProxy(),
jiraTargetConfiguration.passwordProxy()
jiraServerConfiguration.url(),
jiraServerConfiguration.username(),
jiraServerConfiguration.password(),
jiraServerConfiguration.urlProxy(),
jiraServerConfiguration.userProxy(),
jiraServerConfiguration.passwordProxy()
);
Path resolvedFilePath = storeFolderPath.resolve(CONFIGURATION_FILE);
doSave(resolvedFilePath, jiraTargetConfigurationDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

package com.chutneytesting.jira.infra;

import com.chutneytesting.jira.domain.JiraTargetConfiguration;
import com.chutneytesting.jira.domain.JiraServerConfiguration;
import com.chutneytesting.jira.domain.JiraXrayApi;
import com.chutneytesting.jira.domain.JiraXrayClientFactory;

public class JiraXrayFactoryImpl implements JiraXrayClientFactory {

@Override
public JiraXrayApi create(JiraTargetConfiguration jiraTargetConfiguration) {
return new HttpJiraXrayImpl(jiraTargetConfiguration);
public JiraXrayApi create(JiraServerConfiguration jiraServerConfiguration) {
return new HttpJiraXrayImpl(jiraServerConfiguration);
}

}
Loading

0 comments on commit a40bc2c

Please sign in to comment.