package com.njcn.gather.aireport.reportmodel.component;

import com.njcn.gather.aireport.common.storage.AiReportMinioStorageService;
import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.mock.web.MockMultipartFile;

public class ReportModelFileStorageServiceTest {

    @Test
    public void saveShouldDelegateToMinioStorage() {
        AiReportMinioStorageService minioStorageService = Mockito.mock(AiReportMinioStorageService.class);
        ReportModelFileStorageService service = new ReportModelFileStorageService(minioStorageService);
        Mockito.when(minioStorageService.saveMultipartFile(Mockito.anyString(), Mockito.isNull(),
                        Mockito.any(MockMultipartFile.class), Mockito.eq(false)))
                .thenReturn(new AiReportMinioStorageService.StoredFile("cn-tool-ai-report",
                        "report-model/template-id-a_b.docx", "template-id-a_b.docx", "a_b.docx",
                        "application/octet-stream", 7L));

        ReportModelFileStorageService.StoredFile storedFile = service.save(new MockMultipartFile(
                "templateFile", "../a:b.docx", "application/octet-stream", "content".getBytes()));

        Assert.assertEquals("report-model/template-id-a_b.docx", storedFile.getPath());
        Assert.assertEquals("a_b.docx", storedFile.getFileName());
    }

    @Test(expected = IllegalArgumentException.class)
    public void saveShouldRejectNonDocxFile() {
        ReportModelFileStorageService service = new ReportModelFileStorageService(Mockito.mock(AiReportMinioStorageService.class));
        service.save(new MockMultipartFile("templateFile", "template.pdf", "application/pdf", new byte[]{1}));
    }
}
