@Test public void shouldCountViolationsAfterDate() { List<Violation> violations = createViolations(); assertThat(decorator.countViolations(null, fiveDaysAgo), is(0)); assertThat(decorator.countViolations(violations, fiveDaysAgo), is(1)); // 1 rightNow assertThat( decorator.countViolations(violations, tenDaysAgo), is(3)); // 1 rightNow + 2 fiveDaysAgo }
@Test public void shouldExecuteIfLastAnalysis() { Project project = mock(Project.class); when(project.isLatestAnalysis()).thenReturn(false); assertThat(decorator.shouldExecuteOnProject(project), is(false)); when(project.isLatestAnalysis()).thenReturn(true); assertThat(decorator.shouldExecuteOnProject(project), is(true)); }
@Test public void shouldNotNotifyIfNoNewViolations() throws Exception { Project project = new Project("key"); Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS); when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m); // NULL is returned here decorator.notifyNewViolations(project, context); verify(notificationManager, never()).scheduleForSending(any(Notification.class)); // 0 will be returned now m.setVariation1(0.0); decorator.notifyNewViolations(project, context); verify(notificationManager, never()).scheduleForSending(any(Notification.class)); }
@Test public void shouldNotNotifyIfNotRootProject() throws Exception { Project project = mock(Project.class); when(project.getQualifier()).thenReturn(Qualifiers.MODULE); decorator.decorate(project, context); 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.getProjectPastSnapshots()) .thenReturn(new ArrayList<PastSnapshot>()); decorator.notifyNewViolations(project, context); verify(notificationManager, never()).scheduleForSending(any(Notification.class)); }
@Test public void shouldNotNotifyUserIfFirstAnalysis() throws Exception { Project project = new Project("key").setName("LongName"); project.setId(45); // 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); when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m); decorator.decorate(project, context); verify(notificationManager, never()).scheduleForSending(any(Notification.class)); }
@Test public void ruleViolations() { when(context.getViolations()).thenReturn(createViolations()); decorator.decorate(resource, context); // remember : period1 is 5daysAgo, period2 is 10daysAgo verify(context) .saveMeasure( argThat( new IsVariationRuleMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, rule1, 1.0, 1.0))); verify(context) .saveMeasure( argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_MAJOR_VIOLATIONS, rule2, 0.0, 1.0))); verify(context) .saveMeasure( argThat(new IsVariationRuleMeasure(CoreMetrics.NEW_MINOR_VIOLATIONS, rule3, 0.0, 1.0))); }
@Test public void shouldClearCacheAfterExecution() { Violation violation1 = Violation.create(rule1, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow); Violation violation2 = Violation.create(rule2, resource).setSeverity(RulePriority.CRITICAL).setCreatedAt(rightNow); when(context.getViolations()) .thenReturn(Arrays.asList(violation1)) .thenReturn(Arrays.asList(violation2)); decorator.decorate(resource, context); decorator.decorate(resource, context); verify(context, times(2)) .saveMeasure( argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 1.0, 1.0))); verify(context, never()) .saveMeasure( argThat(new IsVariationMeasure(CoreMetrics.NEW_CRITICAL_VIOLATIONS, 2.0, 2.0))); }
@Test public void shouldNotifyUserAboutNewViolations() throws Exception { Project project = new Project("key").setName("LongName"); project.setId(45); Calendar pastDate = new GregorianCalendar(2011, 10, 25); PastSnapshot pastSnapshot = new PastSnapshot("", pastDate.getTime()); when(timeMachineConfiguration.getProjectPastSnapshots()) .thenReturn(Lists.newArrayList(pastSnapshot, pastSnapshot)); Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(32.0); when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m); decorator.decorate(project, context); DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); Notification notification = new Notification("new-violations") .setDefaultMessage("32 new violations on LongName.") .setFieldValue("count", "32") .setFieldValue("projectName", "LongName") .setFieldValue("projectKey", "key") .setFieldValue("projectId", "45") .setFieldValue("fromDate", dateformat.format(pastDate.getTime())); verify(notificationManager, times(1)).scheduleForSending(eq(notification)); }
@Test public void shouldBeDependedUponMetric() { assertThat(decorator.generatesMetric().size(), is(6)); }