3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info 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.ce.task.projectanalysis.step;
22 import com.google.common.collect.ImmutableList;
23 import java.util.Arrays;
24 import java.util.List;
25 import java.util.Optional;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.utils.System2;
29 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
31 import org.sonar.ce.task.step.TestComputationStepContext;
32 import org.sonar.core.util.CloseableIterator;
33 import org.sonar.core.util.UuidFactoryFast;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.component.AnalysisPropertyDto;
36 import org.sonar.scanner.protocol.output.ScannerReport;
38 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.core.api.Assertions.tuple;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
44 public class PersistAnalysisPropertiesStepTest {
45 private static final String SNAPSHOT_UUID = randomAlphanumeric(40);
46 private static final String SMALL_VALUE1 = randomAlphanumeric(50);
47 private static final String SMALL_VALUE2 = randomAlphanumeric(50);
48 private static final String SMALL_VALUE3 = randomAlphanumeric(50);
49 private static final String BIG_VALUE = randomAlphanumeric(5000);
50 private static final String VALUE_PREFIX_FOR_PR_PROPERTIES = "pr_";
51 private static final List<ScannerReport.ContextProperty> PROPERTIES = Arrays.asList(
52 newContextProperty("key1", "value1"),
53 newContextProperty("key2", "value1"),
54 newContextProperty("sonar.analysis", SMALL_VALUE1),
55 newContextProperty("sonar.analysis.branch", SMALL_VALUE2),
56 newContextProperty("sonar.analysis.empty_string", ""),
57 newContextProperty("sonar.analysis.big_value", BIG_VALUE),
58 newContextProperty("sonar.analysis.", SMALL_VALUE3),
59 newContextProperty("sonar.pullrequest", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE1),
60 newContextProperty("sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
61 newContextProperty("sonar.pullrequest.empty_string", ""),
62 newContextProperty("sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
63 newContextProperty("sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
64 private static final String SCM_REV_ID = "sha1";
67 public DbTester dbTester = DbTester.create(System2.INSTANCE);
69 private BatchReportReader batchReportReader = mock(BatchReportReader.class);
70 private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
71 private PersistAnalysisPropertiesStep underTest = new PersistAnalysisPropertiesStep(dbTester.getDbClient(), analysisMetadataHolder, batchReportReader,
72 UuidFactoryFast.getInstance());
75 public void persist_should_stores_sonarDotAnalysisDot_and_sonarDotPullRequestDot_properties() {
76 when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
77 when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
78 when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID));
80 underTest.execute(new TestComputationStepContext());
82 assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(9);
83 List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
84 .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID);
86 assertThat(propertyDtos)
87 .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)
88 .containsExactlyInAnyOrder(
89 tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE2),
90 tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""),
91 tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE),
92 tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE3),
93 tuple(SNAPSHOT_UUID, "sonar.analysis.scm_revision_id", SCM_REV_ID),
94 tuple(SNAPSHOT_UUID, "sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
95 tuple(SNAPSHOT_UUID, "sonar.pullrequest.empty_string", ""),
96 tuple(SNAPSHOT_UUID, "sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
97 tuple(SNAPSHOT_UUID, "sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
101 public void persist_should_not_stores_sonarDotAnalysisDotscm_revision_id_properties_when_its_not_available_in_report_metada() {
102 when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
103 when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
104 when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.empty());
106 underTest.execute(new TestComputationStepContext());
108 assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(8);
109 List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
110 .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID);
112 assertThat(propertyDtos)
113 .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)
114 .containsExactlyInAnyOrder(
115 tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE2),
116 tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""),
117 tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE),
118 tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE3),
119 tuple(SNAPSHOT_UUID, "sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
120 tuple(SNAPSHOT_UUID, "sonar.pullrequest.empty_string", ""),
121 tuple(SNAPSHOT_UUID, "sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
122 tuple(SNAPSHOT_UUID, "sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
126 public void persist_filtering_of_properties_is_case_sensitive() {
127 when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID));
128 when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(ImmutableList.of(
129 newContextProperty("sonar.ANALYSIS.foo", "foo"),
130 newContextProperty("sonar.anaLysis.bar", "bar"),
131 newContextProperty("sonar.anaLYSIS.doo", "doh"),
132 newContextProperty("sonar.PULLREQUEST.foo", "foo"),
133 newContextProperty("sonar.pullRequest.bar", "bar"),
134 newContextProperty("sonar.pullREQUEST.doo", "doh")).iterator()));
135 when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
137 underTest.execute(new TestComputationStepContext());
139 assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(1);
143 public void persist_should_only_store_scmRevisionId_if_there_is_no_context_properties() {
144 when(analysisMetadataHolder.getScmRevisionId()).thenReturn(Optional.of(SCM_REV_ID));
145 when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator());
146 when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
148 underTest.execute(new TestComputationStepContext());
150 assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(1);
151 List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
152 .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID);
154 assertThat(propertyDtos)
155 .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)
156 .containsExactlyInAnyOrder(tuple(SNAPSHOT_UUID, "sonar.analysis.scm_revision_id", SCM_REV_ID));
160 public void verify_description_value() {
161 assertThat(underTest.getDescription()).isEqualTo("Persist analysis properties");
164 private static ScannerReport.ContextProperty newContextProperty(String key, String value) {
165 return ScannerReport.ContextProperty.newBuilder()