]> source.dussan.org Git - sonarqube.git/blob
e22bbe0cd20ce99be30bf6b41894618ee8f8442c
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.platform;
21
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;
39
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
43
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);
53
54   @Before
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()));
61   }
62
63   @Test
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();
70
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}}");
79   }
80
81   private static NodeInfo createNodeInfo(String name) {
82     NodeInfo info = new NodeInfo(name);
83     info.addSection(createSection(name));
84     return info;
85   }
86
87   private static Section createSection(String name) {
88     return Section.newBuilder()
89       .addAttributes(Attribute.newBuilder().setKey("name").setStringValue(name).build())
90       .build();
91   }
92 }