Skip to content

Commit f3d431d

Browse files
Merge pull request #41 from chinmoy12c/hotfix/fix_failing_tests
Fixed failing tests in health service
2 parents 9a5a581 + 2e177bd commit f3d431d

4 files changed

Lines changed: 43 additions & 28 deletions

File tree

src/main/java/com/uci/utils/UtilAppConfiguration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.springframework.mail.javamail.JavaMailSenderImpl;
1616
import org.springframework.web.reactive.function.client.WebClient;
1717

18+
import com.fasterxml.jackson.databind.ObjectMapper;
1819
import com.github.benmanes.caffeine.cache.Cache;
1920
import com.github.benmanes.caffeine.cache.Caffeine;
2021

@@ -113,4 +114,9 @@ public JavaMailSender getJavaMailSender() {
113114
return mailSender;
114115

115116
}
117+
118+
@Bean
119+
public ObjectMapper objectMapper() {
120+
return new ObjectMapper();
121+
}
116122
}

src/main/java/com/uci/utils/UtilHealthService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.springframework.boot.actuate.health.Health;
99
import org.springframework.boot.actuate.health.HealthIndicator;
1010
import org.springframework.boot.actuate.health.Status;
11+
import org.springframework.context.annotation.Import;
1112
import org.springframework.http.HttpStatus;
1213
import org.springframework.http.ResponseEntity;
1314
import org.springframework.stereotype.Service;
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,56 @@
11
package com.uci.utils;
22

33
import com.fasterxml.jackson.databind.JsonNode;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
45
import com.uci.utils.kafka.KafkaConfig;
56
import org.junit.jupiter.api.AfterAll;
67
import org.junit.jupiter.api.Test;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
710
import org.mockito.Mockito;
811

9-
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.boot.actuate.health.Health;
13+
import org.springframework.boot.actuate.health.Status;
1114
import org.springframework.boot.test.context.SpringBootTest;
12-
import org.springframework.boot.test.mock.mockito.MockBean;
1315

14-
import static org.junit.jupiter.api.Assertions.assertFalse;
16+
import static org.junit.jupiter.api.Assertions.assertEquals;
1517
import static org.junit.jupiter.api.Assertions.assertNotNull;
16-
import static org.junit.jupiter.api.Assertions.assertTrue;
1718

1819
@SpringBootTest(classes = UtilsTestConfig.class)
1920
class UtilHealthServiceTest {
2021

21-
@Autowired
22-
UtilHealthService utilHealthService;
23-
24-
@MockBean
22+
@Mock
2523
KafkaConfig kafkaConfig;
2624

27-
@MockBean
25+
@Mock
2826
BotService botService;
2927

28+
@Mock
29+
ObjectMapper mapper;
30+
31+
@InjectMocks
32+
UtilHealthService utilHealthService;
33+
3034
@AfterAll
3135
static void teardown() {
3236
System.out.println("teardown");
3337
}
3438

3539
@Test
3640
void getKafkaHealthNode() {
41+
Mockito.when(mapper.createObjectNode()).thenReturn(new ObjectMapper().createObjectNode());
3742
Mockito.when(kafkaConfig.kafkaHealthIndicator()).thenReturn(() -> Health.up().build());
3843
JsonNode result = utilHealthService.getKafkaHealthNode().block();
3944
assertNotNull(result);
40-
assertTrue(result.get("healthy").asBoolean());
45+
assertEquals(result.get("status").textValue(), Status.UP.getCode());
4146
}
4247

4348
@Test
4449
void getCampaignUrlHealthNode() {
50+
Mockito.when(mapper.createObjectNode()).thenReturn(new ObjectMapper().createObjectNode());
4551
utilHealthService.campaignUrl = "NON_EXISTENT_URL";
4652
JsonNode result = utilHealthService.getCampaignUrlHealthNode().block();
4753
assertNotNull(result);
48-
assertFalse(result.get("healthy").asBoolean());
54+
assertEquals(result.get("status").textValue(), Status.DOWN.getCode());
4955
}
5056
}
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.uci.utils;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.uci.utils.kafka.KafkaConfig;
45
import com.uci.utils.service.UserService;
56
import io.fusionauth.client.FusionAuthClient;
7+
import org.apache.kafka.clients.admin.AdminClient;
68
import org.mockito.Mock;
79
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.boot.test.mock.mockito.MockBean;
@@ -11,37 +13,37 @@
1113

1214
public class UtilsTestConfig {
1315

16+
@Mock
17+
WebClient webClient;
18+
19+
@MockBean
20+
FusionAuthClient fusionAuthClient;
21+
22+
@MockBean
23+
AdminClient adminClient;
24+
1425
@Bean
15-
public UserService getUserService(){
26+
public UserService getUserService() {
1627
return new UserService();
1728
}
1829

1930
@Bean
20-
public UtilHealthService getUtilHealthService(){
31+
public UtilHealthService getUtilHealthService() {
2132
return new UtilHealthService();
2233
}
2334

2435
@Bean
25-
public KafkaConfig getKafkaConfig(){
36+
public KafkaConfig getKafkaConfig() {
2637
return new KafkaConfig();
2738
}
2839

2940
@Bean
30-
public BotService getBotService(){
41+
public BotService getBotService() {
3142
return new BotService(webClient, fusionAuthClient, null);
3243
}
3344

34-
35-
@Mock
36-
WebClient webClient;
37-
38-
@MockBean
39-
FusionAuthClient fusionAuthClient;
40-
41-
42-
@Autowired
43-
UtilHealthService utilHealthService;
44-
45-
46-
45+
@Bean
46+
public ObjectMapper objectMapper() {
47+
return new ObjectMapper();
48+
}
4749
}

0 commit comments

Comments
 (0)