diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollectTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollectTest.java index 5bcad13479b..92c5550d790 100644 --- a/collector/src/test/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollectTest.java +++ b/collector/src/test/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollectTest.java @@ -17,6 +17,14 @@ 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; @@ -24,12 +32,100 @@ * 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); } } diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java index 5cf86da63cb..e20010639dc 100644 --- a/collector/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java +++ b/collector/src/test/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImplTest.java @@ -17,7 +17,11 @@ 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; @@ -25,20 +29,40 @@ * 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); } } \ No newline at end of file diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java index 6142df26052..fe868521d28 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/BulletinServiceImpl.java @@ -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()); } diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/scheduler/CollectorJobSchedulerTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/scheduler/CollectorJobSchedulerTest.java new file mode 100644 index 00000000000..5cf96f030e6 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/scheduler/CollectorJobSchedulerTest.java @@ -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 metricsDataList = new ArrayList(); + metricsDataList.add(CollectRep.MetricsData.newBuilder().build()); + collectorJobScheduler.collectSyncJobResponse(metricsDataList); + }); + } +} diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/BulletinServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/BulletinServiceTest.java new file mode 100644 index 00000000000..a69175eba88 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/BulletinServiceTest.java @@ -0,0 +1,210 @@ +/* + * 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.service; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyList; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.hertzbeat.common.entity.manager.Monitor; +import org.apache.hertzbeat.common.entity.manager.bulletin.Bulletin; +import org.apache.hertzbeat.common.entity.manager.bulletin.BulletinDto; +import org.apache.hertzbeat.common.entity.manager.bulletin.BulletinMetricsData; +import org.apache.hertzbeat.manager.dao.BulletinDao; +import org.apache.hertzbeat.manager.service.impl.BulletinServiceImpl; +import org.apache.hertzbeat.warehouse.store.realtime.RealTimeDataReader; +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; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.jpa.domain.Specification; + +/** + * Test case for {@link BulletinService} + */ +@ExtendWith(MockitoExtension.class) +public class BulletinServiceTest { + @InjectMocks + private BulletinServiceImpl bulletinService; + + @Mock + private BulletinDao bulletinDao; + + @Mock + private MonitorService monitorService; + + @Mock + private RealTimeDataReader realTimeDataReader; + + @Test + public void testValidate() throws Exception { + assertThrows(IllegalArgumentException.class, () -> { + bulletinService.validate(null); + }); + + assertThrows(IllegalArgumentException.class, () -> { + bulletinService.validate(new BulletinDto()); + }); + + assertThrows(IllegalArgumentException.class, () -> { + BulletinDto obj = new BulletinDto(); + obj.setApp("app"); + bulletinService.validate(obj); + }); + + assertThrows(IllegalArgumentException.class, () -> { + Map> fields = new HashMap>(); + fields.put("field1", null); + + BulletinDto obj = new BulletinDto(); + obj.setApp("app"); + obj.setFields(fields); + bulletinService.validate(obj); + }); + + assertDoesNotThrow(() -> { + Map> fields = new HashMap>(); + fields.put("field1", null); + + List ids = new ArrayList(); + ids.add((long) 1); + + BulletinDto obj = new BulletinDto(); + obj.setApp("app"); + obj.setFields(fields); + obj.setMonitorIds(ids); + bulletinService.validate(obj); + }); + } + + @Test + public void testGetBulletinByName() throws Exception { + Bulletin bulletin = new Bulletin(); + + when(bulletinDao.findByName("test")).thenReturn(bulletin); + assertEquals(bulletin, bulletinService.getBulletinByName("test")); + } + + @Test + public void testGetAllNames() throws Exception { + Bulletin bulletin = new Bulletin(); + bulletin.setName("test"); + + List items = new ArrayList(); + items.add(bulletin); + + List names = new ArrayList(); + names.add("test"); + + when(bulletinDao.findAll()).thenReturn(items); + assertEquals(names, bulletinService.getAllNames()); + } + + @Test + public void testAddBulletin() throws Exception { + Map> fields = new HashMap>(); + fields.put("field1", null); + + BulletinDto bulletinDto = new BulletinDto(); + bulletinDto.setApp("app"); + bulletinDto.setFields(fields); + + assertDoesNotThrow(() -> { + bulletinService.addBulletin(bulletinDto); + }); + } + + @Test + public void testGetBulletins() throws Exception { + Bulletin bulletin = new Bulletin(); + bulletin.setId((long) 1); + + PageRequest pageRequest = PageRequest.of(0, 10); + + List content = Collections.singletonList(bulletin); + long total = 1; + + Page items = new PageImpl(content, pageRequest, total); + + when(bulletinDao.findAll(any(Specification.class), any(PageRequest.class))).thenReturn(items); + assertNotNull(bulletinService.getBulletins(null, pageRequest)); + } + + @Test + public void testBuildBulletinMetricsData() throws Exception { + List ids = new ArrayList(); + ids.add((long) 1); + + Bulletin bulletin = new Bulletin(); + bulletin.setId((long) 1); + bulletin.setMonitorIds(ids); + bulletin.setFields(""" + {"1": ["1", "2"]} + """); + + BulletinMetricsData.BulletinMetricsDataBuilder contentBuilder = BulletinMetricsData.builder(); + + Monitor monitor = new Monitor(); + + when(realTimeDataReader.getCurrentMetricsData(any(), any(String.class))).thenReturn(null); + when(monitorService.getMonitor(any(Long.class))).thenReturn(monitor); + assertNotNull(bulletinService.buildBulletinMetricsData(contentBuilder, bulletin)); + } + + @Test + public void testGetBulletinById() throws Exception { + Bulletin bulletin = new Bulletin(); + bulletin.setId((long) 1); + + when(bulletinDao.findById((long) 1)).thenReturn(java.util.Optional.of(bulletin)); + assertEquals(bulletin, bulletinService.getBulletinById((long) 1).get()); + } + + @Test + public void testDeleteBulletinByName() throws Exception { + List names = new ArrayList(); + names.add("test"); + + assertDoesNotThrow(() -> { + bulletinService.deleteBulletinByName(names); + }); + + assertThrows(RuntimeException.class, () -> { + doAnswer(invocation -> { + throw new RuntimeException("test"); + }).when(bulletinDao).deleteByNameIn(anyList()); + + bulletinService.deleteBulletinByName(null); + }); + } +} diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/CollectorServiceTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/CollectorServiceTest.java index d98c282015e..c31ec4c8b6c 100644 --- a/manager/src/test/java/org/apache/hertzbeat/manager/service/CollectorServiceTest.java +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/CollectorServiceTest.java @@ -18,11 +18,18 @@ package org.apache.hertzbeat.manager.service; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; + import java.util.ArrayList; import java.util.List; +import java.util.Optional; + +import org.apache.hertzbeat.common.entity.manager.Collector; +import org.apache.hertzbeat.common.support.exception.CommonException; import org.apache.hertzbeat.manager.dao.CollectorDao; import org.apache.hertzbeat.manager.dao.CollectorMonitorBindDao; import org.apache.hertzbeat.manager.scheduler.ConsistentHash; @@ -76,6 +83,29 @@ public void deleteRegisteredCollector() { @Test public void hasCollector() { - collectorService.hasCollector("test"); + when(collectorDao.findCollectorByName("test")).thenReturn(Optional.empty()); + assertFalse(collectorService.hasCollector("test")); + } + + @Test + public void testGenerateCollectorDeployInfo() { + when(collectorDao.findCollectorByName("test")).thenReturn(Optional.of(new Collector())); + assertThrows(CommonException.class, ()->{ + collectorService.generateCollectorDeployInfo("test"); + }); + } + + @Test + public void testMakeCollectorsOffline() { + assertDoesNotThrow(() -> { + collectorService.makeCollectorsOffline(new ArrayList<>()); + }); + } + + @Test + public void testMakeCollectorsOnline() { + assertDoesNotThrow(() -> { + collectorService.makeCollectorsOnline(new ArrayList<>()); + }); } } diff --git a/manager/src/test/java/org/apache/hertzbeat/manager/service/DefaultPluginRunnerTest.java b/manager/src/test/java/org/apache/hertzbeat/manager/service/DefaultPluginRunnerTest.java new file mode 100644 index 00000000000..84d987b0a21 --- /dev/null +++ b/manager/src/test/java/org/apache/hertzbeat/manager/service/DefaultPluginRunnerTest.java @@ -0,0 +1,71 @@ +/* + * 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.service; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.mockito.Mockito.doAnswer; + +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +import static org.mockito.Mockito.any; + +import org.apache.hertzbeat.manager.service.impl.DefaultPluginRunner; +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 DefaultPluginRunner} + */ +@ExtendWith(MockitoExtension.class) +public class DefaultPluginRunnerTest { + @Mock + private PluginService pluginService; + @InjectMocks + private DefaultPluginRunner defaultPluginRunner; + + @Test + public void testPluginExecute() { + assertDoesNotThrow(() -> { + doAnswer(invocation -> { + // no nothing + return null; + }).when(pluginService).pluginExecute(any(Class.class), any(Consumer.class)); + doAnswer(invocation -> { + // no nothing + return null; + }).when(pluginService).pluginExecute(any(Class.class), any(BiConsumer.class)); + defaultPluginRunner.pluginExecute(String.class, (s) -> {}); + defaultPluginRunner.pluginExecute(String.class, (a, b)->{}); + }); + + assertDoesNotThrow(() -> { + doAnswer(invocation -> { + throw new RuntimeException("sample"); + }).when(pluginService).pluginExecute(any(Class.class), any(Consumer.class)); + doAnswer(invocation -> { + throw new RuntimeException("sample"); + }).when(pluginService).pluginExecute(any(Class.class), any(BiConsumer.class)); + defaultPluginRunner.pluginExecute(String.class, (s) -> {}); + defaultPluginRunner.pluginExecute(String.class, (a, b)->{}); + }); + } +}