]> source.dussan.org Git - sonarqube.git/commitdiff
Fix quality flaws
authorEric Hartmann <hartmann.eric@gmail.com>
Fri, 20 Oct 2017 14:15:53 +0000 (16:15 +0200)
committerEric Hartmann <hartmann.eric@gmail.Com>
Fri, 20 Oct 2017 14:39:51 +0000 (16:39 +0200)
server/sonar-server/src/test/java/org/sonar/server/computation/task/projectanalysis/step/PersistAnalysisPropertiesStepTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/webhook/IssueChangeWebhookTest.java
server/sonar-server/src/test/java/org/sonar/server/settings/ChildSettingsTest.java [new file with mode: 0644]
sonar-plugin-api/src/main/java/org/sonar/api/ce/posttask/PostProjectAnalysisTaskTester.java

index 3006fba9b41e548b503dee2c312045d7346b78df..26069ea64a879f763213a898f196bc1d31e53148 100644 (file)
@@ -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)
index dde3ac524ea6019b1e5a8d37d3ef42cf34a0fb08..b88f25a7228d7ac94ae95da70cb1e0012a4ffb89 100644 (file)
@@ -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 (file)
index 0000000..bf4c39d
--- /dev/null
@@ -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));
+  }
+
+}
index 34f861791b75174d7516aae4099a653690c4c884..3d0e678190a0191f429c9c4af84e7904e2bf5f24 100644 (file)
@@ -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)