Skip to content

Commit

Permalink
Rename GlobalConfig to GlobalSonarQualityGatesConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
kirk-fitz committed Sep 10, 2024
1 parent 3557c7b commit ba34596
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public boolean getStatus(
}
}

GlobalConfigDataForSonarInstance chooseSonarInstance(GlobalConfig globalConfig, JobConfigData jobConfigData) {
GlobalConfigDataForSonarInstance chooseSonarInstance(
GlobalSonarQualityGatesConfiguration globalConfig, JobConfigData jobConfigData) {
if (globalConfig.fetchListOfGlobalConfigData().isEmpty()) {
return noSonarInstance(jobConfigData);
} else if (globalConfig.fetchListOfGlobalConfigData().size() == 1) {
Expand All @@ -46,15 +47,16 @@ private GlobalConfigDataForSonarInstance noSonarInstance(JobConfigData jobConfig
}

private GlobalConfigDataForSonarInstance singleSonarInstance(
GlobalConfig globalConfig, JobConfigData jobConfigData) {
GlobalSonarQualityGatesConfiguration globalConfig, JobConfigData jobConfigData) {
var globalConfigDataForSonarInstance =
globalConfig.fetchListOfGlobalConfigData().get(0);
jobConfigData.setSonarInstanceName(globalConfigDataForSonarInstance.getName());

return globalConfigDataForSonarInstance;
}

public GlobalConfigDataForSonarInstance multipleSonarInstances(String instanceName, GlobalConfig globalConfig) {
public GlobalConfigDataForSonarInstance multipleSonarInstances(
String instanceName, GlobalSonarQualityGatesConfiguration globalConfig) {
return globalConfig.getSonarInstanceByName(instanceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public GlobalConfigDataForSonarInstance() {
this.pass = "";
}

@DataBoundConstructor
@DataBoundConstructor
public GlobalConfigDataForSonarInstance(
String name, String sonarUrl, String username, Secret secretPass, int timeToWait, int maxWaitTime) {
this.name = name;
Expand Down Expand Up @@ -70,7 +70,7 @@ public String getName() {
return name;
}

@DataBoundSetter
@DataBoundSetter
public void setName(String name) {
this.name = name;
}
Expand All @@ -79,7 +79,7 @@ public String getUsername() {
return username;
}

@DataBoundSetter
@DataBoundSetter
public void setUsername(String username) {
this.username = username;
}
Expand All @@ -88,7 +88,7 @@ public Secret getPass() {
return secretPass != null ? secretPass : Secret.fromString("");
}

@DataBoundSetter
@DataBoundSetter
public void setPass(String pass) {
this.secretPass = Secret.fromString(Util.fixEmptyAndTrim(pass));
}
Expand All @@ -97,7 +97,7 @@ public String getSonarUrl() {
return sonarUrl;
}

@DataBoundSetter
@DataBoundSetter
public void setSonarUrl(String sonarUrl) {
this.sonarUrl = sonarUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@

@Extension
@Symbol("sonarQualityGates")
public class GlobalConfig extends GlobalConfiguration {
public class GlobalSonarQualityGatesConfiguration extends GlobalConfiguration {

private List<GlobalConfigDataForSonarInstance> listOfGlobalConfigData;

private final GlobalConfigurationService globalConfigurationService;

public GlobalConfig() {
public GlobalSonarQualityGatesConfiguration() {
load();
globalConfigurationService = new GlobalConfigurationService();
}

public GlobalConfig(
public GlobalSonarQualityGatesConfiguration(
List<GlobalConfigDataForSonarInstance> listOfGlobalConfigData,
GlobalConfigurationService globalConfigurationService) {
this.listOfGlobalConfigData = listOfGlobalConfigData;
Expand All @@ -39,7 +39,7 @@ public List<GlobalConfigDataForSonarInstance> fetchListOfGlobalConfigData() {
return listOfGlobalConfigData;
}

@DataBoundSetter
@DataBoundSetter
public void setGlobalConfigDataForSonarInstances(
List<GlobalConfigDataForSonarInstance> globalConfigDataForSonarInstances) {
this.listOfGlobalConfigData = globalConfigDataForSonarInstances;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class JobConfigurationService {

private static final Pattern ENV_VARIABLE_WITHOUT_BRACES_PATTERN = Pattern.compile("(\\$[a-zA-Z0-9_]+)");

public ListBoxModel getListOfSonarInstanceNames(GlobalConfig globalConfig) {
public ListBoxModel getListOfSonarInstanceNames(GlobalSonarQualityGatesConfiguration globalConfig) {
var listBoxModel = new ListBoxModel();

for (GlobalConfigDataForSonarInstance globalConfigDataForSonarInstance :
Expand All @@ -28,7 +28,7 @@ public ListBoxModel getListOfSonarInstanceNames(GlobalConfig globalConfig) {
return listBoxModel;
}

public JobConfigData createJobConfigData(JSONObject formData, GlobalConfig globalConfig) {
public JobConfigData createJobConfigData(JSONObject formData, GlobalSonarQualityGatesConfiguration globalConfig) {
var firstInstanceJobConfigData = new JobConfigData();
var projectKey = formData.getString("projectKey");

Expand All @@ -49,7 +49,7 @@ public JobConfigData createJobConfigData(JSONObject formData, GlobalConfig globa
return firstInstanceJobConfigData;
}

protected String hasFormDataKey(JSONObject formData, GlobalConfig globalConfig) {
protected String hasFormDataKey(JSONObject formData, GlobalSonarQualityGatesConfiguration globalConfig) {
if (formData.containsKey("sonarInstancesName")) {
return formData.getString("sonarInstancesName");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ public class JobExecutionService {
public static final String GLOBAL_CONFIG_NO_LONGER_EXISTS_ERROR =
"The Sonar Instance in the global configuration with name '%s' no longer exists.\n";

public GlobalConfig getGlobalConfigData() {
var globalConfig = GlobalConfiguration.all().get(GlobalConfig.class);
public GlobalSonarQualityGatesConfiguration getGlobalConfigData() {
var globalConfig = GlobalConfiguration.all().get(GlobalSonarQualityGatesConfiguration.class);

if (globalConfig == null) {
return new GlobalConfig();
return new GlobalSonarQualityGatesConfiguration();
}

return globalConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class BuildDecisionTest {
GlobalConfigDataForSonarInstance globalConfigDataForSonarInstance;

@Mock
GlobalConfig globalConfig;
GlobalSonarQualityGatesConfiguration globalConfig;

@Mock
private BuildListener listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

public class GlobalConfigTest {

private GlobalConfig globalConfig;
private GlobalSonarQualityGatesConfiguration globalConfig;

@InjectMocks
private GlobalConfig spyGlobalConfig;
private GlobalSonarQualityGatesConfiguration spyGlobalConfig;

@Mock
private StaplerRequest staplerRequest;
Expand All @@ -44,7 +44,8 @@ public void setUp() {
closeable = MockitoAnnotations.openMocks(this);
jsonObject = new JSONObject();
globalConfigDataForSonarInstances = new ArrayList<>();
globalConfig = new GlobalConfig(globalConfigDataForSonarInstances, globalConfigurationService);
globalConfig =
new GlobalSonarQualityGatesConfiguration(globalConfigDataForSonarInstances, globalConfigurationService);
spyGlobalConfig = spy(globalConfig);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class JobConfigurationServiceTest {
private JobConfigurationService jobConfigurationService;

@Mock
private GlobalConfig globalConfig;
private GlobalSonarQualityGatesConfiguration globalConfig;

@Mock
private List<GlobalConfigDataForSonarInstance> globalConfigDataForSonarInstances;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class QGBuilderIT {

private FreeStyleProject freeStyleProject;

private GlobalConfig globalConfig;
private GlobalSonarQualityGatesConfiguration globalConfig;

@Mock
private BuildDecision buildDecision;
Expand Down Expand Up @@ -69,7 +69,7 @@ public void setUp() throws IOException {
jobExecutionService,
jobConfigurationService,
globalConfigDataForSonarInstance);
globalConfig = GlobalConfiguration.all().get(GlobalConfig.class);
globalConfig = GlobalConfiguration.all().get(GlobalSonarQualityGatesConfiguration.class);
freeStyleProject = jenkinsRule.createFreeStyleProject("freeStyleProject");
freeStyleProject.getBuildersList().add(qgBuilder);
globalConfigDataForSonarInstanceList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ public void tearDown() throws Exception {

@Test
public void testPrebuildShouldFailGlobalConfigDataInstanceIsNull() {
doReturn(null).when(buildDecision).chooseSonarInstance(any(GlobalConfig.class), any(JobConfigData.class));
doReturn(null)
.when(buildDecision)
.chooseSonarInstance(any(GlobalSonarQualityGatesConfiguration.class), any(JobConfigData.class));
doReturn("TestInstanceName").when(jobConfigData).getSonarInstanceName();
assertFalse(builder.prebuild(abstractBuild, buildListener));
verify(buildListener).error(JobExecutionService.GLOBAL_CONFIG_NO_LONGER_EXISTS_ERROR, "TestInstanceName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class QGPublisherIT {

private FreeStyleProject freeStyleProject;

private GlobalConfig globalConfig;
private GlobalSonarQualityGatesConfiguration globalConfig;

@Mock
private BuildDecision buildDecision;
Expand Down Expand Up @@ -73,7 +73,7 @@ public void setUp() throws IOException {
jobExecutionService,
jobConfigurationService,
globalConfigDataForSonarInstance);
globalConfig = GlobalConfiguration.all().get(GlobalConfig.class);
globalConfig = GlobalConfiguration.all().get(GlobalSonarQualityGatesConfiguration.class);
freeStyleProject = jenkinsRule.createFreeStyleProject("freeStyleProject");
freeStyleProject.getBuildersList().add(builder);
freeStyleProject.getPublishersList().add(publisher);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ public void tearDown() throws Exception {

@Test
public void testPrebuildShouldFail() {
doReturn(null).when(buildDecision).chooseSonarInstance(any(GlobalConfig.class), any(JobConfigData.class));
doReturn(null)
.when(buildDecision)
.chooseSonarInstance(any(GlobalSonarQualityGatesConfiguration.class), any(JobConfigData.class));
doReturn("TestInstanceName").when(jobConfigData).getSonarInstanceName();
assertFalse(publisher.prebuild(abstractBuild, buildListener));
verify(buildListener).error(JobExecutionService.GLOBAL_CONFIG_NO_LONGER_EXISTS_ERROR, "TestInstanceName");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import java.util.List;
import jenkins.model.GlobalConfiguration;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.quality.gates.jenkins.plugin.GlobalConfig;
import org.quality.gates.jenkins.plugin.GlobalConfigDataForSonarInstance;
import org.quality.gates.jenkins.plugin.GlobalSonarQualityGatesConfiguration;

public class ConfigurationAsCodeTest extends RoundTripAbstractTest {

Expand All @@ -23,15 +23,16 @@ protected String stringInLogExpected() {

@Override
protected void assertConfiguredAsExpected(RestartableJenkinsRule j, String configContent) {
GlobalConfig globalSonarQualityGatesConfiguration =
GlobalConfiguration.all().get(GlobalConfig.class);
GlobalSonarQualityGatesConfiguration globalSonarQualityGatesConfiguration =
GlobalConfiguration.all().get(GlobalSonarQualityGatesConfiguration.class);

// Assert that globalSonarQualityGatesConfiguration is not null
assertThat(
"GlobalSonarQualityGatesConfiguration should not be null",
globalSonarQualityGatesConfiguration != null);

List<GlobalConfigDataForSonarInstance> sonarInstances = globalSonarQualityGatesConfiguration.fetchListOfGlobalConfigData();
List<GlobalConfigDataForSonarInstance> sonarInstances =
globalSonarQualityGatesConfiguration.fetchListOfGlobalConfigData();

// Assert that sonarInstances is not null
assertThat("SonarInstances should not be null", sonarInstances != null);
Expand Down

0 comments on commit ba34596

Please sign in to comment.