package com.capgemini.reports.jasper.fillers;

import com.capgemini.reports.jasper.compilers.JasperCompiler;
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 MonoFillerTest {

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

  @Test
  public void generateTest() throws JRException {
    // Compile the report
    InputStream templateStream = getClass().getResourceAsStream(JASPER_TEMPLATE);
    JasperReport reportTemplate = JasperCompiler.compile(templateStream);

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

  private List<Map<String, ?>> createData(final int numLines) {
    List<Map<String, ?>> data = new ArrayList<>();
    for (int num = 0; num < numLines; num++) {
      Map<String, Object> map = new HashMap<>();
      map.put("GENERIC_AVEC_RUPTURE", "Generic-" + num);
      map.put("ID_PART_AVANT", "Part Avant-" + num);
      map.put("ID_PART_APRES", "Part Après-" + num);
      data.add(map);
    }
    return data;
  }
}
