aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-07-02 09:39:03 +0200
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>2014-07-02 09:39:08 +0200
commit49bd8b18dd124c5a5de70249d949d989bcd0f6e6 (patch)
treefed1c9b0bfeabbf20a1f65ce89165a28fdbb7f81
parente1c887c19b3bb85448b4b693bd572bfe6ea64dd7 (diff)
downloadsonarqube-49bd8b18dd124c5a5de70249d949d989bcd0f6e6.tar.gz
sonarqube-49bd8b18dd124c5a5de70249d949d989bcd0f6e6.zip
Fix quality flaw
-rw-r--r--sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java6
-rw-r--r--sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java21
2 files changed, 25 insertions, 2 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java b/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
index 9d1fcc9872b..e627be18a6c 100644
--- a/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
+++ b/sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
@@ -192,7 +192,11 @@ public class QualityGates {
public Collection<QualityGateConditionDto> listConditions(long qGateId) {
Collection<QualityGateConditionDto> conditionsForGate = conditionDao.selectForQualityGate(qGateId);
for (QualityGateConditionDto condition : conditionsForGate) {
- condition.setMetricKey(metricFinder.findById((int) condition.getMetricId()).getKey());
+ Metric metric = metricFinder.findById((int) condition.getMetricId());
+ if (metric == null) {
+ throw new IllegalStateException("Could not find metric with id " + condition.getMetricId());
+ }
+ condition.setMetricKey(metric.getKey());
}
return conditionsForGate;
}
diff --git a/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java b/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java
index 03995927056..bc0f6d85dc0 100644
--- a/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java
@@ -60,7 +60,10 @@ import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class QualityGatesTest {
@@ -470,6 +473,22 @@ public class QualityGatesTest {
assertThat(iterator.next().getMetricKey()).isEqualTo(metric2Key);
}
+ @Test(expected = IllegalStateException.class)
+ public void should_do_a_sanity_check_when_listing_conditions() throws Exception {
+ long qGateId = 42L;
+ long metric1Id = 1L;
+ String metric1Key = "polop";
+ long metric2Id = 2L;
+ QualityGateConditionDto cond1 = new QualityGateConditionDto().setMetricId(metric1Id);
+ QualityGateConditionDto cond2 = new QualityGateConditionDto().setMetricId(metric2Id);
+ Collection<QualityGateConditionDto> conditions = ImmutableList.of(cond1, cond2);
+ when(conditionDao.selectForQualityGate(qGateId)).thenReturn(conditions );
+ Metric metric1 = mock(Metric.class);
+ when(metric1.getKey()).thenReturn(metric1Key);
+ when(metricFinder.findById((int) metric1Id)).thenReturn(metric1);
+ qGates.listConditions(qGateId);
+ }
+
@Test
public void should_delete_condition() throws Exception {
long idToDelete = 42L;