package be.iminds.ilabt.jfed.testing.base;

import be.iminds.ilabt.jfed.CorePreferencesModule;
import be.iminds.ilabt.jfed.MockHttpRepliesTestCoreModule;
import be.iminds.ilabt.jfed.TestAuthoritiesModule;
import be.iminds.ilabt.jfed.TestUserModule;
import be.iminds.ilabt.jfed.call_log_output.LogOutput;
import be.iminds.ilabt.jfed.call_log_output.SerializableApiCallDetails;
import be.iminds.ilabt.jfed.lowlevel.GeniUser;
import be.iminds.ilabt.jfed.lowlevel.TestbedInfoSource;
import be.iminds.ilabt.jfed.lowlevel.api.test.DummyTest;

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

import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;

public class ApiTestMethodResultTest {

    @Test
    public void testXmlMarshalling() throws Exception {
        int id= 5;
        String authorityName = "test-authorityName";
        String authorityUrn = "test-authorityUrn";
        String baseServerUrl = "test-baseServerUrl";
        String callServerUrl = "test-callServerUrl";
        JFedConnection.ProxyInfo proxyInfo= null;
        String connectionSslAuthUserUrn = "test-connectionSslAuthUserUrn";
        String connectionBasicHttpAuthUsername = "test-connectionBasicHttpAuthUsername";
        String connectionId = "test-connectionId";
        String connectionSslAuthUserCertificates = "test-connectionSslAuthUserCertificates";
        String connectionAuthenticationMethod = "test-connectionAuthenticationMethod";
        String connectionProtocol = "test-connectionProtocol";
        String connectionType = "test-connectionType";

        String httpRequestStatusLine = "test-httpRequestStatusLine";
        String httpRequestHeaders = "test-httpRequestHeaders";
        String httpRequest = "test-httpRequest";
        String httpResponseStatusLine = "test-httpResponseStatusLine";
        String httpResponseHeaders = "test-httpResponseHeaders";
        String httpResponse = "test-httpResponse";

        String apiName = "test-apiName";
        String javaMethodName = "test-javaMethodName";
        String geniMethodName = "test-geniMethodName";
        String methodParameters = "test-methodParameters";

        String xmlRpcRequestJsonString = "test-xmlRpcRequestJsonString";
        String xmlRpcResponseJsonString = "test-xmlRpcResponseJsonString";

        String xmlRpcGeniResponseValue = "test-xmlRpcGeniResponseValue";
        String xmlRpcGeniResponseOutput = "test-xmlRpcGeniResponseOutput";
        String xmlRpcGeniResponseCode = "test-xmlRpcGeniResponseCode";

        Integer geniResponseCode= 0;
        String geniResponseCodeDescription= "SUCCESS";
        Boolean geniResponseCodeIsSuccess= true;
        String geniResponseOutput = "test-geniResponseOutput";

        String rawValue = "test-rawValue";

        Date startTime = new Date(1453707691000l);
        Date stopTime = new Date(1453707692000l);
        String protogeniSpewLogUrl = "test-protogeniSpewLogUrl";
        String exceptionString = "test-exceptionString";

        SerializableApiCallDetails originalApiCallDetail = new SerializableApiCallDetails(
                id, authorityName, authorityUrn,
                baseServerUrl,
                callServerUrl,
                proxyInfo,
                connectionSslAuthUserUrn, connectionBasicHttpAuthUsername, connectionId, connectionSslAuthUserCertificates,
                connectionAuthenticationMethod, connectionProtocol, connectionType,
                httpRequestStatusLine, httpRequestHeaders, httpRequest,
                httpResponseStatusLine, httpResponseHeaders, httpResponse,
                apiName, javaMethodName, geniMethodName, methodParameters,
                xmlRpcRequestJsonString, xmlRpcResponseJsonString,
                xmlRpcGeniResponseValue, xmlRpcGeniResponseOutput,
                xmlRpcGeniResponseCode,
                geniResponseCode, geniResponseCodeDescription,
                geniResponseCodeIsSuccess, geniResponseOutput,
                rawValue,
                startTime, stopTime,
                protogeniSpewLogUrl,
                exceptionString);

        String methodName= "test-methodName";
        String description= "test-description";
        List<String> hardDependencies = new ArrayList<String>();
        List<String> softDependencies = new ArrayList<String>();
        List< LogOutput.LogEntry> logLines=  new ArrayList<LogOutput.LogEntry>();
        List<SerializableApiCallDetails> apiCallDetails = new ArrayList<SerializableApiCallDetails>();
        apiCallDetails.add(originalApiCallDetail);
        Throwable exception= null;
        LogOutput.TestResultState state = LogOutput.TestResultState.SUCCESS;
        long timeMs= 90;
        long startTimeMs= 10;
        long stopTimeMs = 100;
        ApiTestResult.ApiTestMethodResult method = new ApiTestResult.ApiTestMethodResult(
                methodName, description, hardDependencies,softDependencies,logLines, apiCallDetails, exception, state, timeMs, startTimeMs, stopTimeMs);

        TestAuthoritiesModule testAuthoritiesModule = new TestAuthoritiesModule();
        Injector injector = Guice.createInjector(testAuthoritiesModule, new CorePreferencesModule(), new MockHttpRepliesTestCoreModule(), new TestUserModule());
        TestbedInfoSource testbedInfoSource = injector.getInstance(TestbedInfoSource.class);
        Server auth = testbedInfoSource.getByUrnExact(testAuthoritiesModule.getTestAuthority2Urn());
        assert auth != null;
        TargetAuthority testedAuthority = new TargetAuthority(auth.getDefaultComponentManagerAsGeniUrn(), auth, auth);
        GeniUser user = injector.getInstance(GeniUser.class);
        assert user != null;
        Class<? extends ApiTest> testClass = DummyTest.class;
        String testGroup = null;
        ApiTestResult apiTestResult = new ApiTestResult(testedAuthority, user, testClass, testGroup);
        apiTestResult.resultList.add(method);

        StringWriter sw = new StringWriter();
        apiTestResult.toXmlOverview(sw, true/*showCredentials*/);
        System.out.println(sw.toString());
        System.out.println("length=" + sw.toString().length());

        assertThat(sw.toString(), containsString(methodName));
        assertThat(sw.toString(), containsString(description));

        assertThat(sw.toString(), containsString(testAuthoritiesModule.getTestAuthority2Urn().toString()));

        assertThat(sw.toString(), containsString(exceptionString));
        assertThat(sw.toString(), containsString(authorityName));
        assertThat(sw.toString(), containsString(callServerUrl));
        assertThat(sw.toString(), containsString(httpRequestStatusLine));
        assertThat(sw.toString(), containsString(javaMethodName));
        assertThat(sw.toString(), containsString(xmlRpcRequestJsonString));
        assertThat(sw.toString(), containsString(geniResponseOutput));
        assertThat(sw.toString(), containsString(protogeniSpewLogUrl));
    }
}
