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.almintegration.ws.gitlab;
22 import java.util.Optional;
23 import java.util.stream.IntStream;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.alm.client.gitlab.GitlabHttpClient;
28 import org.sonar.alm.client.gitlab.Project;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.i18n.I18n;
31 import org.sonar.core.util.SequenceUuidFactory;
32 import org.sonar.core.util.UuidFactory;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.alm.setting.AlmSettingDto;
35 import org.sonar.db.project.ProjectDto;
36 import org.sonar.db.user.UserDto;
37 import org.sonar.server.almintegration.ws.ImportHelper;
38 import org.sonar.server.component.ComponentUpdater;
39 import org.sonar.server.es.TestProjectIndexers;
40 import org.sonar.server.favorite.FavoriteUpdater;
41 import org.sonar.server.permission.PermissionTemplateService;
42 import org.sonar.server.project.ProjectDefaultVisibility;
43 import org.sonar.server.project.Visibility;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.ws.WsActionTester;
46 import org.sonarqube.ws.Projects;
48 import static java.util.stream.Collectors.joining;
49 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
50 import static org.assertj.core.api.Assertions.assertThat;
51 import static org.mockito.ArgumentMatchers.any;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.verify;
54 import static org.mockito.Mockito.when;
55 import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
56 import static org.sonar.server.tester.UserSessionRule.standalone;
58 public class ImportGitLabProjectActionTest {
60 private final System2 system2 = mock(System2.class);
63 public UserSessionRule userSession = standalone();
66 public DbTester db = DbTester.create(system2);
68 private final ComponentUpdater componentUpdater = new ComponentUpdater(db.getDbClient(), mock(I18n.class), System2.INSTANCE,
69 mock(PermissionTemplateService.class), new FavoriteUpdater(db.getDbClient()), new TestProjectIndexers(), new SequenceUuidFactory());
71 private final GitlabHttpClient gitlabHttpClient = mock(GitlabHttpClient.class);
72 private final UuidFactory uuidFactory = mock(UuidFactory.class);
73 private final ImportHelper importHelper = new ImportHelper(db.getDbClient(), userSession);
74 private final ProjectDefaultVisibility projectDefaultVisibility = mock(ProjectDefaultVisibility.class);
75 private final ImportGitLabProjectAction importGitLabProjectAction = new ImportGitLabProjectAction(
76 db.getDbClient(), userSession, projectDefaultVisibility, gitlabHttpClient, componentUpdater, uuidFactory, importHelper);
77 private final WsActionTester ws = new WsActionTester(importGitLabProjectAction);
80 public void before() {
81 when(projectDefaultVisibility.get(any())).thenReturn(Visibility.PRIVATE);
85 public void import_project() {
86 UserDto user = db.users().insertUser();
87 userSession.logIn(user).addPermission(PROVISION_PROJECTS);
88 AlmSettingDto almSetting = db.almSettings().insertGitlabAlmSetting();
89 db.almPats().insert(dto -> {
90 dto.setAlmSettingUuid(almSetting.getUuid());
91 dto.setUserUuid(user.getUuid());
92 dto.setPersonalAccessToken("PAT");
94 Project project = getGitlabProject();
95 when(gitlabHttpClient.getProject(any(), any(), any())).thenReturn(project);
96 when(uuidFactory.create()).thenReturn("uuid");
98 Projects.CreateWsResponse response = ws.newRequest()
99 .setParam("almSetting", almSetting.getKey())
100 .setParam("gitlabProjectId", "12345")
101 .executeProtobuf(Projects.CreateWsResponse.class);
103 verify(gitlabHttpClient).getProject(almSetting.getUrl(), "PAT", 12345L);
105 Projects.CreateWsResponse.Project result = response.getProject();
106 assertThat(result.getKey()).isEqualTo(project.getPathWithNamespace() + "_uuid");
107 assertThat(result.getName()).isEqualTo(project.getName());
109 Optional<ProjectDto> projectDto = db.getDbClient().projectDao().selectProjectByKey(db.getSession(), result.getKey());
110 assertThat(projectDto).isPresent();
111 assertThat(db.getDbClient().projectAlmSettingDao().selectByProject(db.getSession(), projectDto.get())).isPresent();
115 public void generate_project_key_less_than_250() {
116 String name = "abcdeert";
117 assertThat(importGitLabProjectAction.generateProjectKey(name, "uuid")).isEqualTo("abcdeert_uuid");
121 public void generate_project_key_equal_250() {
122 String name = IntStream.range(0, 245).mapToObj(i -> "a").collect(joining());
123 String projectKey = importGitLabProjectAction.generateProjectKey(name, "uuid");
124 assertThat(projectKey)
126 .isEqualTo(name + "_uuid");
131 public void generate_project_key_more_than_250() {
132 String name = IntStream.range(0, 250).mapToObj(i -> "a").collect(joining());
133 String projectKey = importGitLabProjectAction.generateProjectKey(name, "uuid");
134 assertThat(projectKey)
136 .isEqualTo(name.substring(5) + "_uuid");
140 public void generate_project_key_containing_slash() {
141 String name = "a/b/c";
142 assertThat(importGitLabProjectAction.generateProjectKey(name, "uuid")).isEqualTo("a_b_c_uuid");
145 private Project getGitlabProject() {
146 return new Project(randomAlphanumeric(5), randomAlphanumeric(5));