Skip to content

Commit

Permalink
Implement new qualityGates tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Topin2001 committed Jun 4, 2024
1 parent 793c250 commit 0bb01a6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import fr.cnes.sonar.report.model.QualityGate;
import fr.cnes.sonar.report.providers.AbstractDataProvider;

import org.sonarqube.ws.client.WsClient;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -127,19 +125,9 @@ protected AbstractQualityGateProvider(final String pServer, final String pToken,
super(pServer, pToken, pProject, pBranch);
}

/**
* Complete constructor.
* @param wsClient The web client.
* @param project The id of the project to report.
* @param branch The branch of the project to report.
*/
protected AbstractQualityGateProvider(final WsClient wsClient, final String project, final String branch) {
super(wsClient, project, branch);
}

/**
* Generic getter for all the quality gates.
* @return Array containing all the issues.
* @return List<QualityGate> containing all the issues.
* @throws BadSonarQubeRequestException A request is not recognized by the server.
* @throws SonarQubeException When SonarQube server is not callable.
*/
Expand Down Expand Up @@ -187,15 +175,15 @@ protected QualityGate getProjectQualityGateAbstract()
final JsonObject jsonObject = getProjectAsJsonObject();

// search for the good quality gate
final Iterator<QualityGate> iterator = qualityGates.iterator();
final Iterator<QualityGate> qualityGatesIterator = qualityGates.iterator();

QualityGate tmp;
boolean find = false;
final String key = jsonObject.getAsJsonObject(QUALITY_GATE).get(KEY).getAsString();
final String name = jsonObject.getAsJsonObject(QUALITY_GATE).get(NAME).getAsString();

