package meerkat.voting;

import meerkat.voting.gui.configuration.VotingBoothConfigurationCreator;
import org.factcenter.qilin.util.Pair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by Vladimir Eliezer Tokarev on 10/4/2016.
 * this class tests that the VotingBoothVonfigurationCreator does really creates the right configurations
 */
public class VotingBoothConfigurationCreatorTest {
    private VotingBoothConfigurationCreator configCreator;
    private String currentConfigurationPath = "/configuration/VotingBoothConfiguration.json";

    @Before
    public void setup() {
        this.configCreator = new VotingBoothConfigurationCreator();
    }


    @Test
    public void testConfigurationCreation() throws IOException, JSONException {
        List<Pair<JSONObject, Exception>> configs = new ArrayList<>();

        String content = new Scanner(getClass().getResourceAsStream(this.currentConfigurationPath)).useDelimiter("\\Z").next();
        JSONObject jsonObject = new JSONObject(content);
        JSONArray currentBallotQuestions = jsonObject.getJSONArray("questions");

        // simple test case try create current configurations suppose to run without exceptions
        JSONObject testCase1 = new JSONObject();
        testCase1.put("questions",  new JSONArray(currentBallotQuestions.toString()));
        configs.add(new Pair<>(testCase1, new Exception("what")));

        // The creation should fail when the amount of questions is different from what have been expected
        JSONObject testCase2 = new JSONObject();
        JSONArray array = new JSONArray();
        array.put(new JSONArray(currentBallotQuestions.toString()).getJSONObject(0));
        testCase2.put("questions", array);
        configs.add(new Pair<>(testCase2,new IOException(VotingBoothConfigurationCreator.WRONG_QUESTIONS_AMOUNT_ERROR_MESSAGE)));

        // We support only TEXT and IMAGE types for files
        JSONObject testCase3 = new JSONObject();
        array = new JSONArray(currentBallotQuestions.toString());
        JSONObject wrongType = array.getJSONObject(0);
        JSONObject uiAnswers = wrongType.getJSONObject("answers");
        uiAnswers.put("type", 1337);
        wrongType.put("answers", uiAnswers);
        array.put(0, wrongType);
        testCase3.put("questions", array);
        configs.add(new Pair<>(testCase3, new IOException(VotingBoothConfigurationCreator.UNSUPPORTED_VALUE_TYPE_ERROR_MESSAGE)));

    }
}
