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

import be.iminds.ilabt.jfed.fedmon.webapi.service.json.JFedExperimenterGuiConfigBuilder;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.ResourceClass;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.Server;
import be.iminds.ilabt.jfed.fedmon.webapi.service.json.Testbed;
import be.iminds.ilabt.jfed.lowlevel.BasicTestbedInfoSource;
import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;
import be.iminds.ilabt.jfed.lowlevel.authority.AuthorityFinder;
import be.iminds.ilabt.jfed.preferences.JFedCorePreferences;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.GeniUrn;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class JFedGuiConfigImplTest {

    @Test
    /** Test with as much as possible null's in everything. It should be able to handle lot's of them. */
    public void testNull() {
        JFedPreferences jFedPreferences = new JFedCorePreferences();
        jFedPreferences.fillinBlanks();

        JFedExperimenterGuiConfigProvider guiConfigProvider = mock(JFedExperimenterGuiConfigProvider.class);
        when(guiConfigProvider.get()).thenReturn(new JFedExperimenterGuiConfigBuilder().create());

        TestbedInfoSource testbedInfoSource = BasicTestbedInfoSource.createFromTestbeds(null);

        AuthorityFinder authorityFinder = new AuthorityFinder(testbedInfoSource, jFedPreferences);

        JFedGuiConfigImpl conf = new JFedGuiConfigImpl(
                testbedInfoSource,
                authorityFinder,
                guiConfigProvider,
                null /*testbedNodesMapFetcher*/,
                null /*healthServiceClient*/);

        Server server = mock(Server.class);
        when(server.getId()).thenReturn(12345);
        Testbed testbed = mock(Testbed.class);
        when(testbed.getId()).thenReturn("fakeTestbed");
        ResourceClass resourceClass = mock(ResourceClass.class);
        when(resourceClass.getId()).thenReturn("fakeResourceClass");
        GeniUrn compManUrn = GeniUrn.createGeniUrnFromEncodedParts("example.com", "auth", "cm");

        assertThat(conf.getBindableServers(), is(notNullValue()));
        assertThat(conf.getBooleanFlag("test"), is(nullValue()));
        assertThat(conf.getStringFlag("test"), is(nullValue()));
        assertThat(conf.getIntegerFlag("test"), is(nullValue()));
        assertThat(conf.getLongFlag("test"), is(nullValue()));
        assertThat(conf.getComponentInfo(server, JFedGuiConfig.Scope.ALL), hasSize(0));
        assertThat(conf.getComponentInfo(server, JFedGuiConfig.Scope.AVAILABLE_ONLY), hasSize(0));
        assertThat(conf.getAdvertisement(server, null), is(notNullValue()));
        assertThat(conf.getColor(server), is(nullValue()));
        assertThat(conf.getColor(testbed), is(nullValue()));
        assertThat(conf.getDefaultResource(resourceClass), is(nullValue()));
        assertThat(conf.getDefaultResourceClass(), is(nullValue()));
        assertThat(conf.getGtsConfig(), is(nullValue()));
        assertThat(conf.getHardwareTypes(server, JFedGuiConfig.Scope.ALL), hasSize(0));
        assertThat(conf.getHardwareTypes(server, JFedGuiConfig.Scope.AVAILABLE_ONLY), hasSize(0));
        assertThat(conf.getServerGlimpse(server), is(nullValue()));
        assertThat(conf.getImageMap(server), is(nullValue()));
        assertThat(conf.getLeaf(resourceClass), anyOf(is(nullValue()), is(resourceClass)));
        assertThat(conf.getNodeExclusivity(compManUrn, "fake-slivertype"), is(nullValue()));
        assertThat(conf.getNodesVisualization(server), is(notNullValue()));
        assertThat(conf.getResourceClass("fakeId"), is(nullValue()));
        assertThat(conf.getResourceClassesByType(JFedGuiConfig.ResourceType.RAW_PC), hasSize(0));
        assertThat(conf.getResourceType(resourceClass), is(JFedGuiConfig.ResourceType.OTHER));
        assertThat(conf.getSliverTypeNames(server), hasSize(0));
        assertThat(conf.getDiskImages(server), hasSize(0));
        assertThat(conf.getDiskImages(server, "fake-slivertype"), hasSize(0));
    }
}