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.permission.template;
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;
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;
38 public class PermissionTemplateCharacteristicDaoTest {
40 public DbTester db = DbTester.create(System2.INSTANCE);
41 private final DbSession dbSession = db.getSession();
42 private final PermissionTemplateCharacteristicDao underTest = new PermissionTemplateCharacteristicDao();
45 public void selectByTemplateId_filter_by_template_uuid() {
46 PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
48 .setPermission(UserRole.ADMIN)
50 .setWithProjectCreator(true)
51 .setCreatedAt(1_000_000_000L)
52 .setUpdatedAt(2_000_000_000L),
54 PermissionTemplateCharacteristicDto templatePermission2 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
56 .setPermission(UserRole.USER)
58 .setWithProjectCreator(false)
59 .setCreatedAt(1_000_000_000L)
60 .setUpdatedAt(2_000_000_000L),
62 PermissionTemplateCharacteristicDto templatePermission3 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
64 .setPermission(UserRole.USER)
66 .setWithProjectCreator(false)
67 .setCreatedAt(1_000_000_001L)
68 .setUpdatedAt(2_000_000_000L),
70 PermissionTemplateCharacteristicDto templatePermissionForAnotherTemplate = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
72 .setPermission(UserRole.ADMIN)
73 .setTemplateUuid("42")
74 .setWithProjectCreator(true)
75 .setCreatedAt(1_000_000_000L)
76 .setUpdatedAt(2_000_000_000L),
79 List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateUuids(dbSession, newArrayList("1", "2", "3"));
83 .doesNotContain(templatePermissionForAnotherTemplate.getUuid())
84 .containsExactly(templatePermission1.getUuid(), templatePermission2.getUuid(), templatePermission3.getUuid());
85 assertThat(result.get(0))
86 .isEqualToComparingFieldByField(templatePermission1);
90 public void selectByTemplateId_for_empty_list_of_template_uuid() {
91 List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateUuids(dbSession, emptyList());
93 assertThat(result).isEmpty();
97 public void selectByPermissionAndTemplateId() {
98 PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
100 .setPermission(UserRole.ADMIN)
101 .setTemplateUuid("1")
102 .setWithProjectCreator(true)
103 .setCreatedAt(1_000_000_000L)
104 .setUpdatedAt(2_000_000_000L),
106 underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
108 .setPermission(UserRole.USER)
109 .setTemplateUuid("1")
110 .setWithProjectCreator(false)
111 .setCreatedAt(1_000_000_000L)
112 .setUpdatedAt(2_000_000_000L),
114 underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
116 .setPermission(UserRole.ADMIN)
117 .setTemplateUuid("42")
118 .setWithProjectCreator(true)
119 .setCreatedAt(1_000_000_000L)
120 .setUpdatedAt(2_000_000_000L),
123 Optional<PermissionTemplateCharacteristicDto> result = underTest.selectByPermissionAndTemplateId(dbSession, UserRole.ADMIN, "1");
125 assertThat(result).isPresent();
126 assertThat(result.get()).isEqualToComparingFieldByField(templatePermission1);
130 public void insert() {
131 PermissionTemplateCharacteristicDto expectedResult = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
133 .setPermission(UserRole.USER)
134 .setTemplateUuid("1")
135 .setWithProjectCreator(true)
136 .setCreatedAt(123_456_789L)
137 .setUpdatedAt(2_000_000_000L),
140 PermissionTemplateCharacteristicDto result = dbSession.getMapper(PermissionTemplateCharacteristicMapper.class).selectByUuid(expectedResult.getUuid());
141 assertThat(result.getUuid()).isEqualTo("uuid");
142 assertThat(result).isEqualToComparingFieldByField(expectedResult);
146 public void update_only_change_with_project_creator_and_updated_at() {
147 PermissionTemplateCharacteristicDto insertedDto = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
149 .setPermission(UserRole.USER)
150 .setTemplateUuid("1")
151 .setWithProjectCreator(true)
152 .setCreatedAt(123_456_789L)
153 .setUpdatedAt(2_000_000_000L),
156 underTest.update(dbSession, new PermissionTemplateCharacteristicDto()
158 .setPermission("PERMISSION_ARE_NOT_UPDATABLE")
159 .setTemplateUuid("42")
161 .setWithProjectCreator(false)
162 .setUpdatedAt(3_000_000_000L),
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);
173 public void fail_insert_if_created_at_is_equal_to_0() {
174 PermissionTemplateCharacteristicDto characteristicDto = new PermissionTemplateCharacteristicDto()
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);
185 public void fail_insert_if_updated_at_is_equal_to_0() {
186 PermissionTemplateCharacteristicDto characteristicDto = new PermissionTemplateCharacteristicDto()
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);
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);
210 public void delete_by_permission_template_uuid() {
211 underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
213 .setPermission(UserRole.USER)
214 .setTemplateUuid("1")
215 .setWithProjectCreator(true)
216 .setCreatedAt(123_456_789L)
217 .setUpdatedAt(2_000_000_000L),
219 underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
221 .setPermission(UserRole.USER)
222 .setTemplateUuid("2")
223 .setWithProjectCreator(true)
224 .setCreatedAt(123_456_789L)
225 .setUpdatedAt(2_000_000_000L),
228 assertThat(underTest.selectByTemplateUuids(dbSession, singletonList("1"))).hasSize(1);
229 assertThat(underTest.selectByTemplateUuids(dbSession, asList("1", "2"))).hasSize(2);
231 dbSession.getMapper(PermissionTemplateCharacteristicMapper.class).deleteByTemplateUuid("1");
233 assertThat(underTest.selectByTemplateUuids(dbSession, singletonList("1"))).isEmpty();
234 assertThat(underTest.selectByTemplateUuids(dbSession, asList("1", "2"))).hasSize(1);