/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bytesw.bytebot.controller;

import com.bytesw.bytebot.http.FileValidationResponse;
import com.bytesw.bytebot.service.AgentService;
import com.bytesw.bytebot.service.FileManagementService;
import com.bytesw.xdf.config.security.annotation.DummyPermissionEvaluator;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;


import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * @author  Hernán Uriarte Melchor
 * @date 14/09/2020.
 * <p>
 * <p>
 * Copyright (c) 2018 Byte, S.A. Todos los derechos reservados.
 * <p>
 * Este software constituye información confidencial y propietaria de Byte, S.A.
 * ("Información Confidencial"). Usted no debe develar dicha Información
 * Confidencial y debe usarla de acuerdo con los términos de aceptación de
 * licencia de uso que firmó con Byte.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebMvcTest(controllers = AgentController.class)
@ActiveProfiles(value = "test")
@AutoConfigureMockMvc(secure = false)
public class AgentControllerTest {
    
    public static String baseService = "/service/agent";
    
    @TestConfiguration
    static class UserServiceTestContextConfiguration {
        @Bean
        DummyPermissionEvaluator dummyPermissionEvaluator() {
            return new DummyPermissionEvaluator();
        }
    }

    @Autowired
    private MockMvc mvc;
    
    @MockBean
    private AgentService agentService;
    
    @MockBean
    private FileManagementService fileManagementService;
    
    @Before
    public void setUp() {
    }
    
    
    @Test
    @WithMockUser(username="admin",roles={"USER","ADMIN"})
    public void uploadFile_whenFileXLS_isOK() throws Exception {
        
        MockMultipartFile xlsFile = new MockMultipartFile(
                "file", 
                "my-xls-file.xls",
                "application/vnd.ms-excel",
                "my-xls-file".getBytes());

        Mockito.when(fileManagementService.validateAndSaveFile(anyString(), eq(xlsFile) ))
                .thenReturn(new FileValidationResponse());

        mvc.perform(MockMvcRequestBuilders.multipart(baseService + "/file-upload")
                .file(xlsFile))
                .andExpect(status().isOk());
    }
    
    @Test
    @WithMockUser(username="admin",roles={"USER","ADMIN"})
    public void uploadFile_whenFileXLSNotAppropriate_throwException() throws Exception {
        
        MockMultipartFile xlsFile = new MockMultipartFile("file", "my-xls-file.xls", "application/vnd.ms-excel",
                "my-xls-file".getBytes());

        Mockito.when(fileManagementService.validateAndSaveFile(anyString(), eq(xlsFile) ))
                .thenThrow(IOException.class);
        
        mvc.perform(MockMvcRequestBuilders.multipart(baseService + "/file-upload")
                .file(xlsFile))
                .andExpect(status().isInternalServerError());
    }
}
