aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src/it/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoIT.java
blob: fd2cb692e2af01e38c89bd44aa6147c24bd37e77 (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * 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.newcodeperiod;

import java.util.Optional;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.api.utils.System2;
import org.sonar.core.util.SequenceUuidFactory;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.BranchDto;
import org.sonar.db.component.BranchType;
import org.sonar.db.component.ProjectData;
import org.sonar.db.project.ProjectDto;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.tuple;
import static org.sonar.db.newcodeperiod.NewCodePeriodType.NUMBER_OF_DAYS;
import static org.sonar.db.newcodeperiod.NewCodePeriodType.PREVIOUS_VERSION;
import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
import static org.sonar.db.newcodeperiod.NewCodePeriodType.SPECIFIC_ANALYSIS;

class NewCodePeriodDaoIT {

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

  private final DbSession dbSession = db.getSession();
  private final UuidFactory uuidFactory = new SequenceUuidFactory();
  private final NewCodePeriodDao underTest = new NewCodePeriodDao(System2.INSTANCE, uuidFactory);

  @Test
  void insert_new_code_period() {
    insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

    Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "1");

    assertThat(resultOpt).isNotNull()
      .isNotEmpty();

    NewCodePeriodDto result = resultOpt.get();
    assertThat(result.getUuid()).isEqualTo("1");
    assertThat(result.getProjectUuid()).isEqualTo("proj-uuid");
    assertThat(result.getBranchUuid()).isEqualTo("branch-uuid");
    assertThat(result.getType()).isEqualTo(NUMBER_OF_DAYS);
    assertThat(result.getValue()).isEqualTo("5");
    assertThat(result.getCreatedAt()).isNotZero();
    assertThat(result.getUpdatedAt()).isNotZero();

    db.commit();
    assertNewCodePeriodRowCount(1);
  }

  @Test
  void reference_branch_new_code_period_accepts_branches_with_long_names() {
    String branchWithLongName = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabc" +
      "defghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijab" +
      "cdefghijabcdefghijabcdefghijabcdefghijxxxxx";

    insert("1", "proj-uuid", "branch-uuid", REFERENCE_BRANCH, branchWithLongName, null);

    assertThat(db.select("select uuid as \"UUID\", value as \"VALUE\" from new_code_periods"))
      .extracting(r -> r.get("UUID"), r -> r.get("VALUE"))
      .containsExactly(tuple("1", branchWithLongName));
  }

  @Test
  void select_global_with_no_value() {
    assertThat(underTest.selectGlobal(dbSession)).isEmpty();
  }

  @Test
  void update_new_code_period() {
    insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

    underTest.update(dbSession, new NewCodePeriodDto()
      .setUuid("1")
      .setType(SPECIFIC_ANALYSIS)
      .setProjectUuid("proj-uuid")
      .setBranchUuid("branch-uuid")
      .setValue("analysis-uuid"));

    Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "1");

    assertThat(resultOpt).isNotNull()
      .isNotEmpty();

    NewCodePeriodDto result = resultOpt.get();
    assertThat(result.getUuid()).isEqualTo("1");
    assertThat(result.getProjectUuid()).isEqualTo("proj-uuid");
    assertThat(result.getBranchUuid()).isEqualTo("branch-uuid");
    assertThat(result.getType()).isEqualTo(SPECIFIC_ANALYSIS);
    assertThat(result.getValue()).isEqualTo("analysis-uuid");
    assertThat(result.getCreatedAt()).isNotZero();
    assertThat(result.getUpdatedAt()).isNotZero();

    db.commit();
    assertNewCodePeriodRowCount(1);
  }

  @Test
  void update_referenceBranchPeriod_value() {
    insert("1", "proj-uuid", null, REFERENCE_BRANCH, "oldBranchName", null);
    insert("2", "proj-uuid-2", null, REFERENCE_BRANCH, "anotherBranch", null);

    underTest.updateBranchReferenceValues(dbSession, new BranchDto()
      .setBranchType(BranchType.BRANCH)
      .setUuid("branch-uuid")
      .setProjectUuid("proj-uuid")
      .setKey("oldBranchName"), "newBranchName");

    //second project reference branch renaming should not affect the reference branch
    underTest.updateBranchReferenceValues(dbSession, new BranchDto()
      .setBranchType(BranchType.BRANCH)
      .setUuid("branch-uuid")
      .setProjectUuid("proj-uuid-2")
      .setKey("oldBranchName"), "newBranchName");

    db.commit();

    Optional<NewCodePeriodDto> updatedNewCodePeriod = underTest.selectByUuid(dbSession, "1");
    Optional<NewCodePeriodDto> unmodifiedNewCodePeriod = underTest.selectByUuid(dbSession, "2");

    assertThat(updatedNewCodePeriod).isNotNull()
      .isNotEmpty();

    assertThat(unmodifiedNewCodePeriod).isNotNull()
      .isNotEmpty();

    assertNewCodePeriodRowCount(2);

    NewCodePeriodDto updatedResult = updatedNewCodePeriod.get();
    assertThat(updatedResult.getUuid()).isEqualTo("1");
    assertThat(updatedResult.getProjectUuid()).isEqualTo("proj-uuid");
    assertThat(updatedResult.getBranchUuid()).isNull();
    assertThat(updatedResult.getType()).isEqualTo(REFERENCE_BRANCH);
    assertThat(updatedResult.getValue()).isEqualTo("newBranchName");

    NewCodePeriodDto unmodifiedResult = unmodifiedNewCodePeriod.get();
    assertThat(unmodifiedResult.getUuid()).isEqualTo("2");
    assertThat(unmodifiedResult.getProjectUuid()).isEqualTo("proj-uuid-2");
    assertThat(unmodifiedResult.getBranchUuid()).isNull();
    assertThat(unmodifiedResult.getType()).isEqualTo(REFERENCE_BRANCH);
    assertThat(unmodifiedResult.getValue()).isEqualTo("anotherBranch");
  }

  @Test
  void insert_with_upsert() {
    insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

    Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "1");

    assertThat(resultOpt)
      .isNotNull()
      .isNotEmpty();

    NewCodePeriodDto result = resultOpt.get();
    assertThat(result.getUuid()).isEqualTo("1");
    assertThat(result.getProjectUuid()).isEqualTo("proj-uuid");
    assertThat(result.getBranchUuid()).isEqualTo("branch-uuid");
    assertThat(result.getType()).isEqualTo(NUMBER_OF_DAYS);
    assertThat(result.getValue()).isEqualTo("5");
    assertThat(result.getCreatedAt()).isNotZero();
    assertThat(result.getUpdatedAt()).isNotZero();

    db.commit();
    assertNewCodePeriodRowCount(1);
  }

  @Test
  void update_with_upsert() {
    insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

    underTest.upsert(dbSession, new NewCodePeriodDto()
      .setUuid("1")
      .setProjectUuid("proj-uuid")
      .setBranchUuid("branch-uuid")
      .setType(SPECIFIC_ANALYSIS)
      .setValue("analysis-uuid"));

    Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "1");

    assertThat(resultOpt)
      .isNotNull()
      .isNotEmpty();

    NewCodePeriodDto result = resultOpt.get();
    assertThat(result.getUuid()).isEqualTo("1");
    assertThat(result.getProjectUuid()).isEqualTo("proj-uuid");
    assertThat(result.getBranchUuid()).isEqualTo("branch-uuid");
    assertThat(result.getType()).isEqualTo(SPECIFIC_ANALYSIS);
    assertThat(result.getValue()).isEqualTo("analysis-uuid");
    assertThat(result.getCreatedAt()).isNotZero();
    assertThat(result.getUpdatedAt()).isNotZero();

    db.commit();
    assertNewCodePeriodRowCount(1);
  }

  @Test
  void select_by_project_and_branch_uuids() {
    insert("1", "proj-uuid", "branch-uuid", NUMBER_OF_DAYS, "5", null);

    Optional<NewCodePeriodDto> resultOpt = underTest.selectByBranch(dbSession, "proj-uuid", "branch-uuid");
    assertThat(resultOpt)
      .isNotNull()
      .isNotEmpty();

    NewCodePeriodDto result = resultOpt.get();
    assertThat(result.getUuid()).isEqualTo("1");
    assertThat(result.getProjectUuid()).isEqualTo("proj-uuid");
    assertThat(result.getBranchUuid()).isEqualTo("branch-uuid");
    assertThat(result.getType()).isEqualTo(NUMBER_OF_DAYS);
    assertThat(result.getValue()).isEqualTo("5");
    assertThat(result.getCreatedAt()).isNotZero();
    assertThat(result.getUpdatedAt()).isNotZero();
  }

  @Test
  void select_branches_referencing() {
    ProjectData projectData = db.components().insertPrivateProject();
    ProjectDto project = projectData.getProjectDto();
    BranchDto mainBranch = projectData.getMainBranchDto();
    BranchDto branch1 = db.components().insertProjectBranch(project);
    BranchDto branch2 = db.components().insertProjectBranch(project);
    BranchDto branch3 = db.components().insertProjectBranch(project);

    insert("1", project.getUuid(), null, REFERENCE_BRANCH, mainBranch.getKey(), null);
    insert("2", project.getUuid(), branch1.getUuid(), REFERENCE_BRANCH, mainBranch.getKey(), null);
    insert("3", project.getUuid(), branch2.getUuid(), NUMBER_OF_DAYS, "5", null);
    insert("4", project.getUuid(), project.getUuid(), PREVIOUS_VERSION, null, null);
    db.commit();
    assertThat(underTest.selectBranchesReferencing(dbSession, project.getUuid(), mainBranch.getKey())).containsOnly(branch1.getUuid(),
      branch3.getUuid());
  }

  @Test
  void select_by_project_uuid() {
    insert("1", "proj-uuid", null, NUMBER_OF_DAYS, "90", "130");

    Optional<NewCodePeriodDto> resultOpt = underTest.selectByProject(dbSession, "proj-uuid");
    assertThat(resultOpt)
      .isNotNull()
      .isNotEmpty();

    NewCodePeriodDto result = resultOpt.get();
    assertThat(result.getUuid()).isEqualTo("1");
    assertThat(result.getProjectUuid()).isEqualTo("proj-uuid");
    assertThat(result.getBranchUuid()).isNull();
    assertThat(result.getType()).isEqualTo(NUMBER_OF_DAYS);
    assertThat(result.getValue()).isEqualTo("90");
    assertThat(result.getPreviousNonCompliantValue()).isEqualTo("130");
    assertThat(result.getCreatedAt()).isNotZero();
    assertThat(result.getUpdatedAt()).isNotZero();
  }

  @Test
  void select_global() {
    insert("1", null, null, NUMBER_OF_DAYS, "30", null);

    Optional<NewCodePeriodDto> newCodePeriodDto = underTest.selectGlobal(dbSession);
    assertThat(newCodePeriodDto).isNotEmpty();

    NewCodePeriodDto result = newCodePeriodDto.get();
    assertThat(result.getUuid()).isEqualTo("1");
    assertThat(result.getProjectUuid()).isNull();
    assertThat(result.getBranchUuid()).isNull();
    assertThat(result.getType()).isEqualTo(NUMBER_OF_DAYS);
    assertThat(result.getValue()).isEqualTo("30");
    assertThat(result.getPreviousNonCompliantValue()).isNull();
    assertThat(result.getCreatedAt()).isNotZero();
    assertThat(result.getUpdatedAt()).isNotZero();
  }

  @Test
  void exists_by_project_analysis_is_true() {
    insert("1", "proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid", null);

    boolean exists = underTest.existsByProjectAnalysisUuid(dbSession, "analysis-uuid");
    assertThat(exists).isTrue();
  }

  @Test
  void delete_by_project_uuid_and_branch_uuid() {
    insert("1", "proj-uuid", "branch-uuid", SPECIFIC_ANALYSIS, "analysis-uuid", null);

    underTest.delete(dbSession, "proj-uuid", "branch-uuid");
    db.commit();
    assertNewCodePeriodRowCount(0);
  }

  @Test
  void delete_by_project_uuid() {
    insert("1", "proj-uuid", null, SPECIFIC_ANALYSIS, "analysis-uuid", null);

    underTest.delete(dbSession, "proj-uuid", null);
    db.commit();
    assertNewCodePeriodRowCount(0);
  }

  @Test
  void delete_global() {
    insert("1", null, null, SPECIFIC_ANALYSIS, "analysis-uuid", null);

    underTest.delete(dbSession, null, null);
    db.commit();
    assertNewCodePeriodRowCount(0);
  }

  @Test
  void exists_by_project_analysis_is_false() {
    boolean exists = underTest.existsByProjectAnalysisUuid(dbSession, "analysis-uuid");
    assertThat(exists).isFalse();
  }

  @Test
  void fail_select_by_project_and_branch_uuids_if_project_uuid_not_provided() {
    assertThatThrownBy(() -> underTest.selectByBranch(dbSession, null, "random-uuid"))
      .isInstanceOf(NullPointerException.class)
      .hasMessage("Project uuid must be specified.");
  }

  @Test
  void fail_select_by_project_and_branch_uuids_if_branch_uuid_not_provided() {
    assertThatThrownBy(() -> underTest.selectByBranch(dbSession, "random-uuid", null))
      .isInstanceOf(NullPointerException.class)
      .hasMessage("Branch uuid must be specified.");
  }

  @Test
  void fail_select_by_project_uuid_if_project_uuid_not_provided() {
    assertThatThrownBy(() -> underTest.selectByProject(dbSession, null))
      .isInstanceOf(NullPointerException.class)
      .hasMessage("Project uuid must be specified.");
  }

  private void assertNewCodePeriodRowCount(int expected) {
    assertThat(db.countRowsOfTable("new_code_periods"))
      .isEqualTo(expected);
  }

  private void insert(String uuid, @Nullable String projectUuid, @Nullable String branchUuid, NewCodePeriodType type,
    @Nullable String value, @Nullable String previousNonCompliantValue) {
    underTest.insert(dbSession, new NewCodePeriodDto()
      .setUuid(uuid)
      .setProjectUuid(projectUuid)
      .setBranchUuid(branchUuid)
      .setType(type)
      .setValue(value)
      .setPreviousNonCompliantValue(previousNonCompliantValue));
    db.commit();
  }
}