/*
 * This file (ReadXmlTest.java) is part of DebGen-LabelPrintLib  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.08 14:19
 */
package hu.astrid.debgen.labelprint.test;

import hu.astrid.debgen.labelprint.domain.Label;
import hu.astrid.debgen.labelprint.domain.PatientLabel;
import hu.astrid.debgen.labelprint.domain.SampleLabel;
import hu.astrid.debgen.labelprint.exception.LabelPrintException;
import hu.astrid.debgen.labelprint.printer.ZebraLabelPrinter;
import hu.astrid.debgen.labelprint.util.ReadFromXML;
import org.junit.Test;
import org.w3c.dom.Document;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.net.URISyntaxException;

import static org.junit.Assert.assertEquals;

/**
 * Created by IntelliJ IDEA.
 * User: inagy
 * Date: Jan 8, 2010
 * Time: 2:19:29 PM
 */
public class ReadXmlTest {

    static final String SAMPLE_XML = "/namedpatientexample.xml";
    Document doc = null;

    /**
     * Read value from xml
     */
    @Test
    public void testReadXml() throws URISyntaxException {
        doc = ReadFromXML.readFromXml(getClass().getResource(SAMPLE_XML).toURI());
        String patientName = ReadFromXML.getNodeValue(doc, "debgenbiobank:analysis", "patient", "personalIdentity", "patientName");
        assertEquals("Peti", patientName);
    }

    /**
     * Read label list from xml
     */
    @Test
    public void testReadLabels() throws LabelPrintException, URISyntaxException {
        //Create required label list
        List<Label> actualLabels = new ArrayList<Label>();

        List<Label> requiredLabels = Arrays.asList(
                new PatientLabel("DGBR10"),
                new SampleLabel("CDGBR10001"),
                new SampleLabel("CDGBR10002"),
                new SampleLabel("CDGBR10003"),
                new SampleLabel("DDGBR10001"),
                new SampleLabel("DDGBR10002"),
                new SampleLabel("DDGBR10003"),
                new SampleLabel("DDGBR10004")
        );

        //Read labels from xml

        doc = ReadFromXML.readFromXml(getClass().getResource(SAMPLE_XML).toURI());
        actualLabels = new ZebraLabelPrinter().createLabelListFromDocument(doc);
//        List<Node> patientCodes = ReadFromXML.getNodes(doc, "debgenbiobank:patient", "biobankdata", "patientcodes", "patientcode");
//        for (Node n : patientCodes) {
//            String codeType = ReadFromXML.getNodeValue(n, "codetype");
//            String codeValue = ReadFromXML.getNodeValue(n, "codevalue");
//            Label label = Label.getInstance(codeType);
//            label.setCodeValue(codeValue);
//            actualLabels.add(label);
//        }

        // TODO: override equals and hashmap methods of label. Then it is possible to do the following:
        //assertArrayEquals(requiredLabels.toArray(), actualLabels.toArray());

        //Compare lists
        if (requiredLabels.size() == actualLabels.size()) {
            for (int i = 0; i < requiredLabels.size(); i++) {
                assertEquals(requiredLabels.get(i).getCodeValue(), actualLabels.get(i).getCodeValue());
            }
        } else {
            throw new LabelPrintException("The required size of label list is differ from the size of labels in XML");

        }
    }
}
