package be.iminds.ilabt.jfed.test;

import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.ApiInfo;
import be.iminds.ilabt.jfed.lowlevel.api.AggregateManager2;
import be.iminds.ilabt.jfed.lowlevel.connection.BasicConnectionBuilder;
import be.iminds.ilabt.jfed.lowlevel.connection.SfaConnection;
import javafx.embed.swing.JFXPanel;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.net.MalformedURLException;

//These tests only work when you have a test server running at localhost!
@Test(enabled=false)
public class JFedTestExampleWithRealUser {
    JFedTesterBuilder testerBuilder;
    JFedTest jfed;

    @BeforeMethod
    public void setUp() throws Exception {
        //initialize JavaFX
        new JFXPanel();

        testerBuilder = new JFedTesterBuilder();
        //Let the test builder know we want to fetch this authorities info from the normal jFed testbed info
        //You need to do this for all authorities of (non dummy) users, and for all non-local tested servers.
        testerBuilder.registerAuthorityFromJFedAuthorities("urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm");

        //The user login PEM file to use
        testerBuilder.setPemFileUser("/home/someone/.ssl/my_cert.pem");


        //Register the local AM server to test
        //  (this will automatically download and trust it's certificate, this is insecure, but that doesn't matter for a test like this)
        testerBuilder.registerAuthority("urn:publicid:IDN+dummy+authority+cm", 2, "https://localhost:8001");

//        //Instead of the local test authority, we could also test a real authority
//        //We do have to call registerAuthorityFromJFedAuthorities for it as well
//        testerBuilder.registerAuthorityFromJFedAuthorities("urn:publicid:IDN+emulab.net+authority+cm");
//        testerBuilder.useAuthority("urn:publicid:IDN+emulab.net+authority+cm");

        jfed = testerBuilder.build();
    }

    @AfterMethod
    public void tearDown() throws Exception {

    }

    @Test(enabled=false)
    public void testLowLevelGetVersion() throws JFedException, MalformedURLException {
        SfaConnection con = (SfaConnection) jfed.connectionProvider.getConnectionByUrl(
                jfed.trustStore,
                jfed.user,
                ApiInfo.findUrl(jfed.am.getAmServer(), ApiInfo.ApiName.GENI_AM, 2),
                new BasicConnectionBuilder.INSECURE_TRUSTALL_HandleUntrustedCallback(),
                new ApiInfo.Api(ApiInfo.ApiName.GENI_AM, 2),
                null, null);
        AggregateManager2 am2 = new AggregateManager2(jfed.logger, jfed.prefs);
        am2.getVersion(con);
    }

    @Test(enabled=false)
    public void testGetVersion() throws JFedException {
        jfed.am.getVersion();
    }

    @Test(enabled=false)
    public void testListResources() throws JFedException {
        String rspec = jfed.am.listResources(jfed.userCreds, false/*available*/);
        System.out.println("Received Advertisement RSpec: "+rspec);
    }
}
