From 49bd8b18dd124c5a5de70249d949d989bcd0f6e6 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Wed, 2 Jul 2014 09:39:03 +0200 Subject: [PATCH] Fix quality flaw --- .../server/qualitygate/QualityGates.java | 6 +++++- .../server/qualitygate/QualityGatesTest.java | 21 ++++++++++++++++++- 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 listConditions(long qGateId) { Collection 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 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; -- 2.39.5