package com.ruoyi.web.modules.industryservice.support;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.InputStream;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.ruoyi.web.modules.industryservice.support.CooperativeDevelopmentExcelParser.ParsedRow;

@DisplayName("CooperativeDevelopmentExcelParser")
class CooperativeDevelopmentExcelParserTest
{
    @Test
    @DisplayName("识别表头并解析合作社行")
    void parseSample() throws Exception
    {
        try (InputStream in = getClass().getResourceAsStream("/excel/cooperative_development_sample.xlsx"))
        {
            List<ParsedRow> rows = CooperativeDevelopmentExcelParser.parse(in);
            assertEquals(2, rows.size());
            assertEquals("雅安镇", rows.get(0).getTownName());
            assertEquals("雅安镇荣嘎村", rows.get(0).getVillageName());
            assertEquals("示例合作社A", rows.get(0).getCooperativeName());
            assertEquals(50, rows.get(0).getMemberHouseholdCount());
            assertEquals(10, rows.get(0).getCattleShareCount());
            assertEquals("养殖业", rows.get(0).getBusinessScope());
            assertEquals("整村合作社", rows.get(0).getBusinessModel());
            assertEquals("规范", rows.get(0).getOperationStatus());
            assertEquals("种养结合", rows.get(1).getBusinessScope());
            assertEquals("股份合作社", rows.get(1).getBusinessModel());
            assertEquals("一般", rows.get(1).getOperationStatus());
        }
    }

    @Test
    @DisplayName("识别表头行")
    void detectHeaderRow() throws Exception
    {
        try (InputStream in = getClass().getResourceAsStream("/excel/cooperative_development_sample.xlsx"))
        {
            org.apache.poi.ss.usermodel.Workbook wb = org.apache.poi.ss.usermodel.WorkbookFactory.create(in);
            org.apache.poi.ss.usermodel.Sheet sheet = CooperativeDevelopmentExcelParser.resolveSheet(wb);
            int header = CooperativeDevelopmentExcelParser.detectHeaderRow(sheet);
            assertEquals(2, header);
            wb.close();
        }
    }

    @Test
    @DisplayName("模板本身无数据行")
    void emptyTemplateNoRows() throws Exception
    {
        try (InputStream in = getClass().getClassLoader()
                .getResourceAsStream(CooperativeDevelopmentExcelParser.TEMPLATE_CLASSPATH))
        {
            List<ParsedRow> rows = CooperativeDevelopmentExcelParser.parse(in);
            assertTrue(rows.isEmpty());
        }
    }
}
