]> source.dussan.org Git - sonarqube.git/blob
b46e772ee776a38ca35d5d38db0a8fea482bf7ed
[sonarqube.git] /
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
22 import com.sonarsource.governance.projectdump.protobuf.ProjectDump;
23 import java.util.List;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.utils.System2;
28 import org.sonar.api.utils.log.LogTester;
29 import org.sonar.api.utils.log.LoggerLevel;
30 import org.sonar.ce.task.projectexport.component.ComponentRepositoryImpl;
31 import org.sonar.ce.task.step.TestComputationStepContext;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.component.BranchDto;
34 import org.sonar.db.component.BranchType;
35 import org.sonar.db.component.ComponentDto;
36 import org.sonar.db.metric.MetricDto;
37 import org.sonar.db.project.ProjectDto;
38
39 import static com.google.common.collect.Lists.newArrayList;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.tuple;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.sonar.api.measures.Metric.ValueType.INT;
45
46 public class ExportLiveMeasuresStepTest {
47   @Rule
48   public ExpectedException expectedException = ExpectedException.none();
49   @Rule
50   public LogTester logTester = new LogTester();
51   @Rule
52   public DbTester dbTester = DbTester.create(System2.INSTANCE);
53
54   private ComponentRepositoryImpl componentRepository = new ComponentRepositoryImpl();
55   private MutableMetricRepository metricRepository = new MutableMetricRepositoryImpl();
56   private ProjectHolder projectHolder = mock(ProjectHolder.class);
57   private FakeDumpWriter dumpWriter = new FakeDumpWriter();
58   private ExportLiveMeasuresStep underTest = new ExportLiveMeasuresStep(dbTester.getDbClient(), projectHolder, componentRepository, metricRepository, dumpWriter);
59
60   @Test
61   public void export_zero_measures() {
62     when(projectHolder.branches()).thenReturn(newArrayList());
63     when(projectHolder.projectDto()).thenReturn(new ProjectDto().setUuid("does not exist"));
64
65     underTest.execute(new TestComputationStepContext());
66
67     assertThat(dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES)).isEmpty();
68     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("0 live measures exported");
69     assertThat(metricRepository.getRefByUuid()).isEmpty();
70   }
71
72   @Test
73   public void export_measures() {
74     ComponentDto project = createProject(true);
75     componentRepository.register(1, project.uuid(), false);
76     MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()));
77     dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d).setVariation(null));
78     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
79     when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
80       .setProjectUuid(project.uuid())
81       .setUuid(project.uuid())
82       .setKey("master")
83       .setBranchType(BranchType.BRANCH)));
84     underTest.execute(new TestComputationStepContext());
85
86     List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
87     assertThat(exportedMeasures).hasSize(1);
88     assertThat(exportedMeasures)
89       .extracting(ProjectDump.LiveMeasure::getMetricRef, m -> m.getDoubleValue().getValue(), ProjectDump.LiveMeasure::hasVariation)
90       .containsOnly(tuple(0, 4711.0d, false));
91     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported");
92     assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid());
93   }
94
95   @Test
96   public void do_not_export_measures_on_disabled_projects() {
97     ComponentDto project = createProject(false);
98     componentRepository.register(1, project.uuid(), false);
99     MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()));
100     dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d));
101     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
102     when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
103       .setProjectUuid(project.uuid())
104       .setUuid(project.uuid())
105       .setKey("master")
106       .setBranchType(BranchType.BRANCH)));
107
108     underTest.execute(new TestComputationStepContext());
109
110     List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
111     assertThat(exportedMeasures).isEmpty();
112   }
113
114   @Test
115   public void do_not_export_measures_on_disabled_metrics() {
116     ComponentDto project = createProject(true);
117     componentRepository.register(1, project.uuid(), false);
118     MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()).setEnabled(false));
119     dbTester.measures().insertLiveMeasure(project, metric, m -> m.setValue(4711.0d));
120     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
121     when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
122       .setProjectUuid(project.uuid())
123       .setUuid(project.uuid())
124       .setKey("master")
125       .setBranchType(BranchType.BRANCH)));
126
127     underTest.execute(new TestComputationStepContext());
128
129     List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
130     assertThat(exportedMeasures).isEmpty();
131   }
132
133   @Test
134   public void test_exported_fields() {
135     ComponentDto project = createProject(true);
136     componentRepository.register(1, project.uuid(), false);
137     MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()));
138     dbTester.measures().insertLiveMeasure(project, metric, m -> m.setProjectUuid(project.uuid()).setValue(4711.0d).setData("test").setVariation(7.0d));
139     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
140     when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
141       .setProjectUuid(project.uuid())
142       .setUuid(project.uuid())
143       .setKey("master")
144       .setBranchType(BranchType.BRANCH)));
145
146     underTest.execute(new TestComputationStepContext());
147
148     List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
149     assertThat(exportedMeasures).hasSize(1);
150     assertThat(exportedMeasures)
151       .extracting(
152         ProjectDump.LiveMeasure::getComponentRef,
153         ProjectDump.LiveMeasure::getMetricRef,
154         m -> m.getDoubleValue().getValue(),
155         ProjectDump.LiveMeasure::getTextValue,
156         m -> m.getVariation().getValue())
157       .containsOnly(tuple(
158         1L,
159         0,
160         4711.0d,
161         "test",
162         7.0d));
163     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported");
164     assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid());
165   }
166
167   @Test
168   public void test_null_exported_fields() {
169     ComponentDto project = createProject(true);
170     componentRepository.register(1, project.uuid(), false);
171     MetricDto metric = dbTester.measures().insertMetric(m -> m.setValueType(INT.name()));
172     dbTester.measures().insertLiveMeasure(project, metric, m -> m.setProjectUuid(project.uuid()).setValue(null).setData((String) null).setVariation(null));
173     when(projectHolder.projectDto()).thenReturn(dbTester.components().getProjectDto(project));
174     when(projectHolder.branches()).thenReturn(newArrayList(new BranchDto()
175       .setProjectUuid(project.uuid())
176       .setUuid(project.uuid())
177       .setKey("master")
178       .setBranchType(BranchType.BRANCH)));
179
180     underTest.execute(new TestComputationStepContext());
181
182     List<ProjectDump.LiveMeasure> exportedMeasures = dumpWriter.getWrittenMessagesOf(DumpElement.LIVE_MEASURES);
183     assertThat(exportedMeasures).hasSize(1);
184     assertThat(exportedMeasures)
185       .extracting(
186         ProjectDump.LiveMeasure::hasDoubleValue,
187         ProjectDump.LiveMeasure::getTextValue,
188         ProjectDump.LiveMeasure::hasVariation)
189       .containsOnly(tuple(
190         false,
191         "",
192         false));
193     assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("1 live measures exported");
194     assertThat(metricRepository.getRefByUuid().keySet()).containsOnly(metric.getUuid());
195   }
196
197   @Test
198   public void test_getDescription() {
199     assertThat(underTest.getDescription()).isEqualTo("Export live measures");
200   }
201
202   private ComponentDto createProject(boolean enabled) {
203     return dbTester.components().insertPrivateProject(p -> p.setEnabled(enabled));
204   }
205 }