From 71980f9e23296f78ec053d5294a61d9638dfbb14 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Thu, 25 Apr 2024 11:32:31 +0800 Subject: [PATCH] Add test for HttpsdImpl --- .../collector/collect/httpsd/HttpsdImpl.java | 7 +- .../collect/httpsd/HttpsdImplTest.java | 137 ++++++++++++++++++ 2 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 collector/src/test/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImplTest.java diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImpl.java index 6c7f102e10f..a828850c1df 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImpl.java @@ -20,8 +20,10 @@ package org.apache.hertzbeat.collector.collect.httpsd; import com.ecwid.consul.transport.TransportException; +import com.google.common.annotations.VisibleForTesting; import java.lang.reflect.Field; import java.util.Objects; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.hertzbeat.collector.collect.AbstractCollect; @@ -42,7 +44,10 @@ @Slf4j public class HttpsdImpl extends AbstractCollect { private static final String SERVER = "server"; - private final DiscoveryClientManagement discoveryClientManagement = new DiscoveryClientManagement(); + + @Setter + @VisibleForTesting + private DiscoveryClientManagement discoveryClientManagement = new DiscoveryClientManagement(); @Override public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) { diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImplTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImplTest.java new file mode 100644 index 00000000000..b67c63e1be7 --- /dev/null +++ b/collector/src/test/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImplTest.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.collector.collect.httpsd; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.ArrayList; +import java.util.List; +import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClient; +import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClientManagement; +import org.apache.hertzbeat.collector.collect.httpsd.discovery.ServerInfo; +import org.apache.hertzbeat.collector.collect.httpsd.discovery.ServiceInstance; +import org.apache.hertzbeat.common.entity.job.Metrics; +import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol; +import org.apache.hertzbeat.common.entity.message.CollectRep; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.Spy; +import org.mockito.junit.jupiter.MockitoExtension; + +/** + * Test case for {@link HttpsdImpl} + */ +@ExtendWith(MockitoExtension.class) +class HttpsdImplTest { + @InjectMocks + @Spy + private HttpsdImpl httpsd; + + @Mock + private DiscoveryClient client; + + @Mock + private DiscoveryClientManagement discoveryClientManagement; + + + @Test + void testServerCollect() { + CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder(); + + String port = "123"; + String host = "127.0.0.1"; + HttpsdProtocol httpsdProtocol = HttpsdProtocol.builder() + .port(port) + .host(host) + .discoveryClientTypeName("consul") + .build(); + List aliasField = new ArrayList<>(); + aliasField.add("address"); + aliasField.add("port"); + aliasField.add("responseTime"); + Metrics metrics = new Metrics(); + metrics.setName("server"); + metrics.setHttpsd(httpsdProtocol); + metrics.setAliasFields(aliasField); + + Mockito.when(discoveryClientManagement.getClient(httpsdProtocol)).thenReturn(client); + ServerInfo serverInfo = ServerInfo.builder() + .address(host) + .port(port) + .build(); + Mockito.when(client.getServerInfo()).thenReturn(serverInfo); + httpsd.setDiscoveryClientManagement(discoveryClientManagement); + + httpsd.collect(builder, 1L, "test", metrics); + for (CollectRep.ValueRow valueRow : builder.getValuesList()) { + assertEquals(host, valueRow.getColumns(0)); + assertEquals(port, valueRow.getColumns(1)); + assertNotNull(valueRow.getColumns(2)); + } + } + + @Test + void testServiceCollect() { + CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder(); + + String port = "123"; + String host = "127.0.0.1"; + HttpsdProtocol httpsdProtocol = HttpsdProtocol.builder() + .port(port) + .host(host) + .discoveryClientTypeName("consul") + .build(); + List aliasField = new ArrayList<>(); + aliasField.add("serviceId"); + aliasField.add("serviceName"); + aliasField.add("address"); + aliasField.add("port"); + Metrics metrics = new Metrics(); + metrics.setName("service"); + metrics.setHttpsd(httpsdProtocol); + metrics.setAliasFields(aliasField); + + Mockito.when(discoveryClientManagement.getClient(httpsdProtocol)).thenReturn(client); + + String serviceId = "test"; + String serviceName = "service"; + List serviceInstances = new ArrayList<>(); + serviceInstances.add(ServiceInstance.builder() + .serviceId(serviceId) + .serviceName(serviceName) + .address(host) + .port(port) + .build()); + + Mockito.when(client.getServices()).thenReturn(serviceInstances); + httpsd.setDiscoveryClientManagement(discoveryClientManagement); + + httpsd.collect(builder, 1L, "test", metrics); + assertEquals(builder.getValuesCount(), 1); + for (CollectRep.ValueRow valueRow : builder.getValuesList()) { + assertEquals(serviceId, valueRow.getColumns(0)); + assertEquals(serviceName, valueRow.getColumns(1)); + assertEquals(host, valueRow.getColumns(2)); + assertEquals(port, valueRow.getColumns(3)); + } + } + +} \ No newline at end of file