aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/it/java/org/sonar/db/event/EventDaoIT.java
blob: ad918780c59272ebce374930913828da655c7995 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * 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.event;

import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ComponentTesting;
import org.sonar.db.component.SnapshotDto;

import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.component.SnapshotTesting.newAnalysis;
import static org.sonar.db.event.EventTesting.newEvent;

class EventDaoIT {

  @RegisterExtension
  private final DbTester dbTester = DbTester.create(System2.INSTANCE);

  private final DbClient dbClient = dbTester.getDbClient();
  private final DbSession dbSession = dbTester.getSession();
  private final EventDao underTest = dbTester.getDbClient().eventDao();

  @Test
  void select_by_uuid() {
    SnapshotDto analysis = dbTester.components().insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto());
    dbTester.events().insertEvent(newEvent(analysis).setUuid("A1"));
    dbTester.events().insertEvent(newEvent(analysis).setUuid("A2"));
    dbTester.events().insertEvent(newEvent(analysis).setUuid("A3"));

    Optional<EventDto> result = underTest.selectByUuid(dbSession, "A2");

    assertThat(result).isPresent();
    assertThat(result.get().getUuid()).isEqualTo("A2");
  }

  @Test
  void select_by_component_uuid() {
    ComponentDto project1 = ComponentTesting.newPrivateProjectDto();
    ComponentDto project2 = ComponentTesting.newPrivateProjectDto();
    SnapshotDto analysis1 = dbTester.components().insertProjectAndSnapshot(project1);
    SnapshotDto analysis2 = dbTester.components().insertProjectAndSnapshot(project2);
    String[] eventUuids1 = IntStream.range(0, 1 + new Random().nextInt(10))
      .mapToObj(i -> dbTester.events().insertEvent(newEvent(analysis1).setUuid("1_" + i)))
      .map(EventDto::getUuid)
      .toArray(String[]::new);
    EventDto event2 = dbTester.events().insertEvent(new EventDto()
      .setAnalysisUuid(analysis2.getUuid())
      .setComponentUuid(analysis2.getRootComponentUuid())
      .setName("name_2_")
      .setUuid("2_")
      .setCategory("cat_2_")
      .setDescription("desc_2_")
      .setData("dat_2_")
      .setDate(2_000L)
      .setCreatedAt(4_000L));

    assertThat(underTest.selectByComponentUuid(dbTester.getSession(), project1.uuid()))
      .extracting(EventDto::getUuid)
      .containsOnly(eventUuids1);
    List<EventDto> events2 = underTest.selectByComponentUuid(dbTester.getSession(), project2.uuid());
    assertThat(events2)
      .extracting(EventDto::getUuid)
      .containsOnly(event2.getUuid());
    assertThat(underTest.selectByComponentUuid(dbTester.getSession(), "does not exist"))
      .isEmpty();

    EventDto dto = events2.get(0);
    assertThat(dto.getUuid()).isEqualTo(event2.getUuid());
    assertThat(dto.getAnalysisUuid()).isEqualTo(event2.getAnalysisUuid());
    assertThat(dto.getComponentUuid()).isEqualTo(event2.getComponentUuid());
    assertThat(dto.getName()).isEqualTo(event2.getName());
    assertThat(dto.getCategory()).isEqualTo(event2.getCategory());
    assertThat(dto.getDescription()).isEqualTo(event2.getDescription());
    assertThat(dto.getData()).isEqualTo(event2.getData());
    assertThat(dto.getDate()).isEqualTo(event2.getDate());
    assertThat(dto.getCreatedAt()).isEqualTo(event2.getCreatedAt());
  }

  @Test
  void select_by_analysis_uuid() {
    ComponentDto project = ComponentTesting.newPrivateProjectDto();
    SnapshotDto analysis = dbTester.components().insertProjectAndSnapshot(project);
    SnapshotDto otherAnalysis = dbClient.snapshotDao().insert(dbSession, newAnalysis(project));
    dbTester.commit();
    dbTester.events().insertEvent(newEvent(analysis).setUuid("A1"));
    dbTester.events().insertEvent(newEvent(otherAnalysis).setUuid("O1"));
    dbTester.events().insertEvent(newEvent(analysis).setUuid("A2"));
    dbTester.events().insertEvent(newEvent(otherAnalysis).setUuid("O2"));
    dbTester.events().insertEvent(newEvent(analysis).setUuid("A3"));
    dbTester.events().insertEvent(newEvent(otherAnalysis).setUuid("O3"));

    List<EventDto> result = underTest.selectByAnalysisUuid(dbSession, analysis.getUuid());

    assertThat(result).hasSize(3);
    assertThat(result).extracting(EventDto::getUuid).containsOnly("A1", "A2", "A3");
  }

  @Test
  void select_by_analysis_uuids() {
    ComponentDto project = dbTester.components().insertPrivateProject().getMainBranchComponent();
    SnapshotDto a1 = dbTester.components().insertSnapshot(newAnalysis(project));
    SnapshotDto a2 = dbTester.components().insertSnapshot(newAnalysis(project));
    SnapshotDto a42 = dbTester.components().insertSnapshot(newAnalysis(project));
    dbTester.events().insertEvent(newEvent(newAnalysis(project)));
    dbTester.events().insertEvent(newEvent(a1).setUuid("A11"));
    dbTester.events().insertEvent(newEvent(a1).setUuid("A12"));
    dbTester.events().insertEvent(newEvent(a1).setUuid("A13"));
    dbTester.events().insertEvent(newEvent(a2).setUuid("A21"));
    dbTester.events().insertEvent(newEvent(a2).setUuid("A22"));
    dbTester.events().insertEvent(newEvent(a2).setUuid("A23"));
    dbTester.events().insertEvent(newEvent(a42).setUuid("AO1"));
    dbTester.events().insertEvent(newEvent(a42).setUuid("AO2"));
    dbTester.events().insertEvent(newEvent(a42).setUuid("AO3"));

    List<EventDto> result = underTest.selectByAnalysisUuids(dbSession, newArrayList(a1.getUuid(), a2.getUuid()));

    assertThat(result).hasSize(6);
    assertThat(result).extracting(EventDto::getUuid).containsOnly("A11", "A12", "A13", "A21", "A22", "A23");
  }

  @Test
  void return_different_categories() {
    ComponentDto project = ComponentTesting.newPrivateProjectDto();
    SnapshotDto analysis = dbTester.components().insertProjectAndSnapshot(project);
    List<EventDto> events = IntStream.range(0, 1 + new Random().nextInt(10))
      .mapToObj(i -> dbTester.events().insertEvent(newEvent(analysis).setCategory("cat_" + i)))
      .toList();

    List<EventDto> dtos = underTest.selectByComponentUuid(dbTester.getSession(), project.uuid());
    assertThat(dtos)
      .extracting(EventDto::getCategory)
      .containsOnly(events.stream().map(EventDto::getCategory).toArray(String[]::new));
  }

  @Test
  void insert() {
    EventDto expected = new EventDto()
      .setUuid("E1")
      .setAnalysisUuid("uuid_1")
      .setComponentUuid("ABCD")
      .setName("1.0")
      .setCategory(EventDto.CATEGORY_VERSION)
      .setDescription("Version 1.0")
      .setData("some data")
      .setDate(1413407091086L)
      .setCreatedAt(1225630680000L);
    underTest.insert(dbTester.getSession(), expected);
    dbTester.getSession().commit();

    EventDto dto = underTest.selectByUuid(dbSession, expected.getUuid()).get();

    assertThat(dto.getUuid()).isEqualTo(expected.getUuid());
    assertThat(dto.getAnalysisUuid()).isEqualTo(expected.getAnalysisUuid());
    assertThat(dto.getComponentUuid()).isEqualTo(expected.getComponentUuid());
    assertThat(dto.getName()).isEqualTo(expected.getName());
    assertThat(dto.getCategory()).isEqualTo(expected.getCategory());
    assertThat(dto.getDescription()).isEqualTo(expected.getDescription());
    assertThat(dto.getData()).isEqualTo(expected.getData());
    assertThat(dto.getDate()).isEqualTo(expected.getDate());
    assertThat(dto.getCreatedAt()).isEqualTo(expected.getCreatedAt());
  }

  @Test
  void update_name_and_description() {
    SnapshotDto analysis = dbTester.components().insertProjectAndSnapshot(ComponentTesting.newPrivateProjectDto());
    dbTester.events().insertEvent(newEvent(analysis).setUuid("E1"));

    underTest.update(dbSession, "E1", "New Name", "New Description");

    EventDto result = dbClient.eventDao().selectByUuid(dbSession, "E1").get();
    assertThat(result.getName()).isEqualTo("New Name");
    assertThat(result.getDescription()).isEqualTo("New Description");
  }

  @Test
  void givenSomeSqUpgradeEvents_whenRetrieved_shouldReturnCorrectlyOrderedByDateDescending() {
    long olderDate = 1L;
    long newerDate = 2L;

    ComponentDto componentDto = ComponentTesting.newPrivateProjectDto();
    SnapshotDto analysis = dbTester.components().insertProjectAndSnapshot(componentDto);
    dbTester.events().insertEvent(newEvent(analysis).setCategory(EventDto.CATEGORY_SQ_UPGRADE).setDate(olderDate).setUuid("E1"));
    dbTester.events().insertEvent(newEvent(analysis).setCategory(EventDto.CATEGORY_SQ_UPGRADE).setDate(newerDate).setUuid("E2"));

    List<EventDto> events = underTest.selectSqUpgradesByMostRecentFirst(dbSession, componentDto.uuid());
    assertThat(events).hasSize(2);
    assertThat(events.get(0).getUuid()).isEqualTo("E2");
    assertThat(events.get(0).getDate()).isEqualTo(newerDate);
    assertThat(events.get(1).getUuid()).isEqualTo("E1");
    assertThat(events.get(1).getDate()).isEqualTo(olderDate);
  }

  @Test
  void delete_by_uuid() {
    dbTester.events().insertEvent(newEvent(newAnalysis(ComponentTesting.newPrivateProjectDto())).setUuid("E1"));

    underTest.delete(dbTester.getSession(), "E1");
    dbTester.commit();

    assertThat(dbTester.countRowsOfTable("events")).isZero();
  }

}