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.

SubmitActionTest.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.ce.ws;
  21. import com.google.common.base.Strings;
  22. import java.io.ByteArrayInputStream;
  23. import java.util.Map;
  24. import org.junit.Test;
  25. import org.mockito.ArgumentCaptor;
  26. import org.sonar.ce.task.CeTask;
  27. import org.sonar.db.ce.CeTaskTypes;
  28. import org.sonar.server.ce.queue.ReportSubmitter;
  29. import org.sonar.server.organization.DefaultOrganizationProvider;
  30. import org.sonar.server.organization.TestDefaultOrganizationProvider;
  31. import org.sonar.server.ws.TestResponse;
  32. import org.sonar.server.ws.WsActionTester;
  33. import org.sonar.test.JsonAssert;
  34. import org.sonarqube.ws.Ce;
  35. import org.sonarqube.ws.MediaTypes;
  36. import static java.util.Arrays.asList;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.assertj.core.api.Assertions.entry;
  39. import static org.mockito.ArgumentMatchers.any;
  40. import static org.mockito.ArgumentMatchers.anyMap;
  41. import static org.mockito.ArgumentMatchers.eq;
  42. import static org.mockito.Mockito.mock;
  43. import static org.mockito.Mockito.verify;
  44. import static org.mockito.Mockito.when;
  45. public class SubmitActionTest {
  46. private static final String PROJECT_UUID = "PROJECT_1";
  47. private static final CeTask.Component COMPONENT = new CeTask.Component(PROJECT_UUID, "KEY_1", "NAME_1");
  48. private static final CeTask A_CE_TASK = new CeTask.Builder()
  49. .setOrganizationUuid("org1")
  50. .setUuid("TASK_1")
  51. .setType(CeTaskTypes.REPORT)
  52. .setComponent(COMPONENT)
  53. .setMainComponent(COMPONENT)
  54. .setSubmitter(new CeTask.User("UUID_1", "LOGIN_1"))
  55. .build();
  56. private ArgumentCaptor<Map<String, String>> map = ArgumentCaptor.forClass(Map.class);
  57. private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.fromUuid("org1");
  58. private String organizationKey = defaultOrganizationProvider.get().getKey();
  59. private ReportSubmitter reportSubmitter = mock(ReportSubmitter.class);
  60. private SubmitAction underTest = new SubmitAction(reportSubmitter, defaultOrganizationProvider);
  61. private WsActionTester tester = new WsActionTester(underTest);
  62. @Test
  63. public void submit_task_to_the_queue_and_ask_for_immediate_processing() {
  64. when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), eq("My Project"), anyMap(), any())).thenReturn(A_CE_TASK);
  65. Ce.SubmitResponse submitResponse = tester.newRequest()
  66. .setParam("projectKey", "my_project")
  67. .setParam("projectName", "My Project")
  68. .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar")
  69. .setMethod("POST")
  70. .executeProtobuf(Ce.SubmitResponse.class);
  71. verify(reportSubmitter).submit(eq(organizationKey), eq("my_project"), eq("My Project"), anyMap(), any());
  72. assertThat(submitResponse.getTaskId()).isEqualTo("TASK_1");
  73. assertThat(submitResponse.getProjectId()).isEqualTo(PROJECT_UUID);
  74. }
  75. @Test
  76. public void submit_task_with_characteristics() {
  77. when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), eq("My Project"), anyMap(), any())).thenReturn(A_CE_TASK);
  78. String[] characteristics = {"branch=foo", "pullRequest=123", "unsupported=bar"};
  79. Ce.SubmitResponse submitResponse = tester.newRequest()
  80. .setParam("projectKey", "my_project")
  81. .setParam("projectName", "My Project")
  82. .setMultiParam("characteristic", asList(characteristics))
  83. .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar")
  84. .setMethod("POST")
  85. .executeProtobuf(Ce.SubmitResponse.class);
  86. assertThat(submitResponse.getTaskId()).isEqualTo("TASK_1");
  87. verify(reportSubmitter).submit(eq(organizationKey), eq("my_project"), eq("My Project"), map.capture(), any());
  88. // unsupported characteristics are ignored
  89. assertThat(map.getValue()).containsExactly(entry("branch", "foo"), entry("pullRequest", "123"));
  90. }
  91. @Test
  92. public void abbreviate_long_name() {
  93. String longName = Strings.repeat("a", 1_000);
  94. String expectedName = Strings.repeat("a", 497) + "...";
  95. when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), eq(expectedName), anyMap(), any())).thenReturn(A_CE_TASK);
  96. Ce.SubmitResponse submitResponse = tester.newRequest()
  97. .setParam("projectKey", "my_project")
  98. .setParam("projectName", longName)
  99. .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar")
  100. .setMethod("POST")
  101. .executeProtobuf(Ce.SubmitResponse.class);
  102. verify(reportSubmitter).submit(eq(organizationKey), eq("my_project"), eq(expectedName), anyMap(), any());
  103. assertThat(submitResponse.getTaskId()).isEqualTo("TASK_1");
  104. assertThat(submitResponse.getProjectId()).isEqualTo(PROJECT_UUID);
  105. }
  106. @Test
  107. public void test_example_json_response() {
  108. when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), eq("My Project"), anyMap(), any())).thenReturn(A_CE_TASK);
  109. TestResponse wsResponse = tester.newRequest()
  110. .setParam("projectKey", "my_project")
  111. .setParam("projectName", "My Project")
  112. .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar")
  113. .setMediaType(MediaTypes.JSON)
  114. .setMethod("POST")
  115. .execute();
  116. JsonAssert.assertJson(tester.getDef().responseExampleAsString()).isSimilarTo(wsResponse.getInput());
  117. }
  118. /**
  119. * If project name is not specified, then name is the project key
  120. */
  121. @Test
  122. public void project_name_is_optional() {
  123. when(reportSubmitter.submit(eq(organizationKey), eq("my_project"), eq("my_project"), anyMap(), any())).thenReturn(A_CE_TASK);
  124. tester.newRequest()
  125. .setParam("projectKey", "my_project")
  126. .setPart("report", new ByteArrayInputStream("{binary}".getBytes()), "foo.bar")
  127. .setMediaType(MediaTypes.PROTOBUF)
  128. .setMethod("POST")
  129. .execute();
  130. verify(reportSubmitter).submit(eq(organizationKey), eq("my_project"), eq("my_project"), anyMap(), any());
  131. }
  132. }