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.
20 package org.sonar.server.qualityprofile;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.ClassRule;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.permission.GlobalPermissions;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbSession;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.permission.PermissionRepository;
33 import org.sonar.db.qualityprofile.QualityProfileDto;
34 import org.sonar.db.user.UserDto;
35 import org.sonar.server.tester.MockUserSession;
36 import org.sonar.server.tester.ServerTester;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.user.UserSession;
40 import static org.assertj.core.api.Assertions.assertThat;
42 public class QProfileProjectOperationsMediumTest {
45 public static ServerTester tester = new ServerTester().withEsIndexes();
47 public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
51 QProfileFactory factory;
52 QProfileProjectOperations projectOperations;
54 QualityProfileDto profile;
55 static final String PROJECT_KEY = "SonarQube";
56 static final String PROJECT_UUID = "ABCD";
58 UserSession authorizedProfileAdminUserSession = new MockUserSession("john").setName("John").setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
59 UserSession authorizedProjectAdminUserSession = new MockUserSession("john").setName("John").addProjectPermissions(UserRole.ADMIN, PROJECT_KEY);
62 public void before() {
63 tester.clearDbAndIndexes();
64 db = tester.get(DbClient.class);
65 dbSession = db.openSession(false);
66 factory = tester.get(QProfileFactory.class);
67 projectOperations = tester.get(QProfileProjectOperations.class);
69 project = new ComponentDto()
70 .setUuid(PROJECT_UUID)
73 .setLongName("SonarQube")
77 db.componentDao().insert(dbSession, project);
79 profile = QProfileTesting.newXooP1();
80 db.qualityProfileDao().insert(dbSession, profile);
91 public void add_project() {
92 projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
94 assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
98 public void add_project_with_only_project_admin_permission() {
99 projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProjectAdminUserSession);
101 assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
105 public void remove_project_from_project_id() {
106 projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
107 assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
109 projectOperations.removeProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
110 assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNull();
114 public void remove_project_from_language() {
115 projectOperations.addProject(profile.getKey(), project.uuid(), authorizedProfileAdminUserSession);
116 assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNotNull();
118 projectOperations.removeProject(profile.getLanguage(), project.getId(), authorizedProfileAdminUserSession);
119 assertThat(factory.getByProjectAndLanguage(PROJECT_KEY, profile.getLanguage())).isNull();
123 public void remove_all_projects() {
124 ComponentDto project1 = new ComponentDto()
128 .setLongName("project1")
132 ComponentDto project2 = new ComponentDto()
136 .setLongName("project2")
140 db.componentDao().insert(dbSession, project1);
141 db.componentDao().insert(dbSession, project2);
143 // Create a user having user permission on the two projects and the global quality profile admin permission
144 UserDto user = new UserDto().setLogin("john").setName("John").setEmail("jo@hn.com").setCreatedAt(System.currentTimeMillis()).setUpdatedAt(System.currentTimeMillis());
145 db.userDao().insert(dbSession, user);
146 tester.get(PermissionRepository.class).insertUserPermission(project1.getId(), user.getId(), UserRole.USER, dbSession);
147 tester.get(PermissionRepository.class).insertUserPermission(project2.getId(), user.getId(), UserRole.USER, dbSession);
148 UserSession userSession = userSessionRule.login("john").setUserId(user.getId().intValue()).setName("John")
149 .setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
153 projectOperations.addProject(profile.getKey(), project1.uuid(), userSession);
154 projectOperations.addProject(profile.getKey(), project2.uuid(), userSession);
155 assertThat(tester.get(QProfileProjectLookup.class).projects(profile.getId())).hasSize(2);
157 projectOperations.removeAllProjects(profile.getKey(), userSession);
158 assertThat(tester.get(QProfileProjectLookup.class).projects(profile.getId())).isEmpty();