protected void notifyNewViolations(Project project, DecoratorContext context) {
Integer lastAnalysisPeriodIndex = timeMachineConfiguration.getLastAnalysisPeriodIndex();
- if (lastAnalysisPeriodIndex != null) {
- PastSnapshot pastSnapshot = timeMachineConfiguration.getProjectPastSnapshots().get(lastAnalysisPeriodIndex - 1);
+ List<PastSnapshot> projectPastSnapshots = timeMachineConfiguration.getProjectPastSnapshots();
+ if (lastAnalysisPeriodIndex != null && projectPastSnapshots.size() >= lastAnalysisPeriodIndex) {
+ PastSnapshot pastSnapshot = projectPastSnapshots.get(lastAnalysisPeriodIndex - 1);
Double newViolationsCount = context.getMeasure(CoreMetrics.NEW_VIOLATIONS).getVariation(lastAnalysisPeriodIndex);
// Do not send notification if this is the first analysis or if there's no violation
if (pastSnapshot.getTargetDate() != null && newViolationsCount != null && newViolationsCount > 0) {
verify(notificationManager, never()).scheduleForSending(any(Notification.class));
}
+ @Test
+ public void shouldNotNotifyIfNoNotEnoughPastSnapshots() throws Exception {
+ Project project = new Project("key");
+ // the #setUp method adds 2 snapshots: if last period analysis is 3, then it's not enough
+ when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(3);
+
+ decorator.notifyNewViolations(project, context);
+ verify(notificationManager, never()).scheduleForSending(any(Notification.class));
+ }
+
@Test
public void shouldNotNotifyIfNoNewViolations() throws Exception {
Project project = new Project("key");
Project project = new Project("key").setName("LongName");
project.setId(45);
when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(1);
- // PastSnaptho with targetDate==null means first analysis
+ // PastSnapshot with targetDate==null means first analysis
PastSnapshot pastSnapshot = new PastSnapshot("", null);
when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Lists.newArrayList(pastSnapshot));
Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(0.0);