package be.iminds.ilabt.jfed;

import be.iminds.ilabt.jfed.lowlevel.ApiInfo;
import be.iminds.ilabt.jfed.lowlevel.TestCertHelper;
import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;

import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;
import be.iminds.ilabt.jfed.util.GeniUrn;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.testng.annotations.Test;

import java.net.MalformedURLException;
import java.net.URL;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.testng.Assert.assertNotNull;

/**
 * TestAuthoritiesModuleTest
 */
public class TestAuthoritiesModuleTest {
    @Test
    public void testExampleAuth() throws GeniUrn.GeniUrnParseException, MalformedURLException {
//        TestAuthoritiesModule testAuthoritiesModule = new TestAuthoritiesModule(
//                new TestAuthoritiesModule.AMConfig(true/*hasV2*/, true/*hasV3*/, false/*hasUpToDateAdv*/, false/*hasAny*/),
//                new TestAuthoritiesModule.AMConfig(true/*hasV2*/, false/*hasV3*/, false/*hasUpToDateAdv*/, false/*hasAny*/),
//                new TestAuthoritiesModule.AMConfig());
        TestAuthoritiesModule testAuthoritiesModule = new TestAuthoritiesModule();
        Injector injector = Guice.createInjector(testAuthoritiesModule, new CorePreferencesModule(), new MockHttpRepliesTestCoreModule(), new TestUserModule());
        TestbedInfoSource testbedInfoSource = injector.getInstance(TestbedInfoSource.class);
        TestCertHelper testCertHelper = injector.getInstance(TestCertHelper.class);

        GeniUrn exampleUrn = testAuthoritiesModule.getTestAuthorityUrn();
        GeniUrn exampleUrn2 = testAuthoritiesModule.getTestAuthority2Urn();
        GeniUrn exampleUrn3 = GeniUrn.parse(TestCertHelper.scsUrn);
        assertNotNull(exampleUrn);
        assertNotNull(exampleUrn2);
        assertNotNull(exampleUrn3);

        assertThat(testbedInfoSource.getServers(), hasSize(4));

        Server exampleAuth = testbedInfoSource.getByUrnExact(exampleUrn);
        assertNotNull(exampleAuth);
        assertNotNull(exampleAuth.getDefaultComponentManagerUrn());
        assertThat(exampleAuth.getDefaultComponentManagerUrn(), equalTo(exampleUrn.toString()));
        assertThat(exampleAuth.getName(), equalTo(TestAuthoritiesModule.hrnA));
        assertThat(ApiInfo.findUrl(exampleAuth, ApiInfo.ApiName.GENI_AM, 3), equalTo(new URL(TestAuthoritiesModule.urlA_am3)));

        Server exampleAuth1b = testbedInfoSource.getByUrnExact(TestCertHelper.saUrn);
        assertNotNull(exampleAuth1b);
        assertNotNull(exampleAuth1b.getDefaultComponentManagerUrn());

        Server exampleAuth1c = testbedInfoSource.getByUrnExact(TestCertHelper.sa2Urn);
        assertNotNull(exampleAuth1c);
        assertNotNull(exampleAuth1c.getDefaultComponentManagerUrn());

        Server exampleAuth2 = testbedInfoSource.getByUrnExact(exampleUrn2);
        assertNotNull(exampleAuth2);
        assertNotNull(exampleAuth2.getDefaultComponentManagerUrn());
        assertThat(exampleAuth2.getDefaultComponentManagerUrn(), equalTo(exampleUrn2.toString()));
        assertThat(exampleAuth2.getName(), equalTo(TestAuthoritiesModule.hrnB));
        assertThat(ApiInfo.findUrl(exampleAuth2, ApiInfo.ApiName.GENI_AM, 2), equalTo(new URL(TestAuthoritiesModule.urlB_am2)));


        Server exampleAuth3 = testbedInfoSource.getByUrnExact(exampleUrn3);
        assertNotNull(exampleAuth3);
        assertNotNull(exampleAuth3.getDefaultComponentManagerUrn());
        assertThat(exampleAuth3.getDefaultComponentManagerUrn(), equalTo(exampleUrn3.toString()));
        assertThat(exampleAuth3.getName(), equalTo(TestAuthoritiesModule.hrnC));
        assertThat(ApiInfo.findUrl(exampleAuth3, ApiInfo.ApiName.GENI_SCS, 1), equalTo(new URL(TestAuthoritiesModule.urlC_scs)));
    }
}
