You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DefaultBoundProjectsControllerTest.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info 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. package org.sonar.server.v2.api.projects.controller;
  21. import org.junit.Rule;
  22. import org.junit.jupiter.api.Test;
  23. import org.sonar.db.alm.setting.ProjectAlmSettingDto;
  24. import org.sonar.db.project.ProjectDto;
  25. import org.sonar.server.common.project.ImportProjectRequest;
  26. import org.sonar.server.common.project.ImportProjectService;
  27. import org.sonar.server.common.project.ImportedProject;
  28. import org.sonar.server.tester.UserSessionRule;
  29. import org.sonar.server.v2.api.ControllerTester;
  30. import org.springframework.http.MediaType;
  31. import org.springframework.test.web.servlet.MockMvc;
  32. import static org.mockito.ArgumentMatchers.any;
  33. import static org.mockito.Mockito.mock;
  34. import static org.mockito.Mockito.when;
  35. import static org.sonar.db.permission.GlobalPermission.PROVISION_PROJECTS;
  36. import static org.sonar.server.v2.WebApiEndpoints.BOUND_PROJECTS_ENDPOINT;
  37. import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
  38. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
  39. import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  40. class DefaultBoundProjectsControllerTest {
  41. private static final String PROJECT_UUID = "project-uuid";
  42. private static final String PROJECT_ALM_SETTING_UUID = "project-alm-setting-uuid";
  43. private static final String DOP_REPOSITORY_ID = "dop-repository-id";
  44. private static final String DOP_PROJECT_ID = "dop-project-id";
  45. private static final String PROJECT_KEY = "project-key";
  46. private static final String PROJECT_NAME = "project-name";
  47. private static final String ALM_SETTING_ID = "alm-setting-id";
  48. @Rule
  49. public UserSessionRule userSession = UserSessionRule.standalone();
  50. private final ImportProjectService importProjectService = mock();
  51. private final MockMvc mockMvc = ControllerTester.getMockMvc(
  52. new DefaultBoundProjectsController(
  53. userSession, importProjectService));
  54. @Test
  55. void createBoundProject_whenUserDoesntHaveCreateProjectPermission_returnsForbidden() throws Exception {
  56. userSession.logIn();
  57. mockMvc.perform(
  58. post(BOUND_PROJECTS_ENDPOINT)
  59. .contentType(MediaType.APPLICATION_JSON)
  60. .content("""
  61. {
  62. "projectKey": "project-key",
  63. "projectName": "project-name",
  64. "devOpsPlatformSettingId": "alm-setting-id",
  65. "repositoryIdentifier": "repository-id",
  66. "monorepo": true
  67. }
  68. """)
  69. )
  70. .andExpect(status().isForbidden());
  71. }
  72. @Test
  73. void createdBoundProject_whenImportProjectServiceThrowsIllegalArgumentExceptions_returnsBadRequest() throws Exception {
  74. userSession.logIn().addPermission(PROVISION_PROJECTS);
  75. when(importProjectService.importProject(any()))
  76. .thenThrow(new IllegalArgumentException("Error message"));
  77. mockMvc.perform(
  78. post(BOUND_PROJECTS_ENDPOINT)
  79. .contentType(MediaType.APPLICATION_JSON)
  80. .content("""
  81. {
  82. "projectKey": "project-key",
  83. "projectName": "project-name",
  84. "devOpsPlatformSettingId": "alm-setting-id",
  85. "repositoryIdentifier": "repository-id",
  86. "monorepo": true
  87. }
  88. """))
  89. .andExpectAll(
  90. status().isBadRequest(),
  91. content().json("""
  92. {
  93. "message": "Error message"
  94. }
  95. """));
  96. }
  97. @Test
  98. void createBoundProject_whenProjectIsCreatedSuccessfully_returnResponse() throws Exception {
  99. userSession.logIn().addPermission(PROVISION_PROJECTS);
  100. ProjectDto projectDto = mock(ProjectDto.class);
  101. when(projectDto.getUuid()).thenReturn(PROJECT_UUID);
  102. ProjectAlmSettingDto projectAlmSettingDto = mock(ProjectAlmSettingDto.class);
  103. when(projectAlmSettingDto.getUuid()).thenReturn(PROJECT_ALM_SETTING_UUID);
  104. when(importProjectService.importProject(new ImportProjectRequest(
  105. PROJECT_KEY,
  106. PROJECT_NAME,
  107. ALM_SETTING_ID,
  108. DOP_REPOSITORY_ID,
  109. DOP_PROJECT_ID,
  110. "NUMBER_OF_DAYS",
  111. "10",
  112. true)))
  113. .thenReturn(new ImportedProject(
  114. projectDto,
  115. projectAlmSettingDto));
  116. mockMvc.perform(
  117. post(BOUND_PROJECTS_ENDPOINT)
  118. .contentType(MediaType.APPLICATION_JSON)
  119. .content("""
  120. {
  121. "projectKey": "project-key",
  122. "projectName": "project-name",
  123. "devOpsPlatformSettingId": "alm-setting-id",
  124. "repositoryIdentifier": "dop-repository-id",
  125. "projectIdentifier": "dop-project-id",
  126. "newCodeDefinitionType": "NUMBER_OF_DAYS",
  127. "newCodeDefinitionValue": "10",
  128. "monorepo": true
  129. }
  130. """))
  131. .andExpectAll(
  132. status().isCreated(),
  133. content().json("""
  134. {
  135. "projectId": "project-uuid",
  136. "bindingId": "project-alm-setting-uuid"
  137. }
  138. """));
  139. }
  140. }