3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v102;
22 import java.sql.SQLException;
23 import javax.annotation.Nullable;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.db.MigrationDbTester;
28 import static org.assertj.core.api.Assertions.assertThat;
29 import static org.assertj.core.api.Assertions.tuple;
31 public class UpdateValueAndPopulatePreviousNonCompliantValueInNewCodePeriodsIT {
33 private static final String TABLE_NAME = "new_code_periods";
34 private static final String PROJECT_UUID = "project-uuid";
35 private static final String BRANCH_UUID = "branch-uuid";
38 public final MigrationDbTester db = MigrationDbTester.createForMigrationStep(UpdateValueAndPopulatePreviousNonCompliantValueInNewCodePeriods.class);
39 public final UpdateValueAndPopulatePreviousNonCompliantValueInNewCodePeriods underTest = new UpdateValueAndPopulatePreviousNonCompliantValueInNewCodePeriods(db.database());
42 public void execute_whenSnapshotsExist_shouldPopulatePurgedColumn() throws SQLException {
43 insertNewCodePeriods("uuid-1", PROJECT_UUID, BRANCH_UUID, "PREVIOUS_VERSION", null);
44 insertNewCodePeriods("uuid-2", PROJECT_UUID, null, "NUMBER_OF_DAYS", "90");
45 insertNewCodePeriods("uuid-3", null, null, "NUMBER_OF_DAYS", "97");
49 assertThat(db.select("select uuid, value, previous_non_compliant_value from new_code_periods"))
50 .extracting(stringObjectMap -> stringObjectMap.get("uuid"), stringObjectMap -> stringObjectMap.get("value"),
51 stringObjectMap -> stringObjectMap.get("previous_non_compliant_value"))
52 .containsExactlyInAnyOrder(
53 tuple("uuid-1", null, null),
54 tuple("uuid-2", "90", null),
55 tuple("uuid-3", "90", "97"));
58 private void insertNewCodePeriods(String uuid, @Nullable String projectUuid, @Nullable String branchUuid, String type, String value) {
59 db.executeInsert(TABLE_NAME,
61 "PROJECT_UUID", projectUuid,
62 "BRANCH_UUID", branchUuid,
65 "UPDATED_AT", System.currentTimeMillis(),
66 "CREATED_AT", System.currentTimeMillis());