package se.kits.gakusei.controller;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import se.kits.gakusei.content.model.Nugget;
import se.kits.gakusei.content.repository.LessonRepository;
import se.kits.gakusei.test_tools.TestTools;
import se.kits.gakusei.util.QuestionHandler;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

@RunWith(MockitoJUnitRunner.class)
public class QuestionControllerTest {

    @InjectMocks
    private QuestionController questionController;

    @Mock
    private QuestionHandler questionHandler;

    @Mock
    private LessonRepository lessonRepository;

   @Value("${gakusei.questions-quantity}")
    private int quantity;

    private String questionType;
    private String answerType;
    private String lessonName;
    private String userName;
    private List<Nugget> nuggets;

    @Before
    public void setUp() throws Exception {
        questionController = new QuestionController(lessonRepository, questionHandler);
        MockitoAnnotations.initMocks(this);
        questionType = "reading";
        answerType = "swedish";
        lessonName = "Verbs";
        userName = "testUser";
        nuggets = TestTools.generateNuggets();
    }



    @Test
    public void testGetQuestionsFromLessonOK() throws Exception {
        List<HashMap<String, Object>> questionList = Collections.singletonList(TestTools.generateQuestion());

        Mockito.when(lessonRepository.findNuggetsBySuccessrate(userName, lessonName)).thenReturn(nuggets);
        Mockito.when(lessonRepository.findUnansweredNuggets(userName, lessonName)).thenReturn(nuggets);
        Mockito.when(questionHandler.chooseNuggets(nuggets, nuggets, nuggets, quantity)).thenReturn(nuggets);
        Mockito.when(questionHandler.createQuestions(nuggets, questionType, answerType))
                .thenReturn(questionList);

        ResponseEntity<List<HashMap<String, Object>>> re = questionController.getQuestionsFromLesson(lessonName,
                "vocabulary", questionType, answerType, userName);

        assertEquals(questionList, re.getBody());
        assertEquals(200, re.getStatusCodeValue());
    }

    @Test
    public void testGetQuestionsFromLessonInternalServerError() throws Exception {
        List<HashMap<String, Object>> emptyList = Collections.EMPTY_LIST;

        Mockito.when(lessonRepository.findNuggetsBySuccessrate(userName, lessonName)).thenReturn(nuggets);
        Mockito.when(lessonRepository.findUnansweredNuggets(userName, lessonName)).thenReturn(nuggets);
        Mockito.when(questionHandler.chooseNuggets(nuggets, nuggets, nuggets, quantity)).thenReturn(nuggets);
        Mockito.when(questionHandler.createQuestions(nuggets, questionType, answerType))
                .thenReturn(emptyList);

        ResponseEntity<List<HashMap<String, Object>>> re = questionController.getQuestionsFromLesson(lessonName,
                "vocabulary", questionType, answerType, userName);

        assertNull(re.getBody());
        assertEquals(500, re.getStatusCodeValue());
    }
}

@@ -423,16 +423,61 @@
 Entity;%0A
+import se.kits.gakusei.content.model.Lesson;%0A
 import s
@@ -1003,16 +1003,17 @@
 ry;%0A%0A   
+ 
 @Value(%22
@@ -1228,16 +1228,99 @@
 nuggets;
+%0A    private Lesson lesson;%0A    private List%3CHashMap%3CString, Object%3E%3E questionList;
 %0A%0A    @B
@@ -1682,199 +1682,271 @@
     
-%7D%0A%0A%0A%0A    @Test%0A    public void testGetQuestionsFromLessonOK() throws Exception %7B%0A        List%3CHashMap%3CString, Object%3E%3E questionList = Collections.singletonList(TestTools.generateQuestion());%0A
+    lesson = new Lesson();%0A        lesson.setName(lessonName);%0A        lesson.setNuggets(nuggets);%0A        questionList = Collections.singletonList(TestTools.generateQuestion());%0A    %7D%0A%0A%0A%0A    @Test%0A    public void testGetQuestionsFromLessonOK() throws Exception %7B
 %0A   
@@ -2145,32 +2145,114 @@
 eturn(nuggets);%0A
+        Mockito.when(lessonRepository.findByName(lessonName)).thenReturn(lesson);%0A
         Mockito.
@@ -2873,43 +2873,16 @@
     
-List%3CHashMap%3CString, Object%3E%3E empty
+question
 List
@@ -3112,32 +3112,114 @@
 eturn(nuggets);%0A
+        Mockito.when(lessonRepository.findByName(lessonName)).thenReturn(lesson);%0A
         Mockito.
@@ -3433,13 +3433,16 @@
 urn(
-empty
+question
 List

update question controller test