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.

ExportEventsStepTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.ce.task.projectexport.steps;
  21. import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
  22. import java.util.List;
  23. import org.junit.Before;
  24. import org.junit.Rule;
  25. import org.junit.Test;
  26. import org.sonar.api.resources.Qualifiers;
  27. import org.sonar.api.resources.Scopes;
  28. import org.sonar.api.utils.System2;
  29. import org.sonar.api.utils.log.LogTester;
  30. import org.sonar.api.utils.log.LoggerLevel;
  31. import org.sonar.ce.task.projectexport.component.ComponentRepositoryImpl;
  32. import org.sonar.ce.task.step.TestComputationStepContext;
  33. import org.sonar.db.DbTester;
  34. import org.sonar.db.component.ComponentDto;
  35. import org.sonar.db.component.SnapshotDto;
  36. import org.sonar.db.event.EventDto;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  39. import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
  40. import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
  41. public class ExportEventsStepTest {
  42. private static final long NOW = 1_450_000_000_000L;
  43. private static final long IN_THE_PAST = 1_440_000_000_000L;
  44. private static final String PROJECT_UUID = "project_uuid";
  45. private static final ComponentDto PROJECT = new ComponentDto()
  46. .setUuid(PROJECT_UUID)
  47. .setUuidPath(UUID_PATH_OF_ROOT)
  48. .setRootUuid(PROJECT_UUID)
  49. .setProjectUuid(PROJECT_UUID)
  50. .setScope(Scopes.PROJECT)
  51. .setQualifier(Qualifiers.PROJECT)
  52. .setDbKey("the_project")
  53. .setEnabled(true);
  54. @Rule
  55. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  56. @Rule
  57. public LogTester logTester = new LogTester();
  58. private FakeDumpWriter dumpWriter = new FakeDumpWriter();
  59. private MutableProjectHolder projectHolder = new MutableProjectHolderImpl();
  60. private ComponentRepositoryImpl componentRepository = new ComponentRepositoryImpl();
  61. private ExportEventsStep underTest = new ExportEventsStep(dbTester.getDbClient(), projectHolder, componentRepository, dumpWriter);
  62. @Before
  63. public void setUp() {
  64. ComponentDto projectDto = dbTester.components().insertPublicProject(PROJECT);
  65. componentRepository.register(1, projectDto.uuid(), false);
  66. projectHolder.setProjectDto(dbTester.components().getProjectDto(projectDto));
  67. }
  68. @Test
  69. public void export_zero_events() {
  70. underTest.execute(new TestComputationStepContext());
  71. assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 events exported");
  72. List<ProjectDump.Event> events = dumpWriter.getWrittenMessagesOf(DumpElement.EVENTS);
  73. assertThat(events).isEmpty();
  74. }
  75. @Test
  76. public void export_events() {
  77. SnapshotDto snapshot = insertSnapshot();
  78. insertEvent(snapshot, "E1", "one");
  79. insertEvent(snapshot, "E2", "two");
  80. underTest.execute(new TestComputationStepContext());
  81. assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("2 events exported");
  82. List<ProjectDump.Event> events = dumpWriter.getWrittenMessagesOf(DumpElement.EVENTS);
  83. assertThat(events).hasSize(2);
  84. assertThat(events).extracting(ProjectDump.Event::getUuid).containsOnly("E1", "E2");
  85. }
  86. @Test
  87. public void throws_ISE_if_error() {
  88. SnapshotDto snapshot = insertSnapshot();
  89. insertEvent(snapshot, "E1", "one");
  90. insertEvent(snapshot, "E2", "two");
  91. dumpWriter.failIfMoreThan(1, DumpElement.EVENTS);
  92. assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
  93. .isInstanceOf(IllegalStateException.class)
  94. .hasMessage("Event Export failed after processing 1 events successfully");
  95. }
  96. @Test
  97. public void export_all_fields() {
  98. SnapshotDto snapshot = insertSnapshot();
  99. dbTester.getDbClient().eventDao().insert(dbTester.getSession(), new EventDto()
  100. .setUuid("E1")
  101. .setAnalysisUuid(snapshot.getUuid())
  102. .setComponentUuid(snapshot.getComponentUuid())
  103. .setDate(IN_THE_PAST)
  104. .setCreatedAt(NOW)
  105. .setData("data")
  106. .setName("name")
  107. .setCategory("categ")
  108. .setDescription("desc"));
  109. dbTester.commit();
  110. underTest.execute(new TestComputationStepContext());
  111. ProjectDump.Event event = dumpWriter.getWrittenMessagesOf(DumpElement.EVENTS).get(0);
  112. assertThat(event.getUuid()).isEqualTo("E1");
  113. assertThat(event.getName()).isEqualTo("name");
  114. assertThat(event.getData()).isEqualTo("data");
  115. assertThat(event.getCategory()).isEqualTo("categ");
  116. assertThat(event.getDescription()).isEqualTo("desc");
  117. assertThat(event.getDate()).isEqualTo(IN_THE_PAST);
  118. assertThat(event.getAnalysisUuid()).isEqualTo(snapshot.getUuid());
  119. assertThat(event.getComponentRef()).isOne();
  120. }
  121. @Test
  122. public void getDescription_is_defined() {
  123. assertThat(underTest.getDescription()).isEqualTo("Export events");
  124. }
  125. private void insertEvent(SnapshotDto snapshot, String uuid, String name) {
  126. dbTester.getDbClient().eventDao().insert(dbTester.getSession(), new EventDto()
  127. .setUuid(uuid)
  128. .setAnalysisUuid(snapshot.getUuid())
  129. .setComponentUuid(snapshot.getComponentUuid())
  130. .setDate(IN_THE_PAST)
  131. .setCreatedAt(NOW)
  132. .setName(name));
  133. dbTester.commit();
  134. }
  135. private SnapshotDto insertSnapshot() {
  136. SnapshotDto snapshot = new SnapshotDto()
  137. .setUuid("U1")
  138. .setComponentUuid(PROJECT.uuid())
  139. .setStatus(STATUS_PROCESSED)
  140. .setLast(false);
  141. dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), snapshot);
  142. dbTester.commit();
  143. return snapshot;
  144. }
  145. }