]> source.dussan.org Git - sonarqube.git/blob
8cdf7c6d6988d4902ba96cd634aebb6b5511d577
[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.permission.template;
21
22 import java.util.List;
23 import java.util.Optional;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.DbTester;
30
31 import static com.google.common.collect.Lists.newArrayList;
32 import static java.util.Arrays.asList;
33 import static java.util.Collections.emptyList;
34 import static java.util.Collections.singletonList;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.assertThatThrownBy;
37
38 public class PermissionTemplateCharacteristicDaoTest {
39   @Rule
40   public DbTester db = DbTester.create(System2.INSTANCE);
41   private final DbSession dbSession = db.getSession();
42   private final PermissionTemplateCharacteristicDao underTest = new PermissionTemplateCharacteristicDao();
43
44   @Test
45   public void selectByTemplateId_filter_by_template_uuid() {
46     PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
47         .setUuid("uuid1")
48         .setPermission(UserRole.ADMIN)
49         .setTemplateUuid("1")
50         .setWithProjectCreator(true)
51         .setCreatedAt(1_000_000_000L)
52         .setUpdatedAt(2_000_000_000L),
53       "template");
54     PermissionTemplateCharacteristicDto templatePermission2 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
55         .setUuid("uuid2")
56         .setPermission(UserRole.USER)
57         .setTemplateUuid("2")
58         .setWithProjectCreator(false)
59         .setCreatedAt(1_000_000_000L)
60         .setUpdatedAt(2_000_000_000L),
61       "template");
62     PermissionTemplateCharacteristicDto templatePermission3 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
63         .setUuid("uuid3")
64         .setPermission(UserRole.USER)
65         .setTemplateUuid("3")
66         .setWithProjectCreator(false)
67         .setCreatedAt(1_000_000_001L)
68         .setUpdatedAt(2_000_000_000L),
69       "template");
70     PermissionTemplateCharacteristicDto templatePermissionForAnotherTemplate = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
71         .setUuid("uuid4")
72         .setPermission(UserRole.ADMIN)
73         .setTemplateUuid("42")
74         .setWithProjectCreator(true)
75         .setCreatedAt(1_000_000_000L)
76         .setUpdatedAt(2_000_000_000L),
77       "template");
78
79     List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateUuids(dbSession, newArrayList("1", "2", "3"));
80     assertThat(result)
81       .hasSize(3)
82       .extracting("uuid")
83       .doesNotContain(templatePermissionForAnotherTemplate.getUuid())
84       .containsExactly(templatePermission1.getUuid(), templatePermission2.getUuid(), templatePermission3.getUuid());
85     assertThat(result.get(0))
86       .isEqualToComparingFieldByField(templatePermission1);
87   }
88
89   @Test
90   public void selectByTemplateId_for_empty_list_of_template_uuid() {
91     List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateUuids(dbSession, emptyList());
92
93     assertThat(result).isEmpty();
94   }
95
96   @Test
97   public void selectByPermissionAndTemplateId() {
98     PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
99         .setUuid("uuid1")
100         .setPermission(UserRole.ADMIN)
101         .setTemplateUuid("1")
102         .setWithProjectCreator(true)
103         .setCreatedAt(1_000_000_000L)
104         .setUpdatedAt(2_000_000_000L),
105       "template");
106     underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
107         .setUuid("uuid2")
108         .setPermission(UserRole.USER)
109         .setTemplateUuid("1")
110         .setWithProjectCreator(false)
111         .setCreatedAt(1_000_000_000L)
112         .setUpdatedAt(2_000_000_000L),
113       "template");
114     underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
115         .setUuid("uuid3")
116         .setPermission(UserRole.ADMIN)
117         .setTemplateUuid("42")
118         .setWithProjectCreator(true)
119         .setCreatedAt(1_000_000_000L)
120         .setUpdatedAt(2_000_000_000L),
121       "template");
122
123     Optional<PermissionTemplateCharacteristicDto> result = underTest.selectByPermissionAndTemplateId(dbSession, UserRole.ADMIN, "1");
124
125     assertThat(result).isPresent();
126     assertThat(result.get()).isEqualToComparingFieldByField(templatePermission1);
127   }
128
129   @Test
130   public void insert() {
131     PermissionTemplateCharacteristicDto expectedResult = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
132         .setUuid("uuid")
133         .setPermission(UserRole.USER)
134         .setTemplateUuid("1")
135         .setWithProjectCreator(true)
136         .setCreatedAt(123_456_789L)
137         .setUpdatedAt(2_000_000_000L),
138       "template");
139
140     PermissionTemplateCharacteristicDto result = dbSession.getMapper(PermissionTemplateCharacteristicMapper.class).selectByUuid(expectedResult.getUuid());
141     assertThat(result.getUuid()).isEqualTo("uuid");
142     assertThat(result).isEqualToComparingFieldByField(expectedResult);
143   }
144
145   @Test
146   public void update_only_change_with_project_creator_and_updated_at() {
147     PermissionTemplateCharacteristicDto insertedDto = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
148         .setUuid("uuid")
149         .setPermission(UserRole.USER)
150         .setTemplateUuid("1")
151         .setWithProjectCreator(true)
152         .setCreatedAt(123_456_789L)
153         .setUpdatedAt(2_000_000_000L),
154       "template");
155
156     underTest.update(dbSession, new PermissionTemplateCharacteristicDto()
157         .setUuid("uuid")
158         .setPermission("PERMISSION_ARE_NOT_UPDATABLE")
159         .setTemplateUuid("42")
160         .setCreatedAt(42L)
161         .setWithProjectCreator(false)
162         .setUpdatedAt(3_000_000_000L),
163       "template");
164
165     PermissionTemplateCharacteristicDto result = underTest.selectByPermissionAndTemplateId(dbSession, insertedDto.getPermission(), insertedDto.getTemplateUuid()).get();
166     assertThat(result).extracting("uuid", "permission", "templateUuid", "createdAt")
167       .containsExactly(insertedDto.getUuid(), insertedDto.getPermission(), insertedDto.getTemplateUuid(), insertedDto.getCreatedAt());
168     assertThat(result).extracting("withProjectCreator", "updatedAt")
169       .containsExactly(false, 3_000_000_000L);
170   }
171
172   @Test
173   public void fail_insert_if_created_at_is_equal_to_0() {
174     PermissionTemplateCharacteristicDto characteristicDto = new PermissionTemplateCharacteristicDto()
175       .setUuid("uuid")
176       .setPermission(UserRole.USER)
177       .setTemplateUuid("1")
178       .setWithProjectCreator(true)
179       .setUpdatedAt(2_000_000_000L);
180     assertThatThrownBy(() -> underTest.insert(dbSession, characteristicDto, "template"))
181       .isInstanceOf(IllegalArgumentException.class);
182   }
183
184   @Test
185   public void fail_insert_if_updated_at_is_equal_to_0() {
186     PermissionTemplateCharacteristicDto characteristicDto = new PermissionTemplateCharacteristicDto()
187       .setUuid("uuid")
188       .setPermission(UserRole.USER)
189       .setTemplateUuid("1")
190       .setWithProjectCreator(true)
191       .setCreatedAt(2_000_000_000L);
192     assertThatThrownBy(() -> underTest.insert(dbSession, characteristicDto, "template"))
193       .isInstanceOf(IllegalArgumentException.class);
194
195   }
196
197   @Test
198   public void fail_update_if_uuid_is_null() {
199     PermissionTemplateCharacteristicDto characteristicDto = new PermissionTemplateCharacteristicDto()
200       .setPermission(UserRole.USER)
201       .setTemplateUuid("1")
202       .setWithProjectCreator(true)
203       .setCreatedAt(123_456_789L)
204       .setUpdatedAt(2_000_000_000L);
205     assertThatThrownBy(() -> underTest.update(dbSession, characteristicDto, "template"))
206       .isInstanceOf(NullPointerException.class);
207   }
208
209   @Test
210   public void delete_by_permission_template_uuid() {
211     underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
212         .setUuid("uuid1")
213         .setPermission(UserRole.USER)
214         .setTemplateUuid("1")
215         .setWithProjectCreator(true)
216         .setCreatedAt(123_456_789L)
217         .setUpdatedAt(2_000_000_000L),
218       "template");
219     underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
220         .setUuid("uuid2")
221         .setPermission(UserRole.USER)
222         .setTemplateUuid("2")
223         .setWithProjectCreator(true)
224         .setCreatedAt(123_456_789L)
225         .setUpdatedAt(2_000_000_000L),
226       "template");
227
228     assertThat(underTest.selectByTemplateUuids(dbSession, singletonList("1"))).hasSize(1);
229     assertThat(underTest.selectByTemplateUuids(dbSession, asList("1", "2"))).hasSize(2);
230
231     dbSession.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateUuid("1");
232
233     assertThat(underTest.selectByTemplateUuids(dbSession, singletonList("1"))).isEmpty();
234     assertThat(underTest.selectByTemplateUuids(dbSession, asList("1", "2"))).hasSize(1);
235   }
236 }