3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.
21 package org.sonar.db.permission.template;
23 import java.util.List;
24 import java.util.Optional;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.utils.System2;
29 import org.sonar.api.web.UserRole;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.DbTester;
33 import static com.google.common.collect.Lists.newArrayList;
34 import static java.util.Collections.emptyList;
35 import static org.assertj.core.api.Assertions.assertThat;
37 public class PermissionTemplateCharacteristicDaoTest {
39 public ExpectedException expectedException = ExpectedException.none();
41 public DbTester db = DbTester.create(System2.INSTANCE);
42 DbSession dbSession = db.getSession();
43 PermissionTemplateCharacteristicMapper mapper = dbSession.getMapper(PermissionTemplateCharacteristicMapper.class);
45 PermissionTemplateCharacteristicDao underTest = new PermissionTemplateCharacteristicDao();
48 public void selectByTemplateId_filter_by_template_id() {
49 PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
50 .setPermission(UserRole.ADMIN)
52 .setWithProjectCreator(true)
53 .setCreatedAt(1_000_000_000L)
54 .setUpdatedAt(2_000_000_000L));
55 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));
61 PermissionTemplateCharacteristicDto templatePermissionForAnotherTemplate = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
62 .setPermission(UserRole.ADMIN)
64 .setWithProjectCreator(true)
65 .setCreatedAt(1_000_000_000L)
66 .setUpdatedAt(2_000_000_000L));
68 List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateIds(dbSession, newArrayList(1L, 2L));
72 .doesNotContain(templatePermissionForAnotherTemplate.getId())
73 .containsOnly(templatePermission1.getId(), templatePermission2.getId());
74 assertThat(result.get(0))
75 .isEqualToComparingFieldByField(templatePermission1);
79 public void selectByTemplateId_for_empty_list_of_template_id() {
80 List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateIds(dbSession, emptyList());
82 assertThat(result).isEmpty();
86 public void selectByPermissionAndTemplateId() {
87 PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
88 .setPermission(UserRole.ADMIN)
90 .setWithProjectCreator(true)
91 .setCreatedAt(1_000_000_000L)
92 .setUpdatedAt(2_000_000_000L));
93 underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
94 .setPermission(UserRole.USER)
96 .setWithProjectCreator(false)
97 .setCreatedAt(1_000_000_000L)
98 .setUpdatedAt(2_000_000_000L));
99 underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
100 .setPermission(UserRole.ADMIN)
102 .setWithProjectCreator(true)
103 .setCreatedAt(1_000_000_000L)
104 .setUpdatedAt(2_000_000_000L));
106 Optional<PermissionTemplateCharacteristicDto> result = underTest.selectByPermissionAndTemplateId(dbSession, UserRole.ADMIN, 1L);
108 assertThat(result).isPresent();
109 assertThat(result.get()).isEqualToComparingFieldByField(templatePermission1);
113 public void insert() {
114 PermissionTemplateCharacteristicDto expectedResult = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
115 .setPermission(UserRole.USER)
117 .setWithProjectCreator(true)
118 .setCreatedAt(123_456_789L)
119 .setUpdatedAt(2_000_000_000L));
121 PermissionTemplateCharacteristicDto result = mapper.selectById(expectedResult.getId());
122 assertThat(result.getId()).isNotNull();
123 assertThat(result).isEqualToComparingFieldByField(expectedResult);
127 public void update_only_change_with_project_creator_and_updated_at() {
128 PermissionTemplateCharacteristicDto insertedDto = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
129 .setPermission(UserRole.USER)
131 .setWithProjectCreator(true)
132 .setCreatedAt(123_456_789L)
133 .setUpdatedAt(2_000_000_000L));
135 underTest.update(dbSession, new PermissionTemplateCharacteristicDto()
136 .setId(insertedDto.getId())
137 .setPermission("PERMISSION_ARE_NOT_UPDATABLE")
140 .setWithProjectCreator(false)
141 .setUpdatedAt(3_000_000_000L));
143 PermissionTemplateCharacteristicDto result = mapper.selectById(insertedDto.getId());
144 assertThat(result).extracting("id", "permission", "templateId", "createdAt")
145 .containsExactly(insertedDto.getId(), insertedDto.getPermission(), insertedDto.getTemplateId(), insertedDto.getCreatedAt());
146 assertThat(result).extracting("withProjectCreator", "updatedAt")
147 .containsExactly(false, 3_000_000_000L);
151 public void fail_insert_if_created_at_is_equal_to_0() {
152 expectedException.expect(IllegalArgumentException.class);
154 underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
155 .setPermission(UserRole.USER)
157 .setWithProjectCreator(true)
158 .setUpdatedAt(2_000_000_000L));
161 public void fail_insert_if_updated_at_is_equal_to_0() {
162 expectedException.expect(IllegalArgumentException.class);
164 underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
165 .setPermission(UserRole.USER)
167 .setWithProjectCreator(true)
168 .setCreatedAt(2_000_000_000L));
172 public void fail_update_if_id_is_null() {
173 expectedException.expect(NullPointerException.class);
175 underTest.update(dbSession, new PermissionTemplateCharacteristicDto()
176 .setPermission(UserRole.USER)
178 .setWithProjectCreator(true)
179 .setCreatedAt(123_456_789L)
180 .setUpdatedAt(2_000_000_000L));