aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-tests/src/test/java/it/qualityModel/NewDebtRatioMeasureTest.java
blob: 01bb6359be0eb1e6c8fe7ce43213ad73b9cf1ac5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package it.qualityModel;

import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.locator.FileLocation;
import it.Category1Suite;
import java.util.List;
import javax.annotation.Nullable;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.wsclient.services.Measure;
import org.sonar.wsclient.services.Resource;
import org.sonar.wsclient.services.ResourceQuery;
import util.ItUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.within;
import static util.ItUtils.setServerProperty;

/**
 * SONAR-5876
 */
public class NewDebtRatioMeasureTest {

  private static final String NEW_DEBT_RATIO_METRIC_KEY = "new_sqale_debt_ratio";

  @ClassRule
  public static Orchestrator orchestrator = Category1Suite.ORCHESTRATOR;

  @BeforeClass
  public static void initPeriods() throws Exception {
    setServerProperty(orchestrator, "sonar.timemachine.period1", "previous_analysis");
    setServerProperty(orchestrator, "sonar.timemachine.period2", "30");
    setServerProperty(orchestrator, "sonar.timemachine.period3", "previous_version");
  }

  @AfterClass
  public static void resetPeriods() throws Exception {
    ItUtils.resetPeriods(orchestrator);
  }

  @Before
  public void cleanUpAnalysisData() {
    orchestrator.resetData();
  }

  @Test
  public void new_debt_ratio_is_computed_from_nes_debt_and_new_ncloc_count_per_file() throws Exception {
    // This test assumes that period 1 is "since previous analysis" and 2 is "over 30 days"

    // run analysis on the day of after the first commit (2015-09-01), with 'one-issue-per-line' profile
    // => some issues at date 2015-09-02
    defineQualityProfile("one-issue-per-line");
    provisionSampleProject();
    setSampleProjectQualityProfile("one-issue-per-line");
    runSampleProjectAnalysis("v1", "sonar.projectDate", "2015-09-02");

    // first analysis, no previous snapshot => periods not resolved => no value
    assertNoNewDebtRatio();

    // run analysis on the day after of second commit (2015-09-17) 'one-issue-per-line' profile*
    // => 3 new issues will be created at date 2015-09-18
    runSampleProjectAnalysis("v2", "sonar.projectDate", "2015-09-18");
    assertNewDebtRatio(4.44, 4.44);

    // run analysis on the day after of third commit (2015-09-20) 'one-issue-per-line' profile*
    // => 4 new issues will be created at date 2015-09-21
    runSampleProjectAnalysis("v3", "sonar.projectDate", "2015-09-21");
    assertNewDebtRatio(4.17, 4.28);
  }

  private void assertNoNewDebtRatio() {
    assertThat(getFileResourceWithVariations(NEW_DEBT_RATIO_METRIC_KEY)).isNull();
  }

  private void assertNewDebtRatio(@Nullable Double valuePeriod1, @Nullable Double valuePeriod2) {
    Resource newTechnicalDebt = getFileResourceWithVariations(NEW_DEBT_RATIO_METRIC_KEY);
    List<Measure> measures = newTechnicalDebt.getMeasures();
    assertThat(measures.get(0).getVariation1()).isEqualTo(valuePeriod1, within(0.01));
    assertThat(measures.get(0).getVariation2()).isEqualTo(valuePeriod2, within(0.01));
  }

  private void setSampleProjectQualityProfile(String qualityProfileKey) {
    orchestrator.getServer().associateProjectToQualityProfile("sample", "xoo", qualityProfileKey);
  }

  private void provisionSampleProject() {
    orchestrator.getServer().provisionProject("sample", "sample");
  }

  private void defineQualityProfile(String qualityProfileKey) {
    orchestrator.getServer().restoreProfile(FileLocation.ofClasspath("/measure/" + qualityProfileKey + ".xml"));
  }

  private void runSampleProjectAnalysis(String projectVersion, String... properties) {
    ItUtils.runVerboseProjectAnalysis(
      NewDebtRatioMeasureTest.orchestrator,
      "measure/xoo-new-debt-ratio-" + projectVersion,
      ItUtils.concat(properties,
        // disable standard scm support so that it does not interfere with Xoo Scm sensor
        "sonar.scm.disabled", "false")
      );
  }

  private Resource getFileResourceWithVariations(String metricKey) {
    return orchestrator.getServer().getWsClient().find(ResourceQuery.createForMetrics("sample:src/main/xoo/sample/Sample.xoo", metricKey).setIncludeTrends(true));
  }

}