/**
 * Copyright © CENRA (2016)
 *
 * remi.clement@espaces-naturels.fr
 * 
 * This software is a multi-platform and portable application based on 
 * the scientifical toolbox to monitor wetlands. With this toolbox it’s 
 * possible to calculate indicators «RhoMéO» to monitor quality, 
 * fonctionalities and urban and agricultural pressures of wetlands. 
 * Indicators are calculated using naturalist data entered and/or imported,
 * and geographic data downloaded from a geographic server.
 * 
 * This software is governed by the CeCILL license under French law and
 * abiding by the rules of distribution of free software.  You can  use, 
 * modify and/ or redistribute the software under the terms of the CeCILL
 * license as circulated by CEA, CNRS and INRIA at the following URL
 * "http://www.cecill.info". 
 * 
 * As a counterpart to the access to the source code and  rights to copy,
 * modify and redistribute granted by the license, users are provided only
 * with a limited warranty  and the software's author,  the holder of the
 * economic rights,  and the successive licensors  have only  limited
 * liability. 
 * 
 * In this respect, the user's attention is drawn to the risks associated
 * with loading,  using,  modifying and/or developing or reproducing the
 * software by the user in light of its specific status of free software,
 * that may mean  that it is complicated to manipulate,  and  that  also
 * therefore means  that it is reserved for developers  and  experienced
 * professionals having in-depth computer knowledge. Users are therefore
 * encouraged to load and test the software's suitability as regards their
 * requirements in conditions enabling the security of their systems and/or 
 * data to be ensured and,  more generally, to use and operate it in the 
 * same conditions as regards security. 
 * 
 * The fact that you are presently reading this means that you have had
 * knowledge of the CeCILL license and that you accept its terms.
*/
package fr.cenra.rhomeo.core.data;

import fr.cenra.rhomeo.RhomeoTestCase;
import fr.cenra.rhomeo.api.data.Dataset;
import fr.cenra.rhomeo.api.data.Protocol;
import fr.cenra.rhomeo.api.data.TrackingPoint;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Random;
import javafx.collections.ObservableList;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 *
 * @author Alexis Manin (Geomatys)
 */
public class DatasetTest extends RhomeoTestCase {

    @Autowired
    @Qualifier(MockProtocol.NAME)
    Protocol protocol;

    @Test
    public void checkTrackingPoints() {
        final Dataset dataset = new Dataset(protocol);
        final MockStatement first = new MockStatement();
        dataset.getItems().add(first);
        final ObservableList<TrackingPoint> tPoints = dataset.getTrackingPoints();
        Assert.assertTrue("No tracking point should exist", tPoints.isEmpty());

        final LocalDate pointDate = LocalDate.now();
        final String pointName = "lolo";
        first.setDate(pointDate);
        first.setTrackingPoint(pointName);
        Assert.assertTrue("Tracking point list should contain one unique element", tPoints.size() == 1);
        Assert.assertEquals("Tracking point name is invalid !", pointName, tPoints.get(0).getName());
        Assert.assertEquals("Tracking point date is invalid !", pointDate.getYear(), tPoints.get(0).getYear());

        final MockStatement second = new MockStatement("secondPoint", LocalDate.now());
        dataset.getItems().add(second);
        Assert.assertTrue("Tracking point list should contain two elements", tPoints.size() == 2);
        Assert.assertEquals("Tracking point name is invalid !", second.getTrackingPoint(), tPoints.get(1).getName());
        Assert.assertEquals("Tracking point date is invalid !", second.getDate().getYear(), tPoints.get(1).getYear());

        second.setTrackingPoint("another !");
        Assert.assertTrue("Tracking point list should contain two elements", tPoints.size() == 2);
        Assert.assertEquals("Tracking point name is invalid !", second.getTrackingPoint(), tPoints.get(1).getName());
        Assert.assertEquals("Tracking point date is invalid !", second.getDate().getYear(), tPoints.get(1).getYear());

        dataset.getItems().remove(first);
        Assert.assertTrue("Tracking point list should contain one unique element", tPoints.size() == 1);
        Assert.assertEquals("Tracking point name is invalid !", second.getTrackingPoint(), tPoints.get(0).getName());
        Assert.assertEquals("Tracking point date is invalid !", second.getDate().getYear(), tPoints.get(0).getYear());
    }

    @Test
    public void chargeTest() {
        final String points[] = new String[]{"a", "B", "obzo", "ff", "papa", "tata", "titi", "tete", "lala", "lolo", "lele"};
        final Random rand = new Random();
        final Dataset d = new Dataset(protocol);
        final ObservableList<TrackingPoint> tPoints = d.getTrackingPoints();
        for (int i = 0 ; i < 4000 ; i++) {
            d.getItems().add(new MockStatement(points[rand.nextInt(points.length -1)], LocalDate.now().minusYears(rand.nextInt(10))));
        }

        Assert.assertFalse("Tracking point list should contain data !", tPoints.isEmpty());
        final HashSet uniquePoints = new HashSet(tPoints);
        Assert.assertEquals("Uniqueness broken !", uniquePoints.size(), tPoints.size());
    }
}
