diff options
author | Teryk Bellahsene <teryk.bellahsene@sonarsource.com> | 2017-08-21 11:07:55 +0200 |
---|---|---|
committer | Teryk Bellahsene <teryk@users.noreply.github.com> | 2017-08-30 16:24:53 +0200 |
commit | 972b352ce1166382b420aef8af3601418071a774 (patch) | |
tree | 76a6d275ccee3eb6acda14cab91425b394bf9bb7 /server | |
parent | 3bf1fff4825f4dfcb7ba9bab8272a188ceda45a8 (diff) | |
download | sonarqube-972b352ce1166382b420aef8af3601418071a774.tar.gz sonarqube-972b352ce1166382b420aef8af3601418071a774.zip |
SONAR-9721 Send Server UUID to Telemetry server
Diffstat (limited to 'server')
13 files changed, 633 insertions, 0 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 2dd4f1f7378..57f67a4874a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -185,6 +185,7 @@ import org.sonar.server.source.ws.LinesAction; import org.sonar.server.source.ws.RawAction; import org.sonar.server.source.ws.ScmAction; import org.sonar.server.source.ws.SourcesWs; +import org.sonar.server.telemetry.TelemetryModule; import org.sonar.server.test.index.TestIndex; import org.sonar.server.test.index.TestIndexDefinition; import org.sonar.server.test.index.TestIndexer; @@ -529,6 +530,10 @@ public class PlatformLevel4 extends PlatformLevel { RecoveryIndexer.class, ProjectIndexersImpl.class); + addIfStartupLeader( + // Telemetry + TelemetryModule.class); + addAll(level4AddedComponents); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java new file mode 100644 index 00000000000..d318eef39e9 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryClient.java @@ -0,0 +1,60 @@ +/* + * 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 com.google.common.base.Throwables; +import java.io.IOException; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import org.sonar.api.config.Configuration; +import org.sonar.api.server.ServerSide; + +@ServerSide +public class TelemetryClient { + private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + + private final OkHttpClient okHttpClient; + private final TelemetryUrl serverUrl; + + public TelemetryClient(OkHttpClient okHttpClient, Configuration config) { + this.okHttpClient = okHttpClient; + this.serverUrl = new TelemetryUrl(config); + } + + void send(String json) { + try { + Request request = buildHttpRequest(json); + okHttpClient.newCall(request).execute(); + } catch (IOException e) { + Throwables.propagate(e); + } + } + + private Request buildHttpRequest(String json) { + Request.Builder request = new Request.Builder(); + request.url(serverUrl.get()); + RequestBody body = RequestBody.create(JSON, json); + request.post(body); + return request.build(); + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java new file mode 100644 index 00000000000..7ec293d7e19 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryDaemon.java @@ -0,0 +1,84 @@ +/* + * 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 com.google.common.util.concurrent.ThreadFactoryBuilder; +import java.io.StringWriter; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; +import org.picocontainer.Startable; +import org.sonar.api.config.Configuration; +import org.sonar.api.platform.Server; +import org.sonar.api.server.ServerSide; +import org.sonar.api.utils.log.Logger; +import org.sonar.api.utils.log.Loggers; +import org.sonar.api.utils.text.JsonWriter; + +@ServerSide +public class TelemetryDaemon implements Startable { + private static final String THREAD_NAME_PREFIX = "sq-telemetry-service-"; + private static final Logger LOG = Loggers.get(TelemetryDaemon.class); + + private final TelemetryClient telemetryClient; + private final Server server; + private final TelemetryFrequency frequencyInSeconds; + + private ScheduledExecutorService executorService; + + public TelemetryDaemon(TelemetryClient telemetryClient, Server server, Configuration config) { + this.telemetryClient = telemetryClient; + this.server = server; + this.frequencyInSeconds = new TelemetryFrequency(config); + } + + @Override + public void start() { + executorService = Executors.newSingleThreadScheduledExecutor( + new ThreadFactoryBuilder() + .setNameFormat(THREAD_NAME_PREFIX + "%d") + .setPriority(Thread.MIN_PRIORITY) + .build()); + executorService.scheduleWithFixedDelay(() -> { + try { + StringWriter json = new StringWriter(); + try (JsonWriter writer = JsonWriter.of(json)) { + writer.beginObject(); + writer.prop("id", server.getId()); + writer.endObject(); + } + telemetryClient.send(json.toString()); + } catch (Exception e) { + // fail silently + } + // do not check at start up to exclude test instance which are not up for a long time + }, frequencyInSeconds.get(), frequencyInSeconds.get(), TimeUnit.SECONDS); + } + + @Override + public void stop() { + try { + executorService.shutdown(); + executorService.awaitTermination(5, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryFrequency.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryFrequency.java new file mode 100644 index 00000000000..d2d11fe64d9 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryFrequency.java @@ -0,0 +1,41 @@ +/* + * 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.config.Configuration; + +class TelemetryFrequency { + private final Configuration config; + private Long frequency; + + TelemetryFrequency(Configuration config) { + this.config = config; + } + + long get() { + if (frequency == null) { + frequency = config.getLong(TelemetryProperties.PROP_FREQUENCY) + .orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", TelemetryProperties.PROP_FREQUENCY))); + } + + return frequency; + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryModule.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryModule.java new file mode 100644 index 00000000000..4f93a20ca3c --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryModule.java @@ -0,0 +1,32 @@ +/* + * 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.core.platform.Module; + +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 new file mode 100644 index 00000000000..a77a67fa67c --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryProperties.java @@ -0,0 +1,42 @@ +/* + * 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_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_FREQUENCY = "sonar.telemetry.frequency"; + static final String PROP_URL = "sonar.telemetry.url"; +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryUrl.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryUrl.java new file mode 100644 index 00000000000..70bf5d1244e --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/TelemetryUrl.java @@ -0,0 +1,42 @@ +/* + * 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.config.Configuration; + +import static org.sonar.server.telemetry.TelemetryProperties.PROP_URL; + +class TelemetryUrl { + private final Configuration config; + private String url; + + TelemetryUrl(Configuration config) { + this.config = config; + } + + String get() { + if (url == null) { + url = config.get(PROP_URL).orElseThrow(() -> new IllegalStateException(String.format("Setting '%s' must be provided.", PROP_URL))); + } + + return url; + } +} diff --git a/server/sonar-server/src/main/java/org/sonar/server/telemetry/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/telemetry/package-info.java new file mode 100644 index 00000000000..c3b6ec36851 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/telemetry/package-info.java @@ -0,0 +1,25 @@ +/* + * 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. + */ + +@ParametersAreNonnullByDefault +package org.sonar.server.telemetry; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/FakeServer.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/FakeServer.java new file mode 100644 index 00000000000..a4c788e6930 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/FakeServer.java @@ -0,0 +1,92 @@ +/* + * 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 java.io.File; +import java.util.Date; +import javax.annotation.CheckForNull; +import org.sonar.api.platform.Server; + +class FakeServer extends Server { + private String id; + + @Override + public String getId() { + return id; + } + + FakeServer setId(String id) { + this.id = id; + return this; + } + + @CheckForNull + @Override + public String getPermanentServerId() { + return null; + } + + @Override + public String getVersion() { + return null; + } + + @Override + public Date getStartedAt() { + return null; + } + + @Override + public File getRootDir() { + return null; + } + + @CheckForNull + @Override + public File getDeployDir() { + return null; + } + + @Override + public String getContextPath() { + return null; + } + + @Override + public String getPublicRootUrl() { + return null; + } + + @Override + public boolean isDev() { + return false; + } + + @Override + public boolean isSecured() { + return false; + } + + @Override + public String getURL() { + return null; + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java new file mode 100644 index 00000000000..1f523fd723f --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java @@ -0,0 +1,77 @@ +/* + * 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.Before; +import org.junit.Test; +import org.sonar.api.config.PropertyDefinitions; +import org.sonar.api.config.internal.MapSettings; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.contains; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; + +public class TelemetryDaemonTest { + + private TelemetryClient client = mock(TelemetryClient.class); + private FakeServer server = new FakeServer(); + private MapSettings settings; + + private TelemetryDaemon underTest; + + @Before + public void setUp() throws Exception { + settings = new MapSettings(new PropertyDefinitions(TelemetryProperties.class)); + + underTest = new TelemetryDaemon(client, server, settings.asConfig()); + } + + @Test + public void send_data_via_client_at_startup_after_initial_delay() { + settings.setProperty("sonar.telemetry.frequency", "1"); + underTest.start(); + + verify(client, timeout(2_000).atLeastOnce()).send(anyString()); + } + + @Test + public void send_data_periodically() { + settings.setProperty("sonar.telemetry.frequency", "1"); + underTest = new TelemetryDaemon(client, server, settings.asConfig()); + + underTest.start(); + + verify(client, timeout(3_000).atLeast(2)).send(anyString()); + } + + @Test + public void send_server_id() { + settings.setProperty("sonar.telemetry.frequency", "1"); + String id = randomAlphanumeric(40); + server.setId(id); + underTest.start(); + + verify(client, timeout(2_000).atLeastOnce()).send(contains(id)); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryModuleTest.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryModuleTest.java new file mode 100644 index 00000000000..57c07eb666f --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryModuleTest.java @@ -0,0 +1,34 @@ +/* + * 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.core.platform.ComponentContainer; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TelemetryModuleTest { + @Test + public void verify_count_of_added_components() { + ComponentContainer container = new ComponentContainer(); + new TelemetryModule().configure(container); + assertThat(container.size()).isEqualTo(3 + 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 new file mode 100644 index 00000000000..e3453ba7e47 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryPropertiesTest.java @@ -0,0 +1,43 @@ +/* + * 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"); + } +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryUrlTest.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryUrlTest.java new file mode 100644 index 00000000000..7f56216facf --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryUrlTest.java @@ -0,0 +1,56 @@ +/* + * 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.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.config.internal.MapSettings; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TelemetryUrlTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private MapSettings settings = new MapSettings(); + + private TelemetryUrl underTest; + + @Test + public void return_url_as_is_when_no_ending_slash() { + settings.setProperty("sonar.telemetry.url", "http://localhost:9001"); + underTest = new TelemetryUrl(settings.asConfig()); + + assertThat(underTest.get()).isEqualTo("http://localhost:9001"); + } + + @Test + public void fail_when_no_settings_to_define_muppet_url() { + underTest = new TelemetryUrl(settings.asConfig()); + + expectedException.expect(IllegalStateException.class); + expectedException.expectMessage("Setting 'sonar.telemetry.url' must be provided."); + + underTest.get(); + } +} |