--- /dev/null
+sonar.projectKey=sample
+sonar.projectName=Xoo2 Sample
+sonar.projectVersion=1.0-SNAPSHOT
+sonar.sources=src/main/xoo
--- /dev/null
+package sample;
+
+public class Sample {
+
+ public Sample(int i) {
+ int j = i++;
+ }
+
+ private String myMethod() {
+ if (foo == bar) {
+ return "hello";
+ } else {
+ throw new IllegalStateException();
+ }
+ }
+}
--- /dev/null
+ncloc:7
+#Used by dashboard/widgets tests
+complexity:4
+complexity_in_classes:5
+cognitive_complexity:6
+classes:2
+comment_lines:4
+public_api:6
+public_undocumented_api:1
import org.sonarqube.tests.serverSystem.ServerSystemRestartingOrchestrator;
import org.sonarqube.tests.settings.LicensesPageTest;
import org.sonarqube.tests.settings.SettingsTestRestartingOrchestrator;
-import org.sonarqube.tests.telemetry.TelemetryTest;
+import org.sonarqube.tests.telemetry.TelemetryOptOutTest;
+import org.sonarqube.tests.telemetry.TelemetryUploadTest;
import org.sonarqube.tests.updateCenter.UpdateCenterTest;
import org.sonarqube.tests.user.OnboardingTest;
import org.sonarqube.tests.user.RealmAuthenticationTest;
ActiveRuleEsResilienceTest.class,
RuleEsResilienceTest.class,
UserEsResilienceTest.class,
- TelemetryTest.class,
+ TelemetryUploadTest.class,
+ TelemetryOptOutTest.class,
// ce
CeWorkersTest.class
})
--- /dev/null
+/*
+ * 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.sonarqube.tests.telemetry;
+
+import com.sonar.orchestrator.Orchestrator;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+import javax.ws.rs.core.HttpHeaders;
+import okhttp3.mockwebserver.MockWebServer;
+import okhttp3.mockwebserver.RecordedRequest;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.sonarqube.tests.Tester;
+import org.sonarqube.ws.client.GetRequest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.jsonToMap;
+import static util.ItUtils.setServerProperty;
+import static util.ItUtils.xooPlugin;
+
+public class TelemetryOptOutTest {
+
+ @ClassRule
+ public static MockWebServer server = new MockWebServer();
+
+ private static Orchestrator orchestrator = Orchestrator.builderEnv()
+ .addPlugin(xooPlugin())
+ .build();
+ private static Tester tester = new Tester(orchestrator);
+
+ @ClassRule
+ public static RuleChain ruleChain = RuleChain.outerRule(orchestrator)
+ .around(tester);
+
+ @BeforeClass
+ public static void setUp() {
+ setServerProperty(orchestrator, "sonar.telemetry.enable", "false");
+ setServerProperty(orchestrator, "sonar.telemetry.url", server.url("").toString());
+ setServerProperty(orchestrator, "sonar.telemetry.frequencyInSeconds", "1");
+
+ orchestrator.restartServer();
+ }
+
+ @Test
+ public void opt_out_of_telemetry() throws Exception {
+ RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS);
+
+ assertThat(request.getMethod()).isEqualTo("DELETE");
+ assertThat(request.getHeader(HttpHeaders.USER_AGENT)).contains("SonarQube");
+ Map<String, Object> json = jsonToMap(request.getBody().readUtf8());
+ assertThat(json.get("id")).isEqualTo(serverId());
+ }
+
+ private String serverId() {
+ Map<String, Object> json = jsonToMap(tester.wsClient().wsConnector().call(new GetRequest("api/system/status")).failIfNotSuccessful().content());
+ return (String) json.get("id");
+ }
+}
+++ /dev/null
-/*
- * 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.sonarqube.tests.telemetry;
-
-import com.sonar.orchestrator.Orchestrator;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-import javax.ws.rs.core.HttpHeaders;
-import okhttp3.mockwebserver.MockWebServer;
-import okhttp3.mockwebserver.RecordedRequest;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-
-import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
-import static org.assertj.core.api.Assertions.assertThat;
-import static util.ItUtils.jsonToMap;
-import static util.ItUtils.xooPlugin;
-
-public class TelemetryTest {
-
- private static Orchestrator orchestrator;
-
- private MockWebServer server;
- private String url;
-
- @Before
- public void setUp() throws Exception {
- server = new MockWebServer();
- server.start();
- url = server.url("").url().toString();
- }
-
- @After
- public void tearDown() throws Exception {
- server.shutdown();
- }
-
- @Test
- public void send_telemetry_data_at_startup() throws Exception {
- String serverId = randomAlphanumeric(40);
- orchestrator = Orchestrator.builderEnv()
- .addPlugin(xooPlugin())
- .setServerProperty("sonar.telemetry.url", url)
- .setServerProperty("sonar.telemetry.frequencyInSeconds", "1")
- .setServerProperty("sonar.core.id", serverId)
- .build();
- orchestrator.start();
-
- RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS);
-
- assertThat(request.getMethod()).isEqualTo("POST");
- assertThat(request.getHeader(HttpHeaders.USER_AGENT)).contains("SonarQube");
- String body = request.getBody().readUtf8();
- System.out.println(body);
- Map<String, Object> json = jsonToMap(body);
- assertThat(json.get("id")).isEqualTo(serverId);
- assertThat(json.get("ncloc")).isEqualTo(0.0d);
- assertThat(json.get("lines")).isEqualTo(0.0d);
- assertThat(((Map)json.get("plugins")).keySet()).contains("xoo");
-
- orchestrator.stop();
- }
-
- @Test
- public void opt_out_of_telemetry() throws Exception {
- String serverId = randomAlphanumeric(40);
- orchestrator = Orchestrator.builderEnv()
- .addPlugin(xooPlugin())
- .setServerProperty("sonar.telemetry.enable", "false")
- .setServerProperty("sonar.telemetry.url", url)
- .setServerProperty("sonar.telemetry.frequencyInSeconds", "1")
- .setServerProperty("sonar.core.id", serverId)
- .build();
- orchestrator.start();
-
- RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS);
-
- assertThat(request.getMethod()).isEqualTo("DELETE");
- assertThat(request.getBody().readUtf8()).contains(serverId);
- assertThat(request.getHeader(HttpHeaders.USER_AGENT)).contains("SonarQube");
-
- orchestrator.stop();
- }
-}
--- /dev/null
+/*
+ * 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.sonarqube.tests.telemetry;
+
+import com.sonar.orchestrator.Orchestrator;
+import java.util.Map;
+import javax.ws.rs.core.HttpHeaders;
+import okhttp3.mockwebserver.MockWebServer;
+import okhttp3.mockwebserver.RecordedRequest;
+import org.junit.After;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.Timeout;
+import org.sonarqube.ws.client.GetRequest;
+import util.ItUtils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static util.ItUtils.jsonToMap;
+import static util.ItUtils.runProjectAnalysis;
+import static util.ItUtils.setServerProperty;
+import static util.ItUtils.xooPlugin;
+
+public class TelemetryUploadTest {
+
+ @Rule
+ public Timeout safeguard = Timeout.seconds(300);
+
+ @Rule
+ public MockWebServer telemetryServer = new MockWebServer();
+
+ private Orchestrator orchestrator;
+
+ @After
+ public void tearDown() {
+ if (orchestrator != null) {
+ orchestrator.stop();
+ }
+ }
+
+ @Test
+ public void send_telemetry_data() throws Exception {
+ orchestrator = Orchestrator.builderEnv()
+ .addPlugin(xooPlugin())
+ .setServerProperty("sonar.telemetry.url", telemetryServer.url("").toString())
+ .build();
+ // by default telemetry payload is sent 6 hours after startup, once a week
+ orchestrator.start();
+
+ runProjectAnalysis(orchestrator, "shared/xoo-sample", "sonar.projectKey", "xoo-sample-1");
+ runProjectAnalysis(orchestrator, "shared/xoo-sample", "sonar.projectKey", "xoo-sample-2");
+ runProjectAnalysis(orchestrator, "shared/xoo2-sample", "sonar.projectKey", "xoo2-sample");
+
+ // no payload received at that time
+ assertThat(telemetryServer.getRequestCount()).isEqualTo(0);
+
+ // increase frequency so that payload is sent quickly after startup
+ setServerProperty(orchestrator, "sonar.telemetry.frequencyInSeconds", "1");
+ orchestrator.restartServer();
+
+ RecordedRequest request = telemetryServer.takeRequest();
+ assertThat(request.getMethod()).isEqualTo("POST");
+ assertThat(request.getHeader(HttpHeaders.USER_AGENT)).contains("SonarQube");
+ Map<String, Object> json = jsonToMap(request.getBody().readUtf8());
+ assertThat(json.get("id")).isEqualTo(serverId());
+ assertThat(getInteger(json.get("userCount"))).isEqualTo(1);
+ assertThat(((Map) json.get("plugins")).keySet()).contains("xoo");
+ assertThat(getInteger(json.get("ncloc"))).isEqualTo(13 * 2 + 7);
+ assertThat(getInteger(json.get("lines"))).isEqualTo(17 * 3);
+ Map projectCountByLanguage = (Map) json.get("projectCountByLanguage");
+ assertThat(getInteger(projectCountByLanguage.get("xoo"))).isEqualTo(2);
+ assertThat(getInteger(projectCountByLanguage.get("xoo2"))).isEqualTo(1);
+ Map nclocByLanguage = (Map) json.get("nclocByLanguage");
+ assertThat(getInteger(nclocByLanguage.get("xoo"))).isEqualTo(13 * 2);
+ assertThat(getInteger(nclocByLanguage.get("xoo2"))).isEqualTo(7);
+ }
+
+ private String serverId() {
+ Map<String, Object> json = jsonToMap(ItUtils.newWsClient(orchestrator).wsConnector().call(new GetRequest("api/system/status")).failIfNotSuccessful().content());
+ return (String) json.get("id");
+ }
+
+ private static int getInteger(Object jsonValue) {
+ double value = (Double) jsonValue;
+ return (int) Math.round(value);
+ }
+}