package com.alexanderlogistics.xml;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

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

import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;

import org.imixs.workflow.ItemCollection;
import org.imixs.workflow.engine.DocumentService;
import org.imixs.workflow.exceptions.AccessDeniedException;
import org.imixs.workflow.exceptions.ModelException;
import org.imixs.workflow.exceptions.PluginException;
import org.imixs.workflow.exceptions.ProcessingErrorException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;

import com.alexanderlogistics.InvoiceUtil;
import com.alexanderlogistics.KreditorDebitorService;
import com.alexanderlogistics.mahnlauf.MahnlaufService;

/**
 * Testet den Import Vorgang anhand einer Cargosoft XML Datei
 * 
 * 
 * 
 * @author rsoika
 */
@RunWith(MockitoJUnitRunner.class)
public class EAkteImportTester {

	@Spy
	@InjectMocks
	private CargosoftXMLEAkteImportService service;

	@Mock
	private DocumentService documentService;

	@Mock
	private KreditorDebitorService kreditorDebitorService;

	@Mock
	private MahnlaufService mahnlaufService;

	@Before
	public void setUp() {
		// Mock protected Methode 'alreadyImported' -> always false
		Mockito.when(service.alreadyImported(Mockito.anyString())).thenReturn(false);
	}

	/**
	 * Ein Einfacher test
	 * 
	 * @throws TransformerException
	 * @throws XPathExpressionException
	 * @throws ModelException
	 * @throws PluginException
	 * @throws ProcessingErrorException
	 * @throws AccessDeniedException
	 * 
	 * @throws Exception
	 */
	@Test
	public void testEAkteBasic() throws Exception {

		ItemCollection source = new ItemCollection();
		String fileName = "Abgabenbescheid-IM-GCA-2408-116N-001-ATC400012650820242452-_20240806164256864_DEBUG.xml";
		byte[] xmlContent = readTestFile("cargosoft/" + fileName);
		assertNotNull(xmlContent);

		// Act
		ItemCollection result = service.createWorkitem(source, fileName, xmlContent, new HashMap<>());

		// Assert
		assertNotNull(result);
		assertEquals("001", result.getItemValueString("mandant.id"));

		// test Rows
		List<ItemCollection> rows = InvoiceUtil.explodeChildList(result);
		assertNotNull(rows);
		assertEquals(2, rows.size());
		ItemCollection row1 = rows.get(0);
		ItemCollection row2 = rows.get(1);
		assertEquals("LA-ZEL-2403-004", row1.getItemValueString("filenumber"));
		assertEquals("LA-ZEL-2403-00", row2.getItemValueString("filenumber"));
		assertEquals("ZOLL", row1.getItemValueString("activity.type"));
		assertEquals("EUST", row2.getItemValueString("activity.type"));

	}

	/**
	 * Hilfsmethode zum einlesen einer xml test datei
	 * 
	 * @return
	 * @throws IOException
	 */
	private byte[] readTestFile(String filename) throws IOException {
		// Load the file from the resources folder
		ClassLoader classLoader = getClass().getClassLoader();
		InputStream inputStream = classLoader.getResourceAsStream(filename);
		byte[] data = inputStream.readAllBytes();
		inputStream.close();
		return data;
	}
}
