package com.workfusion.odf.impl.s3;

import java.util.Collections;

import jcifs.smb.SmbFile;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;

import com.workfusion.odf.impl.converter.extractor.DataWrapper;
import com.workfusion.rpa.core.storage.S3Manager;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.matches;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import static com.workfusion.rpa.core.storage.S3Manager.AUTO_DETECT_CONTENT_TYPE;

class S3UploaderTest {
    
    private S3Manager s3Manager;
    private S3Uploader uploader;

    @BeforeEach
    void setUp() {
        s3Manager = mock(S3Manager.class);
        uploader = new S3Uploader(s3Manager, mock(Logger.class), "testBucket");
    }

    @Test
    void shouldUploadDataWrapper() {
        // given
        byte[] data = new byte[0];
        DataWrapper wrapper = new DataWrapper(data, "testFile.jpg", "testType", Collections.emptyMap());

        // when
        uploader.upload(wrapper);

        // then
        verify(s3Manager, times(1)).uploadFileToS3(eq(data), matches("testBucket/.*.jpg"), eq(AUTO_DETECT_CONTENT_TYPE));
    }

    @Test
    void shouldUploadSmbFile() throws Exception {
        // given
        SmbFile file = mock(SmbFile.class);
        when(file.getName()).thenReturn("testFile.jpg");

        // when
        uploader.upload(file);

        // then
        verify(s3Manager, times(1)).putFile(matches("testBucket/.*.jpg"), eq(null), eq(AUTO_DETECT_CONTENT_TYPE));
    }

}
