]> source.dussan.org Git - sonarqube.git/blob
c81dcebc7a92c46c770c8705339e8b2151bee25c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 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
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;
38
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;
43
44 public class ExportEventsStepTest {
45
46   private static final long NOW = 1_450_000_000_000L;
47   private static final long IN_THE_PAST = 1_440_000_000_000L;
48
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")
58     .setEnabled(true);
59
60
61   @Rule
62   public DbTester dbTester = DbTester.create(System2.INSTANCE);
63
64   @Rule
65   public LogTester logTester = new LogTester();
66
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);
71
72   @Before
73   public void setUp() {
74     ComponentDto projectDto = dbTester.components().insertPublicProject(PROJECT);
75     componentRepository.register(1, projectDto.uuid(), false);
76     projectHolder.setProjectDto(dbTester.components().getProjectDto(projectDto));
77   }
78
79   @Test
80   public void export_zero_events() {
81     underTest.execute(new TestComputationStepContext());
82
83     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 events exported");
84     List<ProjectDump.Event> events = dumpWriter.getWrittenMessagesOf(DumpElement.EVENTS);
85     assertThat(events).isEmpty();
86   }
87
88   @Test
89   public void export_events() {
90     SnapshotDto snapshot = insertSnapshot();
91     insertEvent(snapshot, "E1", "one");
92     insertEvent(snapshot, "E2", "two");
93
94     underTest.execute(new TestComputationStepContext());
95
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");
100   }
101
102   @Test
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);
108
109     assertThatThrownBy(() -> underTest.execute(new TestComputationStepContext()))
110       .isInstanceOf(IllegalStateException.class)
111       .hasMessage("Event Export failed after processing 1 events successfully");
112   }
113
114   @Test
115   public void export_all_fields() {
116     SnapshotDto snapshot = insertSnapshot();
117     dbTester.getDbClient().eventDao().insert(dbTester.getSession(), new EventDto()
118       .setUuid("E1")
119       .setAnalysisUuid(snapshot.getUuid())
120       .setComponentUuid(snapshot.getComponentUuid())
121       .setDate(IN_THE_PAST)
122       .setCreatedAt(NOW)
123       .setData("data")
124       .setName("name")
125       .setCategory("categ")
126       .setDescription("desc"));
127     dbTester.commit();
128
129     underTest.execute(new TestComputationStepContext());
130
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();
140   }
141
142   @Test
143   public void getDescription_is_defined() {
144     assertThat(underTest.getDescription()).isEqualTo("Export events");
145   }
146
147   private void insertEvent(SnapshotDto snapshot, String uuid, String name) {
148     dbTester.getDbClient().eventDao().insert(dbTester.getSession(), new EventDto()
149       .setUuid(uuid)
150       .setAnalysisUuid(snapshot.getUuid())
151       .setComponentUuid(snapshot.getComponentUuid())
152       .setDate(IN_THE_PAST)
153       .setCreatedAt(NOW)
154       .setName(name));
155     dbTester.commit();
156   }
157
158   private SnapshotDto insertSnapshot() {
159     SnapshotDto snapshot = new SnapshotDto()
160       .setUuid("U1")
161       .setComponentUuid(PROJECT.uuid())
162       .setStatus(STATUS_PROCESSED)
163       .setLast(false);
164     dbTester.getDbClient().snapshotDao().insert(dbTester.getSession(), snapshot);
165     dbTester.commit();
166     return snapshot;
167   }
168 }