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.*;

import java.net.MalformedURLException;

import static org.hamcrest.MatcherAssert.assertThat;

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

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

        testerBuilder = new JFedTesterBuilder();

        //Use the built in test user.
        //   You have to trust the dummy user's authority for this to work, the certificate will be printed when the test runs.
        //   Or you can get it at JFedTesterBuilder.dummyAuthServerCertPem
        testerBuilder.setDummyUser();

        //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");

        jfed = testerBuilder.build();
        assert jfed != null;
        assert jfed.am != null;

        System.out.println("Test setup complete.");
    }

    @AfterMethod
    public void tearDown() throws Exception {
        jfed = null;
    }

    @Test(enabled=false)
    public void testLowLevelGetVersion() throws JFedException, MalformedURLException {
        assert jfed != null;
        assert jfed.am != null;

        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 {
        assert jfed != null;
        assert jfed.am != null;

        jfed.am.getVersion();
    }

    @Test(enabled=false)
    public void testListResources() throws JFedException {
        assert jfed != null;
        assert jfed.am != null;

        String rspec = jfed.am.listResources(jfed.userCreds, false/*available*/);
        System.out.println("Received Advertisement RSpec: "+rspec);
    }
}
