Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scores validator #105

Merged
merged 2 commits into from
Mar 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion xapi-model/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<optional>true</optional>
</dependency>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
2 changes: 2 additions & 0 deletions xapi-model/src/main/java/dev/learning/xapi/model/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import dev.learning.xapi.model.validation.constraints.HasScheme;
import dev.learning.xapi.model.validation.constraints.VaildScore;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import java.net.URI;
Expand All @@ -32,6 +33,7 @@ public class Result {
* The score of the Agent in relation to the success or quality of the experience.
*/
@Valid
@VaildScore
private Score score;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2016-2019 Berry Cloud Ltd. All rights reserved.
*/

package dev.learning.xapi.model.validation.constraints;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import jakarta.validation.Constraint;
import jakarta.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

/**
* The annotated element must be a valid score.
*
* @author Thomas Turrell-Croft
*
* @see <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#Score">Score</a>
*/
@Documented
@Constraint(validatedBy = {})
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface VaildScore {

/**
* Error Message.
*/
String message() default "must be a valid score";

/**
* Groups.
*/
Class<?>[] groups() default {};

/**
* Payload.
*/
Class<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2016-2023 Berry Cloud Ltd. All rights reserved.
*/

package dev.learning.xapi.model.validation.internal.validators;

import dev.learning.xapi.model.Score;
import dev.learning.xapi.model.validation.constraints.VaildScore;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

/**
* The raw score must be greater or equal to min and less or equal to max.
*
* @author István Rátkai (Selindek)
*/
public class ScoreValidator implements ConstraintValidator<VaildScore, Score> {

@Override
public boolean isValid(Score value, ConstraintValidatorContext context) {

if (value == null) {
return true;
}

return (value.getMax() == null || value.getMax() >= value.getRaw())
&& (value.getMin() == null || value.getMin() <= value.getRaw());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ dev.learning.xapi.model.validation.internal.validators.StatementRevisionValidato
dev.learning.xapi.model.validation.internal.validators.StatementPlatformValidator
dev.learning.xapi.model.validation.internal.validators.StatementVerbValidator
dev.learning.xapi.model.validation.internal.validators.StatementsValidator
dev.learning.xapi.model.validation.internal.validators.ScoreValidator
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @author István Rátkai (Selindek)
*/
@DisplayName("ScaledSoreValidator tests")
@DisplayName("ScaledScoreValidator tests")
class ScaledScoreValidatorTests {

private static final ScaledScoreValidator validator = new ScaledScoreValidator();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2016-2023 Berry Cloud Ltd. All rights reserved.
*/

package dev.learning.xapi.model.validation.internal.validators;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import dev.learning.xapi.model.Score;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/**
* ScoreValidator Tests.
*
* @author Thomas Turrell-Croft
*/
@DisplayName("ScoreValidator tests")
class ScoreValidatorTests {

private static final ScoreValidator validator = new ScoreValidator();

@Test
void whenValueIsNullThenResultIsTrue() {

// When Value Is Null
final var result = validator.isValid(null, null);

// Then Result Is True
assertTrue(result);
}

@ParameterizedTest
@CsvSource({"0,100,50", "0,100,0", "0,100,100", "-100,0,-50"})
void whenCallingIsValidWithValidScoreThenResultIsTrue(float min, float max, float raw) {

final var s = Score.builder().min(min).max(max).raw(raw).build();

// When Calling Is Valid With Valid Score
final var result = validator.isValid(s, null);

// Then Result Is True
assertTrue(result);
}

@ParameterizedTest
@CsvSource({"0,100,101", "0,100,-1"})
void whenCallingIsValidWithInvalidScoreThenResultIsFalse(float min, float max, float raw) {

final var s = Score.builder().min(min).max(max).raw(raw).build();

// When Calling Is Valid With Invalid Score
final var result = validator.isValid(s, null);

// Then Result Is False
assertFalse(result);
}

@Test
void whenCallingIsValidWithAScoreWithMinNullThenResultIsTrue() {

final var s = Score.builder().min(null).max(100F).raw(50F).build();

// When Calling Is Valid With A Score With Min Null
final var result = validator.isValid(s, null);

// Then Result Is True
assertTrue(result);
}

@Test
void whenCallingIsValidWithAScoreWithMaxNullThenResultIsTrue() {

final var s = Score.builder().min(0F).max(null).raw(50F).build();

// When Calling Is Valid With A Score With Max Null
final var result = validator.isValid(s, null);

// Then Result Is True
assertTrue(result);
}



}