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.server.project.ws;
22 import java.util.stream.IntStream;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.measures.Metric.Level;
27 import org.sonar.api.measures.Metric.ValueType;
28 import org.sonar.api.server.ws.WebService.Param;
29 import org.sonar.api.utils.DateUtils;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.web.UserRole;
32 import org.sonar.core.util.Uuids;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.component.ComponentDto;
37 import org.sonar.db.component.ComponentTesting;
38 import org.sonar.db.component.ProjectLinkDto;
39 import org.sonar.db.component.SnapshotDto;
40 import org.sonar.db.metric.MetricDto;
41 import org.sonar.db.user.GroupDto;
42 import org.sonar.db.user.UserDto;
43 import org.sonar.server.exceptions.UnauthorizedException;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.ws.WsActionTester;
46 import org.sonarqube.ws.Projects.SearchMyProjectsWsResponse;
47 import org.sonarqube.ws.Projects.SearchMyProjectsWsResponse.Project;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.assertj.core.api.Assertions.assertThatThrownBy;
51 import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY;
52 import static org.sonar.db.component.ComponentTesting.newPrivateProjectDto;
53 import static org.sonar.db.component.SnapshotTesting.newAnalysis;
54 import static org.sonar.db.measure.MeasureTesting.newLiveMeasure;
55 import static org.sonar.db.metric.MetricTesting.newMetricDto;
56 import static org.sonar.db.user.UserTesting.newUserDto;
57 import static org.sonar.test.JsonAssert.assertJson;
59 public class SearchMyProjectsActionTest {
62 public final UserSessionRule userSession = UserSessionRule.standalone();
64 public final DbTester db = DbTester.create(System2.INSTANCE);
66 private final DbClient dbClient = db.getDbClient();
67 private final DbSession dbSession = db.getSession();
68 private WsActionTester ws;
70 private MetricDto alertStatusMetric;
74 user = db.users().insertUser();
75 userSession.logIn(user);
76 alertStatusMetric = dbClient.metricDao().insert(dbSession, newMetricDto().setKey(ALERT_STATUS_KEY).setValueType(ValueType.LEVEL.name()));
79 ws = new WsActionTester(new SearchMyProjectsAction(dbClient, userSession));
83 public void search_json_example() {
84 ComponentDto jdk7 = insertJdk7();
85 ComponentDto cLang = insertClang();
86 db.componentLinks().insertProvidedLink(jdk7, l -> l.setHref("http://www.oracle.com").setType(ProjectLinkDto.TYPE_HOME_PAGE).setName("Home"));
87 db.componentLinks().insertProvidedLink(jdk7, l -> l.setHref("http://download.java.net/openjdk/jdk8/").setType(ProjectLinkDto.TYPE_SOURCES).setName("Sources"));
88 long oneTime = DateUtils.parseDateTime("2016-06-10T13:17:53+0000").getTime();
89 long anotherTime = DateUtils.parseDateTime("2016-06-11T14:25:53+0000").getTime();
90 SnapshotDto jdk7Snapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(jdk7).setCreatedAt(oneTime));
91 SnapshotDto cLangSnapshot = dbClient.snapshotDao().insert(dbSession, newAnalysis(cLang).setCreatedAt(anotherTime));
92 dbClient.liveMeasureDao().insert(dbSession, newLiveMeasure(jdk7, alertStatusMetric).setData(Level.ERROR.name()));
93 dbClient.liveMeasureDao().insert(dbSession, newLiveMeasure(cLang, alertStatusMetric).setData(Level.OK.name()));
94 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk7);
95 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, cLang);
97 System.setProperty("user.timezone", "UTC");
99 String result = ws.newRequest().execute().getInput();
101 assertJson(result).isSimilarTo(getClass().getResource("search_my_projects-example.json"));
105 public void return_only_current_user_projects() {
106 ComponentDto jdk7 = insertJdk7();
107 ComponentDto cLang = insertClang();
108 UserDto anotherUser = db.users().insertUser(newUserDto());
109 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk7);
110 db.users().insertProjectPermissionOnUser(anotherUser, UserRole.ADMIN, cLang);
112 SearchMyProjectsWsResponse result = callWs();
114 assertThat(result.getProjectsCount()).isEqualTo(1);
118 public void return_only_first_1000_projects() {
119 IntStream.range(0, 1_010).forEach(i -> {
120 ComponentDto project = db.components().insertComponent(newPrivateProjectDto());
121 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, project);
124 SearchMyProjectsWsResponse result = callWs();
126 assertThat(result.getPaging().getTotal()).isEqualTo(1_000);
130 public void sort_projects_by_name() {
131 ComponentDto b_project = db.components().insertComponent(ComponentTesting.newPrivateProjectDto().setName("B_project_name"));
132 ComponentDto c_project = db.components().insertComponent(ComponentTesting.newPrivateProjectDto().setName("c_project_name"));
133 ComponentDto a_project = db.components().insertComponent(ComponentTesting.newPrivateProjectDto().setName("A_project_name"));
135 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, b_project);
136 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, a_project);
137 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, c_project);
139 SearchMyProjectsWsResponse result = callWs();
141 assertThat(result.getProjectsCount()).isEqualTo(3);
145 public void paginate_projects() {
146 for (int i = 0; i < 10; i++) {
147 ComponentDto project = db.components().insertComponent(ComponentTesting.newPrivateProjectDto().setName("project-" + i));
148 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, project);
151 SearchMyProjectsWsResponse result = ws.newRequest()
152 .setParam(Param.PAGE, "2")
153 .setParam(Param.PAGE_SIZE, "3")
154 .executeProtobuf(SearchMyProjectsWsResponse.class);
156 assertThat(result.getProjectsCount()).isEqualTo(3);
157 assertThat(result.getProjectsList()).extracting(Project::getName).containsExactly("project-3", "project-4", "project-5");
161 public void return_only_projects_when_user_is_admin() {
162 ComponentDto jdk7 = insertJdk7();
163 ComponentDto clang = insertClang();
165 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk7);
166 db.users().insertProjectPermissionOnUser(user, UserRole.ISSUE_ADMIN, clang);
168 SearchMyProjectsWsResponse result = callWs();
170 assertThat(result.getProjectsCount()).isEqualTo(1);
174 public void does_not_return_views() {
175 ComponentDto jdk7 = insertJdk7();
176 ComponentDto view = insertPortfolio();
178 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk7);
179 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, view);
181 SearchMyProjectsWsResponse result = callWs();
183 assertThat(result.getProjectsCount()).isEqualTo(1);
187 public void does_not_return_branches() {
188 ComponentDto project = db.components().insertPublicProject();
189 ComponentDto branch = db.components().insertProjectBranch(project);
190 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, project);
192 SearchMyProjectsWsResponse result = callWs();
194 assertThat(result.getProjectsList())
195 .extracting(Project::getKey)
196 .containsExactlyInAnyOrder(project.getDbKey());
200 public void admin_via_groups() {
201 ComponentDto jdk7 = insertJdk7();
202 ComponentDto cLang = insertClang();
204 GroupDto group = db.users().insertGroup();
205 db.users().insertMember(group, user);
207 db.users().insertProjectPermissionOnGroup(group, UserRole.ADMIN, jdk7);
208 db.users().insertProjectPermissionOnGroup(group, UserRole.USER, cLang);
210 SearchMyProjectsWsResponse result = callWs();
212 assertThat(result.getProjectsCount()).isEqualTo(1);
216 public void admin_via_groups_and_users() {
217 ComponentDto jdk7 = insertJdk7();
218 ComponentDto cLang = insertClang();
219 ComponentDto sonarqube = db.components().insertPrivateProject();
221 GroupDto group = db.users().insertGroup();
222 db.users().insertMember(group, user);
224 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, jdk7);
225 db.users().insertProjectPermissionOnGroup(group, UserRole.ADMIN, cLang);
226 // admin via group and user
227 db.users().insertProjectPermissionOnUser(user, UserRole.ADMIN, sonarqube);
228 db.users().insertProjectPermissionOnGroup(group, UserRole.ADMIN, sonarqube);
230 SearchMyProjectsWsResponse result = callWs();
232 assertThat(result.getProjectsCount()).isEqualTo(3);
236 public void empty_response() {
237 String result = ws.newRequest().execute().getInput();
238 assertJson(result).isSimilarTo("{\"projects\":[]}");
242 public void fail_if_not_authenticated() {
243 userSession.anonymous();
245 assertThatThrownBy(this::callWs)
246 .isInstanceOf(UnauthorizedException.class);
249 private ComponentDto insertClang() {
250 return db.components().insertComponent(newPrivateProjectDto(Uuids.UUID_EXAMPLE_01)
255 private ComponentDto insertJdk7() {
256 return db.components().insertComponent(newPrivateProjectDto(Uuids.UUID_EXAMPLE_02)
258 .setDbKey("net.java.openjdk:jdk7")
259 .setDescription("JDK"));
262 private ComponentDto insertPortfolio() {
263 return db.components().insertPublicPortfolio(p -> p.setUuid("752d8bfd-420c-4a83-a4e5-8ab19b13c8fc")
268 private SearchMyProjectsWsResponse callWs() {
269 return ws.newRequest()
270 .executeProtobuf(SearchMyProjectsWsResponse.class);