private List<PurgeableAnalysisDto> delete(String rootUuid, List<PurgeableAnalysisDto> snapshots, DbSession session) { if (LOG.isDebugEnabled()) { LOG.debug("<- Delete analyses of component {}: {}", rootUuid, Joiner.on(", ").join( snapshots.stream() .map(snapshot -> snapshot.getAnalysisUuid() + "@" + DateUtils.formatDateTime(snapshot.getDate())) .collect(MoreCollectors.toArrayList(snapshots.size())))); } purgeDao.deleteAnalyses( session, profiler, snapshots.stream().map(DefaultPeriodCleaner::toIdUuidPair).collect(MoreCollectors.toList(snapshots.size()))); return snapshots; }
private static IdUuidPair toIdUuidPair(PurgeableAnalysisDto snapshot) { return new IdUuidPair(snapshot.getAnalysisId(), snapshot.getAnalysisUuid()); }
private static boolean isDeletable(PurgeableAnalysisDto snapshot) { return !snapshot.isLast() && Strings.isNullOrEmpty(snapshot.getVersion()); }
@Test public void selectPurgeableAnalyses() { db.prepareDbUnit(getClass(), "shouldSelectPurgeableAnalysis.xml"); List<PurgeableAnalysisDto> analyses = underTest.selectPurgeableAnalyses(THE_PROJECT_UUID, dbSession); assertThat(analyses).hasSize(3); assertThat(getById(analyses, "u1").isLast()).isTrue(); assertThat(getById(analyses, "u1").hasEvents()).isFalse(); assertThat(getById(analyses, "u1").getVersion()).isNull(); assertThat(getById(analyses, "u4").isLast()).isFalse(); assertThat(getById(analyses, "u4").hasEvents()).isFalse(); assertThat(getById(analyses, "u4").getVersion()).isNull(); assertThat(getById(analyses, "u5").isLast()).isFalse(); assertThat(getById(analyses, "u5").hasEvents()).isTrue(); assertThat(getById(analyses, "u5").getVersion()).isEqualTo("V5"); }
@Override public List<PurgeableAnalysisDto> filter(List<PurgeableAnalysisDto> history) { List<PurgeableAnalysisDto> result = Lists.newArrayList(); for (PurgeableAnalysisDto snapshot : history) { if (snapshot.getDate().before(before)) { result.add(snapshot); } } return result; }
private static PurgeableAnalysisDto getById(List<PurgeableAnalysisDto> snapshots, String uuid) { return snapshots.stream() .filter(snapshot -> uuid.equals(snapshot.getAnalysisUuid())) .findFirst() .orElse(null); }
@Test public void doClean() { PurgeDao dao = mock(PurgeDao.class); DbSession session = mock(DbSession.class); when(dao.selectPurgeableAnalyses("uuid_123", session)).thenReturn(Arrays.asList( new PurgeableAnalysisDto().setAnalysisId(999).setAnalysisUuid("u999").setDate(System2.INSTANCE.now()), new PurgeableAnalysisDto().setAnalysisId(456).setAnalysisUuid("u456").setDate(System2.INSTANCE.now()) )); Filter filter1 = newFirstSnapshotInListFilter(); Filter filter2 = newFirstSnapshotInListFilter(); PurgeProfiler profiler = new PurgeProfiler(); DefaultPeriodCleaner cleaner = new DefaultPeriodCleaner(dao, profiler); cleaner.doClean("uuid_123", Arrays.asList(filter1, filter2), session); InOrder inOrder = Mockito.inOrder(dao, filter1, filter2); inOrder.verify(filter1).log(); inOrder.verify(dao, times(1)).deleteAnalyses(eq(session), eq(profiler), eq(ImmutableList.of(new IdUuidPair(999, "u999")))); inOrder.verify(filter2).log(); inOrder.verify(dao, times(1)).deleteAnalyses(eq(session), eq(profiler), eq(ImmutableList.of(new IdUuidPair(456, "u456")))); inOrder.verifyNoMoreInteractions(); }
public static PurgeableAnalysisDto createAnalysisWithDateTime(String analysisUuid, String datetime) { PurgeableAnalysisDto snapshot = new PurgeableAnalysisDto(); snapshot.setAnalysisUuid(analysisUuid); snapshot.setDate(DateUtils.parseDateTime(datetime).getTime()); return snapshot; }
@Test public void testEquals() { PurgeableAnalysisDto dto1 = new PurgeableAnalysisDto().setAnalysisUuid("u3"); PurgeableAnalysisDto dto2 = new PurgeableAnalysisDto().setAnalysisUuid("u4"); assertThat(dto1.equals(dto2)).isFalse(); assertThat(dto2.equals(dto1)).isFalse(); assertThat(dto1.equals(dto1)).isTrue(); assertThat(dto1.equals(new PurgeableAnalysisDto().setAnalysisUuid("u3"))).isTrue(); assertThat(dto1.equals("bi_bop_a_lou_la")).isFalse(); assertThat(dto1.equals(null)).isFalse(); }
@Test public void testHasCode() { PurgeableAnalysisDto dto = new PurgeableAnalysisDto().setAnalysisUuid("u3"); assertThat(dto.hashCode()).isEqualTo(dto.hashCode()); // no uuid => NPE dto = new PurgeableAnalysisDto(); expectedException.expect(NullPointerException.class); dto.hashCode(); }
@Test public void testToString() { PurgeableAnalysisDto dto = new PurgeableAnalysisDto().setAnalysisUuid("u3"); assertThat(dto.toString()).isNotEmpty(); } }
@Override public List<PurgeableAnalysisDto> filter(List<PurgeableAnalysisDto> history) { return history.stream() .filter(analysis -> analysis.getDate().before(before)) .filter(KeepWithVersionFilter::isDeletable) .collect(MoreCollectors.toList()); }
private static List<String> analysisUuids(List<PurgeableAnalysisDto> snapshotDtos) { return snapshotDtos.stream().map(input -> input.getAnalysisUuid()).collect(Collectors.toList()); }
public static PurgeableAnalysisDto createAnalysisWithDate(String analysisUuid, String date) { PurgeableAnalysisDto snapshot = new PurgeableAnalysisDto(); snapshot.setAnalysisUuid(analysisUuid); snapshot.setDate(DateUtils.parseDate(date).getTime()); return snapshot; }
private List<PurgeableAnalysisDto> delete(String rootUuid, List<PurgeableAnalysisDto> snapshots, DbSession session) { if (LOG.isDebugEnabled()) { LOG.debug("<- Delete analyses of component {}: {}", rootUuid, Joiner.on(", ").join( snapshots.stream() .map(snapshot -> snapshot.getAnalysisUuid() + "@" + DateUtils.formatDateTime(snapshot.getDate())) .collect(Collectors.toArrayList(snapshots.size())))); } purgeDao.deleteAnalyses( session, profiler, snapshots.stream().map(DefaultPeriodCleaner::toIdUuidPair).collect(Collectors.toList(snapshots.size()))); return snapshots; }
static List<Interval> group(List<PurgeableAnalysisDto> snapshots, Date start, Date end, int calendarField) { List<Interval> intervals = Lists.newArrayList(); GregorianCalendar calendar = new GregorianCalendar(); int lastYear = -1; int lastFieldValue = -1; Interval currentInterval = null; for (PurgeableAnalysisDto snapshot : snapshots) { if (!DateUtils.isSameDay(start, snapshot.getDate()) && snapshot.getDate().after(start) && (snapshot.getDate().before(end) || DateUtils.isSameDay(end, snapshot.getDate()))) { calendar.setTime(snapshot.getDate()); int currentFieldValue = calendar.get(calendarField); int currentYear = calendar.get(Calendar.YEAR); if (lastYear != currentYear || lastFieldValue != currentFieldValue) { currentInterval = new Interval(); intervals.add(currentInterval); } lastFieldValue = currentFieldValue; lastYear = currentYear; if (currentInterval != null) { currentInterval.add(snapshot); } } } return intervals; } }
private static IdUuidPair toIdUuidPair(PurgeableAnalysisDto snapshot) { return new IdUuidPair(snapshot.getAnalysisId(), snapshot.getAnalysisUuid()); }
@Test public void shouldIgnoreTimeWhenGroupingByIntervals() { List<PurgeableAnalysisDto> snapshots = Arrays.asList( DbCleanerTestUtils.createAnalysisWithDateTime("u1", "2011-05-25T00:16:48+0100"), DbCleanerTestUtils.createAnalysisWithDateTime("u2", "2012-01-26T00:16:48+0100"), DbCleanerTestUtils.createAnalysisWithDateTime("u3", "2012-01-27T00:16:48+0100") ); List<Interval> intervals = Interval.group(snapshots, DateUtils.parseDateTime("2011-05-25T00:00:00+0100"), DateUtils.parseDateTime("2012-01-26T00:00:00+0100"), Calendar.MONTH); assertThat(intervals.size()).isEqualTo(1); assertThat(intervals.get(0).count()).isEqualTo(1); assertThat(intervals.get(0).get().get(0).getAnalysisUuid()).isEqualTo(("u2")); } }