package de.lcag.common;

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

import java.io.IOException;
import java.util.List;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class GenericTableTest {

	@BeforeAll
	static void setUpBeforeClass() throws Exception {
	}

	@BeforeEach
	void setUp() throws Exception {
	}

	@Test
	void testExcel2Generic() throws IOException {
		String filePath = "src/test/resources/ExcelTable/City-BeforeCorrection.xlsx";
		ExcelWorkbook table = null;
		String s;
		GenericTable genericTable = null;
		List<String> wantedColumns = null;
		int noRowsInTable, noColumnsInTable, noRowsInGenericTable, noExpectedRows;

		// TC1: Read file from and filter-out unwanted columns
		table = new ExcelWorkbook(filePath);
		wantedColumns = List.of(
				"Country Code;Code;Name"
						.split(";"));

		table.setWantedColumns(wantedColumns);

		table.read();
		noRowsInTable = table.lenght();
		assertEquals(11496, noRowsInTable, "Unexpected number of rows found in Source Table");

		genericTable = new GenericTable(table);
		noRowsInGenericTable = genericTable.lenght();

		noColumnsInTable = table.getColumnNames().size();
		noExpectedRows = noColumnsInTable * noRowsInTable;
		assertEquals(noRowsInGenericTable, noExpectedRows,
				"Unexpected number of columns found in generic table: One per cell value expected");
	
	}
	
	@Test
	void testLenght() throws IOException {
		String xmlFilePath = "src/test/resources/CompareTables/MDM_compare_FilePairs/020-14713764_getShipment_reply.xml";
		XmlTable xmlTable = null;
		String s;
		GenericTable genericTable = null;
		List<String> wantedColumns = null;
		int noRowsInXmlTable, noColumnsInXmlTable, noRowsInGenericTable, noExpectedRows;

		// TC2: Read a XML file from a start Xpath and filter-out unwanted columns
		xmlTable = new XmlTable(xmlFilePath);
		wantedColumns = List.of(
				"Origin;Destination;Issuedate;AWBCurrency;CustomsIndicator;CDBNo;PaymentResponsibility;ValuationCharge;ChargeType;TotalValuationCharge"
						.split(";"));

		xmlTable.setStartXPath("//ns2:AWB");
		xmlTable.setWantedColumns(wantedColumns);

		xmlTable.read();
		noRowsInXmlTable = xmlTable.lenght();
		assertEquals(1, noRowsInXmlTable, "Unexpected number of rows found in XML Table");

		s = xmlTable.asCSV();
		noColumnsInXmlTable = xmlTable.getColumnNames().size();
		assertEquals(wantedColumns.size(), noColumnsInXmlTable, "Unexpected number of columns found in XML Table");

		genericTable = new GenericTable(xmlTable);
		noRowsInGenericTable = genericTable.lenght();

		noExpectedRows = noColumnsInXmlTable * noRowsInXmlTable;
		assertEquals(noRowsInGenericTable, noExpectedRows,
				"Unexpected number of columns found in generic table: One per cell value expected");
	}

}
