]> source.dussan.org Git - sonarqube.git/commitdiff
Improve code coverage of TelemetryDaemon
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 11 Sep 2017 15:13:59 +0000 (17:13 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Mon, 11 Sep 2017 15:14:22 +0000 (17:14 +0200)
server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryClientTest.java [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/telemetry/TelemetryDaemonTest.java

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 (file)
index 0000000..fd6f89b
--- /dev/null
@@ -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);
+  }
+}
index f246e455917b447b8c05096a1f27731a796975bf..b1a49758f76c8525c0525a5ce401323cbe788b59 100644 (file)
@@ -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));
   }