package be.iminds.ilabt.jfed.lowlevel.api.test;

import be.iminds.ilabt.jfed.log.Logger;
import be.iminds.ilabt.jfed.lowlevel.ApiInfo;
import be.iminds.ilabt.jfed.lowlevel.GeniUserProvider;
import be.iminds.ilabt.jfed.lowlevel.JFedException;
import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;
import be.iminds.ilabt.jfed.lowlevel.api.ProtogeniSliceAuthority;
import be.iminds.ilabt.jfed.lowlevel.connection.JFedConnectionProvider;
import be.iminds.ilabt.jfed.lowlevel.connection.SfaConnection;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.testing.base.ApiTest;
import be.iminds.ilabt.jfed.util.TargetAuthority;

import javax.inject.Inject;
import java.util.Map;

/**
 * TestSliceAuthority
 */
public class TestSliceAuthority extends ApiTest {
    @Inject
    public TestSliceAuthority(Logger logger, TargetAuthority testedAuthority, GeniUserProvider geniUserProvider, JFedConnectionProvider connectionProvider, TestbedInfoSource testbedInfoSource, JFedPreferences jFedPreferences) {
        super(logger, testedAuthority, geniUserProvider, connectionProvider, testbedInfoSource, jFedPreferences);
    }

    public String getTestDescription() {
        return "Slice Authority Tests. Currently only GetVersion is tested, this needs to be extended.";
    }

    private ProtogeniSliceAuthority sa;


    public SfaConnection getConnection() throws JFedException {
        return (SfaConnection) connectionProvider.getConnectionByAuthority(user, user.getUserAuthorityServer(), new ApiInfo.Api(ApiInfo.ApiName.PROTOGENI_SA, 1));
    }

    public void setUp() {

        sa = new ProtogeniSliceAuthority(logger, jFedPreferences);
    }

    /**
     * Check for correctness of a SA XmlRpc result. Should be tested for each reply received.
     * */
    public static void testSACorrectnessXmlRpcResult(Map res) {
        Object code = res.get("code");
        Object value = res.get("value");
        Object output = res.get("output");
        assert(code != null);
        assert(value != null);
        assert(output != null);
        assert(code instanceof Integer);
        assert(output instanceof String);
    }

    @Test
    public void testGetVersion() throws JFedException {
        System.out.println("GetVersion test starting");
        ProtogeniSliceAuthority.SliceAuthorityReply<String> reply = sa.getVersion(getConnection());
        assert (reply.getGeniResponseCode().isSuccess());
        System.out.println("GetVersion test finished");
    }

}
