package com.nisum.mytime.controllertest;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.bson.types.ObjectId;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nisum.mytime.controller.ProjectTeamController;
import com.nisum.mytime.model.BillingDetails;
import com.nisum.mytime.model.EmployeeDashboardVO; 
import com.nisum.mytime.repository.EmployeeVisaRepo;
import com.nisum.mytime.service.ProjectService;
import com.nisum.mytime.service.UserService;

public class EmployeeBillingControllerTest {
	
    @Mock
    UserService userService;

    @Mock
    ProjectService projectService;
    
    @Mock
    EmployeeVisaRepo employeeVisaRepo;
    
    @InjectMocks
    ProjectTeamController projectTeamController;

    private MockMvc mockMvc;
    
    @Test
    public void testgetEmployeeBillingDetails() throws Exception {
        List<BillingDetails> billings = CreateTeamMateBilling();
        when(projectService.getEmployeeBillingDetailsAll("16167")).thenReturn(billings);
        mockMvc.perform(get("/projectTeam/getProjectAllocations").param("employeeId", "16167"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(projectService).getEmployeeBillingDetailsAll("16167");
    }

    @Test
    public void testaddEmployeeBilling() throws Exception {
              BillingDetails billingDetails = new BillingDetails(
                new ObjectId("9976ef15874c902c98b8a05d"), "16389","Mahesh Mudrakola", "Acc001","Pro001","Macys",
                "Y", new Date(2017 - 11 - 29),
                new Date(2017 - 12 - 20),"Comments",true,new Date(2017 - 11 - 29));
        String jsonvalue = (new ObjectMapper())
                .writeValueAsString(billingDetails).toString();
        when(projectService.addEmployeeBillingDetails(billingDetails))
                .thenReturn(billingDetails);
        mockMvc.perform(post("/projectTeam/addEmployeeBilling")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(jsonvalue))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }
    
    @Test
    public void testupdateEmployeeBilling() throws Exception {
              BillingDetails billingDetails = new BillingDetails(
                new ObjectId("9976ef15874c902c98b8a05d"), "16389","Mahesh Mudrakola", "Acc001","Pro001","Macys",
                "Y", new Date(2017 - 11 - 29),
                new Date(2017 - 12 - 20),"Comments",true,new Date(2017 - 11 - 29));
        String jsonvalue = (new ObjectMapper())
                .writeValueAsString(billingDetails).toString();
        when(projectService.updateEmployeeBilling(billingDetails))
                .thenReturn(billingDetails);
        mockMvc.perform(post("/projectTeam/updateEmployeeBilling")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(jsonvalue))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }
    
    @Test
    public void testdeleteTeammate() throws Exception {
        BillingDetails billingDetails = new BillingDetails(
                new ObjectId("9976ef15874c902c98b8a05d"), "16389","Mahesh Mudrakola", "Acc001","Pro001","Macys",
                "Y", new Date(2017 - 11 - 29),
                new Date(2017 - 12 - 20),"Comments",true,new Date(2017 - 11 - 29));
        String jsonvalue = (new ObjectMapper())
                .writeValueAsString(billingDetails).toString();
        mockMvc.perform(post("/projectTeam/deleteEmployeeBilling")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(jsonvalue))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(projectService).deleteEmployeeBilling(billingDetails);
    }

    @Test
    public void testgetEmployeesDashBoard() throws Exception {
    	List<EmployeeDashboardVO> dashboard = null;
        when(projectService.getEmployeesDashBoard()).thenReturn(dashboard);
        mockMvc.perform(get("/projectTeam/getEmployeesDashBoard"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(projectService).getEmployeesDashBoard();
    }    
    
    private List<BillingDetails> CreateTeamMateBilling() {
        List<BillingDetails> data = new ArrayList<>();

        BillingDetails team1 = new BillingDetails();
        team1.setId(new ObjectId("5b307d7d708ef705c4ca59cc"));
        team1.setEmployeeId("16167");
        team1.setEmployeeName("Mogana Kurmaran");
        team1.setProjectId("Nisum0000");
        team1.setProjectName("Free Pool");
        team1.setBillingStartDate(new Date(2017 - 01 - 10));
        team1.setBillingEndDate(new Date(2017 - 02 - 10));
        team1.setActive(true);
        data.add(team1);

        BillingDetails team2 = new BillingDetails();
        team2.setId(new ObjectId("5b2f4a969ed316fef01adcce"));
        team2.setEmployeeId("16050");
        team2.setEmployeeName("Rajeshekar Sayannagari");
        team2.setProjectId("Nisum0000");
        team2.setProjectName("Free Pool");
        team2.setBillingStartDate(new Date(2017 - 01 - 15));
        team2.setBillingEndDate(new Date(2017 - 02 - 15));
        team2.setActive(false);
        data.add(team2);

        return data;

    }
}