aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2017-09-11 17:13:59 +0200
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2017-09-11 17:14:22 +0200
commite443b392f16be800923f102021332581111ae25c (patch)
tree8066479fe5a55656355b1f8c4c0efa2ea1af72f7
parent7696ef7a87b0c72d8236be8f7cdae143514b62aa (diff)
downloadsonarqube-e443b392f16be800923f102021332581111ae25c.tar.gz
sonarqube-e443b392f16be800923f102021332581111ae25c.zip
Improve code coverage of TelemetryDaemon
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryClientTest.java81
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java2
2 files changed, 82 insertions, 1 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryClientTest.java b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryClientTest.java
new file mode 100644
index 00000000000..fd6f89bae95
--- /dev/null
+++ b/server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryClientTest.java
@@ -0,0 +1,81 @@
+/*
+ * 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.IOException;
+import okhttp3.MediaType;
+import okhttp3.OkHttpClient;
+import okhttp3.Request;
+import okio.Buffer;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.sonar.api.config.internal.MapSettings;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.sonar.core.config.TelemetryProperties.PROP_URL;
+
+public class TelemetryClientTest {
+
+ private static final String JSON = "{\"key\":\"value\"}";
+ private static final String TELEMETRY_URL = "https://telemetry.com/url";
+
+ private OkHttpClient okHttpClient = mock(OkHttpClient.class, RETURNS_DEEP_STUBS);
+ private MapSettings settings = new MapSettings();
+
+ private TelemetryClient underTest = new TelemetryClient(okHttpClient, settings.asConfig());
+
+ @Test
+ public void upload() throws IOException {
+ ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
+ settings.setProperty(PROP_URL, TELEMETRY_URL);
+
+ underTest.upload(JSON);
+
+ verify(okHttpClient).newCall(requestCaptor.capture());
+ Request request = requestCaptor.getValue();
+ assertThat(request.method()).isEqualTo("POST");
+ assertThat(request.body().contentType()).isEqualTo(MediaType.parse("application/json; charset=utf-8"));
+ Buffer body = new Buffer();
+ request.body().writeTo(body);
+ assertThat(body.readUtf8()).isEqualTo(JSON);
+ assertThat(request.url().toString()).isEqualTo(TELEMETRY_URL);
+ }
+
+ @Test
+ public void opt_out() throws IOException {
+ ArgumentCaptor<Request> requestCaptor = ArgumentCaptor.forClass(Request.class);
+ settings.setProperty(PROP_URL, TELEMETRY_URL);
+
+ underTest.optOut(JSON);
+
+ verify(okHttpClient).newCall(requestCaptor.capture());
+ Request request = requestCaptor.getValue();
+ assertThat(request.method()).isEqualTo("DELETE");
+ assertThat(request.body().contentType()).isEqualTo(MediaType.parse("application/json; charset=utf-8"));
+ Buffer body = new Buffer();
+ request.body().writeTo(body);
+ assertThat(body.readUtf8()).isEqualTo(JSON);
+ assertThat(request.url().toString()).isEqualTo(TELEMETRY_URL);
+ }
+}
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
index f246e455917..b1a49758f76 100644
--- 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
@@ -190,7 +190,7 @@ public class TelemetryDaemonTest {
underTest.start();
- verify(client, timeout(2_000).atLeastOnce()).upload(anyString());
+ verify(client, timeout(2_000).times(1)).upload(anyString());
assertThat(internalProperties.read("telemetry.lastPing").get()).isEqualTo(String.valueOf(today));
}