You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PersistAnalysisPropertiesStepTest.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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. import com.google.common.collect.ImmutableList;
  22. import java.util.Arrays;
  23. import java.util.List;
  24. import java.util.Optional;
  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. import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
  37. import static org.assertj.core.api.Assertions.assertThat;
  38. import static org.assertj.core.api.Assertions.tuple;
  39. import static org.mockito.Mockito.mock;
  40. import static org.mockito.Mockito.when;
  41. public class PersistAnalysisPropertiesStepTest {
  42. private static final String SNAPSHOT_UUID = randomAlphanumeric(40);
  43. private static final String SMALL_VALUE1 = randomAlphanumeric(50);
  44. private static final String SMALL_VALUE2 = randomAlphanumeric(50);
  45. private static final String SMALL_VALUE3 = randomAlphanumeric(50);
  46. private static final String BIG_VALUE = randomAlphanumeric(5000);
  47. private static final String VALUE_PREFIX_FOR_PR_PROPERTIES = "pr_";
  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_VALUE2),
  53. newContextProperty("sonar.analysis.empty_string", ""),
  54. newContextProperty("sonar.analysis.big_value", BIG_VALUE),
  55. newContextProperty("sonar.analysis.", SMALL_VALUE3),
  56. newContextProperty("sonar.pullrequest", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE1),
  57. newContextProperty("sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
  58. newContextProperty("sonar.pullrequest.empty_string", ""),
  59. newContextProperty("sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
  60. newContextProperty("sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
  61. private static final String SCM_REV_ID = "sha1";
  62. @Rule
  63. public DbTester dbTester = DbTester.create(System2.INSTANCE);
  64. private BatchReportReader batchReportReader = mock(BatchReportReader.class);
  65. private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
  66. private PersistAnalysisPropertiesStep underTest = new PersistAnalysisPropertiesStep(dbTester.getDbClient(), analysisMetadataHolder, batchReportReader,
  67. UuidFactoryFast.getInstance());
  68. @Test
  69. public void persist_should_stores_sonarDotAnalysisDot_and_sonarDotPullRequestDot_properties() {
  70. when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator()));
  71. when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
  72. when(analysisMetadataHolder.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID));
  73. underTest.execute(new TestComputationStepContext());
  74. assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(8);
  75. List<AnalysisPropertyDto> propertyDtos = dbTester.getDbClient()
  76. .analysisPropertiesDao().selectByAnalysisUuid(dbTester.getSession(), SNAPSHOT_UUID);
  77. assertThat(propertyDtos)
  78. .extracting(AnalysisPropertyDto::getAnalysisUuid, AnalysisPropertyDto::getKey, AnalysisPropertyDto::getValue)
  79. .containsExactlyInAnyOrder(
  80. tuple(SNAPSHOT_UUID, "sonar.analysis.branch", SMALL_VALUE2),
  81. tuple(SNAPSHOT_UUID, "sonar.analysis.empty_string", ""),
  82. tuple(SNAPSHOT_UUID, "sonar.analysis.big_value", BIG_VALUE),
  83. tuple(SNAPSHOT_UUID, "sonar.analysis.", SMALL_VALUE3),
  84. tuple(SNAPSHOT_UUID, "sonar.pullrequest.branch", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE2),
  85. tuple(SNAPSHOT_UUID, "sonar.pullrequest.empty_string", ""),
  86. tuple(SNAPSHOT_UUID, "sonar.pullrequest.big_value", VALUE_PREFIX_FOR_PR_PROPERTIES + BIG_VALUE),
  87. tuple(SNAPSHOT_UUID, "sonar.pullrequest.", VALUE_PREFIX_FOR_PR_PROPERTIES + SMALL_VALUE3));
  88. }
  89. @Test
  90. public void persist_filtering_of_properties_is_case_sensitive() {
  91. when(analysisMetadataHolder.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID));
  92. when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(ImmutableList.of(
  93. newContextProperty("sonar.ANALYSIS.foo", "foo"),
  94. newContextProperty("sonar.anaLysis.bar", "bar"),
  95. newContextProperty("sonar.anaLYSIS.doo", "doh"),
  96. newContextProperty("sonar.PULLREQUEST.foo", "foo"),
  97. newContextProperty("sonar.pullRequest.bar", "bar"),
  98. newContextProperty("sonar.pullREQUEST.doo", "doh")).iterator()));
  99. when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
  100. underTest.execute(new TestComputationStepContext());
  101. assertThat(dbTester.countRowsOfTable("analysis_properties")).isZero();
  102. }
  103. @Test
  104. public void persist_should_store_nothing_if_there_are_no_context_properties() {
  105. when(analysisMetadataHolder.getScmRevision()).thenReturn(Optional.of(SCM_REV_ID));
  106. when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator());
  107. when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID);
  108. underTest.execute(new TestComputationStepContext());
  109. assertThat(dbTester.countRowsOfTable("analysis_properties")).isZero();
  110. }
  111. @Test
  112. public void verify_description_value() {
  113. assertThat(underTest.getDescription()).isEqualTo("Persist analysis properties");
  114. }
  115. private static ScannerReport.ContextProperty newContextProperty(String key, String value) {
  116. return ScannerReport.ContextProperty.newBuilder()
  117. .setKey(key)
  118. .setValue(value)
  119. .build();
  120. }
  121. }