package org.xbib.files.ftp.test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.time.ZonedDateTime;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import org.xbib.files.ftp.FTPClient;
import org.xbib.files.ftp.FTPClientConfig;
import org.xbib.files.ftp.FTPFile;

/*
 * This test was contributed in a different form by W. McDonald Buck
 * of Boulder, Colorado, to help fix some bugs with the FTPClientConfig
 * in a real world setting.  It is a perfect functional test for the
 * Time Zone functionality of FTPClientConfig.
 *
 * A publicly accessible FTP server at the US National Oceanographic and
 * Atmospheric Adminstration houses a directory which contains
 * 300 files, named sn.0000 to sn.0300. Every ten minutes or so
 * the next file in sequence is rewritten with new data. Thus the directory
 * contains observations for more than 24 hours of data.  Since the server
 * has its clock set to GMT this is an excellent functional test for any
 * machine in a different time zone.
 *
 * Noteworthy is the fact that the ftp routines in some web browsers don't
 * work as well as this.  They can't, since they have no way of knowing the
 * server's time zone.  Depending on the local machine's position relative
 * to GMT and the time of day, the browsers may decide that a timestamp
 * would be in the  future if given the current year, so they assume the
 * year to be  last year.  This illustrates the value of FTPClientConfig's
 * time zone functionality.
 */

public class FTPClientConfigFunctionalTest {

    private static FTPClient ftpClient = new FTPClient();

    private static FTPClientConfig ftpClientConfig;

    @BeforeAll
    public static void setUp() throws Exception {
        ftpClientConfig = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
        ftpClientConfig.setServerTimeZoneId("GMT");
        ftpClient.configure(ftpClientConfig);
        ftpClient.connect("tgftp.nws.noaa.gov");
        ftpClient.login("anonymous","testing@apache.org");
        ftpClient.changeWorkingDirectory("SL.us008001/DF.an/DC.sflnd/DS.metar");
        ftpClient.enterLocalPassiveMode();
    }

    @AfterAll
    public static void tearDown() throws Exception {
        ftpClient.disconnect();
    }

    private Set<FTPFile> getSortedList(FTPFile[] files) {
        Set<FTPFile> sorted = new TreeSet<>(Comparator.comparing(FTPFile::getTimestamp));
        for (FTPFile file : files) {
            if (file.getName().startsWith("sn")) {
                sorted.add(file);
            }
        }
        return sorted;
    }

    @Test
    public void testTimeZoneFunctionality() throws Exception {
        FTPFile[] files = ftpClient.listFiles();
        Set<FTPFile> sorted = getSortedList(files);
        FTPFile lastfile = null;
        FTPFile firstfile = null;
        for (FTPFile thisfile : sorted) {
            if (firstfile == null) {
                firstfile = thisfile;
            }
            if (lastfile != null) {
                assertTrue(lastfile.getTimestamp().isBefore(thisfile.getTimestamp()));
            }
            lastfile = thisfile;
        }
        if (firstfile == null)  {
            fail("No files found");
        } else {
            assertTrue(lastfile.getTimestamp().isBefore(ZonedDateTime.now()));
            ZonedDateTime first = firstfile.getTimestamp();
            first = first.plusDays(2);
            assertTrue(lastfile.getTimestamp().isAfter(first), lastfile.getTimestamp() + " after "+ first);
        }
    }
}




