Skip to content

Commit

Permalink
Add isEmpty function to validate that JSON objects are not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinqian00 committed Dec 9, 2024
1 parent 91d5ccd commit 131c380
Show file tree
Hide file tree
Showing 33 changed files with 227 additions and 35 deletions.
8 changes: 8 additions & 0 deletions src/main/java/com/yetanalytics/xapi/model/AbstractActor.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,12 @@ protected int countIFIs() {
}

public abstract boolean isValidAuthority();

@Override
public boolean isEmpty() {
return (
mbox == null && mbox_sha1sum == null &&
openid == null && account == null
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Abstract Class for serialization and deserialization of xAPI Objects
*/
@JsonDeserialize(using = AbstractObjectDeserializer.class)
public class AbstractObject {
public abstract class AbstractObject implements JSONObject {

private ObjectType objectType;

Expand All @@ -18,4 +18,6 @@ public void setObjectType(ObjectType objectType) {
this.objectType = objectType;
}

@Override
public abstract boolean isEmpty();
}
9 changes: 8 additions & 1 deletion src/main/java/com/yetanalytics/xapi/model/Account.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.NotNull;

/**
* Class representation of the Account Component of the
* <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#2424-account-object">9274.1.1 xAPI Specification</a>.
*/
@JsonInclude(Include.NON_NULL)
public class Account {
public class Account implements JSONObject {

@NotNull
private String homePage;
Expand All @@ -30,4 +31,10 @@ public String getName() {
public void setName(String name) {
this.name = name;
}

@Override
@AssertFalse
public boolean isEmpty() {
return homePage == null && name == null;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/yetanalytics/xapi/model/Activity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.NotNull;

/**
Expand Down Expand Up @@ -33,4 +34,10 @@ public ActivityDefinition getDefinition() {
public void setDefinition(ActivityDefinition definition) {
this.definition = definition;
}

@Override
@AssertFalse
public boolean isEmpty() {
return id == null && definition == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.AssertTrue;

/**
* Class representation of the Activity Definition Component of the
* <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#activity-definition">9274.1.1 xAPI Specification</a>.
*/
@JsonInclude(Include.NON_NULL)
public class ActivityDefinition {
public class ActivityDefinition implements JSONObject {

private LangMap name;
private LangMap description;
Expand Down Expand Up @@ -162,6 +163,16 @@ private boolean isNoInteraction() {
);
}

@Override
@AssertFalse
public boolean isEmpty() {
return (
name == null && description == null &&
type == null && moreInfo == null && extensions == null &&
interactionType == null && isNoInteraction()
);
}

@AssertTrue
public boolean isValidInteractionActivity() {
if (interactionType == null) {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/yetanalytics/xapi/model/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.AssertTrue;

/**
Expand All @@ -17,6 +18,12 @@ public class Agent extends AbstractActor {

// Validation

@Override
@AssertFalse
public boolean isEmpty() {
return super.isEmpty();
}

/**
* Assertion that the Agent has only 1 Inverse Functional Identifier (IFI).
* @return true if the Agent has exactly 1 IFI, false otherwise
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/yetanalytics/xapi/model/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.NotNull;

/**
* Class representation of the Attachment Component of the
* <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#2411-attachments">9274.1.1 xAPI Specification</a>.
*/
@JsonInclude(Include.NON_NULL)
public class Attachment {
public class Attachment implements JSONObject {

@NotNull
private String usageType;
Expand Down Expand Up @@ -68,5 +69,17 @@ public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}


@Override
@AssertFalse
public boolean isEmpty() {
return (
usageType == null &&
display == null &&
description == null &&
contentType == null &&
length == null &&
sha2 == null &&
fileUrl == null
);
}
}
20 changes: 19 additions & 1 deletion src/main/java/com/yetanalytics/xapi/model/Context.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.yetanalytics.xapi.model;

import java.util.UUID;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import jakarta.validation.constraints.AssertFalse;

/**
* Class representation of the Context Component of the
* <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#246-context">9274.1.1 xAPI Specification</a>.
*/
@JsonInclude(Include.NON_NULL)
public class Context {
public class Context implements JSONObject {

private UUID registration;
private AbstractActor instructor;
Expand Down Expand Up @@ -76,4 +79,19 @@ public void setExtensions(Extensions extensions) {
this.extensions = extensions;
}

@Override
@AssertFalse
public boolean isEmpty() {
return (
registration == null &&
instructor == null &&
team == null &&
contextActivities == null &&
revision == null &&
platform == null &&
language == null &&
statement == null &&
extensions == null
);
}
}
13 changes: 12 additions & 1 deletion src/main/java/com/yetanalytics/xapi/model/ContextActivities.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.yetanalytics.xapi.model.deserializers.ContextActivityListDeserializer;

import jakarta.validation.constraints.AssertFalse;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

Expand All @@ -12,7 +15,7 @@
* <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#2462-contextactivities-property">9274.1.1 xAPI Specification</a>.
*/
@JsonInclude(Include.NON_NULL)
public class ContextActivities {
public class ContextActivities implements JSONObject {

@JsonDeserialize(using = ContextActivityListDeserializer.class)
private List<Activity> parent;
Expand Down Expand Up @@ -48,4 +51,12 @@ public void setOther(List<Activity> other) {
this.other = other;
}

@Override
@AssertFalse
public boolean isEmpty() {
return (
parent == null && grouping == null &&
category == null && other == null
);
}
}
10 changes: 9 additions & 1 deletion src/main/java/com/yetanalytics/xapi/model/Extensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.yetanalytics.xapi.model.serializers.ExtensionSerializer;
import com.yetanalytics.xapi.util.Mapper;

import jakarta.validation.constraints.AssertFalse;

/**
* A wrapper object for using <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#41-extensions">xAPI Extensions</a>.
*
Expand All @@ -22,7 +24,7 @@
*/
@JsonDeserialize(using = ExtensionDeserializer.class)
@JsonSerialize(using = ExtensionSerializer.class)
public class Extensions {
public class Extensions implements JSONObject {

private Map<String,Object> extMap = new HashMap<>();

Expand Down Expand Up @@ -96,4 +98,10 @@ public Set<String> getKeys() {
public Map<String, Object> getMap() {
return extMap;
}

@Override
@AssertFalse
public boolean isEmpty() {
return extMap.isEmpty();
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/yetanalytics/xapi/model/Group.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.AssertTrue;

/**
Expand Down Expand Up @@ -48,4 +49,11 @@ public boolean isValidAuthority() {
)
);
}

@Override
@AssertFalse
public boolean isEmpty() {
// zero-length member arrays still count as non-empty
return super.isEmpty() && member == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import jakarta.validation.constraints.AssertFalse;
import jakarta.validation.constraints.NotNull;

/**
* Class representation of the Interaction Component of the
* <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#interaction-components">9274.1.1 xAPI Specification</a>.
*/
@JsonInclude(Include.NON_NULL)
public class InteractionComponent {
public class InteractionComponent implements JSONObject {

@NotNull
private String id;
Expand All @@ -32,5 +33,9 @@ public void setDescription(LangMap description) {
this.description = description;
}


@Override
@AssertFalse
public boolean isEmpty() {
return id == null && description == null;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/yetanalytics/xapi/model/JSONObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.yetanalytics.xapi.model;

public interface JSONObject {
public boolean isEmpty();
}
10 changes: 9 additions & 1 deletion src/main/java/com/yetanalytics/xapi/model/LangMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.yetanalytics.xapi.model.deserializers.LangMapDeserializer;
import com.yetanalytics.xapi.model.serializers.LangMapSerializer;

import jakarta.validation.constraints.AssertFalse;

/**
* Java wrapper object for the
* <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#42-language-maps">xAPI Language Map object</a>.
Expand All @@ -18,7 +20,7 @@
*/
@JsonDeserialize(using = LangMapDeserializer.class)
@JsonSerialize(using = LangMapSerializer.class)
public class LangMap {
public class LangMap implements JSONObject {

private HashMap<String,String> languageHashMap = new HashMap<>();

Expand Down Expand Up @@ -71,4 +73,10 @@ public Set<String> getLanguageCodes() {
public Map<String, String> getMap() {
return languageHashMap;
}

@Override
@AssertFalse
public boolean isEmpty() {
return languageHashMap.isEmpty();
}
}
16 changes: 15 additions & 1 deletion src/main/java/com/yetanalytics/xapi/model/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import jakarta.validation.Valid;
import jakarta.validation.constraints.AssertFalse;

/**
* Class representation of the Result component of the
* <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#245-result">9274.1.1 xAPI Specification</a>.
*/
@JsonInclude(Include.NON_NULL)
public class Result {
public class Result implements JSONObject {

@Valid
private Score score;
Expand Down Expand Up @@ -56,4 +57,17 @@ public Extensions getExtensions() {
public void setExtensions(Extensions extensions) {
this.extensions = extensions;
}

@Override
@AssertFalse
public boolean isEmpty() {
return (
score == null &&
success == null &&
completion == null &&
response == null &&
duration == null &&
extensions == null
);
}
}
Loading

0 comments on commit 131c380

Please sign in to comment.