FileServiceTest.java 1.7 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
package pwc.taxtech.atms.service.impl;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import pwc.taxtech.atms.dto.OperationResultDto;

public class FileServiceTest {

    private FileService fileService = new FileService();

    private static final Logger logger = LoggerFactory.getLogger(FileServiceTest.class);

    @Test
    public void saveTempFile() throws IOException {
        ClassPathResource res = new ClassPathResource("excel/customer-atms.xlsx");
        logger.debug("print input file url:" + res.getURL());
        InputStream inputStream = res.getInputStream();
        // String.format("%s/unit_test/testfile.xlsx",
        // FileUtils.getTempDirectory().getAbsolutePath());
        String filePath = FileUtils.getTempDirectory().getAbsolutePath() + "/unit_test/testfile_"
                + System.currentTimeMillis() + ".xlsx";

        // System.out.println("target filePath:" + filePath);
        OperationResultDto<Object> result = fileService.saveFile(inputStream, filePath);
        Assert.assertTrue(result.getResult());
    }

    @Test
    public void importFileThenGetMap() throws IOException {
        ClassPathResource res = new ClassPathResource("excel/customer-atms.xlsx");
        Collection<Map> collection = fileService.readExcelAndClose(res.getInputStream());
        assertThat(collection).isNotEmpty();
        // System.out.println(JSON.toJSONString(collection, true));
    }

}