3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.server.computation.task.projectanalysis.api.posttask;
22 import com.google.common.collect.ImmutableMap;
23 import com.tngtech.java.junit.dataprovider.DataProvider;
24 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
25 import com.tngtech.java.junit.dataprovider.UseDataProvider;
26 import java.util.Date;
27 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.ArgumentCaptor;
33 import org.mockito.InOrder;
34 import org.sonar.api.ce.posttask.PostProjectAnalysisTask;
35 import org.sonar.api.ce.posttask.Project;
36 import org.sonar.ce.queue.CeTask;
37 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolderRule;
38 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
39 import org.sonar.server.computation.task.projectanalysis.qualitygate.Condition;
40 import org.sonar.server.computation.task.projectanalysis.qualitygate.ConditionStatus;
41 import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateHolderRule;
42 import org.sonar.server.computation.task.projectanalysis.qualitygate.MutableQualityGateStatusHolderRule;
43 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGate;
44 import org.sonar.server.computation.task.projectanalysis.qualitygate.QualityGateStatus;
46 import static com.google.common.collect.ImmutableList.of;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.mockito.Mockito.inOrder;
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.verify;
51 import static org.mockito.Mockito.when;
53 @RunWith(DataProviderRunner.class)
54 public class PostProjectAnalysisTasksExecutorTest {
55 private static final long QUALITY_GATE_ID = 98451;
56 private static final String QUALITY_GATE_NAME = "qualityGate name";
57 private static final Condition CONDITION_1 = createCondition("metric key 1");
58 private static final Condition CONDITION_2 = createCondition("metric key 2");
61 public AnalysisMetadataHolderRule analysisMetadataHolder = new AnalysisMetadataHolderRule()
62 .setAnalysisDate(8465132498L);
64 public MutableQualityGateHolderRule qualityGateHolder = new MutableQualityGateHolderRule();
66 public MutableQualityGateStatusHolderRule qualityGateStatusHolder = new MutableQualityGateStatusHolderRule();
68 private ArgumentCaptor<PostProjectAnalysisTask.ProjectAnalysis> projectAnalysisArgumentCaptor = ArgumentCaptor.forClass(PostProjectAnalysisTask.ProjectAnalysis.class);
69 private CeTask ceTask = new CeTask.Builder()
72 .setComponentKey("component key")
73 .setComponentName("component name")
74 .setComponentUuid("component uuid")
76 private PostProjectAnalysisTask postProjectAnalysisTask = mock(PostProjectAnalysisTask.class);
77 private PostProjectAnalysisTasksExecutor underTest = new PostProjectAnalysisTasksExecutor(
78 ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder,
79 new PostProjectAnalysisTask[] {postProjectAnalysisTask});
82 public void setUp() throws Exception {
83 qualityGateHolder.setQualityGate(new QualityGate(QUALITY_GATE_ID, QUALITY_GATE_NAME, of(CONDITION_1, CONDITION_2)));
84 qualityGateStatusHolder.setStatus(QualityGateStatus.OK, ImmutableMap.of(
85 CONDITION_1, ConditionStatus.create(ConditionStatus.EvaluationStatus.OK, "value"),
86 CONDITION_2, ConditionStatus.NO_VALUE_STATUS));
90 @UseDataProvider("booleanValues")
91 public void does_not_fail_when_there_is_no_PostProjectAnalysisTasksExecutor(boolean allStepsExecuted) {
92 new PostProjectAnalysisTasksExecutor(ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder)
93 .finished(allStepsExecuted);
97 @UseDataProvider("booleanValues")
98 public void finished_calls_all_PostProjectAnalysisTask_in_order_of_the_array_and_passes_the_same_object_to_all(boolean allStepsExecuted) {
99 PostProjectAnalysisTask postProjectAnalysisTask1 = mock(PostProjectAnalysisTask.class);
100 PostProjectAnalysisTask postProjectAnalysisTask2 = mock(PostProjectAnalysisTask.class);
101 InOrder inOrder = inOrder(postProjectAnalysisTask1, postProjectAnalysisTask2);
103 new PostProjectAnalysisTasksExecutor(
104 ceTask, analysisMetadataHolder, qualityGateHolder, qualityGateStatusHolder,
105 new PostProjectAnalysisTask[] {postProjectAnalysisTask1, postProjectAnalysisTask2})
106 .finished(allStepsExecuted);
108 inOrder.verify(postProjectAnalysisTask1).finished(projectAnalysisArgumentCaptor.capture());
109 inOrder.verify(postProjectAnalysisTask2).finished(projectAnalysisArgumentCaptor.capture());
110 inOrder.verifyNoMoreInteractions();
112 List<PostProjectAnalysisTask.ProjectAnalysis> allValues = projectAnalysisArgumentCaptor.getAllValues();
113 assertThat(allValues).hasSize(2);
114 assertThat(allValues.get(0)).isSameAs(allValues.get(1));
118 @UseDataProvider("booleanValues")
119 public void CeTask_status_depends_on_finished_method_argument_is_true_or_false(boolean allStepsExecuted) {
120 underTest.finished(allStepsExecuted);
122 verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
124 assertThat(projectAnalysisArgumentCaptor.getValue().getCeTask().getStatus())
126 allStepsExecuted ? org.sonar.api.ce.posttask.CeTask.Status.SUCCESS : org.sonar.api.ce.posttask.CeTask.Status.FAILED);
130 public void ceTask_uuid_is_UUID_of_CeTask() {
131 underTest.finished(true);
133 verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
135 assertThat(projectAnalysisArgumentCaptor.getValue().getCeTask().getId())
136 .isEqualTo(ceTask.getUuid());
140 public void project_uuid_key_and_name_come_from_CeTask() {
141 underTest.finished(true);
143 verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
145 Project project = projectAnalysisArgumentCaptor.getValue().getProject();
146 assertThat(project.getUuid()).isEqualTo(ceTask.getComponentUuid());
147 assertThat(project.getKey()).isEqualTo(ceTask.getComponentKey());
148 assertThat(project.getName()).isEqualTo(ceTask.getComponentName());
152 public void analysisDate_comes_from_AnalysisMetadataHolder() {
153 underTest.finished(true);
155 verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
157 assertThat(projectAnalysisArgumentCaptor.getValue().getDate())
158 .isEqualTo(new Date(analysisMetadataHolder.getAnalysisDate()));
162 public void qualityGate_is_null_when_finished_method_argument_is_false() {
163 underTest.finished(false);
165 verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
167 assertThat(projectAnalysisArgumentCaptor.getValue().getQualityGate()).isNull();
171 public void qualityGate_is_populated_when_finished_method_argument_is_true() {
172 underTest.finished(true);
174 verify(postProjectAnalysisTask).finished(projectAnalysisArgumentCaptor.capture());
176 org.sonar.api.ce.posttask.QualityGate qualityGate = projectAnalysisArgumentCaptor.getValue().getQualityGate();
177 assertThat(qualityGate.getStatus()).isEqualTo(org.sonar.api.ce.posttask.QualityGate.Status.OK);
178 assertThat(qualityGate.getId()).isEqualTo(String.valueOf(QUALITY_GATE_ID));
179 assertThat(qualityGate.getName()).isEqualTo(QUALITY_GATE_NAME);
180 assertThat(qualityGate.getConditions()).hasSize(2);
184 public static Object[][] booleanValues() {
185 return new Object[][] {
191 private static Condition createCondition(String metricKey) {
192 Metric metric = mock(Metric.class);
193 when(metric.getKey()).thenReturn(metricKey);
194 return new Condition(metric, Condition.Operator.EQUALS.getDbValue(), "error threshold", "warn threshold", null);