]> source.dussan.org Git - sonarqube.git/blob
9d5f97bcedd49cd42d1a01d66bbad4ebce356c2c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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
21 package org.sonar.db.permission.template;
22
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;
32
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;
36
37 public class PermissionTemplateCharacteristicDaoTest {
38   @Rule
39   public ExpectedException expectedException = ExpectedException.none();
40   @Rule
41   public DbTester db = DbTester.create(System2.INSTANCE);
42   DbSession dbSession = db.getSession();
43   PermissionTemplateCharacteristicMapper mapper = dbSession.getMapper(PermissionTemplateCharacteristicMapper.class);
44
45   PermissionTemplateCharacteristicDao underTest = new PermissionTemplateCharacteristicDao();
46
47   @Test
48   public void selectByTemplateId_filter_by_template_id() {
49     PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
50       .setPermission(UserRole.ADMIN)
51       .setTemplateId(1L)
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)
57       .setTemplateId(2L)
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)
63       .setTemplateId(42L)
64       .setWithProjectCreator(true)
65       .setCreatedAt(1_000_000_000L)
66       .setUpdatedAt(2_000_000_000L));
67
68     List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateIds(dbSession, newArrayList(1L, 2L));
69     assertThat(result)
70       .hasSize(2)
71       .extracting("id")
72       .doesNotContain(templatePermissionForAnotherTemplate.getId())
73       .containsOnly(templatePermission1.getId(), templatePermission2.getId());
74     assertThat(result.get(0))
75       .isEqualToComparingFieldByField(templatePermission1);
76   }
77
78   @Test
79   public void selectByTemplateId_for_empty_list_of_template_id() {
80     List<PermissionTemplateCharacteristicDto> result = underTest.selectByTemplateIds(dbSession, emptyList());
81
82     assertThat(result).isEmpty();
83   }
84
85   @Test
86   public void selectByPermissionAndTemplateId() {
87     PermissionTemplateCharacteristicDto templatePermission1 = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
88       .setPermission(UserRole.ADMIN)
89       .setTemplateId(1L)
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)
95       .setTemplateId(1L)
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)
101       .setTemplateId(42L)
102       .setWithProjectCreator(true)
103       .setCreatedAt(1_000_000_000L)
104       .setUpdatedAt(2_000_000_000L));
105
106     Optional<PermissionTemplateCharacteristicDto> result = underTest.selectByPermissionAndTemplateId(dbSession, UserRole.ADMIN, 1L);
107
108     assertThat(result).isPresent();
109     assertThat(result.get()).isEqualToComparingFieldByField(templatePermission1);
110   }
111
112   @Test
113   public void insert() {
114     PermissionTemplateCharacteristicDto expectedResult = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
115       .setPermission(UserRole.USER)
116       .setTemplateId(1L)
117       .setWithProjectCreator(true)
118       .setCreatedAt(123_456_789L)
119       .setUpdatedAt(2_000_000_000L));
120
121     PermissionTemplateCharacteristicDto result = mapper.selectById(expectedResult.getId());
122     assertThat(result.getId()).isNotNull();
123     assertThat(result).isEqualToComparingFieldByField(expectedResult);
124   }
125
126   @Test
127   public void update_only_change_with_project_creator_and_updated_at() {
128     PermissionTemplateCharacteristicDto insertedDto = underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
129       .setPermission(UserRole.USER)
130       .setTemplateId(1L)
131       .setWithProjectCreator(true)
132       .setCreatedAt(123_456_789L)
133       .setUpdatedAt(2_000_000_000L));
134
135     underTest.update(dbSession, new PermissionTemplateCharacteristicDto()
136       .setId(insertedDto.getId())
137       .setPermission("PERMISSION_ARE_NOT_UPDATABLE")
138       .setTemplateId(42L)
139       .setCreatedAt(42L)
140       .setWithProjectCreator(false)
141       .setUpdatedAt(3_000_000_000L));
142
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);
148   }
149
150   @Test
151   public void fail_insert_if_created_at_is_equal_to_0() {
152     expectedException.expect(IllegalArgumentException.class);
153
154     underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
155       .setPermission(UserRole.USER)
156       .setTemplateId(1L)
157       .setWithProjectCreator(true)
158       .setUpdatedAt(2_000_000_000L));
159   }
160   @Test
161   public void fail_insert_if_updated_at_is_equal_to_0() {
162     expectedException.expect(IllegalArgumentException.class);
163
164     underTest.insert(dbSession, new PermissionTemplateCharacteristicDto()
165       .setPermission(UserRole.USER)
166       .setTemplateId(1L)
167       .setWithProjectCreator(true)
168       .setCreatedAt(2_000_000_000L));
169   }
170
171   @Test
172   public void fail_update_if_id_is_null() {
173     expectedException.expect(NullPointerException.class);
174
175     underTest.update(dbSession, new PermissionTemplateCharacteristicDto()
176       .setPermission(UserRole.USER)
177       .setTemplateId(1L)
178       .setWithProjectCreator(true)
179       .setCreatedAt(123_456_789L)
180       .setUpdatedAt(2_000_000_000L));
181   }
182 }