]> source.dussan.org Git - sonarqube.git/blob
018bda5d50ecebea8ce2bd519cbb23fd1208e744
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.db.alm.setting;
21
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;
32
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.mockito.ArgumentMatchers.any;
35 import static org.mockito.ArgumentMatchers.eq;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.verify;
38 import static org.mockito.Mockito.verifyNoInteractions;
39 import static org.mockito.Mockito.verifyNoMoreInteractions;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.db.almsettings.AlmSettingsTesting.newGithubProjectAlmSettingDto;
42
43 public class ProjectAlmSettingDaoWithPersisterTest {
44   private static final long A_DATE = 1_000_000_000_000L;
45   private static final long A_DATE_LATER = 1_700_000_000_000L;
46   private static final String A_UUID = "SOME_UUID";
47
48   private final ArgumentCaptor<DevOpsPlatformSettingNewValue> newValueCaptor = ArgumentCaptor.forClass(DevOpsPlatformSettingNewValue.class);
49   private final AuditPersister auditPersister = mock(AuditPersister.class);
50
51   private final TestSystem2 system2 = new TestSystem2().setNow(A_DATE);
52   @Rule
53   public final DbTester db = DbTester.create(system2, auditPersister);
54
55   private final DbSession dbSession = db.getSession();
56   private final UuidFactory uuidFactory = mock(UuidFactory.class);
57   private final ProjectAlmSettingDao underTest = db.getDbClient().projectAlmSettingDao();
58
59   @Test
60   public void insertAndUpdateExistingBindingArePersisted() {
61     when(uuidFactory.create()).thenReturn(A_UUID);
62     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
63     ProjectDto project = db.components().insertPrivateProjectDto();
64     ProjectAlmSettingDto projectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSetting, project)
65       .setSummaryCommentEnabled(false);
66     underTest.insertOrUpdate(dbSession, projectAlmSettingDto, githubAlmSetting.getKey(), project.getName(), project.getKey());
67
68     verify(auditPersister).addDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
69     DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue();
70     assertThat(newValue)
71       .extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey,
72         DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectKey,
73         DevOpsPlatformSettingNewValue::getProjectName)
74       .containsExactly(githubAlmSetting.getUuid(), githubAlmSetting.getKey(), project.getUuid(), project.getKey(), project.getName());
75     assertThat(newValue.toString()).doesNotContain("\"url\"");
76
77     AlmSettingDto anotherGithubAlmSetting = db.almSettings().insertGitHubAlmSetting();
78     system2.setNow(A_DATE_LATER);
79     ProjectAlmSettingDto newProjectAlmSettingDto = newGithubProjectAlmSettingDto(anotherGithubAlmSetting, project)
80       .setSummaryCommentEnabled(false);
81     underTest.insertOrUpdate(dbSession, newProjectAlmSettingDto, anotherGithubAlmSetting.getKey(), project.getName(), project.getKey());
82
83     verify(auditPersister).updateDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
84     newValue = newValueCaptor.getValue();
85     assertThat(newValue)
86       .extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey,
87         DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectName,
88         DevOpsPlatformSettingNewValue::getAlmRepo, DevOpsPlatformSettingNewValue::getAlmSlug,
89         DevOpsPlatformSettingNewValue::isSummaryCommentEnabled, DevOpsPlatformSettingNewValue::isMonorepo)
90       .containsExactly(anotherGithubAlmSetting.getUuid(), anotherGithubAlmSetting.getKey(), project.getUuid(), project.getName(),
91         newProjectAlmSettingDto.getAlmRepo(), newProjectAlmSettingDto.getAlmSlug(),
92         newProjectAlmSettingDto.getSummaryCommentEnabled(), newProjectAlmSettingDto.getMonorepo());
93     assertThat(newValue.toString()).doesNotContain("\"url\"");
94   }
95
96   @Test
97   public void deleteByProjectIsPersisted() {
98     when(uuidFactory.create()).thenReturn(A_UUID);
99     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
100     ProjectDto project = db.components().insertPrivateProjectDto();
101     ProjectAlmSettingDto projectAlmSettingDto = newGithubProjectAlmSettingDto(githubAlmSetting, project)
102       .setSummaryCommentEnabled(false);
103     underTest.insertOrUpdate(dbSession, projectAlmSettingDto, githubAlmSetting.getKey(), project.getName(), project.getKey());
104     underTest.deleteByProject(dbSession, project);
105
106     verify(auditPersister).deleteDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
107     DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue();
108     assertThat(newValue)
109       .extracting(DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectKey,
110         DevOpsPlatformSettingNewValue::getProjectName)
111       .containsExactly(project.getUuid(), project.getKey(), project.getName());
112     assertThat(newValue.toString()).doesNotContain("devOpsPlatformSettingUuid");
113   }
114
115   @Test
116   public void deleteByWithoutAffectedRowsProjectIsNotPersisted() {
117     ProjectDto project = db.components().insertPrivateProjectDto();
118
119     underTest.deleteByProject(dbSession, project);
120
121     verify(auditPersister).addComponent(any(), any(), any());
122     verifyNoMoreInteractions(auditPersister);
123   }
124
125   @Test
126   public void deleteByAlmSettingNotTrackedIsNotPersisted() {
127     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
128     underTest.deleteByAlmSetting(dbSession, githubAlmSetting);
129
130     verifyNoInteractions(auditPersister);
131   }
132 }