/*
 * This file (PdfGeneratorTest.java) is part of DebGen-ReportPrintLib  developed by ASTRID Research.
 * All rights reserved. Reproduction, modification, use or disclosure
 * to third parties without express authority is forbidden.
 *
 *  Copyright ASTRID Research, Hungary, 2010.
 *
 *  ASTRID Research
 *  Headquaters: Csapó utca 42. Debrecen H-4029 HUNGARY
 *  Phone: +36-52-533-173        Fax: +36-52-533-174
 *  Web: http://www.astrid.hu    E-mail: info@astrid.hu
 *
 * Project name: DEBGEN-Biobank-Project.
 * File last modified at 2010.01.10 11:15
 */
package hu.astrid.printxml.generator;

import hu.astrid.printxml.util.ReadFromXML;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map.Entry;
import javax.xml.parsers.DocumentBuilderFactory;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.w3c.dom.Document;

/**
* Tests for Pdf Generation from Document
 * @author inagy
 */
public class PdfGeneratorFromDocumentTest {

    public PdfGeneratorFromDocumentTest() {
    }
    private static HashMap<File, Boolean> files = new HashMap<File, Boolean>();

    @BeforeClass
    public static void setUpClass() throws Exception {
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/namedpatientexample.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/unnamedpatientexample.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/namedpatient_invalid.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/unnamedpatient_invalid.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/namedpatient_invalid_1.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/unnamedpatient_invalid_1.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/namedpatient_invalid_2.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/unnamedpatient_invalid_2.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/namedpatient_invalid_3.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/unnamedpatient_invalid_3.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/namedpatient_invalid_4.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/unnamedpatient_invalid_4.xml"), true);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/namedpatient_not_wellformed.xml"), false);
        files.put(new File("DebGen-ReportPrintLib/src/test/resources/unnamedpatient_not_wellformed.xml"), false);
     }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    /**
     * Test of getAsStream method, of class PdfGeneratorFromDocument.
     */
    @Test
    public void testGetAsStream() throws Exception {
        System.out.println("getAsStream");
        for (Entry<File, Boolean> entry : files.entrySet()) {
            try {
                System.out.println("Testing file: "+entry.getKey().getAbsolutePath());
                System.out.println("Expected value: "+entry.getValue());
//                Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(entry.getKey());
                Document document = ReadFromXML.readFromXml(entry.getKey());
                PdfGeneratorFromDocument instance = new PdfGeneratorFromDocument(new Locale("hu"));
                instance.setResourceName("/CommonAnalysisRequest-");
                InputStream result = instance.getAsStream(document);
                if (entry.getValue()) {
                    assertTrue("OK - " + result.available(), result.available() > 3000);
                } else {
                    assertFalse(entry.getKey().getAbsolutePath(), result != null && result.available() > 3000);
                }
//                PdfGeneratorFromDocument instance2 = new PdfGeneratorFromDocument(Locale.ENGLISH);
//                InputStream result2 = instance2.getAsStream(document);
//                if (entry.getValue()) {
//                    assertTrue("OK - " + result2.available(), result2.available() > 900);
//                } else {
//                    assertFalse(entry.getKey().getAbsolutePath(), result2 != null && result2.available() > 900);
//                }
            } catch (Exception ex) {
                if (entry.getValue()) {
                    fail("No exception allowed, but found: " + ex);
                } else {
                    assertTrue("Exception is allowed now.", !entry.getValue());
                }
            }
        }
    }

    /**
     * Test of getAsURI method, of class PdfGeneratorFromDocument.
     */
    @Test
    public void testGetAsURI() throws Exception {
        System.out.println("getAsURI");
        for (Entry<File, Boolean> entry : files.entrySet()) {
            try {
                System.out.println("Testing file: "+entry.getKey().getAbsolutePath());
                System.out.println("Expected value: "+entry.getValue());
                //Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(entry.getKey());
                Document document = ReadFromXML.readFromXml(entry.getKey());
                PdfGeneratorFromDocument instance = new PdfGeneratorFromDocument(new Locale("hu"));
                instance.setResourceName("/CommonAnalysisRequest-");
                URI result = instance.getAsURI(document);
                if (entry.getValue()) {
                    assertTrue("OK - " + result.toString(), result.normalize().toString() != null && entry.getValue());
                } else {
                    assertFalse(entry.getKey().getAbsolutePath(), result != null && result.normalize().toString() != null && entry.getValue());
                }
//                PdfGeneratorFromDocument instance2 = new PdfGeneratorFromDocument(Locale.ENGLISH);
//                URI result2 = instance2.getAsURI(document);
//                if (entry.getValue()) {
//                    assertTrue("OK - " + result2.toString(), result2.normalize().toString() != null && entry.getValue());
//                } else {
//                    assertFalse(entry.getKey().getAbsolutePath(), result2 != null && result2.normalize().toString() != null && entry.getValue());
//                }
            } catch (Exception ex) {
                if (entry.getValue()) {
                    fail("No exception allowed, but found: " + ex.getMessage());
                } else {
                    assertTrue("Exception is allowed now.", !entry.getValue());
                }
            }
        }
    }
}
