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

import com.njcn.common.pojo.exception.BusinessException;
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 TestDeviceReportStorageServiceTest {

    @Test
    public void saveShouldDelegateToMinioStorage() {
        AiReportMinioStorageService minioStorageService = Mockito.mock(AiReportMinioStorageService.class);
        TestDeviceReportStorageService service = new TestDeviceReportStorageService(minioStorageService);
        Mockito.when(minioStorageService.saveMultipartFile(Mockito.anyString(), Mockito.isNull(),
                        Mockito.any(MockMultipartFile.class), Mockito.eq(false)))
                .thenReturn(new AiReportMinioStorageService.StoredFile("cn-tool-ai-report",
                        "test-device/report-id-report.pdf", "report-id-report.pdf", "report.pdf",
                        "application/pdf", 8L));

        String storedPath = service.save(new MockMultipartFile("reportFile", "../report.pdf",
                "application/pdf", "%PDF-1.4".getBytes()));

        Assert.assertEquals("test-device/report-id-report.pdf", storedPath);
    }

    @Test(expected = BusinessException.class)
    public void saveShouldRejectNonPdfFile() {
        TestDeviceReportStorageService service = new TestDeviceReportStorageService(Mockito.mock(AiReportMinioStorageService.class));
        service.save(new MockMultipartFile("reportFile", "report.docx",
                "application/vnd.openxmlformats-officedocument.wordprocessingml.document", new byte[]{1}));
    }
}
