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

import be.iminds.ilabt.jfed.log.Logger;
import be.iminds.ilabt.jfed.lowlevel.connection.HttpConnection;
import be.iminds.ilabt.jfed.preferences.JFedPreferences;
import be.iminds.ilabt.jfed.util.GeniUrn;
import org.mockito.Mockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import static org.mockito.Mockito.mock;
import static org.testng.Assert.assertEquals;

/**
 * User: twalcari
 * Date: 1/8/2016
 * Time: 12:04
 */
public class ReputationServiceTest extends RestApiTest {

    private ReputationService reputationService;
    private static final GeniUrn USER_URN = GeniUrn.parse("urn:publicid:IDN+fed4fire:global+user+arisleiv");
    private static final GeniUrn SLICE_URN = GeniUrn.parse("urn:publicid:IDN+fed4fire:global:reputation+slice+other");
    private static final Date START_DATE = new Date(1436176077 * 1000L);
    private static final Date END_DATE = new Date(1436172077 * 1000L);

    @BeforeMethod
    public void setUp() throws Exception {
        Logger logger = mock(Logger.class);
        JFedPreferences preferences = mock(JFedPreferences.class);

        reputationService = new ReputationService(logger, preferences);


    }

    @Test
    public void testRegisterExperiment() throws Exception {

        String response = "[\n" +
                "  {\n" +
                "    \"user_urn\": \"urn:publicid:IDN+fed4fire:global+user+arisleiv\",\n" +
                "    \"slice_urn\": \"urn:publicid:IDN+fed4fire:global:reputation+slice+other\",\n" +
                "    \"start_time\": \"2015-07-06T09:47:57+00:00\",\n" +
                "    \"end_time\": \"2015-07-06T08:41:17+00:00\"\n" +
                "  }\n" +
                "]\n";


        HttpConnection con = mockHttpConnection(response);


        RestApi.RestReply<List<ReputationService.SliceReputation>> reply =
                reputationService.registerExperiment(con, USER_URN, SLICE_URN, START_DATE,
                        END_DATE);


        assertEquals(reply.getValue().size(), 1);
        final ReputationService.SliceReputation sliceReputation = reply.getValue().get(0);
        assertEquals(sliceReputation.getUserUrn(), USER_URN);
        assertEquals(sliceReputation.getSliceUrn(), SLICE_URN);
        assertEquals(sliceReputation.getStartTime(), START_DATE);
        assertEquals(sliceReputation.getEndTime(), END_DATE);

    }

    @Test
    public void testGetReputation() throws Exception {
        String response = "[{\"testbed\":\"omf:netmode\",\"services\":[{\"overall\":1.0},{\"availability\":1.0}]}]";

        HttpConnection con = mockHttpConnection(response);

        RestApi.RestReply<ReputationService.TestbedReputation> reply =
                reputationService.getReputation(con, GeniUrn.createGeniUrnFromEncodedParts("authority", "omf:netmode", "cm"));

        assertEquals(reply.getValue().getTestbed(), "omf:netmode");
        assertEquals(reply.getValue().getServices().size(), 2);
        assertEquals(reply.getValue().getServices().get("overall"), 1.0);
        assertEquals(reply.getValue().getServices().get("availability"), 1.0);
    }

    @Test
    public void testGetReputations() throws Exception {
        String responseString =
                "[{\"testbed\":\"omf:netmode\",\"services\":[{\"overall\":1.0},{\"availability\":1.0}]},{\"testbed\":\"omf:nitos\",\"services\":[{\"overall\":1.0},{\"availability\":1.0}]}]";

        HttpConnection con = mockHttpConnection(responseString);

        RestApi.RestReply<List<ReputationService.TestbedReputation>> reply =
                reputationService.getReputations(con);


        assertEquals(reply.getValue().size(), 2);
        Mockito.verify(con).getHttpClient();
    }

    @Test
    public void testRegisterUserQOE() throws Exception {
        String response = "DATA RECEIVED!";

        HttpConnection con = mockHttpConnection(response);

        final RestApi.RestReply<String> reply =
                reputationService.registerUserQOE(con, USER_URN, SLICE_URN, START_DATE, END_DATE, Collections.<GeniUrn>emptyList(), 5, 5, 5);

        assertEquals(reply.getValue(), response);

    }
}