]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9721 Telemetry properties defined in a specific PropertyDefinition class
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 22 Aug 2017 15:42:14 +0000 (17:42 +0200)
committerTeryk Bellahsene <teryk@users.noreply.github.com>
Wed, 30 Aug 2017 14:24:53 +0000 (16:24 +0200)
14 files changed:
server/sonar-ce/src/test/java/org/sonar/ce/container/ComputeEngineContainerImplTest.java
server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java
server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryFrequency.java
server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryModule.java
server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryProperties.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryUrl.java
server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java
server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryModuleTest.java
server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryPropertiesTest.java [deleted file]
sonar-core/src/main/java/org/sonar/core/config/CorePropertyDefinitions.java
sonar-core/src/main/java/org/sonar/core/config/TelemetryProperties.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/config/CorePropertyDefinitionsTest.java
sonar-core/src/test/java/org/sonar/core/config/TelemetryPropertiesTest.java [new file with mode: 0644]
tests/src/test/java/org/sonarqube/tests/telemetry/TelemetryTest.java

index 52c35f2b271c83b16ec6d501a9f416ad5d4e0222..6d4f3e9514add8239d33c4fb0bc6a16ae3ab3ddf 100644 (file)
@@ -137,7 +137,7 @@ public class ComputeEngineContainerImplTest {
         + 25 // level 1
         + 47 // content of DaoModule
         + 3 // content of EsSearchModule
-        + 58 // content of CorePropertyDefinitions
+        + 61 // content of CorePropertyDefinitions
     );
     assertThat(
       picoContainer.getComponentAdapters().stream()
index c303529b94e76aa592764cdbb9100a776409f82d..7c5a6c2c89a99d1e9089904c747a7cef8dced1ef 100644 (file)
@@ -37,15 +37,15 @@ import org.sonar.server.property.InternalProperties;
 
 import static org.sonar.api.utils.DateUtils.formatDate;
 import static org.sonar.api.utils.DateUtils.parseDate;
-import static org.sonar.server.telemetry.TelemetryProperties.PROP_ENABLE;
-import static org.sonar.server.telemetry.TelemetryProperties.PROP_URL;
+import static org.sonar.core.config.TelemetryProperties.PROP_ENABLE;
+import static org.sonar.core.config.TelemetryProperties.PROP_URL;
 
 @ServerSide
 public class TelemetryDaemon implements Startable {
   private static final String THREAD_NAME_PREFIX = "sq-telemetry-service-";
   private static final int SEVEN_DAYS = 7 * 24 * 60 * 60 * 1_000;
-  private static final String I_PROP_LAST_PING = "sonar.telemetry.lastPing";
-  private static final String I_PROP_OPT_OUT = "sonar.telemetry.optOut";
+  static final String I_PROP_LAST_PING = "telemetry.lastPing";
+  static final String I_PROP_OPT_OUT = "telemetry.optOut";
   private static final Logger LOG = Loggers.get(TelemetryDaemon.class);
 
   private final TelemetryClient telemetryClient;
index d2d11fe64d97b48639f9163beec6db7cd05e5c16..54b574c1c9dc0431cfc310807c3563144b697f3a 100644 (file)
@@ -32,8 +32,8 @@ class TelemetryFrequency {
 
   long get() {
     if (frequency == null) {
-      frequency = config.getLong(TelemetryProperties.PROP_FREQUENCY)
-        .orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", TelemetryProperties.PROP_FREQUENCY)));
+      frequency = config.getLong(org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY)
+        .orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY)));
     }
 
     return frequency;
index 4f93a20ca3cfe256f6efa4dfcf08b46b1b4b69f8..d9a576458dcdd5b608b4fb38c5d7a6db77a70ad4 100644 (file)
@@ -25,7 +25,6 @@ public class TelemetryModule extends Module {
   @Override
   protected void configureModule() {
     add(
-      TelemetryProperties.class,
       TelemetryDaemon.class,
       TelemetryClient.class);
   }
diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryProperties.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryProperties.java
deleted file mode 100644 (file)
index 90a5f43..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * 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.telemetry;
-
-import org.sonar.api.Properties;
-import org.sonar.api.Property;
-
-@Properties({
-  @Property(
-    key = TelemetryProperties.PROP_ENABLE,
-    defaultValue = "true",
-    name = "Share SonarQube statistics",
-    global = false),
-   @Property(
-    key = TelemetryProperties.PROP_FREQUENCY,
-    // 6 hours in seconds
-    defaultValue = "21600",
-    name = "Frequency of telemetry checks, in seconds",
-    global = false),
-  @Property(
-    key = TelemetryProperties.PROP_URL,
-    defaultValue = "https://telemetry.sonarsource.com/sonarqube",
-    name = "URL where telemetry data is sent",
-    global = false)
-})
-public class TelemetryProperties {
-  static final String PROP_ENABLE = "sonar.telemetry.enable";
-  static final String PROP_FREQUENCY = "sonar.telemetry.frequency";
-  static final String PROP_URL = "sonar.telemetry.url";
-}
index 70bf5d1244e1062b61e7706d92d16790d1c1365b..8e32b0711709f4c24cffe2e66540cc5498b51bfd 100644 (file)
@@ -22,7 +22,7 @@ package org.sonar.server.telemetry;
 
 import org.sonar.api.config.Configuration;
 
-import static org.sonar.server.telemetry.TelemetryProperties.PROP_URL;
+import static org.sonar.core.config.TelemetryProperties.PROP_URL;
 
 class TelemetryUrl {
   private final Configuration config;
index 3d0d66436384516d0c5bdd33e04388917c473c0c..cb1aeb72ff14b16ba5beab8f1e577ef440fea267 100644 (file)
@@ -25,6 +25,7 @@ import org.junit.Test;
 import org.sonar.api.config.PropertyDefinitions;
 import org.sonar.api.config.internal.MapSettings;
 import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.core.config.TelemetryProperties;
 import org.sonar.server.property.InternalProperties;
 import org.sonar.server.property.MapInternalProperties;
 
@@ -36,6 +37,9 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.timeout;
 import static org.mockito.Mockito.verify;
 import static org.sonar.api.utils.DateUtils.parseDate;
+import static org.sonar.core.config.TelemetryProperties.PROP_ENABLE;
+import static org.sonar.core.config.TelemetryProperties.PROP_FREQUENCY;
+import static org.sonar.server.telemetry.TelemetryDaemon.I_PROP_LAST_PING;
 
 public class TelemetryDaemonTest {
 
@@ -52,7 +56,7 @@ public class TelemetryDaemonTest {
 
   @Before
   public void setUp() throws Exception {
-    settings = new MapSettings(new PropertyDefinitions(TelemetryProperties.class));
+    settings = new MapSettings(new PropertyDefinitions(TelemetryProperties.all()));
     system2.setNow(System.currentTimeMillis());
 
     underTest = new TelemetryDaemon(client, settings.asConfig(), internalProperties, server, system2);
@@ -60,7 +64,7 @@ public class TelemetryDaemonTest {
 
   @Test
   public void send_data_via_client_at_startup_after_initial_delay() {
-    settings.setProperty("sonar.telemetry.frequency", "1");
+    settings.setProperty(PROP_FREQUENCY, "1");
     underTest.start();
 
     verify(client, timeout(1_000).atLeastOnce()).send(anyString());
@@ -71,18 +75,18 @@ public class TelemetryDaemonTest {
     long now = system2.now();
     long sixDaysAgo = now - (ONE_DAY * 6L);
     long sevenDaysAgo = now - (ONE_DAY * 7L);
-    internalProperties.write("sonar.telemetry.lastPing", String.valueOf(sixDaysAgo));
-    settings.setProperty("sonar.telemetry.frequency", "1");
+    internalProperties.write(I_PROP_LAST_PING, String.valueOf(sixDaysAgo));
+    settings.setProperty(PROP_FREQUENCY, "1");
     underTest.start();
     verify(client, timeout(1_000).never()).send(anyString());
-    internalProperties.write("sonar.telemetry.lastPing", String.valueOf(sevenDaysAgo));
+    internalProperties.write(I_PROP_LAST_PING, String.valueOf(sevenDaysAgo));
 
     verify(client, timeout(1_000).atLeastOnce()).send(anyString());
   }
 
   @Test
   public void send_server_id() {
-    settings.setProperty("sonar.telemetry.frequency", "1");
+    settings.setProperty(PROP_FREQUENCY, "1");
     String id = randomAlphanumeric(40);
     server.setId(id);
     underTest.start();
@@ -92,11 +96,11 @@ public class TelemetryDaemonTest {
 
   @Test
   public void do_not_send_data_if_last_ping_earlier_than_one_week_ago() {
-    settings.setProperty("sonar.telemetry.frequency", "1");
+    settings.setProperty(PROP_FREQUENCY, "1");
     long now = system2.now();
     long sixDaysAgo = now - (ONE_DAY * 6L);
 
-    internalProperties.write("sonar.telemetry.lastPing", String.valueOf(sixDaysAgo));
+    internalProperties.write(I_PROP_LAST_PING, String.valueOf(sixDaysAgo));
     underTest.start();
 
     verify(client, timeout(2_000).never()).send(anyString());
@@ -104,23 +108,23 @@ public class TelemetryDaemonTest {
 
   @Test
   public void send_data_if_last_ping_is_one_week_ago() {
-    settings.setProperty("sonar.telemetry.frequency", "1");
+    settings.setProperty(PROP_FREQUENCY, "1");
     long today = parseDate("2017-08-01").getTime();
     system2.setNow(today + 15 * ONE_HOUR);
     long now = system2.now();
     long sevenDaysAgo = now - (ONE_DAY * 7L);
-    internalProperties.write("sonar.telemetry.lastPing", String.valueOf(sevenDaysAgo));
+    internalProperties.write(I_PROP_LAST_PING, String.valueOf(sevenDaysAgo));
 
     underTest.start();
 
     verify(client, timeout(1_000).atLeastOnce()).send(anyString());
-    assertThat(internalProperties.read("sonar.telemetry.lastPing").get()).isEqualTo(String.valueOf(today));
+    assertThat(internalProperties.read(I_PROP_LAST_PING).get()).isEqualTo(String.valueOf(today));
   }
 
   @Test
   public void opt_out_sent_once() {
-    settings.setProperty("sonar.telemetry.frequency", "1");
-    settings.setProperty("sonar.telemetry.enable", "false");
+    settings.setProperty(PROP_FREQUENCY, "1");
+    settings.setProperty(PROP_ENABLE, "false");
     underTest.start();
     underTest.start();
 
index 57c07eb666f37fa5fec44ebbb0c12ee731b1acbe..7a3e7df10065cf8abe323ad3736cd0a58832a261 100644 (file)
@@ -29,6 +29,6 @@ public class TelemetryModuleTest {
   public void verify_count_of_added_components() {
     ComponentContainer container = new ComponentContainer();
     new TelemetryModule().configure(container);
-    assertThat(container.size()).isEqualTo(3 + 2);
+    assertThat(container.size()).isEqualTo(2 + 2);
   }
 }
diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryPropertiesTest.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryPropertiesTest.java
deleted file mode 100644 (file)
index e3453ba..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-/*
- * 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.telemetry;
-
-import org.junit.Test;
-import org.sonar.api.config.Configuration;
-import org.sonar.api.config.PropertyDefinitions;
-import org.sonar.api.config.internal.MapSettings;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class TelemetryPropertiesTest {
-
-  Configuration underTest = new MapSettings(new PropertyDefinitions(TelemetryProperties.class)).asConfig();
-
-  @Test
-  public void default_frequency_is_6_hours_in_seconds() {
-    assertThat(underTest.getInt("sonar.telemetry.frequency")).contains(6 * 60 * 60);
-  }
-
-  @Test
-  public void default_url_point_to_telemetry_server() {
-    assertThat(underTest.get("sonar.telemetry.url")).contains("https://telemetry.sonarsource.com/sonarqube");
-  }
-}
index d1b3dbea6c9f7b18759f509f8747297ad1373b12..66c270011c69790bd370d775160f9e328b122d1c 100644 (file)
@@ -62,6 +62,7 @@ public class CorePropertyDefinitions {
     defs.addAll(PurgeProperties.all());
     defs.addAll(EmailSettings.definitions());
     defs.addAll(WebhookProperties.all());
+    defs.addAll(TelemetryProperties.all());
 
     defs.addAll(ImmutableList.of(
       PropertyDefinition.builder(PROP_PASSWORD)
diff --git a/sonar-core/src/main/java/org/sonar/core/config/TelemetryProperties.java b/sonar-core/src/main/java/org/sonar/core/config/TelemetryProperties.java
new file mode 100644 (file)
index 0000000..7eaf0b4
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * 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.core.config;
+
+import com.google.common.collect.ImmutableList;
+import java.util.List;
+import org.sonar.api.PropertyType;
+import org.sonar.api.config.PropertyDefinition;
+
+public class TelemetryProperties {
+
+  public static final String PROP_ENABLE = "sonar.telemetry.enable";
+  public static final String PROP_FREQUENCY = "sonar.telemetry.frequencyInSeconds";
+  public static final String PROP_URL = "sonar.telemetry.url";
+
+  private TelemetryProperties() {
+    // only static stuff
+  }
+
+  public static List<PropertyDefinition> all() {
+    return ImmutableList.of(
+      PropertyDefinition.builder(PROP_ENABLE)
+        .defaultValue(Boolean.toString(true))
+        .type(PropertyType.BOOLEAN)
+        .name("Share SonarQube statistics")
+        .description("By sharing anonymous SonarQube statistics, you help us understand how SonarQube is used so we can improve the plugin to work even better for you. " +
+          "We don't collect source code or IP addresses. And we don't share the data with anyone else.")
+        .hidden()
+        .build(),
+      PropertyDefinition.builder(PROP_FREQUENCY)
+        // 6 hours in seconds
+        .defaultValue("21600")
+        .type(PropertyType.INTEGER)
+        .name("Frequency of telemetry checks, in seconds")
+        .hidden()
+        .build(),
+      PropertyDefinition.builder(PROP_URL)
+        .defaultValue("https://telemetry.sonarsource.com/sonarqube")
+        .type(PropertyType.STRING)
+        .name("URL where telemetry data is sent")
+        .hidden()
+        .build()
+    );
+
+  }
+}
index 50a3028672f8e5a5c1bd2f709c507fc79db40399..22b04b1f5e47075558d8be17c1bb2ede0a74b491 100644 (file)
@@ -33,7 +33,7 @@ public class CorePropertyDefinitionsTest {
   @Test
   public void all() {
     List<PropertyDefinition> defs = CorePropertyDefinitions.all();
-    assertThat(defs).hasSize(58);
+    assertThat(defs).hasSize(61);
   }
 
   @Test
diff --git a/sonar-core/src/test/java/org/sonar/core/config/TelemetryPropertiesTest.java b/sonar-core/src/test/java/org/sonar/core/config/TelemetryPropertiesTest.java
new file mode 100644 (file)
index 0000000..f66919e
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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.core.config;
+
+import org.junit.Test;
+import org.sonar.api.config.Configuration;
+import org.sonar.api.config.PropertyDefinitions;
+import org.sonar.api.config.internal.MapSettings;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class TelemetryPropertiesTest {
+
+  private Configuration underTest = new MapSettings(new PropertyDefinitions(TelemetryProperties.all())).asConfig();
+
+  @Test
+  public void default_telemetry_properties() {
+    assertThat(underTest.getBoolean("sonar.telemetry.enable")).hasValue(true);
+    assertThat(underTest.getInt("sonar.telemetry.frequencyInSeconds")).hasValue(6 * 60 * 60);
+    assertThat(underTest.get("sonar.telemetry.url")).hasValue("https://telemetry.sonarsource.com/sonarqube");
+  }
+}
index e65fb8651805ec1e6f0482df13f30d7c0ea51e6f..0521010d90a2231cf07ec55d66a16338328f4b0b 100644 (file)
@@ -57,7 +57,7 @@ public class TelemetryTest {
     orchestrator = Orchestrator.builderEnv()
       .addPlugin(xooPlugin())
       .setServerProperty("sonar.telemetry.url", url)
-      .setServerProperty("sonar.telemetry.frequency", "1")
+      .setServerProperty("sonar.telemetry.frequencyInSeconds", "1")
       .setServerProperty("sonar.core.id", serverId)
       .build();
     orchestrator.start();
@@ -78,7 +78,7 @@ public class TelemetryTest {
       .addPlugin(xooPlugin())
       .setServerProperty("sonar.telemetry.enable", "false")
       .setServerProperty("sonar.telemetry.url", url)
-      .setServerProperty("sonar.telemetry.frequency", "1")
+      .setServerProperty("sonar.telemetry.frequencyInSeconds", "1")
       .setServerProperty("sonar.core.id", serverId)
       .build();
     orchestrator.start();