]> source.dussan.org Git - sonarqube.git/blob
27fb4b77b06768e1e596c6e98bfda6956bfb03d8
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.projectanalysis.step;
21
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;
37
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;
43
44 public class PersistAnalysisPropertiesStepIT {
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";
65
66   @Rule
67   public DbTester dbTester = DbTester.create(System2.INSTANCE);
68
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());
73
74   @Test
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.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID));
79
80     underTest.execute(new TestComputationStepContext());
81
82     assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(8);
83     List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
84       .analysisPropertiesDao().selectByAnalysisUuid(dbTester.getSession(), SNAPSHOT_UUID);
85
86     assertThat(propertyDtos)
87       .extracting(AnalysisPropertyDto::getAnalysisUuid, 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.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
94         tuple(SNAPSHOT_UUID, "sonar.pullrequest.empty_string", ""),
95         tuple(SNAPSHOT_UUID, "sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
96         tuple(SNAPSHOT_UUID, "sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
97   }
98
99   @Test
100   public void persist_filtering_of_properties_is_case_sensitive() {
101     when(analysisMetadataHolder.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID));
102     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(ImmutableList.of(
103       newContextProperty("sonar.ANALYSIS.foo", "foo"),
104       newContextProperty("sonar.anaLysis.bar", "bar"),
105       newContextProperty("sonar.anaLYSIS.doo", "doh"),
106       newContextProperty("sonar.PULLREQUEST.foo", "foo"),
107       newContextProperty("sonar.pullRequest.bar", "bar"),
108       newContextProperty("sonar.pullREQUEST.doo", "doh")).iterator()));
109     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
110
111     underTest.execute(new TestComputationStepContext());
112
113     assertThat(dbTester.countRowsOfTable("analysis_properties")).isZero();
114   }
115
116   @Test
117   public void persist_should_store_nothing_if_there_are_no_context_properties() {
118     when(analysisMetadataHolder.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID));
119     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator());
120     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
121
122     underTest.execute(new TestComputationStepContext());
123
124     assertThat(dbTester.countRowsOfTable("analysis_properties")).isZero();
125   }
126
127   @Test
128   public void verify_description_value() {
129     assertThat(underTest.getDescription()).isEqualTo("Persist analysis properties");
130   }
131
132   private static ScannerReport.ContextProperty newContextProperty(String key, String value) {
133     return ScannerReport.ContextProperty.newBuilder()
134       .setKey(key)
135       .setValue(value)
136       .build();
137   }
138 }