diff options
2 files changed, 12 insertions, 17 deletions
diff --git a/plugins/sonar-reviews-plugin/src/main/java/org/sonar/plugins/reviews/jira/JiraLinkReviewAction.java b/plugins/sonar-reviews-plugin/src/main/java/org/sonar/plugins/reviews/jira/JiraLinkReviewAction.java index 5a5495e9b93..625d4d3405d 100644 --- a/plugins/sonar-reviews-plugin/src/main/java/org/sonar/plugins/reviews/jira/JiraLinkReviewAction.java +++ b/plugins/sonar-reviews-plugin/src/main/java/org/sonar/plugins/reviews/jira/JiraLinkReviewAction.java @@ -20,6 +20,7 @@ package org.sonar.plugins.reviews.jira; import com.atlassian.jira.rpc.soap.client.RemoteIssue; +import com.google.common.base.Preconditions; import com.google.common.collect.Lists; import org.apache.commons.lang.StringUtils; import org.sonar.api.database.model.User; @@ -83,24 +84,18 @@ public class JiraLinkReviewAction implements LinkReviewAction { private ReviewDto getReviewId(Map<String, String> reviewContext) { String reviewId = reviewContext.get(REVIEW_ID_PARAM); - ReviewDto review; - try { - review = reviewDao.findById(Long.parseLong(reviewId)); - } catch (NumberFormatException e) { - throw new IllegalArgumentException("The given review with id is not a valid number: " + (reviewId == null ? "NULL" : reviewId)); - } - if (review == null) { - throw new IllegalArgumentException("The review with id '" + reviewId + "' does not exist."); - } + Preconditions.checkState(StringUtils.isNotBlank(reviewId), "The review id is missing."); + Preconditions.checkState(StringUtils.isNumeric(reviewId), "The given review with id is not a valid number: " + reviewId); + ReviewDto review = reviewDao.findById(Long.parseLong(reviewId)); + Preconditions.checkNotNull(review, "The review with id '" + reviewId + "' does not exist."); return review; } private User getUser(Map<String, String> reviewContext) { String userLogin = reviewContext.get(USER_LOGIN_PARAM); + Preconditions.checkState(StringUtils.isNotBlank(userLogin), "The user login is missing."); User user = userFinder.findByLogin(userLogin); - if (user == null) { - throw new IllegalArgumentException("The user with login '" + userLogin + "' does not exist."); - } + Preconditions.checkNotNull(user, "The user with login '" + userLogin + "' does not exist."); return user; } diff --git a/plugins/sonar-reviews-plugin/src/test/java/org/sonar/plugins/reviews/jira/JiraLinkReviewActionTest.java b/plugins/sonar-reviews-plugin/src/test/java/org/sonar/plugins/reviews/jira/JiraLinkReviewActionTest.java index 04e3d1c1643..92486045cb2 100644 --- a/plugins/sonar-reviews-plugin/src/test/java/org/sonar/plugins/reviews/jira/JiraLinkReviewActionTest.java +++ b/plugins/sonar-reviews-plugin/src/test/java/org/sonar/plugins/reviews/jira/JiraLinkReviewActionTest.java @@ -119,8 +119,8 @@ public class JiraLinkReviewActionTest { public void shouldFailIfReviewIdNotProvided() throws Exception { Map<String, String> reviewContext = Maps.newHashMap(); - thrown.expect(IllegalArgumentException.class); - thrown.expectMessage("The given review with id is not a valid number: NULL"); + thrown.expect(IllegalStateException.class); + thrown.expectMessage("The review id is missing."); action.execute(reviewContext); } @@ -129,7 +129,7 @@ public class JiraLinkReviewActionTest { Map<String, String> reviewContext = Maps.newHashMap(); reviewContext.put(JiraLinkReviewAction.REVIEW_ID_PARAM, "toto"); - thrown.expect(IllegalArgumentException.class); + thrown.expect(IllegalStateException.class); thrown.expectMessage("The given review with id is not a valid number: toto"); action.execute(reviewContext); } @@ -139,7 +139,7 @@ public class JiraLinkReviewActionTest { Map<String, String> reviewContext = Maps.newHashMap(); reviewContext.put(JiraLinkReviewAction.REVIEW_ID_PARAM, "100"); - thrown.expect(IllegalArgumentException.class); + thrown.expect(NullPointerException.class); thrown.expectMessage("The review with id '100' does not exist."); action.execute(reviewContext); } @@ -150,7 +150,7 @@ public class JiraLinkReviewActionTest { reviewContext.put(JiraLinkReviewAction.REVIEW_ID_PARAM, "45"); reviewContext.put(JiraLinkReviewAction.USER_LOGIN_PARAM, "invisible_man"); - thrown.expect(IllegalArgumentException.class); + thrown.expect(NullPointerException.class); thrown.expectMessage("The user with login 'invisible_man' does not exist."); action.execute(reviewContext); } |