Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ContextPropertiesPublisherTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.scanner.report;
  21. import com.google.common.collect.Lists;
  22. import java.util.Arrays;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. import org.junit.Before;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.ExpectedException;
  30. import org.sonar.scanner.config.DefaultConfiguration;
  31. import org.sonar.scanner.protocol.output.ScannerReport;
  32. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  33. import org.sonar.scanner.repository.ContextPropertiesCache;
  34. import static java.util.Collections.emptyList;
  35. import static org.mockito.ArgumentMatchers.argThat;
  36. import static org.mockito.Mockito.mock;
  37. import static org.mockito.Mockito.verify;
  38. import static org.mockito.Mockito.when;
  39. public class ContextPropertiesPublisherTest {
  40. @Rule
  41. public ExpectedException expectedException = ExpectedException.none();
  42. private ScannerReportWriter writer = mock(ScannerReportWriter.class);
  43. private ContextPropertiesCache cache = new ContextPropertiesCache();
  44. private DefaultConfiguration config = mock(DefaultConfiguration.class);
  45. private Map<String, String> props = new HashMap<>();
  46. private ContextPropertiesPublisher underTest = new ContextPropertiesPublisher(cache, config);
  47. @Before
  48. public void prepareMock() {
  49. when(config.getProperties()).thenReturn(props);
  50. }
  51. @Test
  52. public void publish_writes_properties_to_report() {
  53. cache.put("foo1", "bar1");
  54. cache.put("foo2", "bar2");
  55. underTest.publish(writer);
  56. List<ScannerReport.ContextProperty> expected = Arrays.asList(
  57. newContextProperty("foo1", "bar1"),
  58. newContextProperty("foo2", "bar2"));
  59. expectWritten(expected);
  60. }
  61. @Test
  62. public void publish_writes_no_properties_to_report() {
  63. underTest.publish(writer);
  64. expectWritten(emptyList());
  65. }
  66. @Test
  67. public void publish_settings_prefixed_with_sonar_analysis_for_webhooks() {
  68. props.put("foo", "should not be exported");
  69. props.put("sonar.analysis.revision", "ab45b3");
  70. props.put("sonar.analysis.build.number", "B123");
  71. underTest.publish(writer);
  72. List<ScannerReport.ContextProperty> expected = Arrays.asList(
  73. newContextProperty("sonar.analysis.revision", "ab45b3"),
  74. newContextProperty("sonar.analysis.build.number", "B123"));
  75. expectWritten(expected);
  76. }
  77. private void expectWritten(List<ScannerReport.ContextProperty> expected) {
  78. verify(writer).writeContextProperties(argThat(props -> {
  79. List<ScannerReport.ContextProperty> copy = Lists.newArrayList(props);
  80. copy.removeAll(expected);
  81. return copy.isEmpty();
  82. }));
  83. }
  84. private static ScannerReport.ContextProperty newContextProperty(String key, String value) {
  85. return ScannerReport.ContextProperty.newBuilder()
  86. .setKey(key)
  87. .setValue(value)
  88. .build();
  89. }
  90. }