/*
 * Copyright 2013 George Athanasopoulos <george.athanasopoulos at gmail.com, UoA | ARC>.
 *
 * 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.espas.util;

import java.io.IOException;
import java.io.StringBufferInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 *
 * @author George Athanasopoulos <george.athanasopoulos at gmail.com, UoA | ARC>
 */
public class MetadataValidatorTest {
    
    private String[] testArray= new String[]{
        "<gml:Point xmlns:gml=\"http://www.opengis.net/gml/3.2\" gml:id=\"p1\" srsName=\"http://ontology.espas-fp7.eu/crs/J2000spherical\"><gml:pos>38.03 23.52 33.2</gml:pos></gml:Point>",
        
        "<gml:Point xmlns:gml=\"http://www.opengis.net/gml/3.2\" gml:id=\"p1\" srsName=\"http://ontology.espas-fp7.eu/crs/J2000spherical\"><gml:pos>38.03 23.52</gml:pos></gml:Point>",

        "bla bla",
        
        "<gml:Curve xmlns:gml=\"http://www.opengis.net/gml/3.2\" gml:id=\"ID\">  "+
            "<gml:metaDataProperty> " +
            "<gml:GenericMetaData>Any text </gml:GenericMetaData>" +
            " </gml:metaDataProperty>  <gml:description>string</gml:description>  <gml:descriptionReference/>" +
            " <gml:identifier codeSpace=\"http://www.example.com/\">string</gml:identifier> <gml:name>string</gml:name>" +
            " <gml:segments> <gml:LineStringSegment> <gml:pos>1.0 1.0</gml:pos> </gml:LineStringSegment> </gml:segments> "+
        "</gml:Curve>",
        
        "<EX_BoundingPolygon xmlns=\"http://www.isotc211.org/2005/gmd\" xmlns:gml=\"http://www.opengis.net/gml/3.2\">" +
        "<polygon><gml:Point gml:id=\"p1\" srsName=\"http://ontology.espas-fp7.eu/crs/J2000spherical\"><gml:pos>38.03 23.52 33.2</gml:pos></gml:Point></polygon>" +
        "</EX_BoundingPolygon>",
        
        "<gml:Polygon gml:id=\"PID\" xmlns:gml=\"http://www.opengis.net/gml/3.2\">" +
            "<gml:exterior>" +
                "<gml:LinearRing>" +
                    "<gml:pos srsName=\"http://ontology.espas-fp7.eu/crs/J2000spherical\" srsDimension=\"3\" >34.0 -5.0 100.0</gml:pos>" +
                    "<gml:pos srsName=\"http://ontology.espas-fp7.eu/crs/J2000spherical\" srsDimension=\"3\" >60.0 -5.0 100.0</gml:pos>" +
                    "<gml:pos srsName=\"http://ontology.espas-fp7.eu/crs/J2000spherical\" srsDimension=\"3\" >60.0 40.0 100.0</gml:pos>" +
                    "<gml:pos srsName=\"http://ontology.espas-fp7.eu/crs/J2000spherical\" srsDimension=\"3\" >34.0 40.0 100.0</gml:pos>" +
                    "<gml:pos srsName=\"http://ontology.espas-fp7.eu/crs/J2000spherical\" srsDimension=\"3\" >34.0 -5.0 100.0</gml:pos>" +
                "</gml:LinearRing>" +
            "</gml:exterior>" +
        "</gml:Polygon>"
    };
    
    public MetadataValidatorTest() {
    }
    
    @Before
    public void setUp() {
        
    }

//    @Test
    public void testIsValidGMLLocation1() throws ParserConfigurationException, SAXException, IOException {
        MetadataValidator.ValidityStatus expResult = MetadataValidator.ValidityStatus.Valid;
        MetadataValidator.ValidityStatus result = MetadataValidator.isValidGMLLocation(testArray[1]);
        assertEquals(expResult, result);
    }

    /**
     * Test of isValidGMLLocation method, of class MetadataValidator.
     */
//    @Test
    public void testIsValidGMLLocation() throws ParserConfigurationException, SAXException, IOException {
        System.out.println("isValidGMLLocation");
        DocumentBuilderFactory docBuilderFac = DocumentBuilderFactory.newInstance();
        docBuilderFac.setNamespaceAware(true);
        DocumentBuilder docBuilder = docBuilderFac.newDocumentBuilder();
        
        Document gmlPointDoc = docBuilder.parse(new StringBufferInputStream(testArray[0]));
        Node geomLocationChild = gmlPointDoc.getDocumentElement();
        MetadataValidator.ValidityStatus expResult = MetadataValidator.ValidityStatus.Valid;
        MetadataValidator.ValidityStatus result = MetadataValidator.isValidGMLLocation(geomLocationChild);
        assertEquals(expResult, result);
    }

    /**
     * Test of isValidExtent method, of class MetadataValidator.
     */
//    @Test
    public void testIsValidExtent() {
        System.out.println("isValidExtent");
        Node extentNode = null;
        MetadataValidator.ValidityStatus expResult = MetadataValidator.ValidityStatus.Invalid;
        MetadataValidator.ValidityStatus result = MetadataValidator.isValidExtent(extentNode);
        assertEquals(expResult, result);
    }

    /**
     * Test of isValidGeoExtent method, of class MetadataValidator.
     */
//    @Test
    public void testIsValidGeoExtent() throws SAXException, IOException, ParserConfigurationException {
        System.out.println("isValidGeoExtent");
        DocumentBuilderFactory docBuilderFac = DocumentBuilderFactory.newInstance();
        docBuilderFac.setNamespaceAware(true);
        DocumentBuilder docBuilder = docBuilderFac.newDocumentBuilder();
        
        Document gmlPointDoc = docBuilder.parse(new StringBufferInputStream(testArray[4]));
        Node geoExtentNode = gmlPointDoc.getDocumentElement();
        MetadataValidator.ValidityStatus expResult = MetadataValidator.ValidityStatus.Valid;
        MetadataValidator.ValidityStatus result = MetadataValidator.isValidGeoExtent(geoExtentNode);
        assertEquals(expResult, result);
    }
    
}
