Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more unit tests for some services #2653

Merged
merged 9 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,115 @@

package org.apache.hertzbeat.collector.collect.database;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.JdbcProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link JdbcCommonCollect}
*/
class JdbcCommonCollectTest {
private JdbcCommonCollect jdbcCommonCollect;

@BeforeEach
void setUp() {
void setup() {
jdbcCommonCollect = new JdbcCommonCollect();
}

@Test
void preCheck() {
assertThrows(IllegalArgumentException.class, () -> {
jdbcCommonCollect.preCheck(null);
});
assertThrows(IllegalArgumentException.class, () -> {
Metrics metrics = new Metrics();
jdbcCommonCollect.preCheck(metrics);
});

assertDoesNotThrow(() -> {
JdbcProtocol jdbc = new JdbcProtocol();
jdbc.setUrl("jdbc:mysql://localhost:3306/test");

Metrics metrics = new Metrics();
metrics.setJdbc(jdbc);
jdbcCommonCollect.preCheck(metrics);
});

String[] invalidKeywords = new String[]{
"allowLoadLocalInfile", "allowLoadLocalInfileInPath", "useLocalInfile"
};
for (String keyword : invalidKeywords) {
// contains not allowed keywords
assertThrows(IllegalArgumentException.class, () -> {
JdbcProtocol jdbc = new JdbcProtocol();
jdbc.setUrl("jdbc:mysql://localhost:3306/test?" + keyword);

Metrics metrics = new Metrics();
metrics.setJdbc(jdbc);
jdbcCommonCollect.preCheck(metrics);
});
}
}

@Test
void collect() {
assertDoesNotThrow(() -> {
JdbcProtocol jdbc = new JdbcProtocol();
jdbc.setUrl("jdbc:mysql://localhost:3306/test");
jdbc.setUsername("root");
jdbc.setPassword("123456");
jdbc.setQueryType("select");

Metrics metrics = new Metrics();
metrics.setJdbc(jdbc);

CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();
jdbcCommonCollect.collect(builder, 1, "test", metrics);
});

String[] platforms = new String[]{
"mysql", "mariadb",
"postgresql",
"clickhouse",
"sqlserver",
"oracle",
"dm"
};
for (String platform : platforms) {
assertDoesNotThrow(() -> {
JdbcProtocol jdbc = new JdbcProtocol();
jdbc.setPlatform(platform);

Metrics metrics = new Metrics();
metrics.setJdbc(jdbc);

CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();
jdbcCommonCollect.collect(builder, 1, "test", metrics);
});
}
// invalid platform
assertThrows(IllegalArgumentException.class, () -> {
JdbcProtocol jdbc = new JdbcProtocol();
jdbc.setPlatform("invalid");

Metrics metrics = new Metrics();
metrics.setJdbc(jdbc);

CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();
jdbcCommonCollect.collect(builder, 1, "test", metrics);
});
}

@Test
void getInstance() {
void supportProtocol() {
String protocol = jdbcCommonCollect.supportProtocol();
assertEquals(DispatchConstants.PROTOCOL_JDBC, protocol);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,52 @@

package org.apache.hertzbeat.collector.collect.http;

import org.junit.jupiter.api.AfterEach;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.apache.hertzbeat.common.entity.job.Metrics;
import org.apache.hertzbeat.common.entity.job.protocol.HttpProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Test case for {@link HttpCollectImpl}
*/
class HttpCollectImplTest {
private HttpCollectImpl httpCollectImpl;

@BeforeEach
void setUp() {
httpCollectImpl = new HttpCollectImpl();
}

@AfterEach
void tearDown() {
@Test
void preCheck() {
assertThrows(IllegalArgumentException.class, () -> {
httpCollectImpl.preCheck(null);
});

assertThrows(IllegalArgumentException.class, () -> {
Metrics metrics = Metrics.builder().build();
httpCollectImpl.preCheck(metrics);
});
}

@Test
void getInstance() {
void collect() {
HttpProtocol http = HttpProtocol.builder().build();
http.setMethod("POST");
Metrics metrics = Metrics.builder()
.http(http)
.build();
CollectRep.MetricsData.Builder builder = CollectRep.MetricsData.newBuilder();

httpCollectImpl.collect(builder, 1L, "app", metrics);
}

@Test
void collect() {
void supportProtocol() {
String protocol = httpCollectImpl.supportProtocol();
assert "http".equals(protocol);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ public BulletinMetricsData buildBulletinMetricsData(BulletinMetricsData.Bulletin
fieldsList = Collections.singletonList(fields.stream()
.map(field -> BulletinMetricsData.Field.builder()
.key(field)
.unit("")
.value("NO_DATA")
.unit(EMPTY_STRING)
.value(NO_DATA)
.build())
.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.manager.scheduler;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import org.apache.hertzbeat.common.entity.message.CollectRep;
import java.util.ArrayList;
import java.util.List;

import org.apache.hertzbeat.common.entity.job.Job;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

/**
* Test case for {@link CollectorJobScheduler}
*/
@ExtendWith(MockitoExtension.class)
public class CollectorJobSchedulerTest {
@InjectMocks
private CollectorJobScheduler collectorJobScheduler;
@Mock
private ConsistentHash consistentHash;

@Test
public void testCollectSyncJobData() {
assertDoesNotThrow(() -> {
Job job = new Job();
when(consistentHash.preDispatchJob(any(String.class))).thenReturn(null);
List<?> list = collectorJobScheduler.collectSyncJobData(job);
assertEquals(1, list.size());
});
}

@Test
public void testCollectSyncJobResource() {
assertDoesNotThrow(() -> {
collectorJobScheduler.collectSyncJobResponse(null);
collectorJobScheduler.collectSyncJobResponse(new ArrayList<>());

List<CollectRep.MetricsData> metricsDataList = new ArrayList<CollectRep.MetricsData>();
metricsDataList.add(CollectRep.MetricsData.newBuilder().build());
collectorJobScheduler.collectSyncJobResponse(metricsDataList);
});
}
}
Loading
Loading