diff --git a/src/test/java/com/yetanalytics/model/AccountTest.java b/src/test/java/com/yetanalytics/model/AccountTest.java index 845c077..fa54cb3 100644 --- a/src/test/java/com/yetanalytics/model/AccountTest.java +++ b/src/test/java/com/yetanalytics/model/AccountTest.java @@ -1,13 +1,11 @@ package com.yetanalytics.model; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Account; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class AccountTest { @@ -15,8 +13,8 @@ public class AccountTest { private Account account; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); account = new Account(); } @@ -24,13 +22,11 @@ public void initValidator() { public void testValidAccount() { account.setHomePage("http://examplehomepage.com"); account.setName("My Account"); - assertTrue(validator.validate(account).isEmpty()); + ValidationUtils.assertValid(validator, account); } @Test public void testEmptyAccount() { - var violations = validator.validate(account); - assertEquals(2, violations.size()); + ValidationUtils.assertInvalid(validator, account, 2); } - } diff --git a/src/test/java/com/yetanalytics/model/ActivityDefinitionTest.java b/src/test/java/com/yetanalytics/model/ActivityDefinitionTest.java index 164e194..19ab497 100644 --- a/src/test/java/com/yetanalytics/model/ActivityDefinitionTest.java +++ b/src/test/java/com/yetanalytics/model/ActivityDefinitionTest.java @@ -4,18 +4,17 @@ import java.util.HashMap; import java.util.List; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.ActivityDefinition; import com.yetanalytics.xapi.model.Extensions; import com.yetanalytics.xapi.model.InteractionComponent; import com.yetanalytics.xapi.model.InteractionType; import com.yetanalytics.xapi.model.LangMap; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class ActivityDefinitionTest { @@ -37,14 +36,14 @@ private List makeComp(String id, String descStr) { } @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); definition = new ActivityDefinition(); } @Test public void testEmptyDefinition() { - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); } @Test @@ -68,6 +67,7 @@ public void testDefinition() { definition.setMoreInfo(moreInfo); definition.setExtensions(ext); + ValidationUtils.assertValid(validator, definition); assertTrue(validator.validate(definition).isEmpty()); List choices = makeComp("component", "Interaction Component"); @@ -78,79 +78,73 @@ public void testDefinition() { correctResponsesPattern.add("Response 2"); definition.setCorrectResponsesPattern(correctResponsesPattern); - var violations = validator.validate(definition); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, definition); } @Test public void testChoiceDefinition() { definition.setInteractionType(InteractionType.CHOICE); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); List choices = makeComp("choice", "Choice"); definition.setChoices(choices); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); definition.setInteractionType(InteractionType.TRUE_FALSE); - var violations = validator.validate(definition); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, definition); } @Test public void testSequencingDefinition() { definition.setInteractionType(InteractionType.SEQUENCING); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); List choices = makeComp("choice", "Choice"); definition.setChoices(choices); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); definition.setInteractionType(InteractionType.FILL_IN); - var violations = validator.validate(definition); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, definition); } @Test public void testLikertDefinition() { definition.setInteractionType(InteractionType.LIKERT); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); List scale = makeComp("scale", "Scale"); definition.setScale(scale); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); definition.setInteractionType(InteractionType.LONG_FILL_IN); - var violations = validator.validate(definition); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, definition); } @Test public void testMatchingDefinition() { definition.setInteractionType(InteractionType.MATCHING); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); List source = makeComp("source", "Source"); List target = makeComp("target", "Target"); definition.setSource(source); definition.setTarget(target); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); definition.setInteractionType(InteractionType.NUMERIC); - var violations = validator.validate(definition); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, definition); } @Test public void testPerformanceDefinition() { definition.setInteractionType(InteractionType.PERFORMANCE); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); List steps = makeComp("steps", "Steps"); definition.setSteps(steps); - assertTrue(validator.validate(definition).isEmpty()); + ValidationUtils.assertValid(validator, definition); definition.setInteractionType(InteractionType.OTHER); - var violations = validator.validate(definition); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, definition); } } diff --git a/src/test/java/com/yetanalytics/model/ActivityTest.java b/src/test/java/com/yetanalytics/model/ActivityTest.java index dcf96a3..32c8082 100644 --- a/src/test/java/com/yetanalytics/model/ActivityTest.java +++ b/src/test/java/com/yetanalytics/model/ActivityTest.java @@ -1,14 +1,12 @@ package com.yetanalytics.model; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Activity; import com.yetanalytics.xapi.model.ActivityDefinition; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class ActivityTest { @@ -17,8 +15,8 @@ public class ActivityTest { private Activity activity; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); definition = new ActivityDefinition(); activity = new Activity(); } @@ -26,17 +24,16 @@ public void initValidator() { @Test public void testActivity() { activity.setId("http://example.org/activity"); - assertTrue(validator.validate(activity).isEmpty()); + ValidationUtils.assertValid(validator, activity); // TODO: Enforce that definitions, as JSON objects, can't be empty // For now we allow them though activity.setDefinition(definition); - assertTrue(validator.validate(activity).isEmpty()); + ValidationUtils.assertValid(validator, activity); } @Test public void testEmptyActivity() { - var violations = validator.validate(activity); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, activity); } } diff --git a/src/test/java/com/yetanalytics/model/AgentTest.java b/src/test/java/com/yetanalytics/model/AgentTest.java index eecf35a..17bfd38 100644 --- a/src/test/java/com/yetanalytics/model/AgentTest.java +++ b/src/test/java/com/yetanalytics/model/AgentTest.java @@ -1,14 +1,12 @@ package com.yetanalytics.model; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Account; import com.yetanalytics.xapi.model.Agent; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class AgentTest { @@ -16,27 +14,27 @@ public class AgentTest { private Agent agent; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); agent = new Agent(); } @Test public void testMbox() { agent.setMbox("mailto:foo@example.com"); - assertTrue(validator.validate(agent).isEmpty()); + ValidationUtils.assertValid(validator, agent); } @Test public void testMboxSha1Sum() { agent.setMbox_sha1sum("767e74eab7081c41e0b83630511139d130249666"); - assertTrue(validator.validate(agent).isEmpty()); + ValidationUtils.assertValid(validator, agent); } @Test public void testOpenid() { agent.setOpenid("http://openid.example.com"); - assertTrue(validator.validate(agent).isEmpty()); + ValidationUtils.assertValid(validator, agent); } @Test @@ -46,28 +44,25 @@ public void testAccount() { account.setName("My Account"); agent.setAccount(account); - assertTrue(validator.validate(agent).isEmpty()); + ValidationUtils.assertValid(validator, agent); } @Test public void testInvalidAccount() { Account account = new Account(); agent.setAccount(account); - var violations = validator.validate(agent); - assertEquals(2, violations.size()); + ValidationUtils.assertInvalid(validator, agent, 2); } @Test public void testNoIFI() { - var violations = validator.validate(agent); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, agent); } @Test public void testMultiIFI() { agent.setMbox("mailto:foo@example.com"); agent.setMbox_sha1sum("767e74eab7081c41e0b83630511139d130249666"); - var violations = validator.validate(agent); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, agent); } } diff --git a/src/test/java/com/yetanalytics/model/AttachmentTest.java b/src/test/java/com/yetanalytics/model/AttachmentTest.java index c82f40b..fe71f99 100644 --- a/src/test/java/com/yetanalytics/model/AttachmentTest.java +++ b/src/test/java/com/yetanalytics/model/AttachmentTest.java @@ -2,15 +2,13 @@ import java.util.HashMap; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Attachment; import com.yetanalytics.xapi.model.LangMap; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class AttachmentTest { @@ -18,8 +16,8 @@ public class AttachmentTest { private Attachment attachment; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); attachment = new Attachment(); } @@ -43,11 +41,12 @@ public void testAttachment() { attachment.setLength(length); attachment.setSha2(sha2); attachment.setFileUrl(fileUrl); - assertTrue(validator.validate(attachment).isEmpty()); + + ValidationUtils.assertValid(validator, attachment); } @Test public void testEmptyAttachment() { - assertEquals(5, validator.validate(attachment).size()); + ValidationUtils.assertInvalid(validator, attachment, 5); } } diff --git a/src/test/java/com/yetanalytics/model/GroupTest.java b/src/test/java/com/yetanalytics/model/GroupTest.java index 8ebbce8..691daf4 100644 --- a/src/test/java/com/yetanalytics/model/GroupTest.java +++ b/src/test/java/com/yetanalytics/model/GroupTest.java @@ -3,16 +3,14 @@ import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Account; import com.yetanalytics.xapi.model.Agent; import com.yetanalytics.xapi.model.Group; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class GroupTest { @@ -20,8 +18,8 @@ public class GroupTest { private Group group; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); group = new Group(); } @@ -32,25 +30,25 @@ public void testAnomyousGroup() { memberAgent.setMbox("mailto:mem@example.com"); member.add(memberAgent); group.setMember(member); - assertTrue(validator.validate(group).isEmpty()); + ValidationUtils.assertValid(validator, group); } @Test public void testMbox() { group.setMbox("mailto:foo@example.com"); - assertTrue(validator.validate(group).isEmpty()); + ValidationUtils.assertValid(validator, group); } @Test public void testMboxSha1Sum() { group.setMbox_sha1sum("767e74eab7081c41e0b83630511139d130249666"); - assertTrue(validator.validate(group).isEmpty()); + ValidationUtils.assertValid(validator, group); } @Test public void testOpenid() { group.setOpenid("http://openid.example.com"); - assertTrue(validator.validate(group).isEmpty()); + ValidationUtils.assertValid(validator, group); } @Test @@ -60,29 +58,25 @@ public void testAccount() { account.setName("My Account"); group.setAccount(account); - assertTrue(validator.validate(group).isEmpty()); + ValidationUtils.assertValid(validator, group); } @Test public void testInvalidAccount() { Account account = new Account(); group.setAccount(account); - var violations = validator.validate(group); - assertEquals(2, violations.size()); + ValidationUtils.assertInvalid(validator, group, 2); } @Test public void testNoIFI() { // No member array => identified group - var violations = validator.validate(group); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, group); } @Test public void testMultiIFI() { group.setMbox("mailto:foo@example.com"); group.setMbox_sha1sum("767e74eab7081c41e0b83630511139d130249666"); - var violations = validator.validate(group); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, group); } - } diff --git a/src/test/java/com/yetanalytics/model/InteractionComponentTest.java b/src/test/java/com/yetanalytics/model/InteractionComponentTest.java index 8149fed..4183b5b 100644 --- a/src/test/java/com/yetanalytics/model/InteractionComponentTest.java +++ b/src/test/java/com/yetanalytics/model/InteractionComponentTest.java @@ -2,15 +2,13 @@ import java.util.HashMap; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.InteractionComponent; import com.yetanalytics.xapi.model.LangMap; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class InteractionComponentTest { @@ -18,8 +16,8 @@ public class InteractionComponentTest { private InteractionComponent interactionComponent; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); interactionComponent = new InteractionComponent(); } @@ -29,11 +27,11 @@ public void testInteractionComponent() { desc.put("en-US", "Foo"); interactionComponent.setId("foo"); interactionComponent.setDescription(desc); - assertTrue(validator.validate(interactionComponent).isEmpty()); + ValidationUtils.assertValid(validator, interactionComponent); } @Test public void testEmpytInteractionComponent() { - assertEquals(1, validator.validate(interactionComponent).size()); + ValidationUtils.assertInvalid(validator, interactionComponent); } } diff --git a/src/test/java/com/yetanalytics/model/ResultTest.java b/src/test/java/com/yetanalytics/model/ResultTest.java index c5f0c94..ea2dbd8 100644 --- a/src/test/java/com/yetanalytics/model/ResultTest.java +++ b/src/test/java/com/yetanalytics/model/ResultTest.java @@ -2,15 +2,13 @@ import java.math.BigDecimal; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Result; import com.yetanalytics.xapi.model.Score; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class ResultTest { @@ -19,8 +17,8 @@ public class ResultTest { private Score score; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); score = new Score(); result = new Result(); } @@ -35,14 +33,13 @@ public void testResult() { // TODO: setExtensions // TODO: setDuration - assertTrue(validator.validate(result).isEmpty()); + ValidationUtils.assertValid(validator, result); } @Test public void testInvalidScore() { score.setScaled(new BigDecimal(3.0)); result.setScore(score); - assertEquals(1, validator.validate(result).size()); + ValidationUtils.assertInvalid(validator, result); } - } diff --git a/src/test/java/com/yetanalytics/model/ScoreTest.java b/src/test/java/com/yetanalytics/model/ScoreTest.java index 66c85f1..a6e856f 100644 --- a/src/test/java/com/yetanalytics/model/ScoreTest.java +++ b/src/test/java/com/yetanalytics/model/ScoreTest.java @@ -2,14 +2,12 @@ import java.math.BigDecimal; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Score; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class ScoreTest { @@ -17,57 +15,52 @@ public class ScoreTest { private Score score; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); score = new Score(); } @Test public void testScore() { - assertTrue(validator.validate(score).isEmpty()); + ValidationUtils.assertValid(validator, score); // TODO: Make invalid score.setMin(new BigDecimal(0)); score.setMax(new BigDecimal(100)); score.setRaw(new BigDecimal(50)); score.setScaled(new BigDecimal(0)); - assertTrue(validator.validate(score).isEmpty()); + ValidationUtils.assertValid(validator, score); } @Test public void testScaledTooSmall() { score.setScaled(new BigDecimal(-2.0)); - var violations = validator.validate(score); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, score); } @Test public void testScaledTooBig() { score.setScaled(new BigDecimal(2.0)); - var violations = validator.validate(score); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, score); } @Test public void testMinBiggerThanMax() { score.setMax(new BigDecimal(0.4)); score.setMin(new BigDecimal(0.6)); - var violations = validator.validate(score); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, score); } @Test public void testRawBiggerThanMax() { score.setMax(new BigDecimal(0.4)); score.setRaw(new BigDecimal(0.6)); - var violations = validator.validate(score); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, score); } @Test public void testMinBiggerThanRaw() { score.setRaw(new BigDecimal(0.4)); score.setMin(new BigDecimal(0.6)); - var violations = validator.validate(score); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, score); } } diff --git a/src/test/java/com/yetanalytics/model/StatementRefTest.java b/src/test/java/com/yetanalytics/model/StatementRefTest.java index 4584ae7..c20fd05 100644 --- a/src/test/java/com/yetanalytics/model/StatementRefTest.java +++ b/src/test/java/com/yetanalytics/model/StatementRefTest.java @@ -1,16 +1,13 @@ package com.yetanalytics.model; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - import java.util.UUID; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.StatementRef; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class StatementRefTest { @@ -18,8 +15,8 @@ public class StatementRefTest { private StatementRef statementRef; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); statementRef = new StatementRef(); } @@ -27,11 +24,11 @@ public void initValidator() { public void testStatementRef() { String id = "00000000-4000-8000-0000-000000000000"; statementRef.setId(UUID.fromString(id)); - assertTrue(validator.validate(statementRef).isEmpty()); + ValidationUtils.assertValid(validator, statementRef); } @Test public void testEmptyStatementRef() { - assertEquals(1, validator.validate(statementRef).size()); + ValidationUtils.assertInvalid(validator, statementRef); } } diff --git a/src/test/java/com/yetanalytics/model/StatementResultTest.java b/src/test/java/com/yetanalytics/model/StatementResultTest.java index b9a9d0f..f39a3d6 100644 --- a/src/test/java/com/yetanalytics/model/StatementResultTest.java +++ b/src/test/java/com/yetanalytics/model/StatementResultTest.java @@ -3,18 +3,16 @@ import java.util.ArrayList; import java.util.List; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Activity; import com.yetanalytics.xapi.model.Agent; import com.yetanalytics.xapi.model.Statement; import com.yetanalytics.xapi.model.StatementResult; import com.yetanalytics.xapi.model.Verb; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class StatementResultTest { @@ -22,8 +20,8 @@ public class StatementResultTest { private StatementResult statementResult; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); statementResult = new StatementResult(); } @@ -47,11 +45,11 @@ public void testStatementResult() { statements.add(statement); statementResult.setStatements(statements); - assertTrue(validator.validate(statements).isEmpty()); + ValidationUtils.assertValid(validator, statementResult); } @Test public void testEmptyStatementResult() { - assertEquals(1, validator.validate(statementResult).size()); + ValidationUtils.assertInvalid(validator, statementResult); } } diff --git a/src/test/java/com/yetanalytics/model/StatementTest.java b/src/test/java/com/yetanalytics/model/StatementTest.java index 780d101..33445e2 100644 --- a/src/test/java/com/yetanalytics/model/StatementTest.java +++ b/src/test/java/com/yetanalytics/model/StatementTest.java @@ -4,11 +4,11 @@ import java.util.List; import java.util.UUID; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.Account; import com.yetanalytics.xapi.model.Activity; import com.yetanalytics.xapi.model.Agent; @@ -18,7 +18,6 @@ import com.yetanalytics.xapi.model.StatementRef; import com.yetanalytics.xapi.model.Verb; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class StatementTest { @@ -33,8 +32,8 @@ private StatementRef genericStatementRef() { } @Before - public void initStatement() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); statement = new Statement(); // Valid statement by default @@ -54,7 +53,7 @@ public void initStatement() { @Test public void testStatement() { - assertTrue(validator.validate(statement).isEmpty()); + ValidationUtils.assertValid(validator, statement); } @Test @@ -62,7 +61,7 @@ public void testEmptyStatement() { statement.setActor(null); statement.setVerb(null); statement.setObject(null); - assertEquals(3, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement, 3); } @Test @@ -71,12 +70,12 @@ public void testVoidingStatement() { voidingVerb.setId(Verb.VOIDING_VERB_IRI); statement.setVerb(voidingVerb); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); StatementRef statementRef = genericStatementRef(); statement.setObject(statementRef); - assertTrue(validator.validate(statement).isEmpty()); + ValidationUtils.assertValid(validator, statement); } @Test @@ -84,12 +83,13 @@ public void testValidContextRevision() { Context context = new Context(); context.setRevision("myRevision"); statement.setContext(context); + ValidationUtils.assertValid(validator, statement); assertTrue(validator.validate(statement).isEmpty()); StatementRef statementRef = genericStatementRef(); statement.setObject(statementRef); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); } @Test @@ -102,7 +102,7 @@ public void testValidContextPlatform() { StatementRef statementRef = genericStatementRef(); statement.setObject(statementRef); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); } @Test @@ -123,7 +123,7 @@ public void testValidAuthority() { groupAuthMember.add(agentAuthority); groupAuthority.setMember(groupAuthMember); statement.setAuthority(groupAuthority); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); // Need to add second member Agent nonConsumer = new Agent(); @@ -133,12 +133,12 @@ public void testValidAuthority() { // At least one member needs to have an Account groupAuthMember.set(0, nonConsumer); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); // Cannot have three (or more) members groupAuthMember.set(0, agentAuthority); groupAuthMember.add(nonConsumer); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); } @Test @@ -159,20 +159,20 @@ public void testValidSubStatement() { UUID id = UUID.fromString("00000000-4000-8000-0000-000000000000"); subStatement.setId(id); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); // TODO: test Stored presence String version = "1.0.3"; subStatement.setId(null); subStatement.setVersion(version); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); Agent authority = new Agent(); authority.setMbox("mailto:myauthority@example.com"); subStatement.setVersion(null); subStatement.setAuthority(authority); - assertEquals(1, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement); Statement subSubStatement = new Statement(); Agent actor2 = new Agent(); @@ -187,6 +187,6 @@ public void testValidSubStatement() { subStatement.setAuthority(null); subStatement.setObject(subStatement); // TODO: Dig deeper why this is 2 errors and not 1 - assertEquals(2, validator.validate(statement).size()); + ValidationUtils.assertInvalid(validator, statement, 2); } } diff --git a/src/test/java/com/yetanalytics/model/VerbTest.java b/src/test/java/com/yetanalytics/model/VerbTest.java index 7bac522..d8920ca 100644 --- a/src/test/java/com/yetanalytics/model/VerbTest.java +++ b/src/test/java/com/yetanalytics/model/VerbTest.java @@ -2,15 +2,13 @@ import java.util.HashMap; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.yetanalytics.util.ValidationUtils; import com.yetanalytics.xapi.model.LangMap; import com.yetanalytics.xapi.model.Verb; -import jakarta.validation.Validation; import jakarta.validation.Validator; public class VerbTest { @@ -18,8 +16,8 @@ public class VerbTest { private Verb verb; @Before - public void initValidator() { - validator = Validation.buildDefaultValidatorFactory().getValidator(); + public void init() { + validator = ValidationUtils.getValidator(); verb = new Verb(); } @@ -30,12 +28,11 @@ public void testVerb() { verb.setId("http://example.com/verb"); verb.setDisplay(display); - assertTrue(validator.validate(verb).isEmpty()); + ValidationUtils.assertValid(validator, verb); } @Test public void testEmptyVerb() { - var violations = validator.validate(verb); - assertEquals(1, violations.size()); + ValidationUtils.assertInvalid(validator, verb); } } diff --git a/src/test/java/com/yetanalytics/util/ValidationUtils.java b/src/test/java/com/yetanalytics/util/ValidationUtils.java new file mode 100644 index 0000000..e3f7b5f --- /dev/null +++ b/src/test/java/com/yetanalytics/util/ValidationUtils.java @@ -0,0 +1,26 @@ +package com.yetanalytics.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import jakarta.validation.Validation; +import jakarta.validation.Validator; + +public class ValidationUtils { + + public static Validator getValidator() { + return Validation.buildDefaultValidatorFactory().getValidator(); + } + + public static void assertValid(Validator validator, Object object) { + assertTrue(validator.validate(object).isEmpty()); + } + + public static void assertInvalid(Validator validator, Object object) { + assertInvalid(validator, object, 1); + } + + public static void assertInvalid(Validator validator, Object object, int numErrors) { + assertEquals(numErrors, validator.validate(object).size()); + } +}