Commit fb6658d2 authored by ppapili's avatar ppapili

Added test cases

parent ba61a2a6
......@@ -36,6 +36,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka-test</artifactId>
......
......@@ -55,7 +55,7 @@ public class EmployeeController {
}
@PostMapping(produces = "application/json", consumes = "application/json", path = "/upsert")
public ResponseEntity<Emp> saveOrUpdateEmployee(@Valid @RequestBody Emp emp) {
public ResponseEntity<Void> saveOrUpdateEmployee(@Valid @RequestBody Emp emp) {
employeeService.saveOrUpdate(emp);
return new ResponseEntity<>(HttpStatus.OK);
}
......
package com.poc.kafka;
package com.poc.kafka.mongo.test;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.poc.kafka.mongo.controller.EmployeeController;
@SpringBootTest
class KafkaPocApplicationTests {
@Autowired
private EmployeeController employeeController;
@Test
void contextLoads() {
assertThat(employeeController).isNotNull();
}
}
package com.poc.kafka.mongo.test.controller;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.poc.kafka.mongo.controller.EmployeeController;
import com.poc.kafka.mongo.exception.EmployeeException;
import com.poc.kafka.mongo.model.Emp;
import com.poc.kafka.mongo.service.EmployeeService;
import com.poc.kafka.mongo.util.ErrorEnum;
@WebMvcTest(EmployeeController.class)
public class EmployeeControllerTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private EmployeeService employeeService;
final List<Emp> list = new ArrayList<>();
@BeforeEach
public void setup() {
Emp e = new Emp();
e.setEmpId(101L);
e.setEmpName("ABC");
list.add(e);
}
@Test
public void findAllEmployees() throws Exception {
when(employeeService.findAll()).thenReturn(list);
/**
* s Provide ContentType and accept headers otherwise will end up with status
* 415
*/
this.mockMvc
.perform(get("/employees/").accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON))
.andDo(print()).andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.[0].empName").value("ABC"));
Mockito.verify(employeeService, times(1)).findAll();
}
@Test
public void findByEmpId() throws Exception {
when(employeeService.findByEmpId(101L)).thenReturn(list.get(0));
this.mockMvc
.perform(get("/employees/empId/{empId}", 101L).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON))
.andDo(print()).andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.empName").value("ABC"));
Mockito.verify(employeeService, times(1)).findByEmpId(any(Long.class));
}
@Test
public void saveEmployee() throws Exception {
Emp e11 = new Emp();
e11.setEmpId(102L);
e11.setEmpName("XYZ");
e11.setDeptId("10");
e11.setDeptName("Development");
e11.setEmpSal(200000.0);
when(employeeService.save(any(Emp.class))).thenReturn(e11);
this.mockMvc
.perform(post("/employees/save").contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).content(mapToJson(e11)))
.andDo(print()).andExpect(status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.empName").value("XYZ"));
Mockito.verify(employeeService, times(1)).save(any(Emp.class));
}
@Test
public void upsertEmployee() throws Exception {
Emp e11 = new Emp();
e11.setEmpId(102L);
e11.setEmpName("XYZ");
e11.setDeptId("10");
e11.setDeptName("Development");
e11.setEmpSal(200005.0);
Mockito.doNothing().when(employeeService).saveOrUpdate(any(Emp.class));
this.mockMvc
.perform(post("/employees/upsert").contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).content(mapToJson(e11)))
.andDo(print()).andExpect(status().isOk());
Mockito.verify(employeeService, times(1)).saveOrUpdate(any(Emp.class));
}
@Test
public void deleteByEmpId() throws Exception {
when(employeeService.deleteByEmpId(101L)).thenReturn(101L);
this.mockMvc.perform(delete("/employees/delete/{empId}", 101L).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk());
Mockito.verify(employeeService, times(1)).deleteByEmpId(any(Long.class));
}
@Test
public void deleteByEmpIdForException() throws Exception {
when(employeeService.deleteByEmpId(101L)).thenThrow(new EmployeeException(ErrorEnum.EC_1.name(), ErrorEnum.getDesription("EC_1")));
this.mockMvc.perform(delete("/employees/delete/{empId}", 101L).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(MockMvcResultMatchers.jsonPath("$.errorDescription").value("Record not found"));
Mockito.verify(employeeService, times(1)).deleteByEmpId(any(Long.class));
}
@Test
public void publishEmployee() throws Exception {
Emp e11 = new Emp();
e11.setEmpId(103L);
e11.setEmpName("MOU");
e11.setDeptId("10");
e11.setDeptName("Development");
e11.setEmpSal(200000.0);
Mockito.doNothing().when(employeeService).send(any(Emp.class));
this.mockMvc
.perform(post("/employees/produce").contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).content(mapToJson(e11)))
.andDo(print()).andExpect(status().isOk()).andExpect(MockMvcResultMatchers.content().string("Employee details published"));
Mockito.verify(employeeService, times(1)).send(any(Emp.class));
}
private String mapToJson(Object obj) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(obj);
}
}
package com.poc.kafka.mongo.test.service;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.poc.kafka.mongo.exception.EmployeeException;
import com.poc.kafka.mongo.model.Emp;
import com.poc.kafka.mongo.service.EmployeeService;
@SpringBootTest
public class EmployeeServiceIntegrationTests {
@Autowired
EmployeeService employeeService;
/**
* Integration Testcases
*/
@Test
public void findAll() {
List<Emp> list=employeeService.findAll();
assertNotNull(list);
}
@Test
public void findByEmpId() {
Assertions.assertThrows(EmployeeException.class, ()->employeeService.findByEmpId(2000L));
}
@Test
public void deleteById() {
Assertions.assertThrows(EmployeeException.class, ()->employeeService.deleteByEmpId(2000L));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment