From 025a7d9259cf1722cc5ad2578ab0380b89e2f799 Mon Sep 17 00:00:00 2001 From: crossoverJie Date: Fri, 26 Apr 2024 23:29:53 +0800 Subject: [PATCH] [test] add test for FtpCollectImpl (#1856) Co-authored-by: tomsun28 --- .../collector/collect/ftp/FtpCollectImpl.java | 7 +- .../collect/ftp/FtpCollectImplTest.java | 149 ++++++++++++++++++ 2 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 collector/src/test/java/org/apache/hertzbeat/collector/collect/ftp/FtpCollectImplTest.java diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/ftp/FtpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/ftp/FtpCollectImpl.java index c4f3afe8646..e03f527b367 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/ftp/FtpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/ftp/FtpCollectImpl.java @@ -41,12 +41,11 @@ public class FtpCollectImpl extends AbstractCollect { private static final String ANONYMOUS = "anonymous"; private static final String PASSWORD = "password"; + @Override public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) { FTPClient ftpClient = new FTPClient(); FtpProtocol ftpProtocol = metrics.getFtp(); - // Set timeout - ftpClient.setControlKeepAliveReplyTimeout(Integer.parseInt(ftpProtocol.getTimeout())); // Judge whether the basic information is wrong try { preCheck(metrics); @@ -56,6 +55,9 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri builder.setMsg(e.getMessage()); return; } + // Set timeout + ftpClient.setControlKeepAliveReplyTimeout(Integer.parseInt(ftpProtocol.getTimeout())); + // Collect data to load in CollectRep.ValueRow.Builder's object CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); Map valueMap; @@ -150,6 +152,7 @@ private void preCheck(Metrics metrics) { Assert.hasText(ftpProtocol.getHost(), "Ftp Protocol host is required."); Assert.hasText(ftpProtocol.getPort(), "Ftp Protocol port is required."); Assert.hasText(ftpProtocol.getDirection(), "Ftp Protocol direction is required."); + Assert.hasText(ftpProtocol.getTimeout(), "Ftp Protocol timeout is required."); } @Override diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/collect/ftp/FtpCollectImplTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/collect/ftp/FtpCollectImplTest.java new file mode 100644 index 00000000000..529559592f0 --- /dev/null +++ b/collector/src/test/java/org/apache/hertzbeat/collector/collect/ftp/FtpCollectImplTest.java @@ -0,0 +1,149 @@ +/* + * 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.ftp; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.net.ftp.FTPClient; +import org.apache.hertzbeat.common.entity.job.Metrics; +import org.apache.hertzbeat.common.entity.job.protocol.FtpProtocol; +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.MockedConstruction; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; + +/** + * Test case for {@link FtpCollectImpl} + */ +@ExtendWith(MockitoExtension.class) +class FtpCollectImplTest { + + @InjectMocks + private FtpCollectImpl ftpCollectImpl; + + @Test + void testPreCheck() { + CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder(); + FtpProtocol ftpProtocol = FtpProtocol.builder() + .host("127.0.0.1") + .username("admin") + .password("123456") + .port("21") + .direction("/") + .build(); + + List aliasField = new ArrayList<>(); + aliasField.add("isActive"); + aliasField.add("responseTime"); + Metrics metrics = new Metrics(); + metrics.setName("server"); + metrics.setFtp(ftpProtocol); + metrics.setAliasFields(aliasField); + ftpCollectImpl.collect(builder, 1L, "test", metrics); + assertEquals(builder.getCode(), CollectRep.Code.UN_CONNECTABLE); + + } + + @Test + void testCollect() throws IOException { + CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder(); + FtpProtocol ftpProtocol = FtpProtocol.builder() + .host("127.0.0.1") + .username("admin") + .password("123456") + .port("21") + .timeout("3000") + .direction("/") + .build(); + + boolean isActive = false; + MockedConstruction mocked = Mockito.mockConstruction(FTPClient.class, + (ftpClient, context) -> { + Mockito.doNothing().when(ftpClient).connect(ftpProtocol.getHost(), + Integer.parseInt(ftpProtocol.getPort())); + + Mockito.doAnswer(invocationOnMock -> true).when(ftpClient) + .login(ftpProtocol.getUsername(), ftpProtocol.getPassword()); + Mockito.when(ftpClient.changeWorkingDirectory(ftpProtocol.getDirection())).thenReturn(isActive); + Mockito.doNothing().when(ftpClient).disconnect(); + }); + + + List aliasField = new ArrayList<>(); + aliasField.add("isActive"); + aliasField.add("responseTime"); + Metrics metrics = new Metrics(); + metrics.setFtp(ftpProtocol); + metrics.setAliasFields(aliasField); + + ftpCollectImpl.collect(builder, 1L, "test", metrics); + assertEquals(builder.getValuesCount(), 1); + for (CollectRep.ValueRow valueRow : builder.getValuesList()) { + assertEquals(Boolean.toString(isActive), valueRow.getColumns(0)); + assertNotNull(valueRow.getColumns(1)); + } + mocked.close(); + + } + + @Test + void testAnonymousCollect() throws IOException { + CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder(); + FtpProtocol ftpProtocol = FtpProtocol.builder() + .host("127.0.0.1") + .port("21") + .timeout("3000") + .direction("/") + .build(); + + boolean isActive = true; + MockedConstruction mocked = Mockito.mockConstruction(FTPClient.class, + (ftpClient, context) -> { + Mockito.doNothing().when(ftpClient).connect(ftpProtocol.getHost(), + Integer.parseInt(ftpProtocol.getPort())); + Mockito.doAnswer(invocationOnMock -> true).when(ftpClient).login("anonymous", "password"); + Mockito.when(ftpClient.changeWorkingDirectory(ftpProtocol.getDirection())).thenReturn(isActive); + Mockito.doNothing().when(ftpClient).disconnect(); + }); + + List aliasField = new ArrayList<>(); + aliasField.add("isActive"); + aliasField.add("responseTime"); + Metrics metrics = new Metrics(); + metrics.setFtp(ftpProtocol); + metrics.setAliasFields(aliasField); + + ftpCollectImpl.collect(builder, 1L, "test", metrics); + assertEquals(builder.getValuesCount(), 1); + for (CollectRep.ValueRow valueRow : builder.getValuesList()) { + assertEquals(Boolean.toString(isActive), valueRow.getColumns(0)); + assertNotNull(valueRow.getColumns(1)); + } + mocked.close(); + + } + + +} \ No newline at end of file