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.Collections;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.Mockito;
27 import org.sonar.api.utils.text.JsonWriter;
28 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Attribute;
29 import org.sonar.process.systeminfo.protobuf.ProtobufSystemInfo.Section;
30 import org.sonar.server.health.ClusterHealth;
31 import org.sonar.server.health.Health;
32 import org.sonar.server.health.HealthChecker;
33 import org.sonar.server.platform.monitoring.cluster.AppNodesInfoLoader;
34 import org.sonar.server.platform.monitoring.cluster.GlobalInfoLoader;
35 import org.sonar.server.platform.monitoring.cluster.NodeInfo;
36 import org.sonar.server.platform.monitoring.cluster.SearchNodesInfoLoader;
37 import org.sonar.server.telemetry.TelemetryDataJsonWriter;
38 import org.sonar.server.telemetry.TelemetryDataLoader;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
44 public class ClusterSystemInfoWriterTest {
45 private final GlobalInfoLoader globalInfoLoader = mock(GlobalInfoLoader.class);
46 private final AppNodesInfoLoader appNodesInfoLoader = mock(AppNodesInfoLoader.class);
47 private final SearchNodesInfoLoader searchNodesInfoLoader = mock(SearchNodesInfoLoader.class);
48 private final HealthChecker healthChecker = mock(HealthChecker.class);
49 private final TelemetryDataLoader telemetry = mock(TelemetryDataLoader.class, Mockito.RETURNS_MOCKS);
50 private final TelemetryDataJsonWriter dataJsonWriter = new TelemetryDataJsonWriter();
51 private final ClusterSystemInfoWriter underTest = new ClusterSystemInfoWriter(globalInfoLoader, appNodesInfoLoader,
52 searchNodesInfoLoader, healthChecker, telemetry, dataJsonWriter);
55 public void before() throws InterruptedException {
56 when(globalInfoLoader.load()).thenReturn(Collections.singletonList(createSection("globalInfo")));
57 when(appNodesInfoLoader.load()).thenReturn(Collections.singletonList(createNodeInfo("appNodes")));
58 when(searchNodesInfoLoader.load()).thenReturn(Collections.singletonList(createNodeInfo("searchNodes")));
59 Health health = Health.newHealthCheckBuilder().setStatus(Health.Status.GREEN).build();
60 when(healthChecker.checkCluster()).thenReturn(new ClusterHealth(health, Collections.emptySet()));
64 public void writeInfo() throws InterruptedException {
65 StringWriter writer = new StringWriter();
66 JsonWriter jsonWriter = JsonWriter.of(writer);
67 jsonWriter.beginObject();
68 underTest.write(jsonWriter);
69 jsonWriter.endObject();
71 assertThat(writer).hasToString("{\"Health\":\"GREEN\","
72 + "\"Health Causes\":[],\"\":{\"name\":\"globalInfo\"},"
73 + "\"Application Nodes\":[{\"Name\":\"appNodes\",\"\":{\"name\":\"appNodes\"}}],"
74 + "\"Search Nodes\":[{\"Name\":\"searchNodes\",\"\":{\"name\":\"searchNodes\"}}],"
75 + "\"Statistics\":{\"id\":\"\",\"version\":\"\",\"database\":{\"name\":\"\",\"version\":\"\"},\"plugins\":[],"
76 + "\"userCount\":0,\"projectCount\":0,\"usingBranches\":false,\"ncloc\":0,\"projectCountByLanguage\":[],"
77 + "\"nclocByLanguage\":[],\"almIntegrationCount\":[],\"externalAuthProviders\":[],\"projectCountByScm\":[],\"sonarlintWeeklyUsers\":0,\"installationDate\":0,"
78 + "\"installationVersion\":\"\",\"docker\":false}}");
81 private static NodeInfo createNodeInfo(String name) {
82 NodeInfo info = new NodeInfo(name);
83 info.addSection(createSection(name));
87 private static Section createSection(String name) {
88 return Section.newBuilder()
89 .addAttributes(Attribute.newBuilder().setKey("name").setStringValue(name).build())