package com.capgemini.reports.jasper.printers;

import com.capgemini.reports.jasper.compilers.JasperCompiler;
import com.capgemini.reports.jasper.fillers.JasperFiller;
import com.capgemini.reports.jasper.fillers.MonoFiller;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import org.testng.annotations.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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


public class JasperPDFPrinterTest {

    private static final String JASPER_TEMPLATE = "/Jasper/template/report_r20/page2/r20_p2_generics.jrxml";

    @Test
    public void testMulti() throws JRException {
        InputStream templateStream = getClass().getResourceAsStream(JASPER_TEMPLATE);
        JasperReport reportTemplate = JasperCompiler.compile(templateStream);

        // Fill with some data
        JasperFiller filler = new MonoFiller(reportTemplate, setupPage2GenericData(10), new HashMap<String, Object>());
        JasperPrint print = filler.generate();
        assertThat(print.getPages()).isNotEmpty();
    }

    private List<Map<String, ?>> setupPage2GenericData(final int numberOfElements) {
        List<Map<String, ?>> lmrMaps = new ArrayList<>();
        for (int num = 0; num < numberOfElements; num++) {
            Map<String, String> lmrMap = new HashMap<>();
            lmrMap.put("GENERIC_AVEC_RUPTURE", "Generic-" + num);
            lmrMap.put("ID_PART_AVANT", "Part-Avant-" + num);
            lmrMap.put("ID_PART_APREs", "Part-Après-" + num);
            lmrMaps.add(lmrMap);
        }
        return lmrMaps;
    }
}
