package com.nisum.mytime.controllertest;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
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.request.MockMvcRequestBuilders.put;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.nisum.mytime.controller.DomainController;
import com.nisum.mytime.model.Domains;
import com.nisum.mytime.service.DomainService;


public class DomainControllerTest {

	
    @Mock
    DomainService domainService;

    @InjectMocks
    DomainController domainController;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mockMvc = MockMvcBuilders.standaloneSetup(domainController).build();
    }

    @Test    
    public void testaddDomain() throws Exception {
        List<String> list = new ArrayList<>();
        list.add("16620");
        list.add("16632");
        String responce=null;
        Domains domains = new Domains(
                new ObjectId("9976ef15874c902c98b8a05d"), "DOM002", "Marketing", "Acc002",
                "Active",list);
        when(domainService.addDomains(domains))
                .thenReturn(responce);
        String jsonvalue = (new ObjectMapper())
                .writeValueAsString(domains).toString();
        mockMvc.perform(post("/domains")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(jsonvalue))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }
    
    @Test
    public void testgetDomains() throws Exception {
    	List<HashMap<Object,Object>> domains = CreateDomainDetails();
        when(domainService.getAllDomains()).thenReturn(domains);
        mockMvc.perform(get("/domains")
                .contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(domainService).getAllDomains();
    }
    
    @Test
    public void testupdateDomains() throws Exception {
        List<String> employeeIds = new ArrayList<>();       
        employeeIds.add("16649");
        employeeIds.add("16650");
        employeeIds.add("16651");      
        String responce=null;
        Domains Domain = new Domains(new ObjectId("9976ef15874c902c98b8a05d"), "DOM002", "Marketing", "Acc002",
                "Active",employeeIds);  
        ObjectMapper mapper = new ObjectMapper();
        String jsonString = mapper.writeValueAsString(Domain);
        when(domainService.updateDomain(any())).thenReturn(responce);
        mockMvc.perform(put("/domains")
                .contentType(MediaType.APPLICATION_JSON_VALUE)
                .content(jsonString))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(domainService).updateDomain(any());
    }

    @Test
    public void testdeleteDomain() throws Exception {
        mockMvc.perform(
                delete("/domains").param("domainId", "DOM001"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(domainService).deleteDomain("DOM001");
    }

    
    private List<HashMap<Object,Object>>CreateDomainDetails() {
    	List<HashMap<Object,Object>> data = new ArrayList<HashMap<Object,Object>> ();
    	HashMap<Object,Object> map1 = new HashMap<Object,Object>();
    	HashMap<Object,Object> map2 = new HashMap<Object,Object>();
    	
        Domains data1 = new Domains();
        data1.setId(new ObjectId("5976ef15874c902c98b8a05d"));
        data1.setDomainId("DOM003");
        data1.setDomainName("MOC");
        data1.setAccountId("ACC001");
        data1.setStatus("Active");
        List<String> list = new ArrayList<>();
        list.add("16101");
        list.add("16102");
        list.add("16103");
        
        data1.setDeliveryManagers(list);
        map1.put(new ObjectId("5976ef15874c902c98b8a05d"), data1);

      	
        Domains data2 = new Domains();
        data2.setId(new ObjectId("9976ef15874c902c98b8a05d"));
        data2.setDomainId("DOM004");
        data2.setDomainName("BIGTICKET");
        data2.setAccountId("ACC001");
        data2.setStatus("Active");
        List<String> list2 = new ArrayList<>();
        list2.add("16103");
        list2.add("16105");
        list2.add("16107");
        
        data1.setDeliveryManagers(list);
        map1.put(new ObjectId("5976ef15874c902c98b8a05d"), data1);
        map2.put(new ObjectId("9976ef15874c902c98b8a05d"), data2);

        data.add(map1);
        data.add(map2);

        return data;
    }
    
}
