package com.nisum.myteam.controllertest;

import java.util.Map;
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.myteam.controller.AccountController;
import com.nisum.myteam.model.dao.Account;
import com.nisum.myteam.service.impl.AccountService;

public class AccountControllerTest {

	
    @Mock
    AccountService Accountserviceimpl;
    
    @InjectMocks
    AccountController AccountController;

    private MockMvc mockMvc;

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

//    
//    public void createAccountTest()
//    {
//    	
//    }
    
    
    
    
//    @Test
//    public void testaddAccount() throws Exception {
//        List<String> list = new ArrayList<>();
//        list.add("16620");
//        list.add("16632");
//        String responce=null;
//        Account persistedAccount=null;
//        Account account = new Account(
//                 "Acc003", "Nisum", 3,
//                "Y","HYD","RetailS",list);
//        when(Accountserviceimpl.createAccount(account))
//                .thenReturn(persistedAccount);
//        String jsonvalue = (new ObjectMapper())
//                .writeValueAsString(account).toString();
//        mockMvc.perform(post("/account/accounts").param("action","N")
//                .contentType(MediaType.APPLICATION_JSON_VALUE)
//                .content(jsonvalue))
//                .andExpect(MockMvcResultMatchers.status().isOk());
//    }
    
    @Test
    public void testgetAccountNames() throws Exception {
    	List<Map<Object, Object>> Account = CreateAccountDetails();
        when(Accountserviceimpl.getAccountsList()).thenReturn(Account);
        mockMvc.perform(get("/account/accountNames")
                .contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }
    
    
    @Test
    public void testgetAccount() throws Exception {
    	List<Account> Account = null;
        when(Accountserviceimpl.getAccounts()).thenReturn(Account);
        mockMvc.perform(get("/account/accounts")
                .contentType(MediaType.APPLICATION_JSON_VALUE))
                .andExpect(MockMvcResultMatchers.status().isOk());
    }
    
//    @Test
//    public void testupdateAccount() throws Exception {
//        List<String> list = new ArrayList<>();
//        list.add("16620");
//        list.add("16632");
//        String responce=null;
//        Account accountUpdated=null;
//        Account account = new Account(
//                "Acc003", "Nisum", 3,
//                "Y","HYD","RetailS",list);
//        when(Accountserviceimpl.createAccount(account))
//                .thenReturn(accountUpdated);
//        String jsonvalue = (new ObjectMapper())
//                .writeValueAsString(account).toString();
//        mockMvc.perform(post("/account/accounts").param("action","U")
//                .contentType(MediaType.APPLICATION_JSON_VALUE)
//                .content(jsonvalue))
//                .andExpect(MockMvcResultMatchers.status().isOk());
//    }
//

    @Test
    public void testdeleteAccount() throws Exception {
        mockMvc.perform(
                delete("/account/accounts").param("accountId", "Acc002"))
                .andExpect(MockMvcResultMatchers.status().isOk());
        verify(Accountserviceimpl).deleteAccount("Acc002");
    }
  
    private List<Map<Object,Object>>CreateAccountDetails() {
    	List<Map<Object,Object>> data = new ArrayList<Map<Object,Object>> ();
    	HashMap<Object,Object> map1 = new HashMap<Object,Object>();
    	HashMap<Object,Object> map2 = new HashMap<Object,Object>();
        Account data1 = new Account();
       // data1.setId(new ObjectId("5976ef15874c902c98b8a05d"));
        data1.setAccountId("Acc004");
        data1.setAccountName("Govt");
        data1.setAccountProjectSequence(4);
        data1.setClientAddress("BNG");
        data1.setIndustryType("Telecom");
        data1.setStatus("Y");
        List<String> list = new ArrayList<>();
        list.add("16101");
        list.add("16102");
        list.add("16103");
        
        data1.setDeliveryManagers(list);
     	
        Account data2 = new Account();
        //data2.setId(new ObjectId("9976ef15874c902c98b8a05d"));
        data2.setAccountId("Acc004");
        data2.setAccountName("Govt");
        data2.setAccountProjectSequence(4);
        data2.setClientAddress("HYD");
        data2.setIndustryType("Telecom");
        data2.setStatus("Y");
        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;
    }
    
}
