/**
 * Copyright 2008-2009 DRIVER PROJECT (ICM UW)
 * Original author: Marek Horst
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dnetlib.data.index.ws.dataprov;


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

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

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import eu.dnetlib.data.index.dmf.DMFHeaderFields;
import eu.dnetlib.data.index.dmf.DMFMetadataFields;

import pl.edu.icm.yadda.service.search.searching.ResultField;
import pl.edu.icm.yadda.service.search.searching.SearchResult;

/**
 * SearchResultParser test class.
 * @author mhorst
 *
 */
public class SearchResultParserTest {

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testParseToXMLFields() {
		SearchResult searchResult = new SearchResult("someDocId");
		List<ResultField> fields = new ArrayList<ResultField>();
		searchResult.setFields(fields);
		String fieldNameBase = "someFieldName";
		String fieldValueBase = "someFieldValue";
		int fieldsTotalCount = 10;
		for (int i=0; i<fieldsTotalCount; i++) {
			fields.add(new ResultField(fieldNameBase+i,new String[] {fieldValueBase+i}, null));
		}
		String result = SearchResultParser.parseToXMLFields(searchResult);
		assertNotNull(result);
	}
	
	@Test
	public void testParseToDMF() throws ParserConfigurationException, SAXException, IOException {
		SearchResult searchResult = new SearchResult("someDocId");
		List<ResultField> fields = new ArrayList<ResultField>();
		searchResult.setFields(fields);
		String fieldNameBase = "someFieldName";
		String fieldValueBase = "someFieldValue";
		int fieldsTotalCount = 10;
		for (int i=0; i<fieldsTotalCount; i++) {
			fields.add(new ResultField(fieldNameBase+i,new String[] {fieldValueBase+i}, null));
		}
		fields.add(new ResultField(DMFHeaderFields.DR_objectIdentifier, new String[] {"123456789"}, null));
		fields.add(new ResultField(DMFMetadataFields.DC_source, 
				new String[] {"s1", "s2", "s3"}, null));
		String result = SearchResultParser.parseToDMF(searchResult);
		assertNotNull(result);
//		validating result xml
		DocumentBuilderFactory factory =
            DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);

        DocumentBuilder db = factory.newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(result)));
        assertNotNull(doc);
        
//      XML validating is disabled because of invalid xsd definition.
        /*
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaFile = new StreamSource(new File("resources/schemas/DMFSchema.xsd"));
        Schema schema = schemaFactory.newSchema(schemaFile);
        Validator validator = schema.newValidator();
        try {
            validator.validate(new DOMSource(doc));
        } catch (SAXException e) {
        	e.printStackTrace();
            fail("document is invalid");
        }
        */
        Element root = doc.getDocumentElement(); 
		assertNotNull(root);
		for (int i=0; i<fieldsTotalCount; i++) {
			assertEquals(0, root.getElementsByTagName(fieldNameBase+i).getLength());
		}

		assertEquals(1, root.getElementsByTagName(
				DMFHeaderFields.DR_objectIdentifier).getLength());
		assertEquals("123456789", root.getElementsByTagName(
				DMFHeaderFields.DR_objectIdentifier).
				item(0).getFirstChild().getTextContent());
		assertEquals(3, root.getElementsByTagName(
				DMFMetadataFields.DC_source).getLength());
		assertEquals("s1", root.getElementsByTagName(DMFMetadataFields.DC_source).
				item(0).getFirstChild().getTextContent());
		assertEquals("s2", root.getElementsByTagName(DMFMetadataFields.DC_source).
				item(1).getFirstChild().getTextContent());
		assertEquals("s3", root.getElementsByTagName(DMFMetadataFields.DC_source).
				item(2).getFirstChild().getTextContent());
//		checking other header fields
		assertEquals(1, root.getElementsByTagName(
				DMFHeaderFields.DR_itemIdentifier).getLength());
		assertEquals(1, root.getElementsByTagName(
				DMFHeaderFields.DR_recordIdentifier).getLength());
		assertEquals(1, root.getElementsByTagName(
				DMFHeaderFields.DR_dateOfCollection).getLength());
//		checking nillable fields:
		assertEquals(0, root.getElementsByTagName(
				DMFMetadataFields.DC_publisher).getLength());
		assertEquals(0, root.getElementsByTagName(
				DMFMetadataFields.DC_contributor).getLength());
		assertEquals(0, root.getElementsByTagName(
				DMFMetadataFields.DC_relation).getLength());
	}
}
