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.7KB

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