package com.workfusion.odf.impl.converter;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

import org.apache.poi.util.IOUtils;
import org.junit.jupiter.api.Test;

import com.workfusion.odf.impl.converter.excel2html.ToHtml;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * @author Maksim Turchyn
 */
class ToHtmlTest {

    @Test
    void differentExcelFormats() throws Exception {
        byte[] excelAsBytes = IOUtils.toByteArray(ClassLoader.class.getResourceAsStream("/excel/excel-different-sheets.xlsx"));
        byte[] htmlAsBytes = convertToHTML(excelAsBytes);
        assertNotNull(htmlAsBytes);
    }

    private byte[] convertToHTML(byte[] content) {
        InputStream input = new ByteArrayInputStream(content);
        ByteArrayOutputStream output = new ByteArrayOutputStream();

        try {
            ToHtml toHtml = ToHtml.create(input, new PrintWriter(output));
            toHtml.setCompleteHTML(true);
            toHtml.printPage();
            output.close();
            return output.toByteArray();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }
}
