Completed
Push — dev ( 39c8fc...3a01d3 )
by Konstantin
11:12 queued 05:12
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.QuestionModelInterface;
5
6
/**
7
 * @author SakhPrace
8
 */
9
public class QuiestionsValidator {
10
11
    private AnswersValidator answersValidator = new AnswersValidator();
0 ignored issues
show
Unused Code introduced by
Consider removing the unused private field answersValidator.
Loading history...
12
13
    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...
14
        final int rightAnswersCount = 0;
15
16
        //for (AnswerModelInterface answerModel: questionModel.getAnswers()) {
17
        //    this.answersValidator.validateAnswer(answerModel);
18
        //    if (answerModel.getRight()) {
19
        //        ++rightAnswersCount;
20
        //    }
21
        //}
22
        switch (questionModel.getQuestionType().getId()) {
23
            case 1:
24
                if (rightAnswersCount == 0) {
0 ignored issues
show
Code Smell introduced by
Remove this expression which always evaluates to "true"
Loading history...
25
                    throw new BadRequestException("Right Answer must be exist");
26
                }
27
                if (rightAnswersCount > 1) {
28
                    throw new BadRequestException("This Question can have only one right Answer");
29
                }
30
                break;
31
32
            case 2:
33
                if (rightAnswersCount == 0) {
0 ignored issues
show
Code Smell introduced by
Remove this expression which always evaluates to "true"
Loading history...
34
                    throw new BadRequestException("Right Answers must be exist");
35
                }
36
                if (rightAnswersCount == 1) {
37
                    throw new BadRequestException("This Question can't have only one right Answer");
38
                }
39
                if (rightAnswersCount == questionModel.getAnswers().size()) {
40
                    throw new BadRequestException("This Question can't have all right Answers");
41
                }
42
                break;
43
44
            case 3:
45
                break;
46
47
            /**
48
             * TODO
49
             * Add check for Numeric And Text Answers
50
             */
51
            default: break;
52
        }
53
    }
54
}
55