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;
}
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 {
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;