|
21 | 21 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
22 | 22 | import static org.junit.jupiter.api.Assertions.assertThrows; |
23 | 23 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 24 | +import static org.mockito.Mockito.doNothing; |
24 | 25 | import java.io.ByteArrayInputStream; |
25 | 26 | import java.io.ByteArrayOutputStream; |
| 27 | +import java.lang.reflect.Field; |
26 | 28 | import java.nio.charset.StandardCharsets; |
27 | 29 | import java.util.List; |
| 30 | +import org.apache.hertzbeat.common.entity.manager.Monitor; |
| 31 | +import org.apache.hertzbeat.common.entity.manager.Param; |
| 32 | +import org.apache.hertzbeat.manager.config.ManagerSseManager; |
28 | 33 | import org.apache.hertzbeat.manager.service.impl.AbstractImExportServiceImpl; |
29 | 34 | import org.apache.hertzbeat.manager.service.impl.JsonImExportServiceImpl; |
30 | 35 | import org.junit.jupiter.api.BeforeEach; |
31 | 36 | import org.junit.jupiter.api.Test; |
| 37 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 38 | +import org.mockito.ArgumentCaptor; |
| 39 | +import org.mockito.Mock; |
| 40 | +import org.mockito.junit.jupiter.MockitoExtension; |
32 | 41 |
|
33 | 42 | /** |
34 | 43 | * Test case for {@link JsonImExportServiceImpl} |
35 | 44 | */ |
36 | | - |
| 45 | +@ExtendWith(MockitoExtension.class) |
37 | 46 | class JsonImExportServiceTest { |
38 | 47 |
|
39 | 48 | private JsonImExportServiceImpl jsonImExportService; |
40 | 49 |
|
| 50 | + @Mock |
| 51 | + private MonitorService monitorService; |
| 52 | + |
| 53 | + @Mock |
| 54 | + private ManagerSseManager managerSseManager; |
| 55 | + |
41 | 56 | @BeforeEach |
42 | | - public void setUp() { |
| 57 | + public void setUp() throws Exception { |
43 | 58 | jsonImExportService = new JsonImExportServiceImpl(); |
| 59 | + Field monitorServiceField = jsonImExportService.getClass().getSuperclass().getDeclaredField("monitorService"); |
| 60 | + monitorServiceField.setAccessible(true); |
| 61 | + monitorServiceField.set(jsonImExportService, monitorService); |
| 62 | + Field sseField = jsonImExportService.getClass().getSuperclass().getDeclaredField("managerSseManager"); |
| 63 | + sseField.setAccessible(true); |
| 64 | + sseField.set(jsonImExportService, managerSseManager); |
44 | 65 | } |
45 | 66 |
|
46 | 67 | @Test |
@@ -89,4 +110,59 @@ void testType() { |
89 | 110 | assertEquals("JSON", jsonImExportService.type()); |
90 | 111 | } |
91 | 112 |
|
| 113 | + @Test |
| 114 | + void testImportConfig_shouldSetInstanceFromHostAndPortParams() { |
| 115 | + String json = "[{\"monitor\":{\"name\":\"test\",\"app\":\"windows\",\"intervals\":6000,\"status\":1}," |
| 116 | + + "\"params\":[{\"field\":\"host\",\"type\":1,\"value\":\"localhost\"}," |
| 117 | + + "{\"field\":\"port\",\"type\":0,\"value\":\"161\"}]}]"; |
| 118 | + |
| 119 | + ArgumentCaptor<Monitor> monitorCaptor = ArgumentCaptor.forClass(Monitor.class); |
| 120 | + ArgumentCaptor<List<Param>> paramsCaptor = ArgumentCaptor.forClass(List.class); |
| 121 | + doNothing().when(monitorService).addMonitor(monitorCaptor.capture(), paramsCaptor.capture(), |
| 122 | + org.mockito.Mockito.any(), org.mockito.Mockito.any()); |
| 123 | + |
| 124 | + ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); |
| 125 | + jsonImExportService.importConfig("test.json", bis); |
| 126 | + |
| 127 | + Monitor captured = monitorCaptor.getValue(); |
| 128 | + assertEquals("localhost:161", captured.getInstance()); |
| 129 | + assertEquals("test", captured.getName()); |
| 130 | + assertEquals("windows", captured.getApp()); |
| 131 | + |
| 132 | + List<Param> capturedParams = paramsCaptor.getValue(); |
| 133 | + assertNotNull(capturedParams); |
| 134 | + assertEquals(2, capturedParams.size()); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + void testImportConfig_shouldSetInstanceWithHostOnly() { |
| 139 | + String json = "[{\"monitor\":{\"name\":\"test\",\"app\":\"linux\",\"intervals\":6000,\"status\":1}," |
| 140 | + + "\"params\":[{\"field\":\"host\",\"type\":1,\"value\":\"192.168.1.1\"}]}]"; |
| 141 | + |
| 142 | + ArgumentCaptor<Monitor> monitorCaptor = ArgumentCaptor.forClass(Monitor.class); |
| 143 | + doNothing().when(monitorService).addMonitor(monitorCaptor.capture(), |
| 144 | + org.mockito.Mockito.any(), org.mockito.Mockito.any(), org.mockito.Mockito.any()); |
| 145 | + |
| 146 | + ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); |
| 147 | + jsonImExportService.importConfig("test.json", bis); |
| 148 | + |
| 149 | + Monitor captured = monitorCaptor.getValue(); |
| 150 | + assertEquals("192.168.1.1", captured.getInstance()); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void testImportConfig_shouldHandleNoHostParam() { |
| 155 | + String json = "[{\"monitor\":{\"name\":\"test\",\"app\":\"website\",\"intervals\":6000,\"status\":1}," |
| 156 | + + "\"params\":[{\"field\":\"url\",\"type\":1,\"value\":\"http://example.com\"}]}]"; |
| 157 | + |
| 158 | + ArgumentCaptor<Monitor> monitorCaptor = ArgumentCaptor.forClass(Monitor.class); |
| 159 | + doNothing().when(monitorService).addMonitor(monitorCaptor.capture(), |
| 160 | + org.mockito.Mockito.any(), org.mockito.Mockito.any(), org.mockito.Mockito.any()); |
| 161 | + |
| 162 | + ByteArrayInputStream bis = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); |
| 163 | + jsonImExportService.importConfig("test.json", bis); |
| 164 | + |
| 165 | + Monitor captured = monitorCaptor.getValue(); |
| 166 | + assertEquals(null, captured.getInstance()); |
| 167 | + } |
92 | 168 | } |
0 commit comments