You are on page 1of 5

@GetMapping("/{employeeID}")

@HasPermission(READ_HEALTH_INSURANCE)
public ResponseEntity<?> getHealthInsuranceByEmployeeId(@PathVariable String
employeeID) {
try {
return
ResponseEntity.ok(healthInsuranceService.findByHealthInsuranceByEmployeeId(employee
ID));
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
}
}

HealthInsurance findByEmployeeId(String employeeId, String organizationId);

package com.beeja.api.financemanagementservice.controllers;

import com.beeja.api.financemanagementservice.Utils.UserContext;
import com.beeja.api.financemanagementservice.modals.HealthInsurance;
import com.beeja.api.financemanagementservice.service.HealthInsuranceService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.junit.jupiter.api.Assertions.assertEquals;


import static org.mockito.Mockito.when;

class HealthInsuranceControllerTest {
@InjectMocks
HealthInsuranceController healthInsuranceController;

@Autowired
MockMvc mockMvc;
@Mock
HealthInsuranceService healthInsuranceService;

@BeforeEach
public void setUp(){
MockitoAnnotations.initMocks(this);
this.mockMvc =
MockMvcBuilders.standaloneSetup(healthInsuranceController).build();
}
public void createUserContext() {
Map<String, Object> loggedInUserOrganization = new HashMap<>();
loggedInUserOrganization.put("id", "testId");
UserContext.setLoggedInUserOrganization(loggedInUserOrganization);
Set<String> loggedInUserPermissions = new HashSet<>();
UserContext.setLoggedInUserPermissions(loggedInUserPermissions);
}

@Test
void testGetHealthInsuranceByEmployeeIdSuccess(){
createUserContext();
HealthInsurance healthInsurance = new HealthInsurance();

when(healthInsuranceService.findByHealthInsuranceByEmployeeId("123")).thenReturn(he
althInsurance);
ResponseEntity<?> responseEntity =
healthInsuranceController.getHealthInsuranceByEmployeeId("123");
assertEquals(HttpStatus.OK,responseEntity.getStatusCode());
}

@Test
void testGetHealthInsuranceByEmployeeIdException(){
createUserContext();
HealthInsurance healthInsurance = new HealthInsurance();

when(healthInsuranceService.findByHealthInsuranceByEmployeeId("12")).thenThrow(new
RuntimeException());
ResponseEntity<?> responseEntity =
healthInsuranceController.getHealthInsuranceByEmployeeId("12");

assertEquals(HttpStatus.INTERNAL_SERVER_ERROR,responseEntity.getStatusCode());
}

package com.beeja.api.financemanagementservice.serviceImpl;

import com.beeja.api.financemanagementservice.Utils.UserContext;
import
com.beeja.api.financemanagementservice.controllers.HealthInsuranceController;
import com.beeja.api.financemanagementservice.modals.HealthInsurance;
import com.beeja.api.financemanagementservice.repository.HealthInsuranceRepository;
import com.beeja.api.financemanagementservice.service.HealthInsuranceService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import java.util.*;

import static
com.beeja.api.financemanagementservice.Utils.Constants.ERROR_FETCHING_HEALTH_INSURA
NCE;
import static
com.beeja.api.financemanagementservice.Utils.Constants.HEALTH_INSURANCE_NOT_FOUND;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

class HealthInsuranceServiceImplTest {

@InjectMocks
HealthInsuranceServiceImpl healthInsuranceServiceImpl;

@Autowired
MockMvc mockMvc;
@Mock
HealthInsuranceRepository healthInsuranceRepository;

@BeforeEach
public void setUp(){
MockitoAnnotations.initMocks(this);
this.mockMvc =
MockMvcBuilders.standaloneSetup(healthInsuranceServiceImpl).build();
Map<String, Object> organizationMap = Collections.singletonMap("id",
"tac");
UserContext.setLoggedInUserOrganization(organizationMap);
UserContext.setLoggedInEmployeeId("123");
}
public void createUserContext() {
Map<String, Object> loggedInUserOrganization = new HashMap<>();
loggedInUserOrganization.put("id", "testId");
UserContext.setLoggedInUserOrganization(loggedInUserOrganization);
Set<String> loggedInUserPermissions = new HashSet<>();
UserContext.setLoggedInUserPermissions(loggedInUserPermissions);
}

@Test
void testGetHealthInsuranceByEmployeeIdSuccess(){
createUserContext();
HealthInsurance healthInsurance = new HealthInsurance();
healthInsurance.setEmployeeId("123");
when(healthInsuranceRepository.findByEmployeeId(anyString(),anyString())).thenRetur
n(healthInsurance);
HealthInsurance result =
healthInsuranceServiceImpl.findByHealthInsuranceByEmployeeId("123");
assertEquals(healthInsurance,result);
}

@Test
void testGetHealthInsuranceByEmployeeIdNotFound(){

when(healthInsuranceRepository.findByEmployeeId(anyString(),anyString())).thenRetur
n(null);
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
healthInsuranceServiceImpl.findByHealthInsuranceByEmployeeId("123");
});
assertEquals("An error occurred while fetching health insurance Health
insurance not found",
exception.getMessage());
}

@Test
void testGetHealthInsuranceByEmployeeIdException(){

when(healthInsuranceRepository.findByEmployeeId(anyString(),anyString())).thenThrow
(new RuntimeException("error"));
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
healthInsuranceServiceImpl.findByHealthInsuranceByEmployeeId("123");
});

assertTrue(exception.getMessage().contains(ERROR_FETCHING_HEALTH_INSURANCE));
}
}

@Override
public HealthInsurance findByHealthInsuranceByEmployeeId(String employeeId) {
try {
HealthInsurance healthInsurance =
healthInsuranceRepository.findByEmployeeId(employeeId,
UserContext.getLoggedInUserOrganization().get("id").toString());
if (healthInsurance != null) {
return healthInsurance;
} else {
throw new Exception(HEALTH_INSURANCE_NOT_FOUND);
}
}catch (Exception e){
throw new RuntimeException(ERROR_FETCHING_HEALTH_INSURANCE + e.getMessage(),
e);
}
}

HealthInsurance findByHealthInsuranceByEmployeeId(String employeeId);

You might also like