fileList : rootJsonElement.getAsJsonObject().entrySet()) {
34 | JsonObject jsonObject = fileList.getValue().getAsJsonObject();
35 | if (jsonObject.has("status") && isMarkAsDeleted(jsonObject)) {
36 | LOG.debug("[GERRIT PLUGIN] File is marked as deleted, won't comment.");
37 | continue;
38 | }
39 |
40 | addFile(fileList.getKey());
41 | }
42 | } catch (IOException e) {
43 | throw new GerritPluginException(ERROR_LISTING, e);
44 | }
45 | }
46 |
47 | @NotNull
48 | private String trimResponse(@NotNull String response) {
49 | return StringUtils.replaceOnce(response, JSON_RESPONSE_PREFIX, "");
50 | }
51 |
52 | private boolean isMarkAsDeleted(JsonObject jsonObject) {
53 | return jsonObject.get("status").getAsCharacter() == 'D';
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/fr/techad/sonar/gerrit/network/ssh/GerritSshConnector.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.network.ssh;
2 |
3 | import fi.jpalomaki.ssh.Result;
4 | import fi.jpalomaki.ssh.SshClient;
5 | import fi.jpalomaki.ssh.UserAtHost;
6 | import fi.jpalomaki.ssh.jsch.JschSshClient;
7 | import fi.jpalomaki.ssh.jsch.JschSshClient.Options;
8 | import fr.techad.sonar.GerritConfiguration;
9 | import fr.techad.sonar.gerrit.GerritConnector;
10 | import org.jetbrains.annotations.NotNull;
11 | import org.sonar.api.utils.log.Logger;
12 | import org.sonar.api.utils.log.Loggers;
13 |
14 | import java.io.File;
15 | import java.io.IOException;
16 | import java.nio.ByteBuffer;
17 | import java.nio.file.Files;
18 | import java.nio.file.LinkOption;
19 | import java.nio.file.Paths;
20 |
21 | public class GerritSshConnector implements GerritConnector {
22 | private static final Logger LOG = Loggers.get(GerritSshConnector.class);
23 | private static final String CMD_LIST_FILES = "gerrit query --format=JSON --files --current-patch-set status:open change:%s limit:1";
24 | private static final String CMD_SET_REVIEW = "gerrit review %s -j";
25 | private static final String SSH_KNWON_HOSTS = ".ssh/known_hosts";
26 | private static final String SSH_STRICT_NO = "StrictHostKeyChecking=no";
27 |
28 | private final GerritConfiguration gerritConfiguration;
29 | private final UserAtHost userAtHost;
30 |
31 | public GerritSshConnector(GerritConfiguration gerritConfiguration) {
32 | LOG.debug("[GERRIT PLUGIN] Instanciating GerritSshConnector");
33 | this.gerritConfiguration = gerritConfiguration;
34 | userAtHost = new UserAtHost(gerritConfiguration.getUsername(), gerritConfiguration.getHost(),
35 | gerritConfiguration.getPort());
36 | }
37 |
38 | @NotNull
39 | @Override
40 | public String listFiles() throws IOException {
41 | SshClient sshClient = getSshClient();
42 |
43 | LOG.debug("[GERRIT PLUGIN] Execute command SSH {}",
44 | String.format(CMD_LIST_FILES, gerritConfiguration.getChangeId()));
45 |
46 | Result cmdResult = sshClient.executeCommand(String.format(CMD_LIST_FILES, gerritConfiguration.getChangeId()),
47 | userAtHost);
48 |
49 | return cmdResult.stdoutAsText("UTF-8");
50 | }
51 |
52 | @NotNull
53 | @Override
54 | public String setReview(String reviewInputAsJson) throws IOException {
55 | LOG.info("[GERRIT PLUGIN] Setting review {}", reviewInputAsJson);
56 |
57 | ByteBuffer stdin = ByteBuffer.wrap(reviewInputAsJson.getBytes("UTF-8"));
58 | SshClient sshClient = getSshClient();
59 | LOG.debug("[GERRIT PLUGIN] Execute command SSH {}",
60 | String.format(CMD_SET_REVIEW, gerritConfiguration.getRevisionId()));
61 |
62 | Result cmdResult = sshClient.executeCommand(String.format(CMD_SET_REVIEW, gerritConfiguration.getRevisionId()),
63 | stdin, userAtHost);
64 |
65 | return cmdResult.stdoutAsText();
66 | }
67 |
68 | private SshClient getSshClient() {
69 | SshClient sc = null;
70 |
71 | if (gerritConfiguration.shouldStrictlyCheckHostKey()) {
72 | LOG.debug("[GERRIT PLUGIN] SSH will check host key.");
73 | sc = new JschSshClient(gerritConfiguration.getSshKeyPath(), gerritConfiguration.getPassword());
74 | } else {
75 | LOG.debug("[GERRIT PLUGIN] SSH will not check host key.");
76 | String userKnownHosts = System.getProperty("user.home") + File.separator + SSH_KNWON_HOSTS;
77 | Boolean knownHostsExists = Files.exists(Paths.get(userKnownHosts), LinkOption.NOFOLLOW_LINKS);
78 |
79 | if (!knownHostsExists) {
80 | LOG.debug("[GERRIT PLUGIN] {} does not exist. Creating.", userKnownHosts);
81 | // known_hosts DOES NOT exists => create it
82 | try {
83 | Files.createFile(Paths.get(userKnownHosts));
84 | } catch (IOException e) {
85 | LOG.warn("[GERRIT PLUGIN] Could not create known_hosts", e);
86 | }
87 | LOG.debug("[GERRIT PLUGIN] {} created.", userKnownHosts);
88 | }
89 |
90 | sc = new JschSshClient(gerritConfiguration.getSshKeyPath(), gerritConfiguration.getPassword(),
91 | userKnownHosts, new Options("5s", "0s", "1M", "1M", SSH_STRICT_NO, false));
92 | }
93 |
94 | return sc;
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/src/main/java/fr/techad/sonar/gerrit/network/ssh/GerritSshFacade.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.network.ssh;
2 |
3 | import com.google.gson.JsonArray;
4 | import com.google.gson.JsonElement;
5 | import com.google.gson.JsonObject;
6 | import com.google.gson.JsonParser;
7 | import fr.techad.sonar.GerritPluginException;
8 | import fr.techad.sonar.gerrit.GerritConnector;
9 | import fr.techad.sonar.gerrit.GerritFacade;
10 | import org.sonar.api.utils.log.Logger;
11 | import org.sonar.api.utils.log.Loggers;
12 |
13 | import java.io.IOException;
14 |
15 | public class GerritSshFacade extends GerritFacade {
16 | private static final Logger LOG = Loggers.get(GerritSshFacade.class);
17 | private static final String ERROR_LISTING = "Error listing files";
18 |
19 | public GerritSshFacade(GerritConnector gerritConnector) {
20 | super(gerritConnector);
21 | LOG.debug("[GERRIT PLUGIN] Instanciating GerritSshFacade");
22 | }
23 |
24 | @Override
25 | protected void fillListFilesFromGerrit() throws GerritPluginException {
26 | try {
27 | String rawJsonString = getGerritConnector().listFiles();
28 | JsonArray files = new JsonParser().parse(rawJsonString.split("\r?\n")[0]).getAsJsonObject()
29 | .getAsJsonObject("currentPatchSet").getAsJsonArray("files");
30 |
31 | for (JsonElement jsonElement : files) {
32 | JsonObject jsonObject = jsonElement.getAsJsonObject();
33 |
34 | if (jsonObject.has("type") && isMarkAsDeleted(jsonObject)) {
35 | LOG.debug("[GERRIT PLUGIN] File is marked as deleted, won't comment.");
36 | continue;
37 | }
38 |
39 | addFile(jsonObject.get("file").getAsString());
40 | }
41 | } catch (IOException e) {
42 | throw new GerritPluginException(ERROR_LISTING, e);
43 | }
44 | }
45 |
46 | private boolean isMarkAsDeleted(JsonObject jsonObject) {
47 | return "DELETED".equals(jsonObject.get("type").getAsString());
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/fr/techad/sonar/gerrit/review/ReviewFileComment.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.review;
2 |
3 | import org.apache.commons.lang3.StringUtils;
4 | import org.sonar.api.utils.log.Logger;
5 | import org.sonar.api.utils.log.Loggers;
6 |
7 | import java.util.Objects;
8 |
9 | /**
10 | * Gerrit comment used with request for review input. Used with JSON marshaller
11 | * only.
12 | */
13 | public class ReviewFileComment {
14 | private static final Logger LOG = Loggers.get(ReviewFileComment.class);
15 |
16 | private String message;
17 | private int severity;
18 |
19 | public String getMessage() {
20 | return message;
21 | }
22 |
23 | public void setMessage(String message) {
24 | LOG.debug("[GERRIT PLUGIN] ReviewFileComment setMessage {}", message);
25 | this.message = message;
26 | if (Objects.isNull(this.message))
27 | this.message = StringUtils.EMPTY;
28 | }
29 |
30 | public int getSeverity() {
31 | return severity;
32 | }
33 |
34 | public void setSeverity(int severity) {
35 | this.severity = severity;
36 | }
37 |
38 | @Override
39 | public String toString() {
40 | return "ReviewFileComment [" + "message:" + message + ", " + "severity: " + severity + "]";
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/fr/techad/sonar/gerrit/review/ReviewInput.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.review;
2 |
3 | import fr.techad.sonar.gerrit.utils.ReviewUtils;
4 | import org.jetbrains.annotations.NotNull;
5 | import org.sonar.api.utils.log.Logger;
6 | import org.sonar.api.utils.log.Loggers;
7 |
8 | import java.util.ArrayList;
9 | import java.util.Iterator;
10 | import java.util.List;
11 | import java.util.Map;
12 | import java.util.concurrent.ConcurrentHashMap;
13 |
14 | /**
15 | * Gerrit request for review input. Used with JSON marshaller only.
16 | *
17 | * Example JSON:
18 | *
19 | * { "message": "Some nits need to be fixed.", "labels": { "Code-Review": -1 },
20 | * "comments": {
21 | * "gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java"
22 | * : [ { "line": 23, "message": "[nit] trailing whitespace" }, { "line": 49,
23 | * "message": "[nit] s/conrtol/control" } ] } }
24 | */
25 | public class ReviewInput {
26 | private static final Logger LOG = Loggers.get(ReviewInput.class);
27 | private String message = "Looks good to me.";
28 | private Map labels = new ConcurrentHashMap<>();
29 | private Map> comments = new ConcurrentHashMap<>();
30 |
31 | public void setValueAndLabel(@NotNull int value, @NotNull String label) {
32 | labels.put(label, value);
33 | }
34 |
35 | public void setLabelToPlusOne(@NotNull String label) {
36 | this.setValueAndLabel(1, label);
37 | }
38 |
39 | public void setLabelToMinusOne(@NotNull String label) {
40 | this.setValueAndLabel(-1, label);
41 | }
42 |
43 | public String getMessage() {
44 | return message;
45 | }
46 |
47 | public void setMessage(@NotNull String message) {
48 | this.message = message;
49 | }
50 |
51 | public void addComments(String key, List reviewFileComments) {
52 | comments.put(key, new ArrayList(reviewFileComments));
53 | }
54 |
55 | public int size() {
56 | return comments.size();
57 | }
58 |
59 | public void emptyComments() {
60 | comments.clear();
61 | }
62 |
63 | public Map getLabels() {
64 | return labels;
65 | }
66 |
67 | public Map> getComments() {
68 | return comments;
69 | }
70 |
71 | public boolean isEmpty() {
72 | return comments.isEmpty();
73 | }
74 |
75 | public int maxLevelSeverity() {
76 | int lvl = ReviewUtils.UNKNOWN_VALUE;
77 |
78 | for (Iterator> i = comments.values().iterator(); i.hasNext(); ) {
79 | List lrfc = i.next();
80 | for (ReviewFileComment review : lrfc) {
81 | lvl = Math.max(review.getSeverity(), lvl);
82 | }
83 | }
84 | LOG.debug("[GERRIT PLUGIN] The max level severity is {}", lvl);
85 |
86 | return lvl;
87 | }
88 |
89 | @Override
90 | public String toString() {
91 | return "ReviewInput [message=" + message + ", labels=" + labels + ", comments=" + comments + "]";
92 | }
93 | }
94 |
--------------------------------------------------------------------------------
/src/main/java/fr/techad/sonar/gerrit/review/ReviewLineComment.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.review;
2 |
3 | import org.sonar.api.utils.log.Logger;
4 | import org.sonar.api.utils.log.Loggers;
5 |
6 | public class ReviewLineComment extends ReviewFileComment {
7 | private static final Logger LOG = Loggers.get(ReviewLineComment.class);
8 | private Integer line = 0;
9 |
10 | public Integer getLine() {
11 | return line;
12 | }
13 |
14 | public void setLine(Integer setLine) {
15 | Integer tmpLine = 0;
16 |
17 | if (null == setLine) {
18 | LOG.debug("[GERRIT PLUGIN] ReviewLineComment line is null, forcing to 0");
19 | } else if (1 > setLine) {
20 | LOG.debug("[GERRIT PLUGIN] ReviewLineComment line < 0, forcing to 0");
21 | } else {
22 | LOG.debug("[GERRIT PLUGIN] ReviewLineComment setLine {}", setLine);
23 | tmpLine = setLine;
24 | }
25 |
26 | this.line = tmpLine;
27 | }
28 |
29 | @Override
30 | public String toString() {
31 | return "ReviewLineComment [line=" + line + ", message=" + getMessage() + "]";
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/src/main/java/fr/techad/sonar/gerrit/utils/ReviewUtils.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.utils;
2 |
3 | import org.sonar.api.batch.rule.Severity;
4 | import org.sonar.api.utils.log.Logger;
5 | import org.sonar.api.utils.log.Loggers;
6 |
7 | public final class ReviewUtils {
8 | public static final String UNKNOWN = "UNKNOWN";
9 | public static final int UNKNOWN_VALUE = -1;
10 | private static final Logger LOG = Loggers.get(ReviewUtils.class);
11 |
12 | private ReviewUtils() {
13 | super();
14 | }
15 |
16 | public static int thresholdToValue(String threshold) {
17 | int thresholdValue = UNKNOWN_VALUE;
18 |
19 | try {
20 | thresholdValue = Severity.valueOf(threshold).ordinal();
21 | } catch (Exception e) {
22 | LOG.warn("[GERRIT PLUGIN] Cannot convert threshold String {} to int. Using UNKNOWN.", threshold);
23 | thresholdValue = UNKNOWN_VALUE;
24 | }
25 |
26 | LOG.debug("[GERRIT PLUGIN] {} is converted to {}", threshold, thresholdValue);
27 |
28 | return thresholdValue;
29 | }
30 |
31 | public static String valueToThreshold(int value) {
32 | String threshold = UNKNOWN;
33 |
34 | try {
35 | threshold = Severity.values()[value].toString();
36 | } catch (Exception e) {
37 | LOG.warn("[GERRIT PLUGIN] Cannot convert threshold int {} to String. Using UNKNOWN.", value);
38 | }
39 |
40 | LOG.debug("[GERRIT PLUGIN] {} is converted to {}", value, threshold);
41 |
42 | return threshold;
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/fr/techad/sonar/utils/MessageUtils.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.utils;
2 |
3 | import org.apache.commons.lang3.text.StrSubstitutor;
4 | import org.sonar.api.batch.postjob.issue.PostJobIssue;
5 | import org.sonar.api.config.Settings;
6 |
7 | import java.util.Collections;
8 | import java.util.HashMap;
9 | import java.util.Map;
10 |
11 | public class MessageUtils {
12 |
13 | private static final String ISSUE_PREFIX = "issue";
14 | private static final String ISSUE_IS_NEW = "isNew";
15 | private static final String ISSUE_RULE_KEY = "ruleKey";
16 | private static final String ISSUE_SEVERITY = "severity";
17 | private static final String ISSUE_MESSAGE = "message";
18 | private static final String ISSUE_SEPARATOR = ".";
19 |
20 | private MessageUtils() {
21 | super();
22 | }
23 |
24 | /**
25 | * Create the issue message. The variables contained in the original message
26 | * are replaced by the settings values and the issue data.
27 | *
28 | * @param originalMessage the original message
29 | * @param settings the settings
30 | * @param issue the issue
31 | * @return a new message with the replaced variables by data.
32 | */
33 | public static String createIssueMessage(String originalMessage, Settings settings, PostJobIssue issue) {
34 | HashMap valueMap = new HashMap<>();
35 | valueMap.put(prefixKey(ISSUE_PREFIX, ISSUE_IS_NEW), issue.isNew());
36 | valueMap.put(prefixKey(ISSUE_PREFIX, ISSUE_RULE_KEY), issue.ruleKey());
37 | valueMap.put(prefixKey(ISSUE_PREFIX, ISSUE_SEVERITY), issue.severity());
38 | valueMap.put(prefixKey(ISSUE_PREFIX, ISSUE_MESSAGE), issue.message());
39 | return substituteProperties(originalMessage, settings, valueMap);
40 | }
41 |
42 | /**
43 | * Create the message from originalMessage and Settings. The variables
44 | * contained in the originalMessage are replaced by the Settings value
45 | *
46 | * @param originalMessage the original message which contains variables
47 | * @param settings the settings
48 | * @return a new string with substituted variables.
49 | */
50 | public static String createMessage(String originalMessage, Settings settings) {
51 | return substituteProperties(originalMessage, settings, Collections.emptyMap());
52 | }
53 |
54 | /**
55 | * Build a string based on an original string and the replacement by
56 | * settings and map values
57 | *
58 | * @param originalMessage the original string
59 | * @param settings the settings
60 | * @param additionalProperties the additional values
61 | * @return the built message
62 | */
63 | private static String substituteProperties(String originalMessage, Settings settings,
64 | Map additionalProperties) {
65 | if (additionalProperties.isEmpty()) {
66 | return StrSubstitutor.replace(originalMessage, settings.getProperties());
67 | }
68 | additionalProperties.putAll(settings.getProperties());
69 | return StrSubstitutor.replace(originalMessage, additionalProperties);
70 | }
71 |
72 | /**
73 | * Create the key
74 | *
75 | * @param prefix the prefix key
76 | * @param key the key
77 | * @return the key
78 | */
79 | private static String prefixKey(String prefix, String key) {
80 | return new StringBuffer(prefix).append(ISSUE_SEPARATOR).append(key).toString();
81 | }
82 |
83 | }
84 |
--------------------------------------------------------------------------------
/src/main/resources/org/sonar/l10n/gerrit.properties:
--------------------------------------------------------------------------------
1 | property.category.Gerrit=Gerrit
2 | property.category.Gerrit.description=Properties of the Gerrit plugin
3 | property.category.Gerrit.Server=Server
4 | property.category.Gerrit.Server.description=Server settings
5 | property.category.Gerrit.Review=Review
6 | property.category.Gerrit.Review.description=Review settings
7 | property.GERRIT_ENABLED.name=Enable plugin
8 | property.GERRIT_ENABLED.description=Enable or disable plugin
9 | property.GERRIT_SCHEME.name=Server scheme
10 | property.GERRIT_SCHEME.description=Define the server connection scheme
11 | property.GERRIT_HOST.name=Server host
12 | property.GERRIT_HOST.description=Define the gerrit host
13 | property.GERRIT_PORT.name=Server port
14 | property.GERRIT_PORT.description=Define the gerrit port
15 | property.GERRIT_USERNAME.name=Username
16 | property.GERRIT_USERNAME.description=Define the username to login with. Blank for anonymous over http.
17 | property.GERRIT_PASSWORD.name=Password
18 | property.GERRIT_PASSWORD.description=Define the password for the username (REST) or the private key (SSH)
19 | property.GERRIT_HTTP_AUTH_SCHEME.name=Server authentication scheme
20 | property.GERRIT_HTTP_AUTH_SCHEME.description=Define the server authentication scheme
21 | property.GERRIT_BASE_PATH.name=Base path
22 | property.GERRIT_BASE_PATH.description=Define an alternate server base path
23 | property.GERRIT_SSH_KEY_PATH.name=SSH private key path
24 | property.GERRIT_SSH_KEY_PATH.description=Path to the SSH private key to use when connection scheme is ssh.
25 | property.GERRIT_LABEL.name=Review label
26 | property.GERRIT_LABEL.description=Define the label which the vote will change
27 | property.GERRIT_MESSAGE.name=Review message
28 | property.GERRIT_MESSAGE.description=Define the message which will be added in comment
29 | property.GERRIT_ISSUE_COMMENT.name=Issue comment
30 | property.GERRIT_ISSUE_COMMENT.description=Define the message which will be used to comment issues
31 | property.GERRIT_CHANGE_ID.name=Change ID
32 | property.GERRIT_CHANGE_ID.description=Leave blank to delegate the definition by gerrit
33 | property.GERRIT_REVISION_ID.name=Patchset Revision ID
34 | property.GERRIT_REVISION_ID.description=Leave blank to delegate the definition by gerrit
35 | property.GERRIT_THRESHOLD.name=Alert threshold
36 | property.GERRIT_THRESHOLD.description=Sonar will vote -1 above this threshold
37 | property.GERRIT_VOTE_NO_ISSUE.name=Vote when no issues
38 | property.GERRIT_VOTE_NO_ISSUE.description=What to vote when no issues are detected
39 | property.GERRIT_VOTE_ISSUE_BELOW_THRESHOLD.name=Vote \u003c threshold
40 | property.GERRIT_VOTE_ISSUE_BELOW_THRESHOLD.description=What to vote when issues below threshold are detected
41 | property.GERRIT_VOTE_ISSUE_ABOVE_THRESHOLD.name=Vote \u2265 threshold
42 | property.GERRIT_VOTE_ISSUE_ABOVE_THRESHOLD.description=What to vote when issues above threshold are detected
43 | property.GERRIT_FORCE_BRANCH.name=Force branch
44 | property.GERRIT_FORCE_BRANCH.description=Set to true to force branch creation in SQ and override its name with Gerrit's branch name.
45 | property.GERRIT_COMMENT_NEW_ISSUES_ONLY.name=Comment new issues only
46 | property.GERRIT_COMMENT_NEW_ISSUES_ONLY.description=Sonar comment only newly created issues. Existing issues will not be reported.
47 | property.GERRIT_STRICT_HOSTKEY.name=SSH StrictHostKeyChecking
48 | property.GERRIT_STRICT_HOSTKEY.description=Enable or disable StrictHostKeyChecking when connection scheme is ssh.
49 |
--------------------------------------------------------------------------------
/src/main/resources/org/sonar/l10n/gerrit_fr.properties:
--------------------------------------------------------------------------------
1 | property.category.Gerrit=Gerrit
2 | property.category.Gerrit.description=Configuration du plugin Gerrit
3 | property.category.Gerrit.Server=Serveur
4 | property.category.Gerrit.Server.description=R\u00e9glages du serveur
5 | property.category.Gerrit.Review=R\u00e9vision
6 | property.category.Gerrit.Review.description=R\u00e9glages de r\u00e9vision
7 | property.GERRIT_ENABLED.name=Plugin actif
8 | property.GERRIT_ENABLED.description=Active ou d\u00e9sactive le plugin
9 | property.GERRIT_SCHEME.name=Sch\u00e9ma d'URL
10 | property.GERRIT_SCHEME.description=D\u00e9fini le sch\u00e9ma d'URL \u00e0 utiliser
11 | property.GERRIT_HOST.name=Serveur
12 | property.GERRIT_HOST.description=D\u00e9fini le serveur Gerrit
13 | property.GERRIT_PORT.name=Port du serveur
14 | property.GERRIT_PORT.description=D\u00e9fini le port du serveur Gerrit
15 | property.GERRIT_USERNAME.name=Nom d'utilisateur
16 | property.GERRIT_USERNAME.description=D\u00e9fini le nom d'utilisateur \u00e0 utiliser. Vide pour une connection anonyme avec http.
17 | property.GERRIT_PASSWORD.name=Mot de passe
18 | property.GERRIT_PASSWORD.description=D\u00e9fini le mot de passe \u00e0 utiliser pour la connection ou la clef priv\u00e9e.
19 | property.GERRIT_HTTP_AUTH_SCHEME.name=Sch\u00e9ma d'authentification
20 | property.GERRIT_HTTP_AUTH_SCHEME.description=D\u00e9fini le sch\u00e9ma d'authentification du serveur
21 | property.GERRIT_BASE_PATH.name=Chemin de base
22 | property.GERRIT_BASE_PATH.description=D\u00e9fini un chemin de base alternatif
23 | property.GERRIT_SSH_KEY_PATH.name=Chemin de la clef priv\u00e9e SSH
24 | property.GERRIT_SSH_KEY_PATH.description=Chemin de la clef priv\u00e9e SSH \u00e0 utiliser lorsque le sch\u00e9ma d'URI est ssh.
25 | property.GERRIT_LABEL.name=\u00c9tiquette de r\u00e9vision
26 | property.GERRIT_LABEL.description=D\u00e9fini l'\u00e9tiquette \u00e0 laquelle Sonar votera
27 | property.GERRIT_MESSAGE.name=Message de r\u00e9vision
28 | property.GERRIT_MESSAGE.description=D\u00e9fini le message qui sera ajout\u00e9 en commentaire de la r\u00e9vision
29 | property.GERRIT_ISSUE_COMMENT.name=Commentaire des d\u00e9fauts
30 | property.GERRIT_ISSUE_COMMENT.description=D\u00e9fini le message qui sera ajout\u00e9 en commentaire des d\u00e9fauts
31 | property.GERRIT_CHANGE_ID.name=ID du changement
32 | property.GERRIT_CHANGE_ID.description=Ne rien renseigner pour laisser Gerrit g\u00e9rer
33 | property.GERRIT_REVISION_ID.name=ID de la r\u00e9vision
34 | property.GERRIT_REVISION_ID.description=Ne rien renseigner pour laisser Gerrit g\u00e9rer
35 | property.GERRIT_THRESHOLD.name=Seuil des alertes
36 | property.GERRIT_THRESHOLD.description=Seuil au dela duquel Sonar votera -1
37 | property.GERRIT_VOTE_NO_ISSUE.name=Vote sans d\u00e9faut
38 | property.GERRIT_VOTE_NO_ISSUE.description=Valeur du vote lorsque aucun d\u00e9faut n'est d\u00e9t\u00e9ct\u00e9
39 | property.GERRIT_VOTE_ISSUE_BELOW_THRESHOLD.name=Vote \u003c seuil
40 | property.GERRIT_VOTE_ISSUE_BELOW_THRESHOLD.description=Valeur du vote si les d\u00e9fauts sont tous inf\u00e8rieurs au seuil
41 | property.GERRIT_VOTE_ISSUE_ABOVE_THRESHOLD.name=Vote \u2265 seuil
42 | property.GERRIT_VOTE_ISSUE_ABOVE_THRESHOLD.description=Valeur du vote si les d\u00e9fauts sont sup\u00e8rieurs au seuil
43 | property.GERRIT_FORCE_BRANCH.name=Forcer la branche
44 | property.GERRIT_FORCE_BRANCH.description=Mettre \u00e0 true pour surcharger et cr\u00e9er automatiquement une nouvelle branche dans SonarQube avec le nom de la branche Gerrit.
45 | property.GERRIT_COMMENT_NEW_ISSUES_ONLY.name=Commenter seulement les nouveaux d\u00e9fauts
46 | property.GERRIT_COMMENT_NEW_ISSUES_ONLY.description=Sonar ne commentera que les nouveaux d\u00e9fauts. Les d\u00e9fauts existants ne seront pas remont\u00e9s.
47 | property.GERRIT_STRICT_HOSTKEY.name=SSH StrictHostKeyChecking
48 | property.GERRIT_STRICT_HOSTKEY.description=Active ou d\u00e9sactive StrictHostKeyChecking lorsque le sch\u00e9ma d'URI est ssh.
49 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/GerritConfigurationTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar;
2 |
3 | import org.junit.jupiter.api.Assertions;
4 | import org.junit.jupiter.api.BeforeEach;
5 | import org.junit.jupiter.api.Test;
6 | import org.sonar.api.config.internal.MapSettings;
7 |
8 | import static org.fest.assertions.Assertions.assertThat;
9 |
10 | public class GerritConfigurationTest {
11 | private final String SCHEME = "http";
12 | private final String HOST = "localhost";
13 | private final Integer PORT = 8080;
14 | private final String USERNAME = "username";
15 | private final String PASSWORD = "sonar";
16 | private final String LABEL = "Code-Review";
17 | private final String PROJECT = "example";
18 | private final String BRANCH = "example";
19 | private final String CHANGE_ID = "I8473b95934b5732ac55d26311a706c9c2bde9940";
20 | private final String REVISION_ID = "674ac754f91e64a0efb8087e59a176484bd534d1";
21 | private GerritConfiguration gerritConfiguration;
22 |
23 | @BeforeEach
24 | public void setUp() {
25 | MapSettings settings = new MapSettings();
26 | settings.setProperty(PropertyKey.GERRIT_SCHEME, SCHEME).setProperty(PropertyKey.GERRIT_HOST, HOST)
27 | .setProperty(PropertyKey.GERRIT_PORT, PORT.toString()).setProperty(PropertyKey.GERRIT_USERNAME, USERNAME)
28 | .setProperty(PropertyKey.GERRIT_PASSWORD, PASSWORD).setProperty(PropertyKey.GERRIT_BASE_PATH, "")
29 | .setProperty(PropertyKey.GERRIT_PROJECT, PROJECT).setProperty(PropertyKey.GERRIT_BRANCH, BRANCH)
30 | .setProperty(PropertyKey.GERRIT_CHANGE_ID, CHANGE_ID)
31 | .setProperty(PropertyKey.GERRIT_REVISION_ID, REVISION_ID)
32 | .setProperty(PropertyKey.GERRIT_LABEL, LABEL);
33 | gerritConfiguration = new GerritConfiguration(settings);
34 | }
35 |
36 | @Test
37 | public void shouldValidateWithDefaults() throws GerritPluginException {
38 | // given
39 | // when
40 | gerritConfiguration.assertGerritConfiguration();
41 | // then
42 | assertThat(gerritConfiguration.isValid()).isTrue();
43 | }
44 |
45 | @Test
46 | public void shouldNotValidateIfHostIsBlank() throws GerritPluginException {
47 | // given
48 | gerritConfiguration.setHost("");
49 | // when
50 | gerritConfiguration.assertGerritConfiguration();
51 | // then
52 | assertThat(gerritConfiguration.isValid()).isFalse();
53 | }
54 |
55 | @Test
56 | public void shouldNotValidateIfPortIsBlank() throws GerritPluginException {
57 | // given
58 | gerritConfiguration.setPort(null);
59 | // when
60 | gerritConfiguration.assertGerritConfiguration();
61 | // then
62 | assertThat(gerritConfiguration.isValid()).isFalse();
63 | }
64 |
65 | @Test
66 | public void shouldNotValidateIfLabelIsBlank() throws GerritPluginException {
67 | // given
68 | gerritConfiguration.setLabel("");
69 | // when
70 | gerritConfiguration.assertGerritConfiguration();
71 | // then
72 | assertThat(gerritConfiguration.isValid()).isFalse();
73 | }
74 |
75 | @Test
76 | public void shouldNotValidateIfProjectNameIsBlank() throws GerritPluginException {
77 | // given
78 | gerritConfiguration.setProjectName("");
79 | // when
80 | gerritConfiguration.assertGerritConfiguration();
81 | // then
82 | assertThat(gerritConfiguration.isValid()).isFalse();
83 | }
84 |
85 | @Test
86 | public void shouldNotValidateIfBranchNameIsBlank() throws GerritPluginException {
87 | // given
88 | gerritConfiguration.setProjectName("");
89 | // when
90 | gerritConfiguration.assertGerritConfiguration();
91 | // then
92 | assertThat(gerritConfiguration.isValid()).isFalse();
93 | }
94 |
95 | @Test
96 | public void shouldNotValidateIfChangeIdIsBlank() throws GerritPluginException {
97 | // given
98 | gerritConfiguration.setChangeId("");
99 | // when
100 | gerritConfiguration.assertGerritConfiguration();
101 | // then
102 | assertThat(gerritConfiguration.isValid()).isFalse();
103 | }
104 |
105 | @Test
106 | public void shouldNotValidateIfRevisionIdIsBlank() throws GerritPluginException {
107 | // given
108 | gerritConfiguration.setRevisionId("");
109 | // when
110 | gerritConfiguration.assertGerritConfiguration();
111 | // then
112 | assertThat(gerritConfiguration.isValid()).isFalse();
113 | }
114 |
115 | @Test
116 | public void shouldHandleNullBasePath() throws GerritPluginException {
117 | // given
118 | gerritConfiguration.setBasePath(null);
119 | // when
120 | gerritConfiguration.assertGerritConfiguration();
121 | // then
122 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/");
123 | }
124 |
125 | @Test
126 | public void shouldHandleEmptyBasePath() throws GerritPluginException {
127 | // given
128 | gerritConfiguration.setBasePath("");
129 | // when
130 | gerritConfiguration.assertGerritConfiguration();
131 | // then
132 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/");
133 | }
134 |
135 | @Test
136 | public void shouldFixBasePathWithoutSlash() throws GerritPluginException {
137 | // given
138 | gerritConfiguration.setBasePath("gerrit");
139 | // when
140 | gerritConfiguration.assertGerritConfiguration();
141 | // then
142 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/gerrit");
143 | }
144 |
145 | @Test
146 | public void shouldNotFixBasePathWithSlash() throws GerritPluginException {
147 | // given
148 | gerritConfiguration.setBasePath("/gerrit");
149 | // when
150 | gerritConfiguration.assertGerritConfiguration();
151 | // then
152 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/gerrit");
153 | }
154 |
155 | @Test
156 | public void shouldFixBasePathWithSingleTrailingSlash() throws GerritPluginException {
157 | // given
158 | gerritConfiguration.setBasePath("/gerrit/");
159 | // when
160 | gerritConfiguration.assertGerritConfiguration();
161 | // then
162 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/gerrit");
163 | }
164 |
165 | @Test
166 | public void shouldFixBasePathWithMultiTrailingSlashs() throws GerritPluginException {
167 | // given
168 | gerritConfiguration.setBasePath("/gerrit///");
169 | // when
170 | gerritConfiguration.assertGerritConfiguration();
171 | // then
172 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/gerrit");
173 | }
174 |
175 | @Test
176 | public void shouldFixBasePathWithMultiHeadingSlashs() throws GerritPluginException {
177 | // given
178 | gerritConfiguration.setBasePath("///gerrit");
179 | // when
180 | gerritConfiguration.assertGerritConfiguration();
181 | // then
182 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/gerrit");
183 | }
184 |
185 | @Test
186 | public void shouldFixBasePathWithMulitHeadingAndTrailingSlashs() throws GerritPluginException {
187 | // given
188 | gerritConfiguration.setBasePath("///gerrit///");
189 | // when
190 | gerritConfiguration.assertGerritConfiguration();
191 | // then
192 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/gerrit");
193 | }
194 |
195 | @Test
196 | public void shouldFixBasePathWithMultiSlashsOnly() throws GerritPluginException {
197 | // given
198 | gerritConfiguration.setBasePath("////");
199 | // when
200 | gerritConfiguration.assertGerritConfiguration();
201 | // then
202 | assertThat(gerritConfiguration.getBasePath()).isEqualTo("/");
203 | }
204 |
205 | @Test
206 | public void shouldBeAnonymous() {
207 | gerritConfiguration.setUsername("");
208 | Assertions.assertTrue(gerritConfiguration.isAnonymous());
209 | }
210 |
211 | @Test
212 | public void shouldNotBeAnonymous() {
213 | gerritConfiguration.setUsername(USERNAME);
214 | Assertions.assertFalse(gerritConfiguration.isAnonymous());
215 | }
216 |
217 | @Test
218 | public void shouldBeAnonymousWithBlankUserName() {
219 | gerritConfiguration.setUsername("");
220 | Assertions.assertTrue(gerritConfiguration.isAnonymous());
221 | gerritConfiguration.setUsername(null);
222 | Assertions.assertTrue(gerritConfiguration.isAnonymous());
223 | }
224 |
225 | @Test
226 | public void shouldCommentNewIssuesOnly() {
227 | gerritConfiguration.commentNewIssuesOnly(true);
228 | Assertions.assertTrue(gerritConfiguration.shouldCommentNewIssuesOnly());
229 | }
230 |
231 | @Test
232 | public void shouldNotCommentNewIssuesOnly() {
233 | gerritConfiguration.commentNewIssuesOnly(false);
234 | Assertions.assertFalse(gerritConfiguration.shouldCommentNewIssuesOnly());
235 | }
236 |
237 | @Test
238 | public void shouldStrictlyCheckHostKey() {
239 | gerritConfiguration.strictlyCheckHostkey(true);
240 | Assertions.assertTrue(gerritConfiguration.shouldStrictlyCheckHostKey());
241 | }
242 |
243 | @Test
244 | public void shouldNotStrictlyCheckHostKey() {
245 | gerritConfiguration.strictlyCheckHostkey(false);
246 | Assertions.assertFalse(gerritConfiguration.shouldStrictlyCheckHostKey());
247 | }
248 |
249 | @Test
250 | public void shouldGetUsername() {
251 | Assertions.assertEquals(USERNAME, this.gerritConfiguration.getUsername());
252 | }
253 |
254 | @Test
255 | public void shouldGetPassword() {
256 | Assertions.assertEquals(PASSWORD, this.gerritConfiguration.getPassword());
257 | }
258 |
259 | @Test
260 | public void shouldGetHttpAuthScheme() {
261 | String httpAuthScheme = "DIGEST";
262 | this.gerritConfiguration.setHttpAuthScheme(httpAuthScheme);
263 | Assertions.assertEquals(httpAuthScheme, this.gerritConfiguration.getHttpAuthScheme());
264 | }
265 |
266 | @Test
267 | public void shouldGetScheme() {
268 | Assertions.assertEquals(SCHEME, this.gerritConfiguration.getScheme());
269 | }
270 |
271 | @Test
272 | public void shouldGetHost() {
273 | Assertions.assertEquals(HOST, this.gerritConfiguration.getHost());
274 | }
275 |
276 | @Test
277 | public void shouldGetPort() {
278 | Assertions.assertEquals(PORT, this.gerritConfiguration.getPort());
279 | }
280 |
281 | @Test
282 | public void shouldGetSshKeyPath() {
283 | String sshKeyPath = "/user/sonar";
284 | this.gerritConfiguration.setSshKeyPath(sshKeyPath);
285 | Assertions.assertEquals(sshKeyPath, this.gerritConfiguration.getSshKeyPath());
286 | }
287 |
288 | @Test
289 | public void shouldGetLabel() {
290 | Assertions.assertEquals(LABEL, this.gerritConfiguration.getLabel());
291 | String label = "Quality-Code";
292 | this.gerritConfiguration.setLabel(label);
293 | Assertions.assertEquals(label, this.gerritConfiguration.getLabel());
294 | }
295 |
296 | @Test
297 | public void shouldGetMessage() {
298 | String msg = "Message ${Replace}";
299 | this.gerritConfiguration.setMessage(msg);
300 | Assertions.assertEquals(msg, this.gerritConfiguration.getMessage());
301 | }
302 |
303 | @Test
304 | public void shouldGetIssueComment() {
305 | String msg = "Issue Comment";
306 | this.gerritConfiguration.setIssueComment(msg);
307 | Assertions.assertEquals(msg, this.gerritConfiguration.getIssueComment());
308 | }
309 |
310 | @Test
311 | public void shouldGetThreshold() {
312 | String threshold = "INFO";
313 | this.gerritConfiguration.setThreshold(threshold);
314 | Assertions.assertEquals(threshold, this.gerritConfiguration.getThreshold());
315 | }
316 |
317 | @Test
318 | public void shouldGetVoteNoIssue() {
319 | int vote = 2;
320 | this.gerritConfiguration.setVoteNoIssue(vote);
321 | Assertions.assertEquals(vote, this.gerritConfiguration.getVoteNoIssue());
322 | }
323 |
324 | @Test
325 | public void shouldGetVoteAboveThreshold() {
326 | int vote = 1;
327 | this.gerritConfiguration.setVoteAboveThreshold(vote);
328 | Assertions.assertEquals(vote, this.gerritConfiguration.getVoteAboveThreshold());
329 | }
330 |
331 | @Test
332 | public void shouldGetVoteBelowThreshold() {
333 | int vote = -11;
334 | this.gerritConfiguration.setVoteBelowThreshold(vote);
335 | Assertions.assertEquals(vote, this.gerritConfiguration.getVoteBelowThreshold());
336 | }
337 |
338 | @Test
339 | public void shouldGetProjectName() {
340 | Assertions.assertEquals(PROJECT, this.gerritConfiguration.getProjectName());
341 | }
342 |
343 | @Test
344 | public void shouldGetBranchName() {
345 | Assertions.assertEquals(BRANCH, this.gerritConfiguration.getBranchName());
346 | }
347 |
348 | @Test
349 | public void shouldGetChangeId() {
350 | Assertions.assertEquals(CHANGE_ID, this.gerritConfiguration.getChangeId());
351 | }
352 |
353 | @Test
354 | public void shouldGetRevisionId() {
355 | Assertions.assertEquals(REVISION_ID, this.gerritConfiguration.getRevisionId());
356 | }
357 |
358 | @Test
359 | public void shouldInvalidateConfigurationNoUserNameInSsh() {
360 | gerritConfiguration.setScheme(GerritConstants.SCHEME_SSH);
361 | gerritConfiguration.setUsername("");
362 | gerritConfiguration.assertGerritConfiguration();
363 | Assertions.assertFalse(gerritConfiguration.isValid());
364 | }
365 |
366 | @Test
367 | public void shouldInvalidateConfigurationNoSshKeyPathInSsh() {
368 | gerritConfiguration.setScheme(GerritConstants.SCHEME_SSH);
369 | gerritConfiguration.setSshKeyPath("");
370 | gerritConfiguration.assertGerritConfiguration();
371 | Assertions.assertFalse(gerritConfiguration.isValid());
372 | }
373 |
374 | @Test
375 | public void shouldInvalidateConfigurationWithBlankLabel() {
376 | gerritConfiguration.setLabel("");
377 | gerritConfiguration.assertGerritConfiguration();
378 | Assertions.assertFalse(gerritConfiguration.isValid());
379 | }
380 |
381 | @Test
382 | public void shouldInvalidateConfigurationWithBlankProjectName() {
383 | gerritConfiguration.setProjectName("");
384 | gerritConfiguration.assertGerritConfiguration();
385 | Assertions.assertFalse(gerritConfiguration.isValid());
386 | }
387 |
388 | @Test
389 | public void shouldInvalidateConfigurationWithBlankBranchName() {
390 | gerritConfiguration.setBranchName("");
391 | gerritConfiguration.assertGerritConfiguration();
392 | Assertions.assertFalse(gerritConfiguration.isValid());
393 | }
394 |
395 | @Test
396 | public void shouldInvalidateConfigurationWithBlankChangeId() {
397 | gerritConfiguration.setChangeId("");
398 | gerritConfiguration.assertGerritConfiguration();
399 | Assertions.assertFalse(gerritConfiguration.isValid());
400 | }
401 |
402 | @Test
403 | public void shouldInvalidateConfigurationWithBlankRevisionId() {
404 | gerritConfiguration.setRevisionId("");
405 | gerritConfiguration.assertGerritConfiguration();
406 | Assertions.assertFalse(gerritConfiguration.isValid());
407 | }
408 | }
409 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/GerritInitializerTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar;
2 |
3 | import fr.techad.sonar.gerrit.GerritFacade;
4 | import fr.techad.sonar.gerrit.factory.GerritFacadeFactory;
5 | import fr.techad.sonar.mockito.MockitoExtension;
6 | import org.junit.jupiter.api.Test;
7 | import org.junit.jupiter.api.extension.ExtendWith;
8 | import org.junit.platform.runner.JUnitPlatform;
9 | import org.junit.runner.RunWith;
10 | import org.mockito.Mock;
11 |
12 | import static org.mockito.Mockito.never;
13 | import static org.mockito.Mockito.times;
14 | import static org.mockito.Mockito.verify;
15 | import static org.mockito.Mockito.when;
16 |
17 | /**
18 | * TECH ADVANTAGE
19 | * All right reserved
20 | * Created by cochon on 22/07/2018.
21 | */
22 | @ExtendWith(MockitoExtension.class)
23 | @RunWith(JUnitPlatform.class)
24 | class GerritInitializerTest {
25 |
26 | @Mock
27 | private GerritConfiguration gerritConfiguration;
28 |
29 | @Mock
30 | private GerritFacadeFactory gerritFacadeFactory;
31 |
32 | @Mock
33 | private GerritFacade gerritFacade;
34 |
35 | @Test
36 | public void shouldListFiles() throws GerritPluginException {
37 | when(gerritConfiguration.isEnabled()).thenReturn(true);
38 | when(gerritConfiguration.isValid()).thenReturn(true);
39 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
40 |
41 | GerritInitializer gerritInitializer = new GerritInitializer(gerritConfiguration, gerritFacadeFactory);
42 | gerritInitializer.execute();
43 | verify(gerritFacade, times(1)).listFiles();
44 | }
45 |
46 | @Test
47 | public void shouldNotListFilesWhenDisables() throws GerritPluginException {
48 | when(gerritConfiguration.isEnabled()).thenReturn(false);
49 | when(gerritConfiguration.isValid()).thenReturn(true);
50 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
51 |
52 | GerritInitializer gerritInitializer = new GerritInitializer(gerritConfiguration, gerritFacadeFactory);
53 | gerritInitializer.execute();
54 | verify(gerritFacade, never()).listFiles();
55 | }
56 |
57 | @Test
58 | public void shouldNotListFilesWhenInvalid() throws GerritPluginException {
59 | when(gerritConfiguration.isEnabled()).thenReturn(true);
60 | when(gerritConfiguration.isValid()).thenReturn(false);
61 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
62 |
63 | GerritInitializer gerritInitializer = new GerritInitializer(gerritConfiguration, gerritFacadeFactory);
64 | gerritInitializer.execute();
65 | verify(gerritFacade, never()).listFiles();
66 | }
67 |
68 | @Test
69 | public void shouldCatchExceptionIfListFilesThrowsIt() throws GerritPluginException {
70 | when(gerritConfiguration.isEnabled()).thenReturn(true);
71 | when(gerritConfiguration.isValid()).thenReturn(true);
72 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
73 | when(gerritFacade.listFiles()).thenThrow(new GerritPluginException("Mock Exception During Test"));
74 |
75 | GerritInitializer gerritInitializer = new GerritInitializer(gerritConfiguration, gerritFacadeFactory);
76 | gerritInitializer.execute();
77 | verify(gerritFacade, times(1)).listFiles();
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/GerritPostJobTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar;
2 |
3 | import fr.techad.sonar.gerrit.GerritFacade;
4 | import fr.techad.sonar.gerrit.factory.GerritFacadeFactory;
5 | import fr.techad.sonar.gerrit.review.ReviewFileComment;
6 | import fr.techad.sonar.gerrit.review.ReviewInput;
7 | import fr.techad.sonar.gerrit.review.ReviewLineComment;
8 | import fr.techad.sonar.mockito.MockitoExtension;
9 | import org.junit.jupiter.api.Assertions;
10 | import org.junit.jupiter.api.BeforeEach;
11 | import org.junit.jupiter.api.Test;
12 | import org.junit.jupiter.api.extension.ExtendWith;
13 | import org.junit.platform.runner.JUnitPlatform;
14 | import org.junit.runner.RunWith;
15 | import org.mockito.ArgumentCaptor;
16 | import org.mockito.Captor;
17 | import org.sonar.api.batch.fs.InputPath;
18 | import org.sonar.api.batch.postjob.PostJobContext;
19 | import org.sonar.api.batch.postjob.PostJobDescriptor;
20 | import org.sonar.api.batch.postjob.issue.PostJobIssue;
21 | import org.sonar.api.batch.rule.Severity;
22 | import org.sonar.api.config.internal.MapSettings;
23 | import org.sonar.api.rule.RuleKey;
24 |
25 | import java.io.File;
26 | import java.util.ArrayList;
27 | import java.util.List;
28 | import java.util.Map;
29 |
30 | import static org.mockito.ArgumentMatchers.any;
31 | import static org.mockito.Mockito.doThrow;
32 | import static org.mockito.Mockito.mock;
33 | import static org.mockito.Mockito.never;
34 | import static org.mockito.Mockito.times;
35 | import static org.mockito.Mockito.verify;
36 | import static org.mockito.Mockito.when;
37 |
38 | /**
39 | * TECH ADVANTAGE
40 | * All right reserved
41 | * Created by cochon on 31/07/2018.
42 | */
43 | @ExtendWith(MockitoExtension.class)
44 | @RunWith(JUnitPlatform.class)
45 | class GerritPostJobTest {
46 | private static final String LABEL = "Code-Review";
47 | private static final String PATH1 = "src/main/java/F1.java";
48 | private static final String PATH2 = "src/main/java/F2.java";
49 |
50 | @Captor
51 | ArgumentCaptor describeStringCaptor;
52 | @Captor
53 | ArgumentCaptor reviewInputCaptor;
54 |
55 | private MapSettings settings;
56 |
57 | @BeforeEach
58 | public void setUp() {
59 | ReviewHolder.getReviewInput().emptyComments();
60 | // Common Settings
61 | settings = new MapSettings();
62 | settings.setProperty(PropertyKey.GERRIT_SCHEME, GerritConstants.SCHEME_HTTP)
63 | .setProperty(PropertyKey.GERRIT_HOST, "localhost")
64 | .appendProperty(PropertyKey.GERRIT_PORT, "10800")
65 | .setProperty(PropertyKey.GERRIT_PROJECT, "project")
66 | .setProperty(PropertyKey.GERRIT_CHANGE_ID, "changeid")
67 | .setProperty(PropertyKey.GERRIT_REVISION_ID, "revisionid")
68 | .setProperty(PropertyKey.GERRIT_VOTE_NO_ISSUE, "1")
69 | .setProperty(PropertyKey.GERRIT_VOTE_ISSUE_ABOVE_THRESHOLD, "-2")
70 | .setProperty(PropertyKey.GERRIT_VOTE_ISSUE_BELOW_THRESHOLD, "-1")
71 | .setProperty(PropertyKey.GERRIT_ENABLED, "true")
72 | .setProperty(PropertyKey.GERRIT_MESSAGE, "Message Test")
73 | .setProperty(PropertyKey.GERRIT_ISSUE_COMMENT, "[New: ${issue.isNew}] ${issue.severity}(${issue.ruleKey}) found: ${issue.message}")
74 | .setProperty(PropertyKey.GERRIT_LABEL, LABEL);
75 | }
76 |
77 | @Test
78 | void shouldDescribeTheJob() {
79 | GerritConfiguration gerritConfiguration = mock(GerritConfiguration.class);
80 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
81 | PostJobDescriptor descriptor = mock(PostJobDescriptor.class);
82 |
83 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
84 | gerritPostJob.describe(descriptor);
85 | verify(descriptor).name(describeStringCaptor.capture());
86 | String name = describeStringCaptor.getValue();
87 | verify(descriptor).requireProperty(describeStringCaptor.capture());
88 | String property = describeStringCaptor.getValue();
89 | Assertions.assertEquals("GERRIT PLUGIN", name);
90 | Assertions.assertEquals(PropertyKey.GERRIT_CHANGE_ID, property);
91 | }
92 |
93 | @Test
94 | public void shouldCallOneListFilesOnAssertOrFetchGerritModifiedFiles() throws GerritPluginException {
95 | GerritConfiguration gerritConfiguration = mock(GerritConfiguration.class);
96 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
97 | GerritFacade gerritFacade = mock(GerritFacade.class);
98 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
99 | when(gerritFacade.listFiles()).thenReturn(new ArrayList<>());
100 |
101 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
102 | gerritPostJob.assertOrFetchGerritModifiedFiles();
103 | gerritPostJob.assertOrFetchGerritModifiedFiles();
104 | verify(gerritFacade, times(1)).listFiles();
105 | }
106 |
107 | @Test
108 | public void shouldConvertIssueToComment() {
109 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
110 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
111 |
112 | RuleKey ruleKey = RuleKey.of("test-repo", "sonar-1");
113 | PostJobIssue postJobIssue = mock(PostJobIssue.class);
114 | when(postJobIssue.line()).thenReturn(12);
115 | when(postJobIssue.severity()).thenReturn(Severity.INFO);
116 | when(postJobIssue.message()).thenReturn("Should be a message test");
117 | when(postJobIssue.ruleKey()).thenReturn(ruleKey);
118 | when(postJobIssue.isNew()).thenReturn(Boolean.TRUE);
119 |
120 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
121 | ReviewLineComment reviewLineComment = gerritPostJob.issueToComment(postJobIssue);
122 | Assertions.assertEquals(new Integer(12), reviewLineComment.getLine());
123 | Assertions.assertEquals(0, reviewLineComment.getSeverity());
124 | Assertions.assertEquals("[New: true] INFO(test-repo:sonar-1) found: Should be a message test", reviewLineComment.getMessage());
125 | }
126 |
127 | @Test
128 | public void shouldDoNothingIfThePluginIsDisabled() throws GerritPluginException {
129 | GerritConfiguration gerritConfiguration = mock(GerritConfiguration.class);
130 | when(gerritConfiguration.isEnabled()).thenReturn(Boolean.FALSE);
131 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
132 | GerritFacade gerritFacade = mock(GerritFacade.class);
133 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
134 | PostJobContext postJobContext = mock(PostJobContext.class);
135 |
136 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
137 | gerritPostJob.execute(postJobContext);
138 | verify(gerritFacade, never()).setReview(any());
139 | }
140 |
141 | @Test
142 | public void shouldSendNoIssueOnExecute() throws GerritPluginException {
143 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
144 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
145 | GerritFacade gerritFacade = mock(GerritFacade.class);
146 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
147 | PostJobContext postJobContext = mock(PostJobContext.class);
148 | when(postJobContext.issues()).thenReturn(new ArrayList<>());
149 |
150 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
151 | gerritPostJob.execute(postJobContext);
152 |
153 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
154 | ReviewInput reviewInput = reviewInputCaptor.getValue();
155 | Map labels = reviewInput.getLabels();
156 | Assertions.assertEquals(1, labels.size());
157 | Integer integer = labels.get(LABEL);
158 | Assertions.assertNotNull(integer);
159 | Assertions.assertEquals(1, integer.intValue());
160 | Assertions.assertEquals(0, reviewInput.getComments().size());
161 | }
162 |
163 | @Test
164 | public void shouldSendBelowMsgIssueOnExecute() throws GerritPluginException {
165 | settings.setProperty(PropertyKey.GERRIT_THRESHOLD, "BLOCKER");
166 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
167 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
168 | GerritFacade gerritFacade = mock(GerritFacade.class);
169 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
170 | List listFiles = new ArrayList<>();
171 | listFiles.add(PATH1);
172 | listFiles.add(PATH2);
173 | when(gerritFacade.listFiles()).thenReturn(listFiles);
174 |
175 | InputPath inputPath1 = createInputPath(PATH1, Boolean.TRUE);
176 | InputPath inputPath2 = createInputPath(PATH2, Boolean.TRUE);
177 |
178 | List postJobIssues = new ArrayList<>();
179 | postJobIssues.add(createPostJobIssueMock(10, inputPath1, Severity.INFO, "R1", "Msg 1", Boolean.TRUE));
180 | postJobIssues.add(createPostJobIssueMock(20, inputPath1, Severity.MAJOR, "R2", "Msg 2", Boolean.TRUE));
181 | postJobIssues.add(createPostJobIssueMock(1020, inputPath2, Severity.INFO, "R3", "Msg 3", Boolean.TRUE));
182 | PostJobContext postJobContext = mock(PostJobContext.class);
183 | when(postJobContext.issues()).thenReturn(postJobIssues);
184 |
185 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
186 | gerritPostJob.execute(postJobContext);
187 |
188 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
189 | ReviewInput reviewInput = reviewInputCaptor.getValue();
190 | Map labels = reviewInput.getLabels();
191 | Assertions.assertEquals(1, labels.size());
192 | // Vote Value
193 | Integer integer = labels.get(LABEL);
194 | Assertions.assertNotNull(integer);
195 | Assertions.assertEquals(-1, integer.intValue());
196 | Map> inputComments = reviewInput.getComments();
197 | Assertions.assertEquals(2, inputComments.size());
198 | List reviewFileComments = inputComments.get(PATH1);
199 | Assertions.assertEquals(2, reviewFileComments.size());
200 | Assertions.assertEquals(0, reviewFileComments.get(0).getSeverity());
201 | ReviewLineComment reviewLineComment = (ReviewLineComment) reviewFileComments.get(0);
202 | Assertions.assertEquals(10, reviewLineComment.getLine().intValue());
203 | Assertions.assertEquals(2, reviewFileComments.get(1).getSeverity());
204 | reviewLineComment = (ReviewLineComment) reviewFileComments.get(1);
205 | Assertions.assertEquals(20, reviewLineComment.getLine().intValue());
206 | reviewFileComments = inputComments.get(PATH2);
207 | Assertions.assertEquals(1, reviewFileComments.size());
208 | Assertions.assertEquals(0, reviewFileComments.get(0).getSeverity());
209 | reviewLineComment = (ReviewLineComment) reviewFileComments.get(0);
210 | Assertions.assertEquals(1020, reviewLineComment.getLine().intValue());
211 | }
212 |
213 | @Test
214 | public void shouldSendAboveMsgIssueOnExecute() throws GerritPluginException {
215 | settings.setProperty(PropertyKey.GERRIT_THRESHOLD, "INFO");
216 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
217 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
218 | GerritFacade gerritFacade = mock(GerritFacade.class);
219 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
220 | List listFiles = new ArrayList<>();
221 | listFiles.add(PATH1);
222 | listFiles.add(PATH2);
223 | when(gerritFacade.listFiles()).thenReturn(listFiles);
224 |
225 | InputPath inputPath1 = createInputPath(PATH1, Boolean.TRUE);
226 | InputPath inputPath2 = createInputPath(PATH2, Boolean.TRUE);
227 |
228 | List postJobIssues = new ArrayList<>();
229 | postJobIssues.add(createPostJobIssueMock(10, inputPath1, Severity.MAJOR, "R1", "Msg 1", Boolean.TRUE));
230 | postJobIssues.add(createPostJobIssueMock(20, inputPath1, Severity.MAJOR, "R2", "Msg 2", Boolean.TRUE));
231 | postJobIssues.add(createPostJobIssueMock(1020, inputPath2, Severity.MAJOR, "R3", "Msg 3", Boolean.TRUE));
232 | PostJobContext postJobContext = mock(PostJobContext.class);
233 | when(postJobContext.issues()).thenReturn(postJobIssues);
234 |
235 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
236 | gerritPostJob.execute(postJobContext);
237 |
238 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
239 | ReviewInput reviewInput = reviewInputCaptor.getValue();
240 | Map labels = reviewInput.getLabels();
241 | Assertions.assertEquals(1, labels.size());
242 | // Vote Value
243 | Integer integer = labels.get(LABEL);
244 | Assertions.assertNotNull(integer);
245 | Assertions.assertEquals(-2, integer.intValue());
246 | Map> inputComments = reviewInput.getComments();
247 | Assertions.assertEquals(2, inputComments.size());
248 | List reviewFileComments = inputComments.get(PATH1);
249 | Assertions.assertEquals(2, reviewFileComments.size());
250 | Assertions.assertEquals(2, reviewFileComments.get(0).getSeverity());
251 | ReviewLineComment reviewLineComment = (ReviewLineComment) reviewFileComments.get(0);
252 | Assertions.assertEquals(10, reviewLineComment.getLine().intValue());
253 | Assertions.assertEquals(2, reviewFileComments.get(1).getSeverity());
254 | reviewLineComment = (ReviewLineComment) reviewFileComments.get(1);
255 | Assertions.assertEquals(20, reviewLineComment.getLine().intValue());
256 | reviewFileComments = inputComments.get(PATH2);
257 | Assertions.assertEquals(1, reviewFileComments.size());
258 | Assertions.assertEquals(2, reviewFileComments.get(0).getSeverity());
259 | reviewLineComment = (ReviewLineComment) reviewFileComments.get(0);
260 | Assertions.assertEquals(1020, reviewLineComment.getLine().intValue());
261 | }
262 |
263 | @Test
264 | public void shouldCommentNewIssueOnly() throws GerritPluginException {
265 | settings.setProperty(PropertyKey.GERRIT_THRESHOLD, "INFO");
266 | settings.setProperty(PropertyKey.GERRIT_COMMENT_NEW_ISSUES_ONLY, "true");
267 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
268 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
269 | GerritFacade gerritFacade = mock(GerritFacade.class);
270 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
271 | List listFiles = new ArrayList<>();
272 | listFiles.add(PATH1);
273 | when(gerritFacade.listFiles()).thenReturn(listFiles);
274 |
275 | InputPath inputPath1 = createInputPath(PATH1, Boolean.TRUE);
276 |
277 | List postJobIssues = new ArrayList<>();
278 | postJobIssues.add(createPostJobIssueMock(10, inputPath1, Severity.MAJOR, "R1", "Msg 1", Boolean.TRUE));
279 | postJobIssues.add(createPostJobIssueMock(20, inputPath1, Severity.BLOCKER, "R2", "Msg 2", Boolean.FALSE));
280 | PostJobContext postJobContext = mock(PostJobContext.class);
281 | when(postJobContext.issues()).thenReturn(postJobIssues);
282 |
283 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
284 | gerritPostJob.execute(postJobContext);
285 |
286 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
287 | ReviewInput reviewInput = reviewInputCaptor.getValue();
288 | Map labels = reviewInput.getLabels();
289 | Assertions.assertEquals(1, labels.size());
290 | // Vote Value
291 | Integer integer = labels.get(LABEL);
292 | Assertions.assertNotNull(integer);
293 | Assertions.assertEquals(-2, integer.intValue());
294 | Map> inputComments = reviewInput.getComments();
295 | Assertions.assertEquals(1, inputComments.size());
296 | List reviewFileComments = inputComments.get(PATH1);
297 | Assertions.assertEquals(1, reviewFileComments.size());
298 | Assertions.assertEquals(2, reviewFileComments.get(0).getSeverity());
299 | ReviewLineComment reviewLineComment = (ReviewLineComment) reviewFileComments.get(0);
300 | Assertions.assertEquals(10, reviewLineComment.getLine().intValue());
301 | }
302 |
303 | @Test
304 | public void shouldFoundFilenameWithPrependValue() throws GerritPluginException {
305 | String path1WithProject = "P1/" + PATH1;
306 | settings.setProperty(PropertyKey.GERRIT_THRESHOLD, "INFO");
307 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
308 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
309 | GerritFacade gerritFacade = mock(GerritFacade.class);
310 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
311 | List listFiles = new ArrayList<>();
312 | listFiles.add(PATH1);
313 | when(gerritFacade.listFiles()).thenReturn(listFiles);
314 | when(gerritFacade.parseFileName(path1WithProject)).thenReturn(PATH1);
315 |
316 | InputPath inputPath1 = createInputPath(path1WithProject, Boolean.TRUE);
317 |
318 | List postJobIssues = new ArrayList<>();
319 | postJobIssues.add(createPostJobIssueMock(10, inputPath1, Severity.MAJOR, "R1", "Msg 1", Boolean.TRUE));
320 | PostJobContext postJobContext = mock(PostJobContext.class);
321 | when(postJobContext.issues()).thenReturn(postJobIssues);
322 |
323 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
324 | gerritPostJob.execute(postJobContext);
325 |
326 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
327 | ReviewInput reviewInput = reviewInputCaptor.getValue();
328 | Map labels = reviewInput.getLabels();
329 | Assertions.assertEquals(1, labels.size());
330 | // Vote Value
331 | Integer integer = labels.get(LABEL);
332 | Assertions.assertNotNull(integer);
333 | Assertions.assertEquals(-2, integer.intValue());
334 | Map> inputComments = reviewInput.getComments();
335 | Assertions.assertEquals(1, inputComments.size());
336 | List reviewFileComments = inputComments.get(PATH1);
337 | Assertions.assertEquals(1, reviewFileComments.size());
338 | Assertions.assertEquals(2, reviewFileComments.get(0).getSeverity());
339 | ReviewLineComment reviewLineComment = (ReviewLineComment) reviewFileComments.get(0);
340 | Assertions.assertEquals(10, reviewLineComment.getLine().intValue());
341 | }
342 |
343 | @Test
344 | public void shouldFoundFilenameByParsingGerritFileList() throws GerritPluginException {
345 | String path1WithProject = "P1/" + PATH1;
346 | settings.setProperty(PropertyKey.GERRIT_THRESHOLD, "INFO");
347 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
348 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
349 | GerritFacade gerritFacade = mock(GerritFacade.class);
350 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
351 | List listFiles = new ArrayList<>();
352 | listFiles.add(path1WithProject);
353 | when(gerritFacade.listFiles()).thenReturn(listFiles);
354 | when(gerritFacade.parseFileName(path1WithProject)).thenReturn(PATH1);
355 |
356 | InputPath inputPath1 = createInputPath(PATH1, Boolean.TRUE);
357 |
358 | List postJobIssues = new ArrayList<>();
359 | postJobIssues.add(createPostJobIssueMock(10, inputPath1, Severity.MAJOR, "R1", "Msg 1", Boolean.TRUE));
360 | PostJobContext postJobContext = mock(PostJobContext.class);
361 | when(postJobContext.issues()).thenReturn(postJobIssues);
362 |
363 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
364 | gerritPostJob.execute(postJobContext);
365 |
366 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
367 | ReviewInput reviewInput = reviewInputCaptor.getValue();
368 | Map labels = reviewInput.getLabels();
369 | Assertions.assertEquals(1, labels.size());
370 | // Vote Value
371 | Integer integer = labels.get(LABEL);
372 | Assertions.assertNotNull(integer);
373 | Assertions.assertEquals(-2, integer.intValue());
374 | Map> inputComments = reviewInput.getComments();
375 | Assertions.assertEquals(1, inputComments.size());
376 | List reviewFileComments = inputComments.get(path1WithProject);
377 | Assertions.assertEquals(1, reviewFileComments.size());
378 | Assertions.assertEquals(2, reviewFileComments.get(0).getSeverity());
379 | ReviewLineComment reviewLineComment = (ReviewLineComment) reviewFileComments.get(0);
380 | Assertions.assertEquals(10, reviewLineComment.getLine().intValue());
381 | }
382 |
383 | @Test
384 | public void shouldIgnoreIssueWhenNotFoundFilename() throws GerritPluginException {
385 | settings.setProperty(PropertyKey.GERRIT_THRESHOLD, "INFO");
386 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
387 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
388 | GerritFacade gerritFacade = mock(GerritFacade.class);
389 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
390 | List listFiles = new ArrayList<>();
391 | listFiles.add(PATH1);
392 | when(gerritFacade.listFiles()).thenReturn(listFiles);
393 | when(gerritFacade.parseFileName(PATH1)).thenReturn(PATH1);
394 |
395 | InputPath inputPath1 = createInputPath(PATH2, Boolean.TRUE);
396 |
397 | List postJobIssues = new ArrayList<>();
398 | postJobIssues.add(createPostJobIssueMock(10, inputPath1, Severity.MAJOR, "R1", "Msg 1", Boolean.TRUE));
399 | PostJobContext postJobContext = mock(PostJobContext.class);
400 | when(postJobContext.issues()).thenReturn(postJobIssues);
401 |
402 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
403 | gerritPostJob.execute(postJobContext);
404 |
405 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
406 | ReviewInput reviewInput = reviewInputCaptor.getValue();
407 | Map labels = reviewInput.getLabels();
408 | Assertions.assertEquals(1, labels.size());
409 | Integer integer = labels.get(LABEL);
410 | Assertions.assertNotNull(integer);
411 | Assertions.assertEquals(1, integer.intValue());
412 | Assertions.assertEquals(0, reviewInput.getComments().size());
413 | }
414 |
415 | @Test
416 | public void shouldIgnoreIssueWhenFileTypeIsNotFile() throws GerritPluginException {
417 | settings.setProperty(PropertyKey.GERRIT_THRESHOLD, "INFO");
418 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
419 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
420 | GerritFacade gerritFacade = mock(GerritFacade.class);
421 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
422 | List listFiles = new ArrayList<>();
423 | listFiles.add(PATH1);
424 | when(gerritFacade.listFiles()).thenReturn(listFiles);
425 | when(gerritFacade.parseFileName(PATH1)).thenReturn(PATH1);
426 |
427 | InputPath inputPath1 = createInputPath(PATH1, Boolean.FALSE);
428 |
429 | List postJobIssues = new ArrayList<>();
430 | postJobIssues.add(createPostJobIssueMock(10, inputPath1, Severity.MAJOR, "R1", "Msg 1", Boolean.TRUE));
431 | PostJobContext postJobContext = mock(PostJobContext.class);
432 | when(postJobContext.issues()).thenReturn(postJobIssues);
433 |
434 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
435 | gerritPostJob.execute(postJobContext);
436 |
437 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
438 | ReviewInput reviewInput = reviewInputCaptor.getValue();
439 | Map labels = reviewInput.getLabels();
440 | Assertions.assertEquals(1, labels.size());
441 | Integer integer = labels.get(LABEL);
442 | Assertions.assertNotNull(integer);
443 | Assertions.assertEquals(1, integer.intValue());
444 | Assertions.assertEquals(0, reviewInput.getComments().size());
445 | }
446 |
447 | @Test
448 | public void shouldCatchThrownExceptionDuringGetListFileFromGerrit() throws GerritPluginException {
449 | settings.setProperty(PropertyKey.GERRIT_THRESHOLD, "INFO");
450 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
451 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
452 | GerritFacade gerritFacade = mock(GerritFacade.class);
453 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
454 | when(gerritFacade.listFiles()).thenThrow(new GerritPluginException("Test"));
455 |
456 | InputPath inputPath1 = createInputPath(PATH1, Boolean.TRUE);
457 |
458 | List postJobIssues = new ArrayList<>();
459 | postJobIssues.add(createPostJobIssueMock(10, inputPath1, Severity.MAJOR, "R1", "Msg 1", Boolean.TRUE));
460 | PostJobContext postJobContext = mock(PostJobContext.class);
461 | when(postJobContext.issues()).thenReturn(postJobIssues);
462 |
463 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
464 | gerritPostJob.execute(postJobContext);
465 |
466 | verify(gerritFacade).setReview(reviewInputCaptor.capture());
467 | ReviewInput reviewInput = reviewInputCaptor.getValue();
468 | Map labels = reviewInput.getLabels();
469 | Assertions.assertEquals(1, labels.size());
470 | Integer integer = labels.get(LABEL);
471 | Assertions.assertNotNull(integer);
472 | Assertions.assertEquals(1, integer.intValue());
473 | Assertions.assertEquals(0, reviewInput.getComments().size());
474 |
475 |
476 | }
477 |
478 | @Test
479 | public void shouldCatchThrownExceptionDuringExecute() throws GerritPluginException {
480 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
481 |
482 | GerritFacadeFactory gerritFacadeFactory = mock(GerritFacadeFactory.class);
483 | GerritFacade gerritFacade = mock(GerritFacade.class);
484 | when(gerritFacadeFactory.getFacade()).thenReturn(gerritFacade);
485 | doThrow(new GerritPluginException("Test")).when(gerritFacade).setReview(any(ReviewInput.class));
486 | PostJobContext postJobContext = mock(PostJobContext.class);
487 | when(postJobContext.issues()).thenReturn(new ArrayList<>());
488 |
489 | GerritPostJob gerritPostJob = new GerritPostJob(settings, gerritConfiguration, gerritFacadeFactory);
490 | gerritPostJob.execute(postJobContext);
491 | }
492 |
493 | private PostJobIssue createPostJobIssueMock(int line, InputPath inputPath, Severity severity, String rule, String msg, Boolean isNew) {
494 | PostJobIssue postJobIssue = mock(PostJobIssue.class);
495 | when(postJobIssue.line()).thenReturn(line);
496 | when(postJobIssue.inputComponent()).thenReturn(inputPath);
497 | when(postJobIssue.severity()).thenReturn(severity);
498 | when(postJobIssue.ruleKey()).thenReturn(RuleKey.of("SQ-repo", rule));
499 | when(postJobIssue.message()).thenReturn(msg);
500 | when(postJobIssue.isNew()).thenReturn(isNew);
501 | return postJobIssue;
502 | }
503 |
504 | private InputPath createInputPath(String path, Boolean isFile) {
505 | File file = mock(File.class);
506 | when(file.isFile()).thenReturn(isFile);
507 | InputPath inputPath = mock(InputPath.class);
508 | when(inputPath.file()).thenReturn(file);
509 | when(inputPath.relativePath()).thenReturn(path);
510 | return inputPath;
511 | }
512 | }
513 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/GerritFacadeTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit;
2 |
3 | import fr.techad.sonar.GerritPluginException;
4 | import fr.techad.sonar.gerrit.review.ReviewFileComment;
5 | import fr.techad.sonar.gerrit.review.ReviewInput;
6 | import fr.techad.sonar.gerrit.review.ReviewLineComment;
7 | import fr.techad.sonar.mockito.MockitoExtension;
8 | import org.junit.jupiter.api.Assertions;
9 | import org.junit.jupiter.api.Test;
10 | import org.junit.jupiter.api.extension.ExtendWith;
11 | import org.junit.platform.runner.JUnitPlatform;
12 | import org.junit.runner.RunWith;
13 | import org.mockito.ArgumentCaptor;
14 | import org.mockito.Captor;
15 | import org.mockito.Mock;
16 | import org.mockito.Mockito;
17 |
18 | import java.io.IOException;
19 | import java.util.ArrayList;
20 | import java.util.HashMap;
21 | import java.util.List;
22 | import java.util.Map;
23 |
24 | import static org.hamcrest.CoreMatchers.is;
25 | import static org.junit.Assert.assertThat;
26 | import static org.mockito.Mockito.when;
27 |
28 | @ExtendWith(MockitoExtension.class)
29 | @RunWith(JUnitPlatform.class)
30 | public class GerritFacadeTest {
31 |
32 | @Mock
33 | GerritConnector gerritConnectorMock;
34 |
35 | @Captor
36 | ArgumentCaptor argCaptor;
37 |
38 | @Test
39 | public void shouldCallOnceFillListFilesFromGerrit() throws GerritPluginException {
40 | GerritFacade gerritFacade = new GerritFacadeUT(gerritConnectorMock);
41 | // First call
42 | List listFiles = gerritFacade.listFiles();
43 | Assertions.assertEquals(1, listFiles.size());
44 | Assertions.assertEquals("FileNameClass.java", listFiles.get(0));
45 | // Second call: should be ignored -> cnt=1
46 | gerritFacade.listFiles();
47 | Assertions.assertEquals(1, ((GerritFacadeUT) gerritFacade).getCnt());
48 | }
49 |
50 | @Test
51 | public void shouldSetReview() throws GerritPluginException, IOException {
52 | ReviewInput reviewInputMock = Mockito.mock(ReviewInput.class);
53 | // Mock the message
54 | when(reviewInputMock.getMessage()).thenReturn("Message Test");
55 | // Mock labels
56 | Map labels = new HashMap<>();
57 | labels.put("Quality Control", 1);
58 | labels.put("Verify", 2);
59 | when(reviewInputMock.getLabels()).thenReturn(labels);
60 | // Mock comments
61 | ReviewLineComment reviewLineCommentMock = Mockito.mock(ReviewLineComment.class);
62 | when(reviewLineCommentMock.getLine()).thenReturn(1, 4, 6, 8).thenReturn(null);
63 | when(reviewLineCommentMock.getMessage()).thenReturn("Msg1", "Msg2", "Msg3", "Msg4").thenReturn(null);
64 | List reviewFileComments = new ArrayList<>();
65 | reviewFileComments.add(reviewLineCommentMock);
66 | reviewFileComments.add(reviewLineCommentMock);
67 | Map> comments = new HashMap<>();
68 | comments.put("FileTest1.java", reviewFileComments);
69 | comments.put("FileTest2.java", reviewFileComments);
70 | when(reviewInputMock.getComments()).thenReturn(comments);
71 |
72 | GerritFacade gerritFacade = new GerritFacadeUT(gerritConnectorMock);
73 | gerritFacade.setReview(reviewInputMock);
74 |
75 | Mockito.verify(gerritConnectorMock).setReview(argCaptor.capture());
76 | String captorValue = argCaptor.getValue();
77 | Assertions.assertEquals("{\"message\":\"Message Test\",\"labels\":{\"Verify\":2,\"Quality Control\":1},\"comments\":{\"FileTest2.java\":[{\"line\":1,\"message\":\"Msg1\"},{\"line\":4,\"message\":\"Msg2\"}],\"FileTest1.java\":[{\"line\":6,\"message\":\"Msg3\"},{\"line\":8,\"message\":\"Msg4\"}]}}", captorValue);
78 | }
79 |
80 | @Test
81 | public void shouldSetReviewOnlyMessage() throws GerritPluginException, IOException {
82 | ReviewInput reviewInputMock = Mockito.mock(ReviewInput.class);
83 | when(reviewInputMock.getMessage()).thenReturn("Message Test");
84 |
85 | GerritFacade gerritFacade = new GerritFacadeUT(gerritConnectorMock);
86 | gerritFacade.setReview(reviewInputMock);
87 |
88 | Mockito.verify(gerritConnectorMock).setReview(argCaptor.capture());
89 | String captorValue = argCaptor.getValue();
90 | Assertions.assertEquals("{\"message\":\"Message Test\"}", captorValue);
91 | }
92 |
93 | @Test
94 | public void shouldSetReviewWithNullMessage() throws GerritPluginException, IOException {
95 |
96 | ReviewInput reviewInputMock = Mockito.mock(ReviewInput.class);
97 | when(reviewInputMock.getMessage()).thenReturn(null);
98 |
99 | GerritFacade gerritFacade = new GerritFacadeUT(gerritConnectorMock);
100 | gerritFacade.setReview(reviewInputMock);
101 |
102 | Mockito.verify(gerritConnectorMock).setReview(argCaptor.capture());
103 | String captorValue = argCaptor.getValue();
104 | Assertions.assertEquals("{\"message\":null}", captorValue);
105 | }
106 |
107 | @Test
108 | public void shouldThrowException() {
109 | Assertions.assertThrows(GerritPluginException.class, () -> {
110 | ReviewInput reviewInputMock = Mockito.mock(ReviewInput.class);
111 | GerritFacade gerritFacade = new GerritFacadeUT(gerritConnectorMock);
112 | when(gerritConnectorMock.setReview(Mockito.any())).thenThrow(new IOException("Test"));
113 | gerritFacade.setReview(reviewInputMock);
114 | });
115 | }
116 |
117 | @Test
118 | public void shouldGetGerritConnector() {
119 | GerritFacade gerritFacade = new GerritFacadeUT(gerritConnectorMock);
120 | Assertions.assertEquals(gerritConnectorMock, gerritFacade.getGerritConnector());
121 | }
122 |
123 | @Test
124 | public void testParseFileName() {
125 | GerritFacade facade = Mockito.mock(GerritFacade.class, Mockito.CALLS_REAL_METHODS);
126 | assertThat(facade.parseFileName("subdirectory/src/fr/techad/sonar/gerrit/GerritFacadeTest.java"),
127 | is("src/fr/techad/sonar/gerrit/GerritFacadeTest.java"));
128 | assertThat(facade.parseFileName("fr/techad/sonar/gerrit/GerritFacadeTest.java"),
129 | is("fr/techad/sonar/gerrit/GerritFacadeTest.java"));
130 | assertThat(facade.parseFileName("sub1/sub2/sub3/sub4/src/fr/techad/sonar/gerrit/GerritFacadeTest.java"),
131 | is("src/fr/techad/sonar/gerrit/GerritFacadeTest.java"));
132 | assertThat(facade.parseFileName("subdirectory/src/main/java/src/fr/techad/sonar/gerrit/GerritFacadeTest.java"),
133 | is("src/main/java/src/fr/techad/sonar/gerrit/GerritFacadeTest.java"));
134 | assertThat(facade.parseFileName("src/main/java/src/fr/techad/sonar/gerrit/GerritFacadeTest.java"),
135 | is("src/main/java/src/fr/techad/sonar/gerrit/GerritFacadeTest.java"));
136 | assertThat(facade.parseFileName("/src/main/java/src/fr/techad/sonar/gerrit/GerritFacadeTest.java"),
137 | is("src/main/java/src/fr/techad/sonar/gerrit/GerritFacadeTest.java"));
138 | }
139 |
140 | class GerritFacadeUT extends GerritFacade {
141 | private int cnt = 0;
142 |
143 | public GerritFacadeUT(GerritConnector gerritConnector) {
144 | super(gerritConnector);
145 | }
146 |
147 | @Override
148 | protected void fillListFilesFromGerrit() throws GerritPluginException {
149 | addFile("FileNameClass.java");
150 | addFile("/COMMIT_MSG"); // Should be ignored because it's the commit message "key"
151 | cnt++;
152 | }
153 |
154 | public int getCnt() {
155 | return cnt;
156 | }
157 | }
158 |
159 | }
160 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/factory/GerritConnectorFactoryTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.factory;
2 |
3 | import fr.techad.sonar.GerritConfiguration;
4 | import fr.techad.sonar.GerritConstants;
5 | import fr.techad.sonar.gerrit.GerritConnector;
6 | import fr.techad.sonar.gerrit.network.rest.GerritRestConnector;
7 | import fr.techad.sonar.gerrit.network.ssh.GerritSshConnector;
8 | import fr.techad.sonar.mockito.MockitoExtension;
9 | import org.junit.jupiter.api.Test;
10 | import org.junit.jupiter.api.extension.ExtendWith;
11 | import org.junit.platform.runner.JUnitPlatform;
12 | import org.junit.runner.RunWith;
13 | import org.mockito.Mock;
14 | import org.mockito.Mockito;
15 |
16 | import static org.junit.Assert.assertEquals;
17 |
18 | @ExtendWith(MockitoExtension.class)
19 | @RunWith(JUnitPlatform.class)
20 | public class GerritConnectorFactoryTest {
21 | @Mock
22 | private GerritConfiguration gerritConfiguration;
23 |
24 | @Test
25 | public void shouldGetConnectorAsGerritRestConnectorWhenSchemeIsHttp() throws Exception {
26 | Mockito.when(gerritConfiguration.getScheme()).thenReturn(GerritConstants.SCHEME_HTTP);
27 | GerritConnectorFactory gerritConnectorFactory = new GerritConnectorFactory(gerritConfiguration);
28 | GerritConnector gerritConnector = gerritConnectorFactory.getConnector();
29 | assertEquals(true, gerritConnector instanceof GerritRestConnector);
30 | }
31 |
32 | @Test
33 | public void shouldGetConnectorAsGerritRestConnectorWhenSchemeIsHttps() throws Exception {
34 | Mockito.when(gerritConfiguration.getScheme()).thenReturn(GerritConstants.SCHEME_HTTPS);
35 | GerritConnectorFactory gerritConnectorFactory = new GerritConnectorFactory(gerritConfiguration);
36 | GerritConnector gerritConnector = gerritConnectorFactory.getConnector();
37 | assertEquals(true, gerritConnector instanceof GerritRestConnector);
38 | }
39 |
40 | @Test
41 | public void shouldGetConnectorAsGerritSshConnectorWhenSchemeIsSSh() throws Exception {
42 | Mockito.when(gerritConfiguration.getScheme()).thenReturn(GerritConstants.SCHEME_SSH);
43 | Mockito.when(gerritConfiguration.getUsername()).thenReturn("user");
44 | Mockito.when(gerritConfiguration.getHost()).thenReturn("localhost");
45 | Mockito.when(gerritConfiguration.getPort()).thenReturn(22);
46 | GerritConnectorFactory gerritConnectorFactory = new GerritConnectorFactory(gerritConfiguration);
47 | GerritConnector gerritConnector = gerritConnectorFactory.getConnector();
48 | assertEquals(true, gerritConnector instanceof GerritSshConnector);
49 | }
50 |
51 | @Test
52 | public void shouldGetConnectorAsNullWhenSchemeIsUnknown() throws Exception {
53 | Mockito.when(gerritConfiguration.getScheme()).thenReturn("UnknownProtocol");
54 | GerritConnectorFactory gerritConnectorFactory = new GerritConnectorFactory(gerritConfiguration);
55 | GerritConnector gerritConnector = gerritConnectorFactory.getConnector();
56 | assertEquals(null, gerritConnector);
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/factory/GerritFacadeFactoryTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.factory;
2 |
3 | import fr.techad.sonar.GerritConfiguration;
4 | import fr.techad.sonar.GerritConstants;
5 | import fr.techad.sonar.gerrit.GerritFacade;
6 | import fr.techad.sonar.gerrit.network.rest.GerritRestFacade;
7 | import fr.techad.sonar.gerrit.network.ssh.GerritSshFacade;
8 | import fr.techad.sonar.mockito.MockitoExtension;
9 | import org.junit.jupiter.api.Test;
10 | import org.junit.jupiter.api.extension.ExtendWith;
11 | import org.junit.platform.runner.JUnitPlatform;
12 | import org.junit.runner.RunWith;
13 | import org.mockito.Mock;
14 | import org.mockito.Mockito;
15 |
16 | import static org.junit.Assert.assertEquals;
17 |
18 | @ExtendWith(MockitoExtension.class)
19 | @RunWith(JUnitPlatform.class)
20 | public class GerritFacadeFactoryTest {
21 | @Mock
22 | private GerritConfiguration gerritConfiguration;
23 |
24 | @Test
25 | public void shouldGetFacadeAsGerritRestFaceWhenConnectorIsGerritRestConnector() throws Exception {
26 | Mockito.when(gerritConfiguration.getScheme()).thenReturn(GerritConstants.SCHEME_HTTP);
27 | GerritConnectorFactory gerritConnectorFactory = new GerritConnectorFactory(gerritConfiguration);
28 | GerritFacadeFactory gerritFacadeFactory = new GerritFacadeFactory(gerritConnectorFactory);
29 | GerritFacade gerritFacade = gerritFacadeFactory.getFacade();
30 | assertEquals(true, gerritFacade instanceof GerritRestFacade);
31 | }
32 |
33 | @Test
34 | public void shouldGetFacadeAsGerritSshFacadeWhenConnectorIsGerritSshConnector() throws Exception {
35 | Mockito.when(gerritConfiguration.getScheme()).thenReturn(GerritConstants.SCHEME_SSH);
36 | Mockito.when(gerritConfiguration.getUsername()).thenReturn("user");
37 | Mockito.when(gerritConfiguration.getHost()).thenReturn("localhost");
38 | Mockito.when(gerritConfiguration.getPort()).thenReturn(22);
39 | GerritConnectorFactory gerritConnectorFactory = new GerritConnectorFactory(gerritConfiguration);
40 | GerritFacadeFactory gerritFacadeFactory = new GerritFacadeFactory(gerritConnectorFactory);
41 | GerritFacade gerritFacade = gerritFacadeFactory.getFacade();
42 | assertEquals(true, gerritFacade instanceof GerritSshFacade);
43 | }
44 |
45 | @Test
46 | public void shouldGetFacadeAsNullWhenConnectorIsUnknown() throws Exception {
47 | Mockito.when(gerritConfiguration.getScheme()).thenReturn("UnknownProtocol");
48 | GerritConnectorFactory gerritConnectorFactory = new GerritConnectorFactory(gerritConfiguration);
49 | GerritFacadeFactory gerritFacadeFactory = new GerritFacadeFactory(gerritConnectorFactory);
50 | GerritFacade gerritFacade = gerritFacadeFactory.getFacade();
51 | assertEquals(null, gerritFacade);
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/network/rest/GerritRestConnectorTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.network.rest;
2 |
3 | import fr.techad.sonar.GerritConfiguration;
4 | import fr.techad.sonar.GerritConstants;
5 | import fr.techad.sonar.GerritPluginException;
6 | import fr.techad.sonar.PropertyKey;
7 | import fr.techad.sonar.gerrit.GerritConnector;
8 | import fr.techad.sonar.gerrit.factory.GerritConnectorFactory;
9 | import fr.techad.sonar.mockito.MockitoExtension;
10 | import org.apache.commons.lang3.StringUtils;
11 | import org.junit.jupiter.api.AfterAll;
12 | import org.junit.jupiter.api.Assertions;
13 | import org.junit.jupiter.api.BeforeAll;
14 | import org.junit.jupiter.api.BeforeEach;
15 | import org.junit.jupiter.api.Test;
16 | import org.junit.jupiter.api.TestInstance;
17 | import org.junit.jupiter.api.extension.ExtendWith;
18 | import org.junit.platform.runner.JUnitPlatform;
19 | import org.junit.runner.RunWith;
20 | import org.mockserver.integration.ClientAndServer;
21 | import org.mockserver.model.Header;
22 | import org.sonar.api.config.internal.MapSettings;
23 |
24 | import java.io.IOException;
25 | import java.util.concurrent.TimeUnit;
26 |
27 | import static org.junit.jupiter.api.Assertions.assertEquals;
28 | import static org.mockserver.integration.ClientAndServer.startClientAndServer;
29 | import static org.mockserver.model.HttpRequest.request;
30 | import static org.mockserver.model.HttpResponse.response;
31 |
32 | @ExtendWith(MockitoExtension.class)
33 | @RunWith(JUnitPlatform.class)
34 | @TestInstance(TestInstance.Lifecycle.PER_CLASS)
35 | public class GerritRestConnectorTest {
36 | static private String listFiles = ")]}'" +
37 | " {" +
38 | " \"/COMMIT_MSG\": {" +
39 | " \"status\": \"A\"," +
40 | " \"lines_inserted\": 7," +
41 | " \"size_delta\": 551," +
42 | " \"size\": 551" +
43 | " }," +
44 | " \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java\": {" +
45 | " \"status\": \"D\"," +
46 | " \"lines_inserted\": 5," +
47 | " \"lines_deleted\": 3," +
48 | " \"size_delta\": 98," +
49 | " \"size\": 23348" +
50 | " }" +
51 | " }";
52 | private MapSettings settings;
53 | private ClientAndServer mockServer;
54 |
55 | @BeforeAll
56 | public void startServer() {
57 | mockServer = startClientAndServer(10800);
58 | }
59 |
60 | @BeforeEach
61 | public void setUp() {
62 | // Common Settings
63 | settings = new MapSettings();
64 | settings.setProperty(PropertyKey.GERRIT_SCHEME, GerritConstants.SCHEME_HTTP)
65 | .setProperty(PropertyKey.GERRIT_HOST, "localhost")
66 | .appendProperty(PropertyKey.GERRIT_PORT, "10800")
67 | .setProperty(PropertyKey.GERRIT_PROJECT, "project")
68 | .setProperty(PropertyKey.GERRIT_CHANGE_ID, "changeid")
69 | .setProperty(PropertyKey.GERRIT_REVISION_ID, "revisionid")
70 | .setProperty(PropertyKey.GERRIT_LABEL, "Code-Review");
71 | }
72 |
73 | @AfterAll
74 | public void stopServer() {
75 | mockServer.stop();
76 | }
77 |
78 | @Test
79 | public void shouldAggregateBasicParamsWhenAuthenticated() throws GerritPluginException {
80 | // given
81 | settings.setProperty(PropertyKey.GERRIT_USERNAME, "sonar")
82 | .setProperty(PropertyKey.GERRIT_PASSWORD, "sonar")
83 | .appendProperty(PropertyKey.GERRIT_BASE_PATH, "")
84 | .setProperty(PropertyKey.GERRIT_BRANCH, "branch");
85 |
86 | // when
87 | GerritRestConnector gerritRestConnector = getRestConnector();
88 |
89 | // then
90 | assertEquals("/a/changes/project~branch~changeid/revisions/revisionid",
91 | gerritRestConnector.rootUriBuilder());
92 | }
93 |
94 | @Test
95 | public void shouldEncodeBranchWithSlash() throws GerritPluginException {
96 | // given
97 | settings.setProperty(PropertyKey.GERRIT_USERNAME, "sonar")
98 | .setProperty(PropertyKey.GERRIT_PASSWORD, "sonar")
99 | .appendProperty(PropertyKey.GERRIT_BASE_PATH, "")
100 | .setProperty(PropertyKey.GERRIT_BRANCH, "branch/subbranch");
101 |
102 | // when
103 | GerritRestConnector gerritRestConnector = getRestConnector();
104 |
105 | // then
106 | assertEquals("/a/changes/project~branch%2Fsubbranch~changeid/revisions/revisionid",
107 | gerritRestConnector.rootUriBuilder());
108 | }
109 |
110 | @Test
111 | public void shouldPrependCustomBasePath() throws GerritPluginException {
112 | // given
113 | settings.setProperty(PropertyKey.GERRIT_USERNAME, "sonar")
114 | .setProperty(PropertyKey.GERRIT_PASSWORD, "sonar")
115 | .appendProperty(PropertyKey.GERRIT_BASE_PATH, "/r")
116 | .setProperty(PropertyKey.GERRIT_BRANCH, "branch/subbranch");
117 |
118 | // when
119 | GerritRestConnector gerritRestConnector = getRestConnector();
120 |
121 | // then
122 | assertEquals("/r/a/changes/project~branch%2Fsubbranch~changeid/revisions/revisionid",
123 | gerritRestConnector.rootUriBuilder());
124 | }
125 |
126 | @Test
127 | public void shouldAggregateBasicParamsWhenAnonymous() throws GerritPluginException {
128 | // given
129 | settings.setProperty(PropertyKey.GERRIT_USERNAME, "").appendProperty(PropertyKey.GERRIT_PASSWORD, "")
130 | .setProperty(PropertyKey.GERRIT_BASE_PATH, "")
131 | .setProperty(PropertyKey.GERRIT_BRANCH, "branch/subbranch");
132 | // when
133 | GerritRestConnector gerritRestConnector = getRestConnector();
134 |
135 | // then
136 | assertEquals("/changes/project~branch%2Fsubbranch~changeid/revisions/revisionid",
137 | gerritRestConnector.rootUriBuilder());
138 | }
139 |
140 | @Test
141 | public void shouldPrependCustomBasePathWhenAnonymous() throws GerritPluginException {
142 | // given
143 | settings.setProperty(PropertyKey.GERRIT_USERNAME, "").appendProperty(PropertyKey.GERRIT_PASSWORD, "")
144 | .setProperty(PropertyKey.GERRIT_BASE_PATH, "/r")
145 | .setProperty(PropertyKey.GERRIT_BRANCH, "branch/subbranch");
146 | // when
147 | GerritRestConnector gerritRestConnector = getRestConnector();
148 |
149 | // then
150 | assertEquals("/r/changes/project~branch%2Fsubbranch~changeid/revisions/revisionid",
151 | gerritRestConnector.rootUriBuilder());
152 | }
153 |
154 | @Test
155 | public void shouldSetReview() throws IOException {
156 | mockServer.when(
157 | request()
158 | .withPath("/a/changes/project~branch~changeid/revisions/revisionid/review")
159 | .withMethod("POST"))
160 | .respond(
161 | response()
162 | .withStatusCode(200)
163 | .withHeaders(
164 | new Header("Content-Type", "application/json; charset=utf-8"),
165 | new Header("Cache-Control", "public, max-age=86400"))
166 | .withBody("{ message: 'Review committed' }")
167 | .withDelay(TimeUnit.SECONDS, 1)
168 | );
169 | settings.setProperty(PropertyKey.GERRIT_USERNAME, "sonar")
170 | .setProperty(PropertyKey.GERRIT_PASSWORD, "sonar")
171 | .appendProperty(PropertyKey.GERRIT_BASE_PATH, "")
172 | .setProperty(PropertyKey.GERRIT_BRANCH, "branch");
173 |
174 | String response = getRestConnector().setReview("review");
175 | Assertions.assertEquals("{ message: 'Review committed' }", response);
176 | }
177 |
178 | @Test
179 | public void shouldSetReviewWithNullResponseBody() throws IOException {
180 | mockServer.when(
181 | request()
182 | .withPath("/a/changes/project~branch~changeid2/revisions/revisionid/review")
183 | .withMethod("POST"))
184 | .respond(
185 | response()
186 | .withStatusCode(200)
187 | .withHeaders(
188 | new Header("Content-Type", "application/json; charset=utf-8"),
189 | new Header("Cache-Control", "public, max-age=86400"))
190 | .withDelay(TimeUnit.SECONDS, 1)
191 | );
192 | settings.setProperty(PropertyKey.GERRIT_USERNAME, "sonar")
193 | .setProperty(PropertyKey.GERRIT_PASSWORD, "sonar")
194 | .appendProperty(PropertyKey.GERRIT_BASE_PATH, "")
195 | .setProperty(PropertyKey.GERRIT_BRANCH, "branch")
196 | .setProperty(PropertyKey.GERRIT_CHANGE_ID, "changeid2")
197 | ;
198 |
199 | String response = getRestConnector().setReview("review");
200 | Assertions.assertEquals(StringUtils.EMPTY, response);
201 | }
202 |
203 | @Test
204 | public void shouldSetReviewAsAnonymous() throws IOException {
205 | mockServer.when(
206 | request()
207 | .withPath("/changes/project~branch~changeid/revisions/revisionid/review")
208 | .withMethod("POST"))
209 | .respond(
210 | response()
211 | .withStatusCode(200)
212 | .withHeaders(
213 | new Header("Content-Type", "application/json; charset=utf-8"),
214 | new Header("Cache-Control", "public, max-age=86400"))
215 | .withBody("{ message: 'Review committed' }")
216 | .withDelay(TimeUnit.SECONDS, 1)
217 | );
218 | settings.setProperty(PropertyKey.GERRIT_BRANCH, "branch");
219 |
220 | String response = getRestConnector().setReview("review");
221 | Assertions.assertEquals("{ message: 'Review committed' }", response);
222 | }
223 |
224 | @Test
225 | public void shouldListFiles() throws IOException {
226 | mockServer.when(
227 | request()
228 | .withPath("/a/changes/project~branch~changeid/revisions/revisionid/files/")
229 | .withMethod("GET"))
230 | .respond(
231 | response()
232 | .withStatusCode(200)
233 | .withHeaders(
234 | new Header("Content-Type", "application/json; charset=utf-8"),
235 | new Header("Cache-Control", "public, max-age=86400"))
236 | .withBody(listFiles)
237 | .withDelay(TimeUnit.SECONDS, 1)
238 | );
239 | settings.setProperty(PropertyKey.GERRIT_USERNAME, "sonar")
240 | .setProperty(PropertyKey.GERRIT_PASSWORD, "sonar")
241 | .appendProperty(PropertyKey.GERRIT_BASE_PATH, "")
242 | .setProperty(PropertyKey.GERRIT_BRANCH, "branch");
243 |
244 | String response = getRestConnector().listFiles();
245 | Assertions.assertEquals(listFiles, response);
246 | }
247 |
248 | private GerritRestConnector getRestConnector() {
249 | GerritConfiguration gerritConfiguration = new GerritConfiguration(settings);
250 | GerritConnector gerritConnector = new GerritConnectorFactory(gerritConfiguration).getConnector();
251 | return (GerritRestConnector) gerritConnector;
252 | }
253 | }
254 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/network/rest/GerritRestFacadeTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.network.rest;
2 |
3 | import fr.techad.sonar.GerritPluginException;
4 | import fr.techad.sonar.gerrit.GerritConnector;
5 | import fr.techad.sonar.mockito.MockitoExtension;
6 | import org.junit.jupiter.api.Assertions;
7 | import org.junit.jupiter.api.DisplayName;
8 | import org.junit.jupiter.api.Test;
9 | import org.junit.jupiter.api.extension.ExtendWith;
10 | import org.junit.platform.runner.JUnitPlatform;
11 | import org.junit.runner.RunWith;
12 | import org.mockito.Mock;
13 |
14 | import java.io.IOException;
15 | import java.util.List;
16 |
17 | import static org.mockito.Mockito.when;
18 |
19 | /**
20 | * TECH ADVANTAGE
21 | * All right reserved
22 | * Created by cochon on 21/07/2018.
23 | */
24 | @ExtendWith(MockitoExtension.class)
25 | @RunWith(JUnitPlatform.class)
26 | public class GerritRestFacadeTest {
27 |
28 | @Mock
29 | private GerritConnector gerritConnector;
30 |
31 | @Test
32 | @DisplayName("Should return a list files")
33 | public void shouldGetListFiles() throws IOException, GerritPluginException {
34 | String response = ")]}'\n" +
35 | " {\n" +
36 | " \"/COMMIT_MSG\": {\n" +
37 | " \"status\": \"A\",\n" +
38 | " \"lines_inserted\": 7,\n" +
39 | " \"size_delta\": 551,\n" +
40 | " \"size\": 551\n" +
41 | " },\n" +
42 | " \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java\": {\n" +
43 | " \"lines_inserted\": 5,\n" +
44 | " \"lines_deleted\": 3,\n" +
45 | " \"size_delta\": 98,\n" +
46 | " \"size\": 23348\n" +
47 | " },\n" +
48 | " \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl2.java\": {\n" +
49 | " \"lines_inserted\": 5,\n" +
50 | " \"lines_deleted\": 3,\n" +
51 | " \"size_delta\": 98,\n" +
52 | " \"size\": 23348\n" +
53 | " }\n" +
54 | " }";
55 | when(gerritConnector.listFiles()).thenReturn(response);
56 |
57 | GerritRestFacade gerritRestFacade = new GerritRestFacade(gerritConnector);
58 | // Will call fillListFilesFromGerrit
59 | List listFiles = gerritRestFacade.listFiles();
60 | Assertions.assertEquals(2, listFiles.size());
61 | Assertions.assertEquals("gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java", listFiles.get(0));
62 | Assertions.assertEquals("gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl2.java", listFiles.get(1));
63 | }
64 |
65 | @Test
66 | @DisplayName("Should return a list files with a defined status")
67 | public void shouldGetListFilesWithStatus() throws IOException, GerritPluginException {
68 | String response = ")]}'\n" +
69 | " {\n" +
70 | " \"/COMMIT_MSG\": {\n" +
71 | " \"status\": \"A\",\n" +
72 | " \"lines_inserted\": 7,\n" +
73 | " \"size_delta\": 551,\n" +
74 | " \"size\": 551\n" +
75 | " },\n" +
76 | " \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java\": {\n" +
77 | " \"status\": \"A\",\n" +
78 | " \"lines_inserted\": 5,\n" +
79 | " \"lines_deleted\": 3,\n" +
80 | " \"size_delta\": 98,\n" +
81 | " \"size\": 23348\n" +
82 | " }\n" +
83 | " }";
84 | when(gerritConnector.listFiles()).thenReturn(response);
85 |
86 | GerritRestFacade gerritRestFacade = new GerritRestFacade(gerritConnector);
87 | // Will call fillListFilesFromGerrit
88 | List listFiles = gerritRestFacade.listFiles();
89 | Assertions.assertEquals(1, listFiles.size());
90 | Assertions.assertEquals("gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java", listFiles.get(0));
91 |
92 | }
93 |
94 | @Test
95 | @DisplayName("Should ignored deleted file")
96 | public void shouldIgnoredDeletedFiles() throws IOException, GerritPluginException {
97 | String response = ")]}'\n" +
98 | " {\n" +
99 | " \"/COMMIT_MSG\": {\n" +
100 | " \"status\": \"A\",\n" +
101 | " \"lines_inserted\": 7,\n" +
102 | " \"size_delta\": 551,\n" +
103 | " \"size\": 551\n" +
104 | " },\n" +
105 | " \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java\": {\n" +
106 | " \"status\": \"D\",\n" +
107 | " \"lines_inserted\": 5,\n" +
108 | " \"lines_deleted\": 3,\n" +
109 | " \"size_delta\": 98,\n" +
110 | " \"size\": 23348\n" +
111 | " }\n" +
112 | " }";
113 | when(gerritConnector.listFiles()).thenReturn(response);
114 |
115 | GerritRestFacade gerritRestFacade = new GerritRestFacade(gerritConnector);
116 | // Will call fillListFilesFromGerrit
117 | List listFiles = gerritRestFacade.listFiles();
118 | Assertions.assertEquals(0, listFiles.size());
119 |
120 | }
121 |
122 | @Test
123 | @DisplayName("Should return an empty list if the review doesn't contain file")
124 | public void shouldReturnEmptyListIfReviewIsEmpty() throws IOException, GerritPluginException {
125 | String response = ")]}'\n" +
126 | " {\n" +
127 | " \"/COMMIT_MSG\": {\n" +
128 | " \"status\": \"A\",\n" +
129 | " \"lines_inserted\": 7,\n" +
130 | " \"size_delta\": 551,\n" +
131 | " \"size\": 551\n" +
132 | " }\n" +
133 | " }";
134 | when(gerritConnector.listFiles()).thenReturn(response);
135 |
136 | GerritRestFacade gerritRestFacade = new GerritRestFacade(gerritConnector);
137 | // Will call fillListFilesFromGerrit
138 | List listFiles = gerritRestFacade.listFiles();
139 | Assertions.assertEquals(0, listFiles.size());
140 |
141 | }
142 |
143 | @Test
144 | @DisplayName("Should throw an exception on IOException")
145 | public void shouldThrowAnGerritException() {
146 | Assertions.assertThrows(GerritPluginException.class, () -> {
147 | when(gerritConnector.listFiles()).thenThrow(new IOException("Error during Test"));
148 |
149 | GerritRestFacade gerritRestFacade = new GerritRestFacade(gerritConnector);
150 | // Will call fillListFilesFromGerrit
151 | gerritRestFacade.listFiles();
152 | });
153 | }
154 | }
155 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/network/ssh/GerritSshFacadeTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.network.ssh;
2 |
3 | import fr.techad.sonar.GerritPluginException;
4 | import fr.techad.sonar.gerrit.GerritConnector;
5 | import fr.techad.sonar.mockito.MockitoExtension;
6 | import org.junit.jupiter.api.Assertions;
7 | import org.junit.jupiter.api.DisplayName;
8 | import org.junit.jupiter.api.Test;
9 | import org.junit.jupiter.api.extension.ExtendWith;
10 | import org.junit.platform.runner.JUnitPlatform;
11 | import org.junit.runner.RunWith;
12 | import org.mockito.Mock;
13 |
14 | import java.io.IOException;
15 | import java.util.List;
16 |
17 | import static org.mockito.Mockito.when;
18 |
19 | /**
20 | * TECH ADVANTAGE
21 | * All right reserved
22 | * Created by cochon on 21/07/2018.
23 | */
24 | @ExtendWith(MockitoExtension.class)
25 | @RunWith(JUnitPlatform.class)
26 | public class GerritSshFacadeTest {
27 | private static String COMMON_RESPONSE = "\"project\":\"sonar-gerrit-plugin\",\"branch\":\"feature/test\",\"id\":\"I218fb47cc01cbca2809dc82d54de019a60\",\"number\":11782,\"subject\":\"To develop\",";
28 | @Mock
29 | private GerritConnector gerritConnector;
30 |
31 | @Test
32 | @DisplayName("Should return a list files")
33 | public void shouldGetListFiles() throws IOException, GerritPluginException {
34 | String response = "{" + COMMON_RESPONSE +
35 | " \"currentPatchSet\": {" +
36 | "\"files\": [" +
37 | "{ " +
38 | "\"file\": \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java\"," +
39 | "\"type\": \"ADDED\"" +
40 | "}," +
41 | "{ " +
42 | "\"file\": \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl2.java\"," +
43 | "\"type\": \"ADDED\"" +
44 | "}" +
45 | "]" +
46 | "}" +
47 | "}";
48 | when(gerritConnector.listFiles()).thenReturn(response);
49 |
50 | GerritSshFacade gerritSshFacade = new GerritSshFacade(gerritConnector);
51 | // Will call fillListFilesFromGerrit
52 | List listFiles = gerritSshFacade.listFiles();
53 | Assertions.assertEquals(2, listFiles.size());
54 | Assertions.assertEquals("gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java", listFiles.get(0));
55 | Assertions.assertEquals("gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl2.java", listFiles.get(1));
56 | }
57 |
58 | @Test
59 | @DisplayName("Should return a list files with a defined type")
60 | public void shouldGetListFilesWithType() throws IOException, GerritPluginException {
61 | String response = "{" + COMMON_RESPONSE +
62 | " \"currentPatchSet\": {" +
63 | "\"files\": [" +
64 | "{ " +
65 | "\"file\": \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java\"," +
66 | "\"type\": \"ADDED\"" +
67 | "}" +
68 | "]" +
69 | "}" +
70 | "}";
71 | when(gerritConnector.listFiles()).thenReturn(response);
72 |
73 | GerritSshFacade gerritSshFacade = new GerritSshFacade(gerritConnector);
74 | // Will call fillListFilesFromGerrit
75 | List listFiles = gerritSshFacade.listFiles();
76 | Assertions.assertEquals(1, listFiles.size());
77 | Assertions.assertEquals("gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java", listFiles.get(0));
78 | }
79 |
80 | @Test
81 | @DisplayName("Should ignored deleted file")
82 | public void shouldIgnoredDeletedFiles() throws IOException, GerritPluginException {
83 | String response = "{" + COMMON_RESPONSE +
84 | " \"currentPatchSet\": {" +
85 | "\"files\": [" +
86 | "{ " +
87 | "\"file\": \"gerrit-server/src/main/java/com/google/gerrit/server/project/RefControl.java\"," +
88 | "\"type\": \"DELETED\"" +
89 | "}" +
90 | "]" +
91 | "}" +
92 | "}";
93 | when(gerritConnector.listFiles()).thenReturn(response);
94 |
95 | GerritSshFacade gerritSshFacade = new GerritSshFacade(gerritConnector);
96 | // Will call fillListFilesFromGerrit
97 | List listFiles = gerritSshFacade.listFiles();
98 | Assertions.assertEquals(0, listFiles.size());
99 | }
100 |
101 | @Test
102 | @DisplayName("Should return an empty list if the review doesn't contain file")
103 | public void shouldReturnEmptyListIfReviewIsEmpty() throws IOException, GerritPluginException {
104 | String response = "{" + COMMON_RESPONSE +
105 | " \"currentPatchSet\": {" +
106 | "\"files\": [" +
107 | "]" +
108 | "}" +
109 | "}";
110 |
111 | when(gerritConnector.listFiles()).thenReturn(response);
112 |
113 | GerritSshFacade gerritSshFacade = new GerritSshFacade(gerritConnector);
114 | // Will call fillListFilesFromGerrit
115 | List listFiles = gerritSshFacade.listFiles();
116 | Assertions.assertEquals(0, listFiles.size());
117 | }
118 |
119 | @Test
120 | @DisplayName("Should throw an exception on IOException")
121 | public void shouldThrowAnGerritException() {
122 | Assertions.assertThrows(GerritPluginException.class, () -> {
123 | when(gerritConnector.listFiles()).thenThrow(new IOException("Error during Test"));
124 |
125 | GerritSshFacade gerritRestFacade = new GerritSshFacade(gerritConnector);
126 | // Will call fillListFilesFromGerrit
127 | gerritRestFacade.listFiles();
128 | });
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/review/ReviewFileCommentTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.review;
2 |
3 | import org.apache.commons.lang3.StringUtils;
4 | import org.junit.jupiter.api.Assertions;
5 | import org.junit.jupiter.api.Test;
6 | import org.sonar.api.batch.rule.Severity;
7 |
8 | public class ReviewFileCommentTest {
9 |
10 | static private final String MESSAGE = "Gerrit Message";
11 |
12 | @Test
13 | public void testMessage() {
14 | ReviewFileComment reviewFileComment = new ReviewFileComment();
15 | reviewFileComment.setMessage(MESSAGE);
16 | Assertions.assertEquals(MESSAGE, reviewFileComment.getMessage());
17 | }
18 |
19 | @Test
20 | public void testNullMessage() {
21 | ReviewFileComment reviewFileComment = new ReviewFileComment();
22 | reviewFileComment.setMessage(null);
23 | Assertions.assertEquals(StringUtils.EMPTY, reviewFileComment.getMessage());
24 | }
25 |
26 | @Test
27 | public void testSeverity() {
28 | ReviewFileComment reviewFileComment = new ReviewFileComment();
29 | reviewFileComment.setMessage(MESSAGE);
30 | reviewFileComment.setSeverity(Severity.BLOCKER.ordinal());
31 | Assertions.assertEquals(Severity.BLOCKER.ordinal(), reviewFileComment.getSeverity());
32 | }
33 |
34 | @Test
35 | public void testToString() {
36 | ReviewFileComment reviewFileComment = new ReviewFileComment();
37 | reviewFileComment.setMessage(MESSAGE);
38 | reviewFileComment.setSeverity(Severity.BLOCKER.ordinal());
39 | Assertions.assertEquals(
40 | "ReviewFileComment [message:" + MESSAGE + ", severity: " + Severity.BLOCKER.ordinal() + "]",
41 | reviewFileComment.toString());
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/review/ReviewInputTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.review;
2 |
3 | import org.junit.jupiter.api.Test;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.Map;
8 |
9 | import static org.fest.assertions.Assertions.assertThat;
10 | import static org.junit.jupiter.api.Assertions.assertEquals;
11 |
12 | public class ReviewInputTest {
13 | private static final String DEFAULT_MESSAGE = "Looks good to me.";
14 | private static final String NEW_MESSAGE = "A new message";
15 |
16 | private static final String PLUS_ONE_LABEL = "+1 Label";
17 | private static final String MINUS_ONE_LABEL = "-1 Label";
18 | private static final String OTHER_LABEL = "other Label";
19 |
20 | private static final Integer PLUS_ONE = new Integer(1);
21 | private static final Integer MINUS_ONE = new Integer(-1);
22 | private static final Integer OTHER_VALUE = new Integer(42);
23 |
24 | private static final String KEY_COMMENT1 = "Key1";
25 | private static final String KEY_COMMENT2 = "Key2";
26 |
27 | @Test
28 | public void shouldHaveAMessage() {
29 | ReviewInput reviewInput = new ReviewInput();
30 | assertEquals(DEFAULT_MESSAGE, reviewInput.getMessage());
31 | reviewInput.setMessage(NEW_MESSAGE);
32 | assertEquals(NEW_MESSAGE, reviewInput.getMessage());
33 | }
34 |
35 | @Test
36 | public void shouldHaveAPlusOneLabel() {
37 | ReviewInput reviewInput = new ReviewInput();
38 | reviewInput.setLabelToPlusOne(PLUS_ONE_LABEL);
39 | Map labels = reviewInput.getLabels();
40 | assertEquals(1, labels.size());
41 | assertEquals(PLUS_ONE, labels.get(PLUS_ONE_LABEL));
42 |
43 | }
44 |
45 | @Test
46 | public void shouldHaveAMinusOneLabel() {
47 | ReviewInput reviewInput = new ReviewInput();
48 | reviewInput.setLabelToMinusOne(MINUS_ONE_LABEL);
49 | Map labels = reviewInput.getLabels();
50 | assertEquals(1, labels.size());
51 | assertEquals(MINUS_ONE, labels.get(MINUS_ONE_LABEL));
52 |
53 | }
54 |
55 | @Test
56 | public void shouldHaveAOtherLabel() {
57 | ReviewInput reviewInput = new ReviewInput();
58 | reviewInput.setValueAndLabel(OTHER_VALUE, OTHER_LABEL);
59 | Map labels = reviewInput.getLabels();
60 | assertEquals(1, labels.size());
61 | assertEquals(OTHER_VALUE, labels.get(OTHER_LABEL));
62 | }
63 |
64 | @Test
65 | public void shouldHaveSeveralLabel() {
66 | ReviewInput reviewInput = new ReviewInput();
67 | reviewInput.setLabelToPlusOne(PLUS_ONE_LABEL);
68 | reviewInput.setValueAndLabel(OTHER_VALUE, OTHER_LABEL);
69 | reviewInput.setLabelToMinusOne(MINUS_ONE_LABEL);
70 | Map labels = reviewInput.getLabels();
71 | assertEquals(3, labels.size());
72 | assertEquals(OTHER_VALUE, labels.get(OTHER_LABEL));
73 | assertEquals(PLUS_ONE, labels.get(PLUS_ONE_LABEL));
74 | assertEquals(MINUS_ONE, labels.get(MINUS_ONE_LABEL));
75 | }
76 |
77 | @Test
78 | public void shouldEmptyTheComments() {
79 | ReviewInput reviewInput = new ReviewInput();
80 | List list = new ArrayList<>();
81 | list.add(new ReviewFileComment());
82 | reviewInput.addComments(KEY_COMMENT1, list);
83 | assertEquals(1, reviewInput.size());
84 | reviewInput.emptyComments();
85 | assertEquals(0, reviewInput.size());
86 | }
87 |
88 | @Test
89 | public void shouldHaveTheComments() {
90 | ReviewInput reviewInput = new ReviewInput();
91 | List list1 = new ArrayList<>();
92 | list1.add(new ReviewFileComment());
93 | list1.add(new ReviewFileComment());
94 | reviewInput.addComments(KEY_COMMENT1, list1);
95 | List list2 = new ArrayList<>();
96 | list2.add(new ReviewFileComment());
97 | reviewInput.addComments(KEY_COMMENT2, list2);
98 | assertEquals(2, reviewInput.size());
99 | Map> comments = reviewInput.getComments();
100 |
101 | assertEquals(2, comments.get(KEY_COMMENT1).size());
102 | assertEquals(1, comments.get(KEY_COMMENT2).size());
103 | }
104 |
105 | @Test
106 | public void shouldHaveTheUnmodifiedComments() {
107 | ReviewInput reviewInput = new ReviewInput();
108 | List list1 = new ArrayList<>();
109 | list1.add(new ReviewFileComment());
110 | list1.add(new ReviewFileComment());
111 | reviewInput.addComments(KEY_COMMENT1, list1);
112 | list1.add(new ReviewFileComment());
113 | assertEquals(1, reviewInput.size());
114 | Map> comments = reviewInput.getComments();
115 |
116 | assertEquals(2, comments.get(KEY_COMMENT1).size());
117 | }
118 |
119 | @Test
120 | public void shouldHaveAToString() {
121 | ReviewInput reviewInput = new ReviewInput();
122 | reviewInput.setLabelToPlusOne(PLUS_ONE_LABEL);
123 | reviewInput.setMessage(NEW_MESSAGE);
124 | List list1 = new ArrayList<>();
125 | list1.add(new ReviewFileComment());
126 | assertEquals("ReviewInput [message=A new message, labels={+1 Label=1}, comments={}]", reviewInput.toString());
127 | }
128 |
129 | @Test
130 | public void shoulsHaveAEmptyReview() {
131 | ReviewInput reviewInput = new ReviewInput();
132 | reviewInput.emptyComments();
133 | assertThat(reviewInput.isEmpty()).isTrue();
134 | }
135 |
136 | @Test
137 | public void shouldHaveANoEmptyReview() {
138 | ReviewInput reviewInput = new ReviewInput();
139 | List list = new ArrayList<>();
140 | list.add(new ReviewFileComment());
141 | reviewInput.addComments(KEY_COMMENT1, list);
142 | assertThat(reviewInput.isEmpty()).isFalse();
143 | }
144 |
145 | }
146 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/review/ReviewLineCommentTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.review;
2 |
3 | import fr.techad.sonar.GerritPluginException;
4 | import fr.techad.sonar.mockito.MockitoExtension;
5 | import org.junit.jupiter.api.BeforeEach;
6 | import org.junit.jupiter.api.Test;
7 | import org.junit.jupiter.api.extension.ExtendWith;
8 | import org.junit.platform.runner.JUnitPlatform;
9 | import org.junit.runner.RunWith;
10 |
11 | import static org.fest.assertions.Assertions.assertThat;
12 |
13 | @ExtendWith(MockitoExtension.class)
14 | @RunWith(JUnitPlatform.class)
15 | public class ReviewLineCommentTest {
16 | static final private String MESSAGE = "Gerrit Message";
17 | static final private Integer DEFAULT_LINE = new Integer(0);
18 | static final private Integer LINE = 42;
19 | static final private Integer NEGATIVE_LINE = -42;
20 | private ReviewLineComment reviewLineComment;
21 |
22 | @BeforeEach
23 | public void setUp() {
24 | reviewLineComment = new ReviewLineComment();
25 | }
26 |
27 | @Test
28 | public void shouldHandleLine() throws GerritPluginException {
29 | // given
30 | // when
31 | reviewLineComment.setLine(LINE);
32 | reviewLineComment.setMessage(MESSAGE);
33 | // then
34 | assertThat(reviewLineComment.getLine()).isEqualTo(42);
35 | assertThat(reviewLineComment.toString())
36 | .isEqualTo("ReviewLineComment [line=" + LINE + ", message=" + MESSAGE + "]");
37 | }
38 |
39 | @Test
40 | public void shouldHandleNullLine() throws GerritPluginException {
41 | // given
42 | // when
43 | reviewLineComment.setLine(null);
44 | // then
45 | assertThat(reviewLineComment.getLine()).isEqualTo(DEFAULT_LINE);
46 | }
47 |
48 | @Test
49 | public void shouldHandleNegativeLine() throws GerritPluginException {
50 | // given
51 | // when
52 | reviewLineComment.setLine(NEGATIVE_LINE);
53 | // then
54 | assertThat(reviewLineComment.getLine()).isEqualTo(DEFAULT_LINE);
55 | }
56 |
57 | @Test
58 | public void shouldHandleZeroLine() throws GerritPluginException {
59 | // given
60 | // when
61 | reviewLineComment.setLine(DEFAULT_LINE);
62 | // then
63 | assertThat(reviewLineComment.getLine()).isEqualTo(DEFAULT_LINE);
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/gerrit/utils/ReviewUtilsTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.gerrit.utils;
2 |
3 | import fr.techad.sonar.gerrit.review.ReviewFileComment;
4 | import fr.techad.sonar.gerrit.review.ReviewInput;
5 | import fr.techad.sonar.gerrit.review.ReviewLineComment;
6 | import org.junit.jupiter.api.BeforeEach;
7 | import org.junit.jupiter.api.DisplayName;
8 | import org.junit.jupiter.api.Test;
9 | import org.sonar.api.batch.rule.Severity;
10 |
11 | import java.util.ArrayList;
12 | import java.util.List;
13 |
14 | import static org.fest.assertions.Assertions.assertThat;
15 |
16 | public class ReviewUtilsTest {
17 |
18 | private ReviewInput reviewInput;
19 | private ReviewLineComment rlcInfo;
20 | private ReviewLineComment rlcCritical;
21 | private List reviewList;
22 |
23 | @BeforeEach
24 | public void setUp() {
25 | reviewInput = new ReviewInput();
26 |
27 | reviewInput.setMessage("Not the default message.");
28 |
29 | rlcInfo = new ReviewLineComment();
30 | rlcInfo.setLine(12);
31 | rlcInfo.setMessage("INFORMATION tldr");
32 | rlcInfo.setSeverity(Severity.INFO.ordinal());
33 |
34 | rlcCritical = new ReviewLineComment();
35 | rlcCritical.setLine(34);
36 | rlcCritical.setMessage("CRITI tldr");
37 | rlcCritical.setSeverity(Severity.CRITICAL.ordinal());
38 |
39 | reviewList = new ArrayList(2);
40 | reviewList.add(rlcInfo);
41 | reviewList.add(rlcCritical);
42 | }
43 |
44 | @Test
45 | @DisplayName("Validate Threshold To Value")
46 | public void validateThresholdToValue() {
47 | // given
48 | // when
49 | // then
50 | assertThat(ReviewUtils.thresholdToValue("INFO")).isEqualTo(Severity.INFO.ordinal());
51 | assertThat(ReviewUtils.thresholdToValue("MINOR")).isEqualTo(Severity.MINOR.ordinal());
52 | assertThat(ReviewUtils.thresholdToValue("MAJOR")).isEqualTo(Severity.MAJOR.ordinal());
53 | assertThat(ReviewUtils.thresholdToValue("CRITICAL")).isEqualTo(Severity.CRITICAL.ordinal());
54 | assertThat(ReviewUtils.thresholdToValue("BLOCKER")).isEqualTo(Severity.BLOCKER.ordinal());
55 | assertThat(ReviewUtils.thresholdToValue("NOOP")).isEqualTo(ReviewUtils.UNKNOWN_VALUE);
56 | }
57 |
58 | @Test
59 | public void validateValueToThreshold() {
60 | // given
61 | // when
62 | // then
63 | assertThat(ReviewUtils.valueToThreshold(Severity.INFO.ordinal())).isEqualTo("INFO");
64 | assertThat(ReviewUtils.valueToThreshold(Severity.MINOR.ordinal())).isEqualTo("MINOR");
65 | assertThat(ReviewUtils.valueToThreshold(Severity.MAJOR.ordinal())).isEqualTo("MAJOR");
66 | assertThat(ReviewUtils.valueToThreshold(Severity.CRITICAL.ordinal())).isEqualTo("CRITICAL");
67 | assertThat(ReviewUtils.valueToThreshold(Severity.BLOCKER.ordinal())).isEqualTo("BLOCKER");
68 | assertThat(ReviewUtils.valueToThreshold(42)).isEqualTo(ReviewUtils.UNKNOWN);
69 | assertThat(ReviewUtils.valueToThreshold(-1)).isEqualTo(ReviewUtils.UNKNOWN);
70 | }
71 |
72 | @Test
73 | public void detectInfoLevel() {
74 | // given
75 | reviewInput.emptyComments();
76 | reviewList.clear();
77 | reviewList.add(rlcInfo);
78 | // when
79 | reviewInput.addComments("TLDR", reviewList);
80 | // then
81 | assertThat(reviewInput.maxLevelSeverity()).isEqualTo(ReviewUtils.thresholdToValue("INFO"));
82 | }
83 |
84 | @Test
85 | public void detectCriticalLevel() {
86 | // given
87 | // when
88 | reviewInput.addComments("TLDR", reviewList);
89 | // then
90 | assertThat(reviewInput.maxLevelSeverity()).isEqualTo(ReviewUtils.thresholdToValue("CRITICAL"));
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/mockito/MockitoExtension.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.mockito;
2 |
3 | import org.junit.jupiter.api.extension.ExtensionContext;
4 | import org.junit.jupiter.api.extension.ParameterContext;
5 | import org.junit.jupiter.api.extension.ParameterResolver;
6 | import org.junit.jupiter.api.extension.TestInstancePostProcessor;
7 | import org.mockito.Mock;
8 | import org.mockito.MockitoAnnotations;
9 |
10 | import java.lang.reflect.Parameter;
11 |
12 | import static org.mockito.Mockito.mock;
13 |
14 | /**
15 | * TECH ADVANTAGE
16 | * All right reserved
17 | * Created by cochon on 21/07/2018.
18 | */
19 | public class MockitoExtension
20 | implements TestInstancePostProcessor, ParameterResolver {
21 |
22 | @Override
23 | public void postProcessTestInstance(Object testInstance,
24 | ExtensionContext context) {
25 | MockitoAnnotations.initMocks(testInstance);
26 | }
27 |
28 | @Override
29 | public boolean supportsParameter(ParameterContext parameterContext,
30 | ExtensionContext extensionContext) {
31 | return
32 | parameterContext.getParameter().isAnnotationPresent(Mock.class);
33 | }
34 |
35 | @Override
36 | public Object resolveParameter(ParameterContext parameterContext,
37 | ExtensionContext extensionContext) {
38 | return getMock(parameterContext.getParameter(), extensionContext);
39 | }
40 |
41 | private Object getMock(
42 | Parameter parameter, ExtensionContext extensionContext) {
43 |
44 | Class> mockType = parameter.getType();
45 | ExtensionContext.Store mocks = extensionContext.getStore(ExtensionContext.Namespace.create(
46 | MockitoExtension.class, mockType));
47 | String mockName = getMockName(parameter);
48 |
49 | if (mockName != null) {
50 | return mocks.getOrComputeIfAbsent(
51 | mockName, key -> mock(mockType, mockName));
52 | }
53 | else {
54 | return mocks.getOrComputeIfAbsent(
55 | mockType.getCanonicalName(), key -> mock(mockType));
56 | }
57 | }
58 |
59 | private String getMockName(Parameter parameter) {
60 | String explicitMockName = parameter.getAnnotation(Mock.class)
61 | .name().trim();
62 | if (!explicitMockName.isEmpty()) {
63 | return explicitMockName;
64 | }
65 | else if (parameter.isNamePresent()) {
66 | return parameter.getName();
67 | }
68 | return null;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/src/test/java/fr/techad/sonar/utils/MessageUtilsTest.java:
--------------------------------------------------------------------------------
1 | package fr.techad.sonar.utils;
2 |
3 | import fr.techad.sonar.PropertyKey;
4 | import org.junit.jupiter.api.Test;
5 | import org.sonar.api.batch.postjob.issue.PostJobIssue;
6 | import org.sonar.api.batch.rule.Severity;
7 | import org.sonar.api.config.internal.MapSettings;
8 | import org.sonar.api.rule.RuleKey;
9 |
10 | import static org.fest.assertions.Assertions.assertThat;
11 | import static org.mockito.Mockito.mock;
12 | import static org.mockito.Mockito.when;
13 |
14 | public class MessageUtilsTest {
15 |
16 | @Test
17 | public void validateSubstitution() {
18 | // given
19 | MapSettings settings = new MapSettings();
20 | settings.setProperty(PropertyKey.GERRIT_MESSAGE, "Sonar review at ${sonar.host.url}")
21 | .setProperty("sonar.host.url", "http://sq.example.com/");
22 | // then
23 | assertThat(MessageUtils.createMessage(settings.getString(PropertyKey.GERRIT_MESSAGE), settings))
24 | .isEqualTo("Sonar review at http://sq.example.com/");
25 | }
26 |
27 | @Test
28 | public void validateIssueSubstitution() {
29 | // given
30 | PostJobIssue issue = mock(PostJobIssue.class);
31 | when(issue.isNew()).thenReturn(true);
32 | when(issue.ruleKey()).thenReturn(RuleKey.of("squid", "XX12"));
33 | when(issue.message()).thenReturn("You have a problem there");
34 | when(issue.severity()).thenReturn(Severity.BLOCKER);
35 | // when
36 | MapSettings settings = new MapSettings();
37 | settings.setProperty(PropertyKey.GERRIT_ISSUE_COMMENT,
38 | "[${issue.isNew}] New: ${issue.ruleKey} on ${sonar.host.url} Severity: ${issue.severity}, Message: ${issue.message}")
39 | .setProperty("sonar.host.url", "http://sq.example.com/");
40 | // then
41 | assertThat(MessageUtils.createIssueMessage(settings.getString(PropertyKey.GERRIT_ISSUE_COMMENT), settings,
42 | issue)).isEqualTo(
43 | "[true] New: squid:XX12 on http://sq.example.com/ Severity: BLOCKER, Message: You have a problem there");
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------