while (iterator.hasNext() && !find) {
tmp = iterator.next();
while (qualityGatesIterator.hasNext() && !find) {
tmp = qualityGatesIterator.next();
if (tmp.getName().equals(name) || tmp.getId().equals(key)) {
res = tmp;
find = true;
Expand Down Expand Up @@ -274,7 +262,7 @@ protected String getErrorExplanation(String actualValue, String errorThreshold,
threshold = workDurationToTime(errorThreshold);
break;
case PERCENT:
actual = String.valueOf(Precision.round(Double.valueOf(actualValue), 1)).concat("%");
actual = String.valueOf(Precision.round(Double.parseDouble(actualValue), 1)).concat("%");
compare = comparatorToString(comparator);
threshold = errorThreshold.concat("%");
break;
Expand All @@ -299,28 +287,14 @@ protected String getErrorExplanation(String actualValue, String errorThreshold,
* @return the letter corresponding to the rating number
*/
private String ratingToLetter(String rating) {
String res;
switch (rating) {
case "1":
res = "A";
break;
case "2":
res = "B";
break;
case "3":
res = "C";
break;
case "4":
res = "D";
break;
case "5":
res = "E";
break;
default:
res = rating;
break;
}
return res;
return switch (rating) {
case "1" -> "A";
case "2" -> "B";
case "3" -> "C";
case "4" -> "D";
case "5" -> "E";
default -> rating;
};
}

/**
Expand Down
19 changes: 0 additions & 19 deletions src/test/ut/java/fr/cnes/sonar/report/model/ReportTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import org.jetbrains.annotations.NotNull;
import org.junit.Test;

import fr.cnes.sonar.report.exceptions.BadSonarQubeRequestException;
Expand All @@ -54,18 +56,7 @@ public void getQualityGatesEmptyTest() throws BadSonarQubeRequestException, Sona
@Test
public void getQualityGatesWithCorrespondingDetailsTest() throws BadSonarQubeRequestException, SonarQubeException {
// Fake API response
JsonObject qualityGate1 = new JsonObject();
qualityGate1.addProperty("id", "test1");
JsonObject qualityGate2 = new JsonObject();
qualityGate2.addProperty("id", "test2");

JsonArray qualityGateList = new JsonArray();
qualityGateList.add(qualityGate1);
qualityGateList.add(qualityGate2);

JsonObject qualityGatesResponse = new JsonObject();
qualityGatesResponse.addProperty("default", "test1");
qualityGatesResponse.add("qualitygates", qualityGateList);
JsonObject qualityGatesResponse = getJsonObject();

// Call the wrapper to test
QualityGateProviderWrapper provider = new QualityGateProviderWrapper();
Expand All @@ -79,7 +70,23 @@ public void getQualityGatesWithCorrespondingDetailsTest() throws BadSonarQubeReq
assertFalse(list.get(1).isDefault());
}

@Test(expected = NullPointerException.class) //To be fixed, should be UnknownQualityGateException
private static @NotNull JsonObject getJsonObject() {
JsonObject qualityGate1 = new JsonObject();
qualityGate1.addProperty("id", "test1");
JsonObject qualityGate2 = new JsonObject();
qualityGate2.addProperty("id", "test2");

JsonArray qualityGateList = new JsonArray();
qualityGateList.add(qualityGate1);
qualityGateList.add(qualityGate2);

JsonObject qualityGatesResponse = new JsonObject();
qualityGatesResponse.addProperty("default", "test1");
qualityGatesResponse.add("qualitygates", qualityGateList);
return qualityGatesResponse;
}

@Test(expected = UnknownQualityGateException.class)
public void getProjectNoQualityGateTest() throws UnknownQualityGateException, BadSonarQubeRequestException, SonarQubeException {
// Empty API response
JsonObject empty = new JsonObject();
Expand All @@ -88,6 +95,7 @@ public void getProjectNoQualityGateTest() throws UnknownQualityGateException, Ba

JsonObject qualityGateProperty = new JsonObject();
qualityGateProperty.addProperty("key", "no_matching_key");
qualityGateProperty.addProperty("name", "No matching name");
JsonObject project = new JsonObject();
project.add("qualityGate", qualityGateProperty);

Expand All @@ -100,24 +108,14 @@ public void getProjectNoQualityGateTest() throws UnknownQualityGateException, Ba
provider.getProjectQualityGate();
}

@Test(expected = NullPointerException.class) //To be fixed too, should be UnknownQualityGateException
@Test(expected = UnknownQualityGateException.class)
public void getProjectNoMatchingQualityGateTest() throws UnknownQualityGateException, BadSonarQubeRequestException, SonarQubeException {
// Fake API response
JsonObject qualityGate1 = new JsonObject();
qualityGate1.addProperty("id", "test1");
JsonObject qualityGate2 = new JsonObject();
qualityGate2.addProperty("id", "test2");

JsonArray qualityGateList = new JsonArray();
qualityGateList.add(qualityGate1);
qualityGateList.add(qualityGate2);

JsonObject qualityGatesResponse = new JsonObject();
qualityGatesResponse.addProperty("default", "test1");
qualityGatesResponse.add("qualitygates", qualityGateList);
JsonObject qualityGatesResponse = getObject();

JsonObject qualityGateProperty = new JsonObject();
qualityGateProperty.addProperty("key", "no_matching_key");
qualityGateProperty.addProperty("name", "No matching name");
JsonObject project = new JsonObject();
project.add("qualityGate", qualityGateProperty);

Expand All @@ -130,27 +128,32 @@ public void getProjectNoMatchingQualityGateTest() throws UnknownQualityGateExcep
provider.getProjectQualityGate();
}

@Test
public void getProjectWithMatchingQualityGateTest() throws UnknownQualityGateException, BadSonarQubeRequestException, SonarQubeException {
// Fake API response
private static @NotNull JsonObject getObject() {
JsonObject qualityGate1 = new JsonObject();
qualityGate1.addProperty("id", "test1");
qualityGate1.addProperty("name", "Test 1");
JsonObject qualityGate2 = new JsonObject();
qualityGate2.addProperty("id", "test2");
JsonObject qualityGate3 = new JsonObject();
qualityGate3.addProperty("id", "test3");

qualityGate2.addProperty("name", "Test 2");

JsonArray qualityGateList = new JsonArray();
qualityGateList.add(qualityGate1);
qualityGateList.add(qualityGate2);
qualityGateList.add(qualityGate3);


JsonObject qualityGatesResponse = new JsonObject();
qualityGatesResponse.addProperty("default", "test1");
qualityGatesResponse.add("qualitygates", qualityGateList);
return qualityGatesResponse;
}

@Test
public void getProjectWithMatchingQualityGateTest() throws UnknownQualityGateException, BadSonarQubeRequestException, SonarQubeException {
// Fake API response
JsonObject qualityGatesResponse = getQualityGatesResponse();

JsonObject qualityGateProperty = new JsonObject();
qualityGateProperty.addProperty("key", "test2");
qualityGateProperty.addProperty("name", "Test 2");
JsonObject project = new JsonObject();
project.add("qualityGate", qualityGateProperty);

Expand All @@ -160,13 +163,57 @@ public void getProjectWithMatchingQualityGateTest() throws UnknownQualityGateExc
provider.setFakeQualityGatesDetails(new JsonObject());
provider.setFakeProject(project);

//QualityGate result = provider.getProjectQualityGate();
//assertEquals("test2", result.getId());
QualityGate result = provider.getProjectQualityGate();
assertEquals("test2", result.getId());
assertEquals("Test 2", result.getName());
}

private static @NotNull JsonObject getQualityGatesResponse() {
JsonObject qualityGate1 = new JsonObject();
qualityGate1.addProperty("id", "test1");
qualityGate1.addProperty("name", "Test 1");
JsonObject qualityGate2 = new JsonObject();
qualityGate2.addProperty("id", "test2");
qualityGate2.addProperty("name", "Test 2");
JsonObject qualityGate3 = new JsonObject();
qualityGate3.addProperty("id", "test3");
qualityGate3.addProperty("name", "Test 3");

JsonArray qualityGateList = new JsonArray();
qualityGateList.add(qualityGate1);
qualityGateList.add(qualityGate2);
qualityGateList.add(qualityGate3);

JsonObject qualityGatesResponse = new JsonObject();
qualityGatesResponse.addProperty("default", "test1");
qualityGatesResponse.add("qualitygates", qualityGateList);
return qualityGatesResponse;
}

@Test
public void getQualityGateStatusTest() throws BadSonarQubeRequestException, SonarQubeException {
// Create fake quality gate status
JsonObject qualityGateStatus = getQualityGateStatus();

// Test with a successful quality gate
QualityGateProviderWrapper provider = new QualityGateProviderWrapper();
provider.setFakeQualityGateStatus(qualityGateStatus);

Map<String,String> result = provider.getQualityGateStatus();
assertEquals(2, result.size());
for(Map.Entry<String,String> entry: result.entrySet()) {

assertTrue(entry.getKey().endsWith("_TEST"));

if (Objects.equals(entry.getValue(), "SUCCESS")) {
assertEquals("SUCCESS", entry.getValue());
} else {
assertEquals("ERROR (100000.0% is greater than 10%)", entry.getValue());
}
}
}

private static @NotNull JsonObject getQualityGateStatus() {
JsonObject condition1 = new JsonObject();
condition1.addProperty("status", "SUCCESS");
condition1.addProperty("metricKey", "metric_1");
Expand All @@ -186,23 +233,7 @@ public void getQualityGateStatusTest() throws BadSonarQubeRequestException, Sona
status.add("conditions", conditions);
JsonObject qualityGateStatus = new JsonObject();
qualityGateStatus.add("projectStatus", status);

// Test with a successful quality gate
QualityGateProviderWrapper provider = new QualityGateProviderWrapper();
provider.setFakeQualityGateStatus(qualityGateStatus);

Map<String,String> result = provider.getQualityGateStatus();
assertEquals(2, result.size());
for(Map.Entry<String,String> entry: result.entrySet()) {

assertTrue(entry.getKey().endsWith("_TEST"));

if (entry.getValue() == "SUCCESS") {
assertEquals("SUCCESS", entry.getValue());
} else {
assertEquals("ERROR (100000.0% is greater than 10%)", entry.getValue());
}
}
return qualityGateStatus;
}

@Test
Expand All @@ -220,6 +251,12 @@ public void getErrorExplanationTest() {
actual.add(qualityGateProvider.getErrorExplanationPublic("10000", "5000", "GT", "MILLISEC"));
actual.add(qualityGateProvider.getErrorExplanationPublic("3", "0", "GT", "INT"));

List<String> expected = getStrings();

assertEquals(expected, actual);
}

private static @NotNull List<String> getStrings() {
List<String> expected = new ArrayList<>();
expected.add(" (8 is worse than test)");
expected.add(" (E is worse than A)");
Expand All @@ -230,14 +267,13 @@ public void getErrorExplanationTest() {
expected.add(" (50.3% is less than 80%)");
expected.add(" (10000ms is greater than 5000ms)");
expected.add(" (3 is greater than 0)");

assertEquals(expected, actual);
return expected;
}

/**
* Wrapper on QualityGateProvider for testing purposes
*/
private class QualityGateProviderWrapper extends AbstractQualityGateProvider {
private static class QualityGateProviderWrapper extends AbstractQualityGateProvider {

// Stores the fake JsonObject responses to mock the API
private JsonObject fakeQualityGates;
Expand Down Expand Up @@ -273,26 +309,23 @@ public void setFakeQualityGateStatus(JsonObject pFake) {
/**
* Wrapper methods to mock the API response
*/
protected JsonObject getQualityGatesAsJsonObject() throws BadSonarQubeRequestException, SonarQubeException {
protected JsonObject getQualityGatesAsJsonObject() {
return fakeQualityGates;
}

protected JsonObject getQualityGatesDetailsAsJsonObject(final QualityGate qualityGate)
throws BadSonarQubeRequestException, SonarQubeException {
protected JsonObject getQualityGatesDetailsAsJsonObject(final QualityGate qualityGate) {
return fakeQualityGatesDetails;
}

protected JsonObject getProjectAsJsonObject() throws BadSonarQubeRequestException, SonarQubeException {
protected JsonObject getProjectAsJsonObject() {
return fakeProject;
}

protected JsonObject getQualityGateStatusAsJsonObject()
throws BadSonarQubeRequestException, SonarQubeException {
protected JsonObject getQualityGateStatusAsJsonObject() {
return fakeQualityGateStatus;
}

protected JsonObject getMetricAsJsonObject(final String metricKey)
throws BadSonarQubeRequestException, SonarQubeException {
protected JsonObject getMetricAsJsonObject(final String metricKey) {
// Create fake metric response
JsonObject metric = new JsonObject();
metric.addProperty("name", metricKey + "_TEST");
Expand Down

0 comments on commit 0bb01a6

Please sign in to comment.