Skip to content

Commit

Permalink
Merge pull request #41 from chinmoy12c/hotfix/fix_failing_tests
Browse files Browse the repository at this point in the history
Fixed failing tests in health service
  • Loading branch information
pankajjangid05 authored Apr 28, 2023
2 parents 9a5a581 + 2e177bd commit f3d431d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 28 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/uci/utils/UtilAppConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.reactive.function.client.WebClient;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

Expand Down Expand Up @@ -113,4 +114,9 @@ public JavaMailSender getJavaMailSender() {
return mailSender;

}

@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}
1 change: 1 addition & 0 deletions src/main/java/com/uci/utils/UtilHealthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.actuate.health.Status;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand Down
28 changes: 17 additions & 11 deletions src/test/java/com/uci/utils/UtilHealthServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,56 @@
package com.uci.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.uci.utils.kafka.KafkaConfig;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest(classes = UtilsTestConfig.class)
class UtilHealthServiceTest {

@Autowired
UtilHealthService utilHealthService;

@MockBean
@Mock
KafkaConfig kafkaConfig;

@MockBean
@Mock
BotService botService;

@Mock
ObjectMapper mapper;

@InjectMocks
UtilHealthService utilHealthService;

@AfterAll
static void teardown() {
System.out.println("teardown");
}

@Test
void getKafkaHealthNode() {
Mockito.when(mapper.createObjectNode()).thenReturn(new ObjectMapper().createObjectNode());
Mockito.when(kafkaConfig.kafkaHealthIndicator()).thenReturn(() -> Health.up().build());
JsonNode result = utilHealthService.getKafkaHealthNode().block();
assertNotNull(result);
assertTrue(result.get("healthy").asBoolean());
assertEquals(result.get("status").textValue(), Status.UP.getCode());
}

@Test
void getCampaignUrlHealthNode() {
Mockito.when(mapper.createObjectNode()).thenReturn(new ObjectMapper().createObjectNode());
utilHealthService.campaignUrl = "NON_EXISTENT_URL";
JsonNode result = utilHealthService.getCampaignUrlHealthNode().block();
assertNotNull(result);
assertFalse(result.get("healthy").asBoolean());
assertEquals(result.get("status").textValue(), Status.DOWN.getCode());
}
}
36 changes: 19 additions & 17 deletions src/test/java/com/uci/utils/UtilsTestConfig.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.uci.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.uci.utils.kafka.KafkaConfig;
import com.uci.utils.service.UserService;
import io.fusionauth.client.FusionAuthClient;
import org.apache.kafka.clients.admin.AdminClient;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
Expand All @@ -11,37 +13,37 @@

public class UtilsTestConfig {

@Mock
WebClient webClient;

@MockBean
FusionAuthClient fusionAuthClient;

@MockBean
AdminClient adminClient;

@Bean
public UserService getUserService(){
public UserService getUserService() {
return new UserService();
}

@Bean
public UtilHealthService getUtilHealthService(){
public UtilHealthService getUtilHealthService() {
return new UtilHealthService();
}

@Bean
public KafkaConfig getKafkaConfig(){
public KafkaConfig getKafkaConfig() {
return new KafkaConfig();
}

@Bean
public BotService getBotService(){
public BotService getBotService() {
return new BotService(webClient, fusionAuthClient, null);
}


@Mock
WebClient webClient;

@MockBean
FusionAuthClient fusionAuthClient;


@Autowired
UtilHealthService utilHealthService;



@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper();
}
}

0 comments on commit f3d431d

Please sign in to comment.