]> source.dussan.org Git - sonarqube.git/blob
25d3ce811ea9f0ab4af493b35f7f3403cd6f1568
[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.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;
40
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";
45
46   private final ArgumentCaptor<DevOpsPlatformSettingNewValue> newValueCaptor = ArgumentCaptor.forClass(DevOpsPlatformSettingNewValue.class);
47   private final AuditPersister auditPersister = mock(AuditPersister.class);
48
49   private final TestSystem2 system2 = new TestSystem2().setNow(A_DATE);
50   @Rule
51   public final DbTester db = DbTester.create(system2, auditPersister);
52
53   private final DbSession dbSession = db.getSession();
54   private final UuidFactory uuidFactory = mock(UuidFactory.class);
55   private final ProjectAlmSettingDao underTest = db.getDbClient().projectAlmSettingDao();
56
57   @Test
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());
65
66     verify(auditPersister).addDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
67     DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue();
68     assertThat(newValue)
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\"");
73
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());
79
80     verify(auditPersister).updateDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
81     newValue = newValueCaptor.getValue();
82     assertThat(newValue)
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\"");
91   }
92
93   @Test
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);
102
103     verify(auditPersister).deleteDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
104     DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue();
105     assertThat(newValue)
106       .extracting(DevOpsPlatformSettingNewValue::getProjectUuid, DevOpsPlatformSettingNewValue::getProjectName)
107       .containsExactly(project.getUuid(), project.getName());
108     assertThat(newValue.toString()).doesNotContain("devOpsPlatformSettingUuid");
109   }
110
111   @Test
112   public void deleteByAlmSettingWhenNotTracked() {
113     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
114     underTest.deleteByAlmSetting(dbSession, githubAlmSetting);
115
116     verifyNoInteractions(auditPersister);
117   }
118
119   @Test
120   public void deleteByAlmSettingWhenTracked() {
121     AlmSettingDto githubAlmSetting = db.almSettings().insertGitHubAlmSetting();
122     underTest.deleteByAlmSetting(dbSession, githubAlmSetting, true);
123
124     verify(auditPersister).deleteDevOpsPlatformSetting(eq(dbSession), newValueCaptor.capture());
125     DevOpsPlatformSettingNewValue newValue = newValueCaptor.getValue();
126     assertThat(newValue)
127       .extracting(DevOpsPlatformSettingNewValue::getDevOpsPlatformSettingUuid, DevOpsPlatformSettingNewValue::getKey)
128       .containsExactly(githubAlmSetting.getUuid(), githubAlmSetting.getKey());
129     assertThat(newValue.toString()).doesNotContain("url");
130   }
131
132 }