|
1 | 1 | package com.uci.utils; |
2 | 2 |
|
3 | 3 | import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
4 | 5 | import com.uci.utils.kafka.KafkaConfig; |
5 | 6 | import org.junit.jupiter.api.AfterAll; |
6 | 7 | import org.junit.jupiter.api.Test; |
| 8 | +import org.mockito.InjectMocks; |
| 9 | +import org.mockito.Mock; |
7 | 10 | import org.mockito.Mockito; |
8 | 11 |
|
9 | | -import org.springframework.beans.factory.annotation.Autowired; |
10 | 12 | import org.springframework.boot.actuate.health.Health; |
| 13 | +import org.springframework.boot.actuate.health.Status; |
11 | 14 | import org.springframework.boot.test.context.SpringBootTest; |
12 | | -import org.springframework.boot.test.mock.mockito.MockBean; |
13 | 15 |
|
14 | | -import static org.junit.jupiter.api.Assertions.assertFalse; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
15 | 17 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
16 | | -import static org.junit.jupiter.api.Assertions.assertTrue; |
17 | 18 |
|
18 | 19 | @SpringBootTest(classes = UtilsTestConfig.class) |
19 | 20 | class UtilHealthServiceTest { |
20 | 21 |
|
21 | | - @Autowired |
22 | | - UtilHealthService utilHealthService; |
23 | | - |
24 | | - @MockBean |
| 22 | + @Mock |
25 | 23 | KafkaConfig kafkaConfig; |
26 | 24 |
|
27 | | - @MockBean |
| 25 | + @Mock |
28 | 26 | BotService botService; |
29 | 27 |
|
| 28 | + @Mock |
| 29 | + ObjectMapper mapper; |
| 30 | + |
| 31 | + @InjectMocks |
| 32 | + UtilHealthService utilHealthService; |
| 33 | + |
30 | 34 | @AfterAll |
31 | 35 | static void teardown() { |
32 | 36 | System.out.println("teardown"); |
33 | 37 | } |
34 | 38 |
|
35 | 39 | @Test |
36 | 40 | void getKafkaHealthNode() { |
| 41 | + Mockito.when(mapper.createObjectNode()).thenReturn(new ObjectMapper().createObjectNode()); |
37 | 42 | Mockito.when(kafkaConfig.kafkaHealthIndicator()).thenReturn(() -> Health.up().build()); |
38 | 43 | JsonNode result = utilHealthService.getKafkaHealthNode().block(); |
39 | 44 | assertNotNull(result); |
40 | | - assertTrue(result.get("healthy").asBoolean()); |
| 45 | + assertEquals(result.get("status").textValue(), Status.UP.getCode()); |
41 | 46 | } |
42 | 47 |
|
43 | 48 | @Test |
44 | 49 | void getCampaignUrlHealthNode() { |
| 50 | + Mockito.when(mapper.createObjectNode()).thenReturn(new ObjectMapper().createObjectNode()); |
45 | 51 | utilHealthService.campaignUrl = "NON_EXISTENT_URL"; |
46 | 52 | JsonNode result = utilHealthService.getCampaignUrlHealthNode().block(); |
47 | 53 | assertNotNull(result); |
48 | | - assertFalse(result.get("healthy").asBoolean()); |
| 54 | + assertEquals(result.get("status").textValue(), Status.DOWN.getCode()); |
49 | 55 | } |
50 | 56 | } |
0 commit comments