3 * Copyright (C) 2009-2017 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.
21 package org.sonar.server.computation.task.projectanalysis.step;
23 import java.util.Arrays;
24 import java.util.List;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.utils.System2;
29 import org.sonar.core.util.CloseableIterator;
30 import org.sonar.core.util.UuidFactoryFast;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.component.AnalysisPropertyDto;
33 import org.sonar.scanner.protocol.output.ScannerReport;
34 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
35 import org.sonar.server.computation.task.projectanalysis.batch.BatchReportReader;
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;
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 BIG_VALUE = randomAlphanumeric(5000);
48 private static final List<ScannerReport.ContextProperty> PROPERTIES = Arrays.asList(
49 newContextProperty("key1", "value1"),
50 newContextProperty("key2", "value1"),
51 newContextProperty("sonar.analysis", SMALL_VALUE1),
52 newContextProperty("sonar.analysis.branch", SMALL_VALUE1),
53 newContextProperty("sonar.analysis.empty_string", ""),
54 newContextProperty("sonar.analysis.big_value", BIG_VALUE),
55 newContextProperty("sonar.analysis.", SMALL_VALUE2));
58 public DbTester dbTester = DbTester.create(System2.INSTANCE);
60 private BatchReportReader batchReportReader = mock(BatchReportReader.class);
61 private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
62 private PersistAnalysisPropertiesStep underTest = new PersistAnalysisPropertiesStep(dbTester.getDbClient(), analysisMetadataHolder, batchReportReader, UuidFactoryFast.getInstance());
66 when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
67 when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
71 public void persist_should_store_only_sonarDotAnalysis_properties() {
73 assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(4);
75 List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
76 .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID);
78 assertThat(propertyDtos)
79 .extracting(AnalysisPropertyDto::getSnapshotUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)
80 .containsExactlyInAnyOrder(
81 tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE1),
82 tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""),
83 tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE),
84 tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE2)
88 private static ScannerReport.ContextProperty newContextProperty(String key, String value) {
89 return ScannerReport.ContextProperty.newBuilder()