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.

ProjectSearchTest.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.sonarqube.tests.project;
  21. import com.sonar.orchestrator.Orchestrator;
  22. import java.util.Date;
  23. import org.apache.commons.lang.time.DateUtils;
  24. import org.junit.ClassRule;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.sonarqube.qa.util.Tester;
  28. import org.sonarqube.ws.Organizations;
  29. import org.sonarqube.ws.Projects.CreateWsResponse;
  30. import org.sonarqube.ws.Projects.SearchWsResponse;
  31. import org.sonarqube.ws.Projects.SearchWsResponse.Component;
  32. import org.sonarqube.ws.client.GetRequest;
  33. import org.sonarqube.ws.client.projects.SearchRequest;
  34. import static java.util.Collections.singletonList;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static util.ItUtils.formatDate;
  37. import static util.ItUtils.runProjectAnalysis;
  38. public class ProjectSearchTest {
  39. @ClassRule
  40. public static Orchestrator orchestrator = ProjectSuite.ORCHESTRATOR;
  41. @Rule
  42. public Tester tester = new Tester(orchestrator);
  43. @Test
  44. public void search_old_projects() {
  45. Organizations.Organization organization = tester.organizations().generate();
  46. CreateWsResponse.Project oldProject = tester.projects().provision(organization);
  47. CreateWsResponse.Project recentProject = tester.projects().provision(organization);
  48. Date now = new Date();
  49. Date oneYearAgo = DateUtils.addDays(now, -365);
  50. Date moreThanOneYearAgo = DateUtils.addDays(now, -366);
  51. analyzeProject(oldProject.getKey(), moreThanOneYearAgo, organization.getKey());
  52. analyzeProject(recentProject.getKey(), now, organization.getKey());
  53. SearchWsResponse result = tester.wsClient().projects().search(new SearchRequest()
  54. .setOrganization(organization.getKey())
  55. .setQualifiers(singletonList("TRK"))
  56. .setAnalyzedBefore(formatDate(oneYearAgo)));
  57. assertThat(result.getComponentsList()).extracting(Component::getKey).containsExactlyInAnyOrder(oldProject.getKey());
  58. }
  59. @Test
  60. public void search_on_key_query_partial_match_case_insensitive() {
  61. Organizations.Organization organization = tester.organizations().generate();
  62. CreateWsResponse.Project lowerCaseProject = tester.projects().provision(organization, p -> p.setProject("project-key"));
  63. CreateWsResponse.Project upperCaseProject = tester.projects().provision(organization, p -> p.setProject("PROJECT-KEY"));
  64. CreateWsResponse.Project anotherProject = tester.projects().provision(organization, p -> p.setProject("another-project"));
  65. analyzeProject(lowerCaseProject.getKey(), organization.getKey());
  66. analyzeProject(upperCaseProject.getKey(), organization.getKey());
  67. analyzeProject(anotherProject.getKey(), organization.getKey());
  68. SearchWsResponse result = tester.wsClient().projects().search(new SearchRequest()
  69. .setOrganization(organization.getKey())
  70. .setQualifiers(singletonList("TRK"))
  71. .setQ("JeCt-K"));
  72. assertThat(result.getComponentsList()).extracting(Component::getKey)
  73. .containsExactlyInAnyOrder(lowerCaseProject.getKey(), upperCaseProject.getKey())
  74. .doesNotContain(anotherProject.getKey());
  75. }
  76. @Test
  77. public void search_provisioned_projects() {
  78. Organizations.Organization organization = tester.organizations().generate();
  79. CreateWsResponse.Project firstProvisionedProject = tester.projects().provision(organization);
  80. CreateWsResponse.Project secondProvisionedProject = tester.projects().provision(organization);
  81. CreateWsResponse.Project analyzedProject = tester.projects().provision(organization);
  82. analyzeProject(analyzedProject.getKey(), organization.getKey());
  83. String result = tester.wsClient().wsConnector().call(new GetRequest("api/projects/provisioned")
  84. .setParam("organization", organization.getKey()))
  85. .failIfNotSuccessful().content();
  86. SearchWsResponse searchResult = tester.wsClient().projects().search(new SearchRequest()
  87. .setQualifiers(singletonList("TRK"))
  88. .setOrganization(organization.getKey())
  89. .setOnProvisionedOnly("true"));
  90. assertThat(result).contains(firstProvisionedProject.getKey(), secondProvisionedProject.getKey()).doesNotContain(analyzedProject.getKey());
  91. assertThat(searchResult.getComponentsList()).extracting(Component::getKey)
  92. .containsOnly(firstProvisionedProject.getKey(), secondProvisionedProject.getKey())
  93. .doesNotContain(analyzedProject.getKey());
  94. }
  95. private void analyzeProject(String projectKey, Date analysisDate, String organizationKey) {
  96. runProjectAnalysis(orchestrator, "shared/xoo-sample",
  97. "sonar.organization", organizationKey,
  98. "sonar.projectKey", projectKey,
  99. "sonar.projectDate", formatDate(analysisDate),
  100. "sonar.login", "admin",
  101. "sonar.password", "admin");
  102. }
  103. private void analyzeProject(String projectKey, String organizationKey) {
  104. analyzeProject(projectKey, new Date(), organizationKey);
  105. }
  106. }