From dd4f70dcfab97cb1c14d49aa28a0c63b166c1131 Mon Sep 17 00:00:00 2001 From: Eric Hartmann Date: Fri, 20 Oct 2017 16:15:53 +0200 Subject: [PATCH] Fix quality flaws --- .../PersistAnalysisPropertiesStepTest.java | 22 +++- .../issue/webhook/IssueChangeWebhookTest.java | 35 +++++ .../server/settings/ChildSettingsTest.java | 124 ++++++++++++++++++ .../PostProjectAnalysisTaskTester.java | 2 +- 4 files changed, 176 insertions(+), 7 deletions(-) create mode 100644 server/sonar-server/src/test/java/org/sonar/server/settings/ChildSettingsTest.java diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java index 3006fba9b41..26069ea64a8 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java @@ -61,14 +61,10 @@ public class PersistAnalysisPropertiesStepTest { private AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class); private PersistAnalysisPropertiesStep underTest = new PersistAnalysisPropertiesStep(dbTester.getDbClient(), analysisMetadataHolder, batchReportReader, UuidFactoryFast.getInstance()); - @Before - public void setup() { - when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator())); - when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID); - } - @Test public void persist_should_store_only_sonarDotAnalysis_properties() { + when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.from(PROPERTIES.iterator())); + when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID); underTest.execute(); assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(4); @@ -85,6 +81,20 @@ public class PersistAnalysisPropertiesStepTest { ); } + @Test + public void persist_should_not_store_anything_if_there_is_no_sonarDotAnalysis_properties() { + when(batchReportReader.readContextProperties()).thenReturn(CloseableIterator.emptyCloseableIterator()); + when(analysisMetadataHolder.getUuid()).thenReturn(SNAPSHOT_UUID); + + underTest.execute(); + assertThat(dbTester.countRowsOfTable("analysis_properties")).isEqualTo(0); + } + + @Test + public void verify_description_value() { + assertThat(underTest.getDescription()).isEqualTo("Persist analysis properties"); + } + private static ScannerReport.ContextProperty newContextProperty(String key, String value) { return ScannerReport.ContextProperty.newBuilder() .setKey(key) diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/webhook/IssueChangeWebhookTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/webhook/IssueChangeWebhookTest.java index dde3ac524ea..b88f25a7228 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/webhook/IssueChangeWebhookTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/webhook/IssueChangeWebhookTest.java @@ -24,6 +24,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.rules.RuleType; +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; public class IssueChangeWebhookTest { @@ -50,4 +51,38 @@ public class IssueChangeWebhookTest { assertThat(transitionKeyAndRuleType.getTransitionKey()).contains("bar"); assertThat(transitionKeyAndRuleType.getRuleType()).contains(RuleType.VULNERABILITY); } + + @Test + public void verify_IssueChange_equality() { + IssueChangeWebhook.IssueChange underTest = new IssueChangeWebhook.IssueChange(RuleType.BUG); + + assertThat(underTest).isEqualTo(new IssueChangeWebhook.IssueChange(RuleType.BUG)); + assertThat(underTest).isEqualTo(new IssueChangeWebhook.IssueChange(RuleType.BUG, null)); + + assertThat(underTest).isNotEqualTo(null); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.BUG, randomAlphanumeric(10))); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.CODE_SMELL)); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.CODE_SMELL, randomAlphanumeric(10))); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.VULNERABILITY)); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.VULNERABILITY, randomAlphanumeric(10))); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(randomAlphanumeric(10))); + + String transitionKey = randomAlphanumeric(10); + underTest = new IssueChangeWebhook.IssueChange(transitionKey); + + assertThat(underTest).isEqualTo(new IssueChangeWebhook.IssueChange(transitionKey)); + assertThat(underTest).isEqualTo(new IssueChangeWebhook.IssueChange(null, transitionKey)); + + assertThat(underTest).isNotEqualTo(null); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.BUG)); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.BUG, transitionKey)); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.BUG, randomAlphanumeric(10))); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.CODE_SMELL)); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.CODE_SMELL, randomAlphanumeric(10))); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.CODE_SMELL, transitionKey)); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.VULNERABILITY)); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.VULNERABILITY, randomAlphanumeric(10))); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(RuleType.VULNERABILITY, transitionKey)); + assertThat(underTest).isNotEqualTo(new IssueChangeWebhook.IssueChange(randomAlphanumeric(9))); + } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/settings/ChildSettingsTest.java b/server/sonar-server/src/test/java/org/sonar/server/settings/ChildSettingsTest.java new file mode 100644 index 00000000000..bf4c39d4fbd --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/settings/ChildSettingsTest.java @@ -0,0 +1,124 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.server.settings; + +import java.util.Collections; +import java.util.Date; +import java.util.Optional; +import java.util.Random; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.config.PropertyDefinition; +import org.sonar.api.config.PropertyDefinitions; +import org.sonar.api.config.internal.MapSettings; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class ChildSettingsTest { + private static final Random RANDOM = new Random(); + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MapSettings parent = new MapSettings(); + private ChildSettings underTest = new ChildSettings(parent); + + @Test + public void childSettings_should_retrieve_parent_settings() { + String multipleValuesKey = randomAlphanumeric(19); + PropertyDefinition multipleValues = PropertyDefinition.builder(multipleValuesKey).multiValues(true).build(); + MapSettings parent = new MapSettings(new PropertyDefinitions(Collections.singletonList(multipleValues))); + ChildSettings underTest = new ChildSettings(parent); + + parent.setProperty(randomAlphanumeric(10), randomAlphanumeric(20)); + parent.setProperty(randomAlphanumeric(11), RANDOM.nextLong()); + parent.setProperty(randomAlphanumeric(12), RANDOM.nextDouble()); + parent.setProperty(randomAlphanumeric(13), RANDOM.nextFloat()); + parent.setProperty(randomAlphanumeric(14), RANDOM.nextBoolean()); + parent.setProperty(randomAlphanumeric(15), RANDOM.nextInt()); + parent.setProperty(randomAlphanumeric(16), new Date(RANDOM.nextInt())); + parent.setProperty(randomAlphanumeric(17), new Date(RANDOM.nextInt()), true); + parent.setProperty(randomAlphanumeric(18), new Date(RANDOM.nextInt()), false); + parent.setProperty(multipleValuesKey, new String[] { randomAlphanumeric(10), randomAlphanumeric(20) }); + + assertThat(underTest.getProperties()).isEqualTo(parent.getProperties()); + } + + @Test + public void set_will_throw_NPE_if_key_is_null() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("key can't be null"); + + underTest.set(null, ""); + } + + + @Test + public void set_will_throw_NPE_if_value_is_null() { + expectedException.expect(NullPointerException.class); + expectedException.expectMessage("value can't be null"); + + underTest.set(randomAlphanumeric(10), null); + } + + @Test + public void childSettings_override_parent() { + String key = randomAlphanumeric(10); + parent.setProperty(key, randomAlphanumeric(20)); + underTest.setProperty(key, randomAlphanumeric(10)); + + assertThat(underTest.get(key)).isNotEqualTo(parent.getString(key)); + } + + @Test + public void remove_should_not_throw_exception_if_key_is_not_present() { + underTest.remove(randomAlphanumeric(90)); + } + + @Test + public void remove_should_remove_value() { + String key = randomAlphanumeric(10); + String childValue = randomAlphanumeric(10); + + underTest.set(key, childValue); + assertThat(underTest.get(key)).isEqualTo(Optional.of(childValue)); + + underTest.remove(key); + assertThat(underTest.get(key)).isEqualTo(Optional.empty()); + } + + @Test + public void remove_should_retrieve_parent_value() { + String key = randomAlphanumeric(10); + String childValue = randomAlphanumeric(10); + String parentValue = randomAlphanumeric(10); + + parent.setProperty(key, parentValue); + underTest.set(key, childValue); + assertThat(underTest.get(key)).isEqualTo(Optional.of(childValue)); + + underTest.remove(key); + assertThat(underTest.get(key)).isEqualTo(Optional.of(parentValue)); + } + +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java index 34f861791b7..3d0e678190a 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java @@ -193,7 +193,7 @@ public class PostProjectAnalysisTaskTester { requireNonNull(date, DATE_CAN_NOT_BE_NULL); Analysis analysis = null; - if (date != null && analysisUuid != null) { + if (analysisUuid != null) { analysis = new AnalysisBuilder() .setDate(date) .setAnalysisUuid(analysisUuid) -- 2.39.5