@Before public void setUp() { rightNow = new Date(); tenDaysAgo = DateUtils.addDays(rightNow, -10); fiveDaysAgo = DateUtils.addDays(rightNow, -5); PastSnapshot pastSnapshot = mock(PastSnapshot.class); when(pastSnapshot.getIndex()).thenReturn(1); when(pastSnapshot.getTargetDate()).thenReturn(fiveDaysAgo); PastSnapshot pastSnapshot2 = mock(PastSnapshot.class); when(pastSnapshot2.getIndex()).thenReturn(2); when(pastSnapshot2.getTargetDate()).thenReturn(tenDaysAgo); timeMachineConfiguration = mock(TimeMachineConfiguration.class); when(timeMachineConfiguration.getProjectPastSnapshots()) .thenReturn(Arrays.asList(pastSnapshot, pastSnapshot2)); context = mock(DecoratorContext.class); resource = new File("com/foo/bar"); when(context.getResource()).thenReturn(resource); notificationManager = mock(NotificationManager.class); decorator = new NewViolationsDecorator(timeMachineConfiguration, notificationManager); rule1 = Rule.create().setRepositoryKey("rule1").setKey("rule1").setName("name1"); rule2 = Rule.create().setRepositoryKey("rule2").setKey("rule2").setName("name2"); rule3 = Rule.create().setRepositoryKey("rule3").setKey("rule3").setName("name3"); }
@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)); }
public VariationDecorator( PastMeasuresLoader pastMeasuresLoader, MetricFinder metricFinder, TimeMachineConfiguration configuration) { this( pastMeasuresLoader, metricFinder, configuration.getProjectPastSnapshots(), configuration.isFileVariationEnabled()); }
void persistConfiguration() { List<PastSnapshot> pastSnapshots = configuration.getProjectPastSnapshots(); for (PastSnapshot pastSnapshot : pastSnapshots) { projectSnapshot = session.reattach(Snapshot.class, projectSnapshot.getId()); projectSnapshot.setPeriodMode(pastSnapshot.getIndex(), pastSnapshot.getMode()); projectSnapshot.setPeriodModeParameter( pastSnapshot.getIndex(), pastSnapshot.getModeParameter()); projectSnapshot.setPeriodDate(pastSnapshot.getIndex(), pastSnapshot.getTargetDate()); session.save(projectSnapshot); } session.commit(); }
@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 shouldInitPastSnapshots() throws ParseException { PropertiesConfiguration conf = new PropertiesConfiguration(); PastSnapshotFinder pastSnapshotFinder = mock(PastSnapshotFinder.class); when(pastSnapshotFinder.find(null, conf, 1)) .thenReturn(new PastSnapshot("days", null, newSnapshot("2010-10-15"))); when(pastSnapshotFinder.find(null, conf, 3)) .thenReturn(new PastSnapshot("days", null, newSnapshot("2010-10-13"))); TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(conf, pastSnapshotFinder, null); verify(pastSnapshotFinder).find(null, conf, 1); verify(pastSnapshotFinder).find(null, conf, 2); verify(pastSnapshotFinder).find(null, conf, 3); assertThat(timeMachineConfiguration.getProjectPastSnapshots().size(), is(2)); }
@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)); }