package com.agilex.healthcare.schedulingmanager.linkbuilder;

import com.agilex.healthcare.schedulingmanager.domain.WaitListEntries;
import com.agilex.healthcare.schedulingmanager.domain.WaitListEntry;
import junit.framework.TestCase;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;

/**
 * Created by
 * User: ralph
 * Date: 11/5/13
 * Time: 5:28 PM
 */
public class WaitListLinkBuilderTest extends TestCase {

    public static final String      TEST_BASE_URI = "http://localhost:8080/MobileHealthcare";
    public static final String      TEST_FACILITY = "777";
    public static final String      TEST_STOP_CODE_1 = "xyz123";
    public static final String      TEST_ID_1 = "ABC123";
    public static final String      TEST_STOP_CODE_2 = "xyz$%^";
    public static final String      TEST_ID_2 = "ABC456";

    public static final String[]    RESULT_URL_1 = {
            TEST_BASE_URI + "/scheduling-service/site/" + TEST_FACILITY + "/waitlist/" + TEST_STOP_CODE_1 + "/id/" + TEST_ID_1,
            TEST_BASE_URI + "/scheduling-service/site/" + TEST_FACILITY + "/waitlist/" + TEST_STOP_CODE_1 + "/id/" + TEST_ID_1 + "/detail"
    };

    public static final String[]    RESULT_URL_2 = {
            TEST_BASE_URI + "/scheduling-service/site/" + TEST_FACILITY + "/waitlist/" + TEST_STOP_CODE_2 + "/id/" + TEST_ID_2,
            TEST_BASE_URI + "/scheduling-service/site/" + TEST_FACILITY + "/waitlist/" + TEST_STOP_CODE_2 + "/id/" + TEST_ID_2 + "/detail"
    };

    public static final ArrayList<String[]>  RESULT_LIST = new ArrayList<String[]>();


    @Before
    public void setup() {
        RESULT_LIST.clear();
        RESULT_LIST.add(RESULT_URL_1);
        RESULT_LIST.add(RESULT_URL_2);
    }

    @After
    public void teardown() {

    }

    @Test
    public void  testFillLinksWaitListEntries()
            throws URISyntaxException, IOException {
        WaitListLinkBuilder linkBuilder = new WaitListLinkBuilder(new URI(TEST_BASE_URI));
        WaitListEntries                         waitListEntries;
        WaitListEntry                           waitListEntry;
        WaitListEntries                         returnedWaitListEntries;

        waitListEntries = new WaitListEntries();

        waitListEntry = new WaitListEntry();
        waitListEntry.setId(TEST_ID_1);
        waitListEntry.setStopCode(TEST_STOP_CODE_1);
        waitListEntries.add(waitListEntry);

        waitListEntry = new WaitListEntry();
        waitListEntry.setId(TEST_ID_2);
        waitListEntry.setStopCode(TEST_STOP_CODE_2);
        waitListEntries.add(waitListEntry);

        returnedWaitListEntries = linkBuilder.fillLinks(waitListEntries, new URI(TEST_BASE_URI), TEST_FACILITY);

        for(int  y = 0; y < RESULT_LIST.size(); y++) {

            assertEquals("The wrong number of links were built", RESULT_LIST.get(y).length,
                         returnedWaitListEntries.get(y).getLink().size());

            for(int  x = 0; x < RESULT_LIST.get(y).length; x++) {
                assertEquals("The URL was not built correctly (x = " + x + ")", RESULT_LIST.get(y)[x],
                             returnedWaitListEntries.get(y).getLink().get(0).getHref().toString());
            }
        }
    }
}
