Skip to content

Commit

Permalink
[test] add test for FtpCollectImpl (#1856)
Browse files Browse the repository at this point in the history
Co-authored-by: tomsun28 <[email protected]>
  • Loading branch information
crossoverJie and tomsun28 authored Apr 26, 2024
1 parent 72ff8c3 commit 025a7d9
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<String, String> valueMap;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<FTPClient> 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<String> 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<FTPClient> 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<String> 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();

}


}

0 comments on commit 025a7d9

Please sign in to comment.