3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectexport.steps;
22 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
23 import java.util.List;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.resources.Qualifiers;
28 import org.sonar.api.resources.Scopes;
29 import org.sonar.api.utils.System2;
30 import org.sonar.api.utils.log.LogTester;
31 import org.sonar.api.utils.log.LoggerLevel;
32 import org.sonar.ce.task.projectexport.component.ComponentRepositoryImpl;
33 import org.sonar.ce.task.step.TestComputationStepContext;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.db.component.SnapshotDto;
37 import org.sonar.db.event.EventDto;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.core.api.Assertions.assertThatThrownBy;
41 import static org.sonar.db.component.ComponentDto.UUID_PATH_OF_ROOT;
42 import static org.sonar.db.component.SnapshotDto.STATUS_PROCESSED;
44 public class ExportEventsStepTest {
46 private static final long NOW = 1_450_000_000_000L;
47 private static final long IN_THE_PAST = 1_440_000_000_000L;
49 private static final String PROJECT_UUID = "project_uuid";
50 private static final ComponentDto PROJECT = new ComponentDto()
51 .setUuid(PROJECT_UUID)
52 .setUuidPath(UUID_PATH_OF_ROOT)
53 .setRootUuid(PROJECT_UUID)
54 .setProjectUuid(PROJECT_UUID)
55 .setScope(Scopes.PROJECT)
56 .setQualifier(Qualifiers.PROJECT)
57 .setDbKey("the_project")
62 public DbTester dbTester = DbTester.create(System2.INSTANCE);
65 public LogTester logTester = new LogTester();
67 private FakeDumpWriter dumpWriter = new FakeDumpWriter();
68 private MutableProjectHolder projectHolder = new MutableProjectHolderImpl();
69 private ComponentRepositoryImpl componentRepository = new ComponentRepositoryImpl();
70 private ExportEventsStep underTest = new ExportEventsStep(dbTester.getDbClient(), projectHolder, componentRepository, dumpWriter);
74 ComponentDto projectDto = dbTester.components().insertPublicProject(PROJECT);
75 componentRepository.register(1, projectDto.uuid(), false);
76 projectHolder.setProjectDto(dbTester.components().getProjectDto(projectDto));
80 public void export_zero_events() {
81 underTest.execute(new TestComputationStepContext());
83 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 events exported");
84 List<ProjectDump.Event> events = dumpWriter.getWrittenMessagesOf(DumpElement.EVENTS);
85 assertThat(events).isEmpty();
89 public void export_events() {
90 SnapshotDto snapshot = insertSnapshot();
91 insertEvent(snapshot, "E1", "one");
92 insertEvent(snapshot, "E2", "two");
94 underTest.execute(new TestComputationStepContext());
96 assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("2 events exported");
97 List<ProjectDump.Event> events = dumpWriter.getWrittenMessagesOf(DumpElement.EVENTS);
98 assertThat(events).hasSize(2);
99 assertThat(events).extracting(ProjectDump.Event::getUuid).containsOnly("E1", "E2");
103 public void throws_ISE_if_error() {
104 SnapshotDto snapshot = insertSnapshot();
105 insertEvent(snapshot, "E1", "one");
106 insertEvent(snapshot, "E2", "two");
107 dumpWriter.failIfMoreThan(1, DumpElement.EVENTS);
109 assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
110 .isInstanceOf(IllegalStateException.class)
111 .hasMessage("Event Export failed after processing 1 events successfully");
115 public void export_all_fields() {
116 SnapshotDto snapshot = insertSnapshot();
117 dbTester.getDbClient().eventDao().insert(dbTester.getSession(), new EventDto()
119 .setAnalysisUuid(snapshot.getUuid())
120 .setComponentUuid(snapshot.getComponentUuid())
121 .setDate(IN_THE_PAST)
125 .setCategory("categ")
126 .setDescription("desc"));
129 underTest.execute(new TestComputationStepContext());
131 ProjectDump.Event event = dumpWriter.getWrittenMessagesOf(DumpElement.EVENTS).get(0);
132 assertThat(event.getUuid()).isEqualTo("E1");
133 assertThat(event.getName()).isEqualTo("name");
134 assertThat(event.getData()).isEqualTo("data");
135 assertThat(event.getCategory()).isEqualTo("categ");
136 assertThat(event.getDescription()).isEqualTo("desc");
137 assertThat(event.getDate()).isEqualTo(IN_THE_PAST);
138 assertThat(event.getAnalysisUuid()).isEqualTo(snapshot.getUuid());
139 assertThat(event.getComponentRef()).isOne();
143 public void getDescription_is_defined() {
144 assertThat(underTest.getDescription()).isEqualTo("Export events");
147 private void insertEvent(SnapshotDto snapshot, String uuid, String name) {
148 dbTester.getDbClient().eventDao().insert(dbTester.getSession(), new EventDto()
150 .setAnalysisUuid(snapshot.getUuid())
151 .setComponentUuid(snapshot.getComponentUuid())
152 .setDate(IN_THE_PAST)
158 private SnapshotDto insertSnapshot() {
159 SnapshotDto snapshot = new SnapshotDto()
161 .setComponentUuid(PROJECT.uuid())
162 .setStatus(STATUS_PROCESSED)
164 dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), snapshot);