3 * Copyright (C) 2009-2021 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.db.alm.setting;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.sonar.api.impl.utils.TestSystem2;
26 import org.sonar.core.util.UuidFactory;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.audit.AuditPersister;
30 import org.sonar.db.audit.model.DevOpsPlatformSettingNewValue;
31 import org.sonar.db.project.ProjectDto;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.ArgumentMatchers.eq;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.verifyNoInteractions;
38 import static org.mockito.Mockito.when;
39 import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubProjectAlmSettingDto;
41 public class ProjectAlmSettingDaoWithPersisterTest {
42 private static final long A_DATE = 1_000_000_000_000L;
43 private static final long A_DATE_LATER = 1_700_000_000_000L;
44 private static final String A_UUID = "SOME_UUID";
46 private final ArgumentCaptor<DevOpsPlatformSettingNewValue> newValueCaptor = ArgumentCaptor.forClass(DevOpsPlatformSettingNewValue.class);
47 private final AuditPersister auditPersister = mock(AuditPersister.class);
49 private final TestSystem2 system2 = new TestSystem2().setNow(A_DATE);
51 public final DbTester db = DbTester.create(system2, auditPersister);
53 private final DbSession dbSession = db.getSession();
54 private final UuidFactory uuidFactory = mock(UuidFactory.class);
55 private final ProjectAlmSettingDao underTest = db.getDbClient().projectAlmSettingDao();
58 public void insertAndUpdateExistingBindingArePersisted() {
59 when(uuidFactory.create()).thenReturn(A_UUID);
60 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
61 ProjectDto project = db.components().insertPrivateProjectDto();
62 ProjectAlmSettingDto projectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSetting, project)
63 .setSummaryCommentEnabled(false);
64 underTest.insertOrUpdate(dbSession, projectAlmSettingDto, githubAlmSetting.getKey(), project.getName());
66 verify(auditPersister).addDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
67 DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue();
69 .extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey,
70 DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectName)
71 .containsExactly(githubAlmSetting.getUuid(), githubAlmSetting.getKey(), project.getUuid(), project.getName());
72 assertThat(newValue.toString()).doesNotContain("url");
74 AlmSettingDto anotherGithubAlmSetting = db.almSettings().insertGitHubAlmSetting();
75 system2.setNow(A_DATE_LATER);
76 ProjectAlmSettingDto newProjectAlmSettingDto = newGithubProjectAlmSettingDto(anotherGithubAlmSetting, project)
77 .setSummaryCommentEnabled(false);
78 underTest.insertOrUpdate(dbSession, newProjectAlmSettingDto, anotherGithubAlmSetting.getKey(), project.getName());
80 verify(auditPersister).updateDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
81 newValue = newValueCaptor.getValue();
83 .extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey,
84 DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectName,
85 DevOpsPlatformSettingNewValue::getAlmRepo, DevOpsPlatformSettingNewValue::getAlmSlug,
86 DevOpsPlatformSettingNewValue::isSummaryCommentEnabled, DevOpsPlatformSettingNewValue::isMonorepo)
87 .containsExactly(anotherGithubAlmSetting.getUuid(), anotherGithubAlmSetting.getKey(), project.getUuid(), project.getName(),
88 newProjectAlmSettingDto.getAlmRepo(), newProjectAlmSettingDto.getAlmSlug(),
89 newProjectAlmSettingDto.getSummaryCommentEnabled(), newProjectAlmSettingDto.getMonorepo());
90 assertThat(newValue.toString()).doesNotContain("url");
94 public void deleteByProject() {
95 when(uuidFactory.create()).thenReturn(A_UUID);
96 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
97 ProjectDto project = db.components().insertPrivateProjectDto();
98 ProjectAlmSettingDto projectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSetting, project)
99 .setSummaryCommentEnabled(false);
100 underTest.insertOrUpdate(dbSession, projectAlmSettingDto, githubAlmSetting.getKey(), project.getName());
101 underTest.deleteByProject(dbSession, project);
103 verify(auditPersister).deleteDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
104 DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue();
106 .extracting(DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectName)
107 .containsExactly(project.getUuid(), project.getName());
108 assertThat(newValue.toString()).doesNotContain("devOpsPlatformSettingUuid");
112 public void deleteByAlmSettingWhenNotTracked() {
113 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
114 underTest.deleteByAlmSetting(dbSession, githubAlmSetting);
116 verifyNoInteractions(auditPersister);
120 public void deleteByAlmSettingWhenTracked() {
121 AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
122 underTest.deleteByAlmSetting(dbSession, githubAlmSetting, true);
124 verify(auditPersister).deleteDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
125 DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue();
127 .extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey)
128 .containsExactly(githubAlmSetting.getUuid(), githubAlmSetting.getKey());
129 assertThat(newValue.toString()).doesNotContain("url");