package com.quizdeck.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.quizdeck.analysis.inputs.Question;
import com.quizdeck.analysis.outputs.QuizAnalysisData;
import org.junit.Test;
import org.springframework.http.MediaType;

import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Test the analysis controller
 *
 * @author Alex
 */
public class QuizAccuracyAnalysisControllerTest extends AbstractQuizAnalysisControllerTest {

    @Test
    public void testQuizAccuracyResults() throws Exception {
        String result = mockMvc.perform(post("/rest/secure/analysis/accuracy/").content(this.json(accuracyInput))
                                                .contentType(MediaType.APPLICATION_JSON)
                                        ).andExpect(status().is2xxSuccessful())
                                        .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                                        .andReturn()
                                        .getResponse()
                                        .getContentAsString();

        ObjectMapper mapper = new ObjectMapper();
        QuizAnalysisData data = mapper.readValue(result, QuizAnalysisData.class);

        checkAnswerKey(data.getQuestions());
        checkQuizInfo(data);
        checkQuizStats(data);
    }

    private void checkAnswerKey(List<Question> questions) {
        assertThat(questions.toString(), questions.size(), is(completeQuiz.getQuiz().getQuestions().size()));

        int numHits = 0;
        for(Question question : questions) {
            if(completeQuiz.getQuiz().getQuestions().contains(question))
                numHits++;
        }

        assertThat("Incorrect questions in answer key", numHits, is(completeQuiz.getQuiz().getQuestions().size()));
    }

    private void checkQuizInfo(QuizAnalysisData data){
        assertThat("Bad quiz owner ID", data.getOwnerID(), is(completeQuiz.getOwner()));
        assertThat("Bad quiz ID", data.getQuizID(), is(completeQuiz.getQuizId()));
    }

    private void checkQuizStats(QuizAnalysisData data) {
        assertThat("Bad \'Average Accuracy Per Participant\'", data.getStats().get("Average Accuracy Per Participant"), is("0.3"));
    }
}
@@ -1612,1005 +1612,168 @@
     
-checkAnswerKey(data.getQuestions());%0A        checkQuizInfo(data);%0A        checkQuizStats(data);%0A    %7D%0A%0A    private void checkAnswerKey(List%3CQuestion%3E questions) %7B%0A        assertThat(questions.toString(), questions.size(), is(completeQuiz.getQuiz().getQuestions().size()));%0A%0A        int numHits = 0;%0A        for(Question question : questions) %7B%0A            if(completeQuiz.getQuiz().getQuestions().contains(question))%0A                numHits++;%0A        %7D%0A%0A        assertThat(%22Incorrect questions in answer key%22, numHits, is(completeQuiz.getQuiz().getQuestions().size()));%0A    %7D%0A%0A    private void checkQuizInfo(QuizAnalysisData data)%7B%0A        assertThat(%22Bad quiz owner ID%22, data.getOwnerID(), is(completeQuiz.getOwner()));%0A        assertThat(%22Bad quiz ID%22, data.getQuizID(), is(completeQuiz.getQuizId()));%0A    %7D%0A%0A    private void checkQuizStats(QuizAnalysisData data) %7B%0A        assertThat(%22Bad %5C'Average Accuracy Per Participant%5C'%22, data.getStats().get(%22Average Accuracy Per Participant%22), is(%220.3%22
+assertThat(%22Bad quiz owner ID%22, data.getOwnerID(), is(completeQuiz.getOwner()));%0A        assertThat(%22Bad quiz ID%22, data.getQuizID(), is(completeQuiz.getQuizId()
 ));%0A

Simplify accuracy controller test