]> source.dussan.org Git - sonarqube.git/blob
3006fba9b41e548b503dee2c312045d7346b78df
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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
21 package org.sonar.server.computation.task.projectanalysis.step;
22
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;
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 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));
56
57   @Rule
58   public DbTester dbTester = DbTester.create(System2.INSTANCE);
59
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());
63
64   @Before
65   public void setup() {
66     when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
67     when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
68   }
69
70   @Test
71   public void persist_should_store_only_sonarDotAnalysis_properties() {
72     underTest.execute();
73     assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(4);
74
75     List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
76       .analysisPropertiesDao().selectBySnapshotUuid(dbTester.getSession(), SNAPSHOT_UUID);
77
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)
85       );
86   }
87
88   private static ScannerReport.ContextProperty newContextProperty(String key, String value) {
89     return ScannerReport.ContextProperty.newBuilder()
90       .setKey(key)
91       .setValue(value)
92       .build();
93   }
94 }