aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/test/java/org/sonar/db/purge/period/IntervalTest.java
blob: 350f1bebfa5871dac0e7c4bb806fa84cbd9e8a4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.purge.period;

import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.sonar.api.utils.DateUtils;
import org.sonar.db.purge.DbCleanerTestUtils;
import org.sonar.db.purge.PurgeableAnalysisDto;

import static org.assertj.core.api.Assertions.assertThat;

class IntervalTest {
  static int calendarField(Interval interval, int field) {
    if (interval.count() == 0) {
      return -1;
    }

    PurgeableAnalysisDto first = interval.get().iterator().next();
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(first.getDate());
    return cal.get(field);
  }

  @Test
  void shouldGroupByIntervals() {
    List<PurgeableAnalysisDto> snapshots = Arrays.asList(
      DbCleanerTestUtils.createAnalysisWithDate("u1", "2011-04-03"),

      DbCleanerTestUtils.createAnalysisWithDate("u2", "2011-05-01"),
      DbCleanerTestUtils.createAnalysisWithDate("u3", "2011-05-19"),

      DbCleanerTestUtils.createAnalysisWithDate("u4", "2011-06-02"),
      DbCleanerTestUtils.createAnalysisWithDate("u5", "2011-06-20"),

      DbCleanerTestUtils.createAnalysisWithDate("u6", "2012-06-29") // out of scope
    );

    List<Interval> intervals = Interval.group(snapshots, DateUtils.parseDate("2010-01-01"), DateUtils.parseDate("2011-12-31"),
      Calendar.MONTH);
    assertThat(intervals).hasSize(3);

    assertThat(intervals.get(0).count()).isOne();
    assertThat(calendarField(intervals.get(0), Calendar.MONTH)).isEqualTo((Calendar.APRIL));

    assertThat(intervals.get(1).count()).isEqualTo(2);
    assertThat(calendarField(intervals.get(1), Calendar.MONTH)).isEqualTo((Calendar.MAY));

    assertThat(intervals.get(2).count()).isEqualTo(2);
    assertThat(calendarField(intervals.get(2), Calendar.MONTH)).isEqualTo((Calendar.JUNE));
  }

  @Test
  void shouldNotJoinMonthsOfDifferentYears() {
    List<PurgeableAnalysisDto> snapshots = Arrays.asList(
      DbCleanerTestUtils.createAnalysisWithDate("u1", "2010-04-03"),
      DbCleanerTestUtils.createAnalysisWithDate("u2", "2011-04-13"));

    List<Interval> intervals = Interval.group(snapshots,
      DateUtils.parseDateTime("2010-01-01T00:00:00+0100"), DateUtils.parseDateTime("2011-12-31T00:00:00+0100"), Calendar.MONTH);
    assertThat(intervals).hasSize(2);

    assertThat(intervals.get(0).count()).isOne();
    assertThat(calendarField(intervals.get(0), Calendar.MONTH)).isEqualTo((Calendar.APRIL));
    assertThat(calendarField(intervals.get(0), Calendar.YEAR)).isEqualTo((2010));

    assertThat(intervals.get(1).count()).isOne();
    assertThat(calendarField(intervals.get(1), Calendar.MONTH)).isEqualTo((Calendar.APRIL));
    assertThat(calendarField(intervals.get(1), Calendar.YEAR)).isEqualTo((2011));
  }

  @Test
  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()).isOne();
    assertThat(intervals.get(0).count()).isOne();
    assertThat(intervals.get(0).get().get(0).getAnalysisUuid()).isEqualTo(("u2"));
  }
}