]> source.dussan.org Git - sonarqube.git/blob
67fe41265df12b728d3e14e8ca96406cc8a9eac7
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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 org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.utils.System2;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.ce.task.projectanalysis.batch.BatchReportReader;
30 import org.sonar.ce.task.step.TestComputationStepContext;
31 import org.sonar.core.util.CloseableIterator;
32 import org.sonar.core.util.UuidFactoryFast;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.component.AnalysisPropertyDto;
35 import org.sonar.scanner.protocol.output.ScannerReport;
36
37 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.tuple;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 public class PersistAnalysisPropertiesStepTest {
44   private static final String SNAPSHOT_UUID = randomAlphanumeric(40);
45   private static final String SMALL_VALUE1 = randomAlphanumeric(50);
46   private static final String SMALL_VALUE2 = randomAlphanumeric(50);
47   private static final String SMALL_VALUE3 = randomAlphanumeric(50);
48   private static final String BIG_VALUE = randomAlphanumeric(5000);
49   private static final String VALUE_PREFIX_FOR_PR_PROPERTIES = "pr_";
50   private static final List<ScannerReport.ContextProperty> PROPERTIES = Arrays.asList(
51     newContextProperty("key1", "value1"),
52     newContextProperty("key2", "value1"),
53     newContextProperty("sonar.analysis", SMALL_VALUE1),
54     newContextProperty("sonar.analysis.branch", SMALL_VALUE2),
55     newContextProperty("sonar.analysis.empty_string", ""),
56     newContextProperty("sonar.analysis.big_value", BIG_VALUE),
57     newContextProperty("sonar.analysis.", SMALL_VALUE3),
58     newContextProperty("sonar.pullrequest", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE1),
59     newContextProperty("sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
60     newContextProperty("sonar.pullrequest.empty_string", ""),
61     newContextProperty("sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
62     newContextProperty("sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
63
64   @Rule
65   public DbTester dbTester = DbTester.create(System2.INSTANCE);
66
67   private BatchReportReader batchReportReader = mock(BatchReportReader.class);
68   private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
69   private PersistAnalysisPropertiesStep underTest = new PersistAnalysisPropertiesStep(dbTester.getDbClient(), analysisMetadataHolder, batchReportReader,
70     UuidFactoryFast.getInstance());
71
72   @Test
73   public void persist_should_stores_sonarDotAnalysisDot_and_sonarDotPullRequestDot_properties() {
74     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
75     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
76
77     underTest.execute(new TestComputationStepContext());
78
79     assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(8);
80     List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
81       .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID);
82
83     assertThat(propertyDtos)
84       .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)
85       .containsExactlyInAnyOrder(
86         tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE2),
87         tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""),
88         tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE),
89         tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE3),
90         tuple(SNAPSHOT_UUID, "sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
91         tuple(SNAPSHOT_UUID, "sonar.pullrequest.empty_string", ""),
92         tuple(SNAPSHOT_UUID, "sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
93         tuple(SNAPSHOT_UUID, "sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
94   }
95
96   @Test
97   public void persist_filtering_of_properties_is_case_sensitive() {
98     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(ImmutableList.of(
99       newContextProperty("sonar.ANALYSIS.foo", "foo"),
100       newContextProperty("sonar.anaLysis.bar", "bar"),
101       newContextProperty("sonar.anaLYSIS.doo", "doh"),
102       newContextProperty("sonar.PULLREQUEST.foo", "foo"),
103       newContextProperty("sonar.pullRequest.bar", "bar"),
104       newContextProperty("sonar.pullREQUEST.doo", "doh")).iterator()));
105     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
106
107     underTest.execute(new TestComputationStepContext());
108
109     assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(0);
110   }
111
112   @Test
113   public void persist_should_not_store_anything_if_there_is_no_context_properties() {
114     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator());
115     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
116
117     underTest.execute(new TestComputationStepContext());
118
119     assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(0);
120   }
121
122   @Test
123   public void verify_description_value() {
124     assertThat(underTest.getDescription()).isEqualTo("Persist analysis properties");
125   }
126
127   private static ScannerReport.ContextProperty newContextProperty(String key, String value) {
128     return ScannerReport.ContextProperty.newBuilder()
129       .setKey(key)
130       .setValue(value)
131       .build();
132   }
133 }