## Summary

Below are the List of Features available in Code

1. Reading payload from JSON File and using it as mocked Response
2. Object creation from JSON File
---

## Things Newly Implemented

1. JSON Payload files are found in src/test/resources/json-data
2. In the Code Converstion to Employee Object after reading Employee JSON and Stringify of JSON Object are both done  
3. MockMvc Testing is implemented for Controllers	
	- For AddEmployee Endpoint - Request from JSON file, Response from mocked response  
	- For GetEmployee Endpoint - Request by URL, Response from mocked Employee object constructed from JSON File
	- For GetAllEmployees Endpoint - Request by URL, Response from method returning Employee Object
	- For DeleteEmployee Endpoint - Request by URL, Response from mocked method	 	  
---

## Changes in pom.xml 
```xml
<build>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
        </testResource>			
    </testResources>
    .
    .
    .
</build>
```
---

# API endpoints
## GET
`Get All Employees Detail` [/empmgmt/employees](#getallemployee) <br/>
`Get Employee Details` [/empmgmt/employees/{empId}](#getemployeesempid) <br/>

## POST
`Add Employee Details` [/empmgmt/employees](#addemployee) <br/>

## DELETE
`Delete EmpDetails` [/empmgmt/employees/{empId}](#deleteemployee) <br/>
___

<a name="getemployeesempid"></a>
### GET /empmgmt/employees/{empId}
Get Employee Detail

**Parameters**

|          Name | Required |  Type   | Description                                                                                                                                                           |
| -------------:|:--------:|:-------:| --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|     `empId` | required | string  | Employee Id

**Response**

```
// Employee Details added Succesfully
[
    {
        "empID": 1,
        "empName": "Mugil",
        "empAge": "36"
    }
]
```
___

<a name="getallemployee"></a>
### GET /empmgmt/employees

**Response**
```
// Employee Details added Succesfully
[
    {
        "empID": 1,
        "empName": "Mugil",
        "empAge": "36"
    }
]
```
___

<a name="addemployee"></a>
### POST /empmgmt/employees
Add Employee Details

**Parameters**

|          Name | Required |  Type   | Description                                                                                                                                                           |
| -------------:|:--------:|:-------:| --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|     `empName` | required | string  | Name of Employee
|     `empAge` | required | string  | Age of Employee

**Request**
```
// Employee Details 
{
    "empName" : "Mugil",
    "empAge" : "36"
}
```


**Response**

```
// Employee Details added Succesfully
[
    {
        "empID": 1,
        "empName": "Mugil",
        "empAge": "36"
    }
]
```
___

<a name="deleteemployee"></a>
### DELETE /empmgmt/employees/{empId}

**Parameters**

|          Name | Required |  Type   | Description                                                                                                                                                           |
| -------------:|:--------:|:-------:| --------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|     `empId` | required | string  | Employee Id


**Response**

```
Employee Deleted Successfully
```
___



## Note

* While reading file from resources folder I continously got error telling the file doesnot exists. 
  The Same problem is faced while working in office project. After adding above lines in pom.xml it started 
  working. It kept working, even after removing above lines
* The URL returned in _Link in add employee wont have a port number. The test keeps failing if you copy and paste
  URL from postman and try to asset the _links in mockMVC

___    

Copy paste the code in URL to see in action : https://markdownlivepreview.com/

Readme file screen shot for above code

TestTraits.java

package com.mugil.org;

import com.google.gson.Gson;
import com.mugil.org.models.Employee;
import org.springframework.core.io.ClassPathResource;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public interface TestTraits {
    public static Object getObjfromJson(String fileLoctaion) throws IOException {
        Gson gson = new Gson();
        Employee objEmployee2 = gson.fromJson(getReader(fileLoctaion), Employee.class);
        return objEmployee2;
    }

    public static Reader getReader(String fileLoctaion) throws IOException {
        return new FileReader(new ClassPathResource(fileLoctaion).getFile());
    }
}

EmployeeObject.json

{
  "empID": 101,
  "empName": "Mani",
  "empAge": "35"
}

EmployeeControllerTest.java
Read JSON Value as from File and Convert to Employee Object and to JSON String

JSON Value from File -> Employee Object -> JSON String from Object

  @Test
    void addEmployeeDetails() throws Exception {
        
        //Read values from JSON  
        Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json");

        //Convert Object to JSON String
        String jsonRequestString = mapper.writeValueAsString(objEmployee);

        when(employeeServiceImpl.saveEmployee(any())).thenReturn("101");

        ResultActions objResultActions = this.mockMvc.perform(post("/empmgmt/employees")
                .contentType(MediaType.APPLICATION_JSON)
                .content(jsonRequestString))
                .andDo(print())
                .andExpect(jsonPath("$.empID", is(Matchers.notNullValue())))
                .andExpect(jsonPath("$.empID", is(101)))
                .andExpect(jsonPath("$.empName", is("Mani")))
                .andExpect(status().isCreated());
    }

EmployeeControllerTest.java
Read JSON Value as from File and Convert to Employee Object and to JSON String

JSON Value from File -> Employee Object for Mocking Service Call


@Test
    void getEmployeeDetails() throws Exception {
          
        //Object Construction by reading file
        Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json");

        //Making object as Optional
        Optional<Employee> objEmp = Optional.ofNullable(objEmployee);

        //Mocked service response from object created
        when(employeeServiceImpl.findEmployee(EMP_ID)).thenReturn(objEmp);

        ResultActions objResultActions = this.mockMvc.perform(get("/empmgmt/employees/{empID}",EMP_ID))
                                                    .andDo(print())
                                                    .andExpect(jsonPath("$.empID", is(101)))
                                                    .andExpect(jsonPath("$.empName", is("Mani")))
                                                    .andExpect(jsonPath("$._links.all-employees.href", is("http://localhost/empmgmt/employees")))
                                                    .andExpect(status().isOk());
    }

Repo Link
https://bitbucket.org/Mugil/restwithspringboot/branch/feature/7MockMVCTesting_3

POST

 @Test
    void addEmployeeDetails() throws Exception {
        Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json");

        String jsonRequestString = mapper.writeValueAsString(objEmployee);

        when(employeeServiceImpl.saveEmployee(any())).thenReturn("101");

        ResultActions objResultActions = this.mockMvc.perform(post("/empmgmt/employees")
                .contentType(MediaType.APPLICATION_JSON)
                .content(jsonRequestString))
                .andDo(print())
                .andExpect(jsonPath("$.empID", is(Matchers.notNullValue())))
                .andExpect(jsonPath("$.empID", is(101)))
                .andExpect(jsonPath("$.empName", is("Mani")))
                .andExpect(status().isCreated());
    }

GET

 @Test
    void getEmployeeDetails() throws Exception {
        ClassLoader loader = Test.class.getClassLoader();
        Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json");

        Optional<Employee> objEmp = Optional.ofNullable(objEmployee);
        when(employeeServiceImpl.findEmployee(EMP_ID)).thenReturn(objEmp);

        ResultActions objResultActions = this.mockMvc.perform(get("/empmgmt/employees/{empID}",EMP_ID))
                                                    .andDo(print())
                                                    .andExpect(jsonPath("$.empID", is(101)))
                                                    .andExpect(jsonPath("$.empName", is("Mani")))
                                                    .andExpect(jsonPath("$._links.all-employees.href", is("http://localhost/empmgmt/employees")))
                                                    .andExpect(status().isOk());
    }

DELETE

 @Test
    void deleteEmployeeDetails() throws Exception {
        when(employeeServiceImpl.deleteEmployee(Long.valueOf(EMP_ID))).thenReturn(true);

        this.mockMvc.perform(delete("/empmgmt/employees/{empID}",EMP_ID))
                .andDo(print())
                .andExpect(content().string(containsString("Employee Deleted Successfully")))
                .andExpect(status().isOk());
    }

EmployeeControllerTest.java

package com.mugil.org.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.mugil.org.TestTraits;
import com.mugil.org.models.Employee;
import com.mugil.org.services.EmployeeServiceImpl;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@ExtendWith(SpringExtension.class)
@WebMvcTest(EmployeeController.class)
class EmployeeControllerTest {

    @MockBean
    EmployeeServiceImpl employeeServiceImpl;

    ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private MockMvc mockMvc;

    private static final String EMP_ID = "101";

    @Test
    void getEmployeeDetails() throws Exception {
        ClassLoader loader = Test.class.getClassLoader();
        Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json");

        Optional<Employee> objEmp = Optional.ofNullable(objEmployee);
        when(employeeServiceImpl.findEmployee(EMP_ID)).thenReturn(objEmp);

        ResultActions objResultActions = this.mockMvc.perform(get("/empmgmt/employees/{empID}",EMP_ID))
                                                    .andDo(print())
                                                    .andExpect(jsonPath("$.empID", is(101)))
                                                    .andExpect(jsonPath("$.empName", is("Mani")))
                                                    .andExpect(jsonPath("$._links.all-employees.href", is("http://localhost/empmgmt/employees")))
                                                    .andExpect(status().isOk());
    }

    @Test
    void deleteEmployeeDetails() throws Exception {
        when(employeeServiceImpl.deleteEmployee(Long.valueOf(EMP_ID))).thenReturn(true);

        this.mockMvc.perform(delete("/empmgmt/employees/{empID}",EMP_ID))
                .andDo(print())
                .andExpect(content().string(containsString("Employee Deleted Successfully")))
                .andExpect(status().isOk());
    }

    @Test
    void getAllEmployeeDetails() throws Exception {
        List<Employee> arrEmployees = new ArrayList<>();
        arrEmployees.add(getEmployee());

        when(employeeServiceImpl.findAllEmployee()).thenReturn(arrEmployees);

        ResultActions objResultActions = this.mockMvc.perform(get("/empmgmt/employees")
                .contentType(MediaType.APPLICATION_JSON))
                .andDo(print())
                .andExpect(jsonPath("$[0].empID", is(Matchers.notNullValue())))
                .andExpect(jsonPath("$[0].empID", is(101)))
                .andExpect(jsonPath("$[0].empName", is("Shivaji")))
                .andExpect(status().is2xxSuccessful());
    }

    @Test
    void addEmployeeDetails() throws Exception {
        Employee objEmployee = (Employee) TestTraits.getObjfromJson("json-data/EmployeeObject.json");

        String jsonRequestString = mapper.writeValueAsString(objEmployee);

        when(employeeServiceImpl.saveEmployee(any())).thenReturn("101");

        ResultActions objResultActions = this.mockMvc.perform(post("/empmgmt/employees")
                .contentType(MediaType.APPLICATION_JSON)
                .content(jsonRequestString))
                .andDo(print())
                .andExpect(jsonPath("$.empID", is(Matchers.notNullValue())))
                .andExpect(jsonPath("$.empID", is(101)))
                .andExpect(jsonPath("$.empName", is("Mani")))
                .andExpect(status().isCreated());
    }

    private Employee getEmployee(){
        Employee objEmployee = new Employee();

        objEmployee.setEmpID(Long.valueOf(101));
        objEmployee.setEmpAge("35");
        objEmployee.setEmpName("Shivaji");

        return objEmployee;
    }
}