]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaw
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 2 Jul 2014 07:39:03 +0000 (09:39 +0200)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 2 Jul 2014 07:39:08 +0000 (09:39 +0200)
sonar-server/src/main/java/org/sonar/server/qualitygate/QualityGates.java
sonar-server/src/test/java/org/sonar/server/qualitygate/QualityGatesTest.java

index 9d1fcc9872b7f56c0fa9ea95895267d2c7f0d9da..e627be18a6c9d450a2c9b8fedfa9582860d58695 100644 (file)
@@ -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;
   }
index 03995927056eb5f758c83fc2101297b5b4f0c9f0..bc0f6d85dc0566602e2e3c3d8fdf4e19a75ae4a9 100644 (file)
@@ -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;