]> source.dussan.org Git - sonarqube.git/blob
3f3a1ca81849447c6551953af33ba461d8408dce
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.server.computation.task.projectanalysis.qualitygate;
21
22 import com.google.common.base.Optional;
23 import com.google.common.collect.ImmutableList;
24 import java.util.Collections;
25 import org.junit.Test;
26 import org.sonar.db.qualitygate.QualityGateConditionDao;
27 import org.sonar.db.qualitygate.QualityGateConditionDto;
28 import org.sonar.db.qualitygate.QualityGateDao;
29 import org.sonar.db.qualitygate.QualityGateDto;
30 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
31 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
32
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.guava.api.Assertions.assertThat;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
37
38 public class QualityGateServiceImplTest {
39   private static final long SOME_ID = 123;
40   private static final String SOME_NAME = "some name";
41   private static final QualityGateDto QUALITY_GATE_DTO = new QualityGateDto().setId(SOME_ID).setName(SOME_NAME);
42   private static final long METRIC_ID_1 = 951;
43   private static final long METRIC_ID_2 = 753;
44   private static final Metric METRIC_1 = mock(Metric.class);
45   private static final Metric METRIC_2 = mock(Metric.class);
46   private static final QualityGateConditionDto CONDITION_1 = new QualityGateConditionDto().setId(321).setMetricId(METRIC_ID_1).setOperator("EQ").setPeriod(1).setWarningThreshold("warnin_th").setErrorThreshold("error_th");
47   private static final QualityGateConditionDto CONDITION_2 = new QualityGateConditionDto().setId(456).setMetricId(METRIC_ID_2).setOperator("NE");
48
49   private QualityGateDao qualityGateDao = mock(QualityGateDao.class);
50   private QualityGateConditionDao qualityGateConditionDao = mock(QualityGateConditionDao.class);
51   private MetricRepository metricRepository = mock(MetricRepository.class);
52   private QualityGateServiceImpl underTest = new QualityGateServiceImpl(qualityGateDao, qualityGateConditionDao, metricRepository);
53
54   @Test
55   public void findById_returns_absent_when_QualityGateDto_does_not_exist() {
56     assertThat(underTest.findById(SOME_ID)).isAbsent();
57   }
58
59   @Test
60   public void findById_returns_QualityGate_with_empty_set_of_conditions_when_there_is_none_in_DB() {
61     when(qualityGateDao.selectById(SOME_ID)).thenReturn(QUALITY_GATE_DTO);
62     when(qualityGateConditionDao.selectForQualityGate(SOME_ID)).thenReturn(Collections.<QualityGateConditionDto>emptyList());
63
64     Optional<QualityGate> res = underTest.findById(SOME_ID);
65
66     assertThat(res).isPresent();
67     assertThat(res.get().getId()).isEqualTo(SOME_ID);
68     assertThat(res.get().getName()).isEqualTo(SOME_NAME);
69     assertThat(res.get().getConditions()).isEmpty();
70   }
71
72   @Test
73   public void findById_returns_conditions_when_there_is_some_in_DB() {
74     when(qualityGateDao.selectById(SOME_ID)).thenReturn(QUALITY_GATE_DTO);
75     when(qualityGateConditionDao.selectForQualityGate(SOME_ID)).thenReturn(ImmutableList.of(CONDITION_1, CONDITION_2));
76     // metrics are always supposed to be there
77     when(metricRepository.getById(METRIC_ID_1)).thenReturn(METRIC_1);
78     when(metricRepository.getById(METRIC_ID_2)).thenReturn(METRIC_2);
79
80     Optional<QualityGate> res = underTest.findById(SOME_ID);
81
82     assertThat(res).isPresent();
83     assertThat(res.get().getId()).isEqualTo(SOME_ID);
84     assertThat(res.get().getName()).isEqualTo(SOME_NAME);
85     assertThat(res.get().getConditions()).containsOnly(
86         new Condition(METRIC_1, CONDITION_1.getOperator(), CONDITION_1.getErrorThreshold(), CONDITION_1.getWarningThreshold(), CONDITION_1.getPeriod()),
87         new Condition(METRIC_2, CONDITION_2.getOperator(), CONDITION_2.getErrorThreshold(), CONDITION_2.getWarningThreshold(), CONDITION_2.getPeriod())
88         );
89   }
90 }