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.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
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;

@ExtendWith(MockitoExtension.class)
class S3UploaderTest {

    @Mock
    private S3Manager s3Manager;
    @Mock
    private Logger logger;

    private S3Uploader uploader;

    @BeforeEach
    void setUp() {
        uploader = new S3Uploader(s3Manager, logger, "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));
    }

}
