Skip to content

Commit

Permalink
Add ValidationUtils class with custom asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinqian00 committed Dec 9, 2024
1 parent 9074e73 commit 91d5ccd
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 156 deletions.
14 changes: 5 additions & 9 deletions src/test/java/com/yetanalytics/model/AccountTest.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
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 {
private Validator validator;
private Account account;

@Before
public void initValidator() {
validator = Validation.buildDefaultValidatorFactory().getValidator();
public void init() {
validator = ValidationUtils.getValidator();
account = new Account();
}

@Test
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);
}

}
48 changes: 21 additions & 27 deletions src/test/java/com/yetanalytics/model/ActivityDefinitionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -37,14 +36,14 @@ private List<InteractionComponent> 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
Expand All @@ -68,6 +67,7 @@ public void testDefinition() {
definition.setMoreInfo(moreInfo);
definition.setExtensions(ext);

ValidationUtils.assertValid(validator, definition);
assertTrue(validator.validate(definition).isEmpty());

List<InteractionComponent> choices = makeComp("component", "Interaction Component");
Expand All @@ -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<InteractionComponent> 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<InteractionComponent> 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<InteractionComponent> 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<InteractionComponent> source = makeComp("source", "Source");
List<InteractionComponent> 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<InteractionComponent> 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);
}
}
15 changes: 6 additions & 9 deletions src/test/java/com/yetanalytics/model/ActivityTest.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -17,26 +15,25 @@ 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();
}

@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);
}
}
25 changes: 10 additions & 15 deletions src/test/java/com/yetanalytics/model/AgentTest.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,40 @@
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 {
private Validator validator;
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:[email protected]");
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
Expand All @@ -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:[email protected]");
agent.setMbox_sha1sum("767e74eab7081c41e0b83630511139d130249666");
var violations = validator.validate(agent);
assertEquals(1, violations.size());
ValidationUtils.assertInvalid(validator, agent);
}
}
13 changes: 6 additions & 7 deletions src/test/java/com/yetanalytics/model/AttachmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@

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 {
private Validator validator;
private Attachment attachment;

@Before
public void initValidator() {
validator = Validation.buildDefaultValidatorFactory().getValidator();
public void init() {
validator = ValidationUtils.getValidator();
attachment = new Attachment();
}

Expand All @@ -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);
}
}
Loading

0 comments on commit 91d5ccd

Please sign in to comment.