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 SystemInfoSection section1 = mock(SystemInfoSection.class);
52 private SystemInfoSection section2 = mock(SystemInfoSection.class);
53 private CeHttpClient ceHttpClient = mock(CeHttpClientImpl.class, Mockito.RETURNS_MOCKS);
54 private TestStandaloneHealthChecker healthChecker = new TestStandaloneHealthChecker();
55 private TelemetryDataLoader telemetry = mock(TelemetryDataLoader.class, Mockito.RETURNS_MOCKS);
56 private TelemetryDataJsonWriter dataJsonWriter = new TelemetryDataJsonWriter();
58 private StandaloneSystemInfoWriter underTest = new StandaloneSystemInfoWriter(telemetry, ceHttpClient, healthChecker, dataJsonWriter, section1, section2);
61 public void write_json() {
62 logInAsSystemAdministrator();
64 ProtobufSystemInfo.Section.Builder attributes1 = ProtobufSystemInfo.Section.newBuilder()
65 .setName("Section One");
66 setAttribute(attributes1, "foo", "bar");
67 when(section1.toProtobuf()).thenReturn(attributes1.build());
69 ProtobufSystemInfo.Section.Builder attributes2 = ProtobufSystemInfo.Section.newBuilder()
70 .setName("Section Two");
71 setAttribute(attributes2, "one", 1);
72 setAttribute(attributes2, "two", 2);
73 when(section2.toProtobuf()).thenReturn(attributes2.build());
74 when(ceHttpClient.retrieveSystemInfo()).thenReturn(Optional.empty());
76 StringWriter writer = new StringWriter();
77 JsonWriter jsonWriter = JsonWriter.of(writer);
78 jsonWriter.beginObject();
79 underTest.write(jsonWriter);
80 jsonWriter.endObject();
81 // response does not contain empty "Section Three"
82 assertThat(writer.toString()).isEqualTo("{\"Health\":\"GREEN\",\"Health Causes\":[],\"Section One\":{\"foo\":\"bar\"},\"Section Two\":{\"one\":1,\"two\":2}," +
83 "\"Statistics\":{\"id\":\"\",\"version\":\"\",\"database\":{\"name\":\"\",\"version\":\"\"},\"plugins\":[],\"userCount\":0,\"projectCount\":0,\"usingBranches\":false," +
84 "\"ncloc\":0,\"projectCountByLanguage\":[],\"nclocByLanguage\":[],\"installationDate\":0,\"installationVersion\":\"\",\"docker\":false}}");
87 private void logInAsSystemAdministrator() {
88 userSessionRule.logIn().setSystemAdministrator();