/**
 * 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.preferences.ftp;

import fr.cenra.rhomeo.RhomeoTestCase;
import java.security.GeneralSecurityException;
import org.apache.sis.test.DependsOnMethod;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

/**
 * Test {@link FTPPreferencesImpl} component, by trying to get and change information
 * about an FTP access.
 *
 * TODO : test {@link FTPAccess#createClient() }
 *
 * @author Alexis Manin (Geomatys)
 */
public class FTPPreferencesTest extends RhomeoTestCase {

    @Autowired
    @Qualifier(FTPPreferencesImpl.NAME)
    FTPPreferences prefs;

    /**
     * Test that methods which should not be used by user throw exception or expected empty value.
     */
    @Test
    public void testForbiddenMethods() {
        Assert.assertTrue("Key collection should be empty !" , prefs.getKeys().isEmpty());

        try {
            prefs.getPreference("whatever");
            Assert.fail("getProperty() method should not be usable !");
        } catch (UnsupportedOperationException e) {
            // Expected
        }

        try {
            prefs.setPreference("whatever", "blabla");
            Assert.fail("setProperty() method should not be usable !");
        } catch (UnsupportedOperationException e) {
            // Expected
        }
    }

    @DependsOnMethod("testForbiddenMethods")
    @Test
    public void testAccessPoint() throws Exception {
        final FTPAccess access = prefs.getAccess(FTPPreferences.ACCESS_POINTS.RESULTS);
        Assert.assertNotNull("No access point found !", access);

        final String expectedAd = "myAdress";
        final String expectedLogin = "toto";
        final String expectedWorkDir = "/my/work/dir";
        assertChanges(access, expectedAd, expectedLogin, null, expectedWorkDir);

        assertChanges(access, "newAdress", "newLogin", "newPassword!£²345*ù%", "newWorkDir");
    }

    public static void assertChanges(final FTPAccess access, final String adress, final String login, final String password, final String workDir) throws GeneralSecurityException {
        // Keep reference to access previous values to rollback user configuration after the test.
        final String oldAdress = access.getAdress();
        final String oldLogin = access.getLogin();
        final String oldPw = access.getPassword();
        final String oldDir = access.getWorkingDir();

        try {
            access.setAdress(adress);
            access.setLogin(login);
            access.setPassword(password);
            access.setWorkingDir(workDir);

            Assert.assertEquals("Adress", adress, access.getAdress());
            Assert.assertEquals("Login", login, access.getLogin());
            Assert.assertEquals("Working directory", workDir, access.getWorkingDir());
            if (password == null) {
                Assert.assertTrue("Password should be empty", access.getPassword().isEmpty());
            } else {
                Assert.assertEquals("Password", password, access.getPassword());
            }
        } finally {
            // Rollback to initial values.
            access.setAdress(oldAdress);
            access.setLogin(oldLogin);
            access.setPassword(oldPw);
            access.setWorkingDir(oldDir);
        }
    }
}