package be.iminds.ilabt.jfed.experimenter_gui.config;

import be.iminds.ilabt.jfed.JFedWebApiClientModule;
import be.iminds.ilabt.jfed.experimenter_gui.JFedVersionInfo;
import be.iminds.ilabt.jfed.experimenter_gui.util.ConnectivityDetector;
import be.iminds.ilabt.jfed.experimenter_gui.util.FileUtils;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.JFedExperimenterGuiConfig;
import be.iminds.ilabt.jfed.lowlevel.GeniUser;
import be.iminds.ilabt.jfed.lowlevel.GeniUserProvider;
import be.iminds.ilabt.jfed.preferences.CorePreferenceKey;
import be.iminds.ilabt.jfed.preferences.JFedCorePreferences;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.GeniUrn;
import be.iminds.ilabt.jfed.util.IOUtils;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import static org.testng.Assert.*;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import static org.mockito.Mockito.*;

public class JFedExperimenterGuiConfigProviderTest {
    @Test
    /** Test with as much as possible null's in everything. It should be able to handle lot's of them. */
    public void testNull() throws IOException {
        File emptyGuiConfigFile = File.createTempFile("JFedExperimenterGuiConfigProviderTest-emptyGuiConfigFile-", ".json");
        String emptyGuiConfigContent = "{}\n";
        IOUtils.stringToFile(emptyGuiConfigFile, emptyGuiConfigContent);

        JFedPreferences jFedPreferences = new JFedCorePreferences();
        jFedPreferences.fillinBlanks();
        jFedPreferences.setString(CorePreferenceKey.PREF_GUICONFIG_SOURCE, "file:"+emptyGuiConfigFile.getPath());

        GeniUrn geniUserUrn = GeniUrn.createGeniUrnFromEncodedParts("example.com", "user", "test");
        GeniUser geniUser = mock(GeniUser.class);
        when(geniUser.getUserUrn()).thenReturn(geniUserUrn);
        when(geniUser.getUserUrnString()).thenReturn(geniUserUrn.toString());
        GeniUserProvider geniUserProvider =  mock(GeniUserProvider.class);
        when(geniUserProvider.getLoggedInGeniUser()).thenReturn(geniUser);
        when(geniUserProvider.isUserLoggedIn()).thenReturn(true);

        ConnectivityDetector connectivityDetector = mock(ConnectivityDetector.class);
        when(connectivityDetector.isNoInternet()).thenReturn(true);
        UserInfoProvider userInfoProvider = new UserInfoProvider(new JFedVersionInfo(), jFedPreferences);

        URL webApiUrl = new URL(JFedWebApiClientModule.DEFAULT_URL_STRING);
        be.iminds.ilabt.jfed.log.Logger logger = null; //not mandatory

        JFedExperimenterGuiConfigProvider guiConfigProvider = new JFedExperimenterGuiConfigProvider(
                geniUserProvider, jFedPreferences, connectivityDetector, userInfoProvider, webApiUrl, logger, false/*saveToCache*/);

        assertThat(guiConfigProvider.get(), is(notNullValue()));
        JFedExperimenterGuiConfig emptyConf = guiConfigProvider.get();
        assertThat(emptyConf.getBindableServers(), is(nullValue()));
        assertThat(emptyConf.getHideServers(), is(nullValue()));
        assertThat(emptyConf.getGtsConfig(), is(nullValue()));
        assertThat(emptyConf.getResourceClasses(), is(nullValue()));
        assertThat(emptyConf.getFlags(), is(nullValue()));
        assertThat(emptyConf.getUser(), is(nullValue()));
        assertThat(emptyConf.getClassName(), is(notNullValue()));
    }
}