3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform;
22 import java.io.StringWriter;
23 import java.util.Optional;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.mockito.Mockito;
28 import org.sonar.api.utils.text.JsonWriter;
29 import org.sonar.process.systeminfo.SystemInfoSection;
30 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo;
31 import org.sonar.server.ce.http.CeHttpClient;
32 import org.sonar.server.ce.http.CeHttpClientImpl;
33 import org.sonar.server.health.TestStandaloneHealthChecker;
34 import org.sonar.server.telemetry.TelemetryDataJsonWriter;
35 import org.sonar.server.telemetry.TelemetryDataLoader;
36 import org.sonar.server.tester.UserSessionRule;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.mock;
40 import static org.mockito.Mockito.when;
41 import static org.sonar.process.systeminfo.SystemInfoUtils.setAttribute;
43 public class StandaloneSystemInfoWriterTest {
45 public UserSessionRule userSessionRule = UserSessionRule.standalone()
49 public ExpectedException expectedException = ExpectedException.none();
51 private final SystemInfoSection section1 = mock(SystemInfoSection.class);
52 private final SystemInfoSection section2 = mock(SystemInfoSection.class);
53 private final CeHttpClient ceHttpClient = mock(CeHttpClientImpl.class, Mockito.RETURNS_MOCKS);
54 private final TestStandaloneHealthChecker healthChecker = new TestStandaloneHealthChecker();
55 private final TelemetryDataLoader telemetry = mock(TelemetryDataLoader.class, Mockito.RETURNS_MOCKS);
56 private final TelemetryDataJsonWriter dataJsonWriter = new TelemetryDataJsonWriter();
57 private final StandaloneSystemInfoWriter underTest = new StandaloneSystemInfoWriter(telemetry, ceHttpClient, healthChecker, dataJsonWriter, section1, section2);
60 public void write_json() {
61 logInAsSystemAdministrator();
63 ProtobufSystemInfo.Section.Builder attributes1 = ProtobufSystemInfo.Section.newBuilder()
64 .setName("Section One");
65 setAttribute(attributes1, "foo", "bar");
66 when(section1.toProtobuf()).thenReturn(attributes1.build());
68 ProtobufSystemInfo.Section.Builder attributes2 = ProtobufSystemInfo.Section.newBuilder()
69 .setName("Section Two");
70 setAttribute(attributes2, "one", 1);
71 setAttribute(attributes2, "two", 2);
72 when(section2.toProtobuf()).thenReturn(attributes2.build());
73 when(ceHttpClient.retrieveSystemInfo()).thenReturn(Optional.empty());
75 StringWriter writer = new StringWriter();
76 JsonWriter jsonWriter = JsonWriter.of(writer);
77 jsonWriter.beginObject();
78 underTest.write(jsonWriter);
79 jsonWriter.endObject();
80 // response does not contain empty "Section Three"
81 assertThat(writer).hasToString("{\"Health\":\"GREEN\",\"Health Causes\":[],\"Section One\":{\"foo\":\"bar\"},\"Section Two\":{\"one\":1,\"two\":2}," +
82 "\"Statistics\":{\"id\":\"\",\"version\":\"\",\"database\":{\"name\":\"\",\"version\":\"\"},\"plugins\":[],\"userCount\":0,\"projectCount\":0,\"usingBranches\":false," +
83 "\"ncloc\":0,\"projectCountByLanguage\":[],\"nclocByLanguage\":[],\"almIntegrationCount\":[],\"externalAuthProviders\":[],\"projectCountByScm\":[],"
84 + "\"sonarlintWeeklyUsers\":0,\"installationDate\":0,\"installationVersion\":\"\",\"docker\":false}}");
87 private void logInAsSystemAdministrator() {
88 userSessionRule.logIn().setSystemAdministrator();