Passed
Pull Request — dev (#396)
by
unknown
04:37
created

validateQuestion

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 40
1
package easytests.api.v1.validators;
2
3
import easytests.api.v1.exceptions.BadRequestException;
4
import easytests.core.models.AnswerModelInterface;
5
import easytests.core.models.QuestionModelInterface;
6
7
/**
8
 * @author SakhPrace
9
 */
10
public class QuiestionsValidator {
11
12
    private AnswersValidator answersValidator = new AnswersValidator();
13
14
    public void validateQuestion(QuestionModelInterface questionModel) throws Exception {
15
        int rightAnswersCount = 0;
16
17
        for (AnswerModelInterface answerModel: questionModel.getAnswers()) {
18
            this.answersValidator.validateAnswer(answerModel);
19
            if (answerModel.getRight()) {
20
                ++rightAnswersCount;
21
            }
22
        }
23
        switch (questionModel.getQuestionType().getId()) {
24
            case 1: {
25
                if (rightAnswersCount == 0) {
26
                    throw new BadRequestException("Right Answer must be exist");
27
                }
28
                if (rightAnswersCount > 1) {
29
                    throw new BadRequestException("This Question can have only one right Answer");
30
                }
31
                break;
32
            }
33
            case 2: {
34
                if (rightAnswersCount == 0) {
35
                    throw new BadRequestException("Right Answers must be exist");
36
                }
37
                if (rightAnswersCount == 1) {
38
                    throw new BadRequestException("This Question can't have only one right Answer");
39
                }
40
                if (rightAnswersCount == questionModel.getAnswers().size()) {
41
                    throw new BadRequestException("This Question can't have all right Answers");
42
                }
43
                break;
44
            }
45
46
            case 3: {
47
                break;
48
            }
49
            /**
50
             * TODO
51
             * Add check for Numeric And Text Answers
52
             */
53
            default: break;
54
        }
55
    }
56
}
57