Completed
Pull Request — dev (#396)
by
unknown
05:33
created

validateQuestion

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 39
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();
0 ignored issues
show
Unused Code introduced by
Consider removing the unused private field answersValidator.
Loading history...
13
14
    public void validateQuestion(QuestionModelInterface questionModel) throws Exception {
0 ignored issues
show
Best Practice introduced by
Dedicated exceptions should be preferred over throwing the generic Exception.
Loading history...
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) {
0 ignored issues
show
Code Smell introduced by
Remove this expression which always evaluates to "true"
Loading history...
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) {
0 ignored issues
show
Code Smell introduced by
Remove this expression which always evaluates to "true"
Loading history...
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
            case 3:
46
                break;
47
48
            /**
49
             * TODO
50
             * Add check for Numeric And Text Answers
51
             */
52
            default: break;
53
        }
54
    }
55
}
56