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

MIME Type and Version Strings #7

Open
wants to merge 9 commits into
base: lang-tags
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@
<artifactId>json-path</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.semver4j</groupId>
<artifactId>semver4j</artifactId>
<version>5.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public abstract class AbstractActor extends AbstractObject {

//IFI
private URI mbox;
// TODO: Validate that mbox_sha1sum is a SHA1, 40-char hex string
private String mbox_sha1sum;
private URI openid;
private Account account;
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/com/yetanalytics/xapi/model/Attachment.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.yetanalytics.xapi.model.deserializers.MimeTypeDeserializer;

import jakarta.activation.MimeType;

/**
* Class representation of the Attachment Component of the
Expand All @@ -15,8 +21,11 @@ public class Attachment {
private URI usageType;
private LangMap display;
private LangMap description;
private String contentType;
@JsonDeserialize(using = MimeTypeDeserializer.class)
@JsonSerialize(using = ToStringSerializer.class)
private MimeType contentType;
private Integer length;
// TODO: Validate that sha2 is a SHA256, 64-char hex string
private String sha2;
private URI fileUrl;

Expand All @@ -38,10 +47,10 @@ public LangMap getDescription() {
public void setDescription(LangMap description) {
this.description = description;
}
public String getContentType() {
public MimeType getContentType() {
return contentType;
}
public void setContentType(String contentType) {
public void setContentType(MimeType contentType) {
this.contentType = contentType;
}
public Integer getLength() {
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/yetanalytics/xapi/model/Statement.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.yetanalytics.xapi.model;

import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.yetanalytics.xapi.model.serializers.DateTimeSerializer;
import org.semver4j.Semver;

import java.time.ZonedDateTime;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.yetanalytics.xapi.model.serializers.DateTimeSerializer;
import com.yetanalytics.xapi.model.serializers.SemverSerializer;

/**
* Class representation of an Statement from the <a href="https://github.com/adlnet/xAPI-Spec/blob/master/xAPI-Data.md#24-statement-properties">9274.1.1 xAPI Specification</a>.
Expand Down Expand Up @@ -38,7 +40,8 @@ public class Statement extends AbstractObject {
@JsonSerialize(using = DateTimeSerializer.class)
private ZonedDateTime stored;

private String version;
@JsonSerialize(using = SemverSerializer.class)
private Semver version;
Copy link
Member

@cliffcaseyyet cliffcaseyyet Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol this is so overkill, but whatever its fine.


private List<Attachment> attachments;

Expand Down Expand Up @@ -114,11 +117,11 @@ public void setStored(ZonedDateTime stored) {
this.stored = stored;
}

public String getVersion() {
public Semver getVersion() {
return version;
}

public void setVersion(String version) {
public void setVersion(Semver version) {
this.version = version;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.yetanalytics.xapi.model.deserializers;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.yetanalytics.xapi.util.Mapper;

import jakarta.activation.MimeType;
import jakarta.activation.MimeTypeParseException;

/**
* Custom serializer for the Jakart Activation MIME types.
*/
public class MimeTypeDeserializer extends StdDeserializer<MimeType> {

public MimeTypeDeserializer() {
this(null);
}

public MimeTypeDeserializer(final Class<?> vc) {
super(vc);
}

@Override
public MimeType deserialize(final JsonParser jp, final DeserializationContext context) {
try {
ObjectMapper mapper = Mapper.getMapper();
return new MimeType(mapper.readValue(jp, String.class));
} catch (MimeTypeParseException e) {
// Need special case since MimeTypeParseException does not
// extend RuntimeException, so it angers the compiler.
throw new IllegalArgumentException(e);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.yetanalytics.xapi.model.serializers;

import java.io.IOException;

import org.semver4j.Semver;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.yetanalytics.xapi.util.Mapper;

/**
* Custom serializer for Semver4j Semver class.
*/
public class SemverSerializer extends StdSerializer<Semver> {
public SemverSerializer() {
this(null);
}

public SemverSerializer(Class<Semver> t) {
super(t);
}

@Override
public void serialize(Semver ver, JsonGenerator gen, SerializerProvider provider) throws IOException, JsonProcessingException {
Mapper.getMapper().writeValue(gen, ver.getVersion());
}
}
111 changes: 111 additions & 0 deletions src/test/java/com/yetanalytics/ValueSerializationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.yetanalytics;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.flipkart.zjsonpatch.JsonDiff;
import com.yetanalytics.xapi.model.Agent;
import com.yetanalytics.xapi.model.Attachment;
import com.yetanalytics.xapi.model.LangMap;
import com.yetanalytics.xapi.model.Result;
import com.yetanalytics.xapi.model.Statement;
import com.yetanalytics.xapi.model.Verb;
import com.yetanalytics.xapi.util.Mapper;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class ValueSerializationTest extends TestCase {
public ValueSerializationTest(String testName) {
super(testName);
}

public static Test suite() {
return new TestSuite(ValueSerializationTest.class);
}

public <T> ArrayNode reserializeAndDiff(String original, Class<T> toConvert) throws JsonProcessingException {
ObjectMapper mapper = Mapper.getMapper();
// Deserialize
T value = mapper.readValue(original, toConvert);
// Reserialize
String reserialized = mapper.writeValueAsString(value);

JsonNode before = mapper.readTree(original);
JsonNode after = mapper.readTree(reserialized);

// Get Diff
return (ArrayNode) JsonDiff.asJson(before, after);
}

// TODO: Figure out how not have to wrap string properties in objects

public void testUUID() throws JsonProcessingException {
String uuidStr = "{\"id\": \"00000000-4000-8000-0000-000000000000\"}";
ArrayNode diff = reserializeAndDiff(uuidStr, Statement.class);
assertEquals(0, diff.size());
}

public void testUri() throws JsonProcessingException {
String uriStr = "{\"id\": \"http://EXAMPLE.com\"}";
ArrayNode diff = reserializeAndDiff(uriStr, Verb.class);
assertEquals(0, diff.size());
}

public void testUri2() throws JsonProcessingException {
String uriStr = "{\"id\": \"http://你好世界.com\"}";
ArrayNode diff = reserializeAndDiff(uriStr, Verb.class);
assertEquals(0, diff.size());
}

public void testTimestamp() throws JsonProcessingException {
String timestampStr = "{\"timestamp\": \"2023-10-27T09:03:21.722Z\"}";
ArrayNode diff = reserializeAndDiff(timestampStr, Statement.class);
assertEquals(0, diff.size());
}

public void testDuration() throws JsonProcessingException {
String durationStr = "{\"duration\": \"PT4H35M59.14S\"}";
ArrayNode diff = reserializeAndDiff(durationStr, Result.class);
assertEquals(0, diff.size());
}

public void testDuration2() throws JsonProcessingException {
String durationStr = "{\"duration\": \"PT16559.14S\"}";
ArrayNode diff = reserializeAndDiff(durationStr, Result.class);
assertEquals(0, diff.size());
}

public void testMimeType() throws JsonProcessingException {
// TODO: Deal with when there is whitespace after the semicolon
String mimeTypeStr = "{\"contentType\": \"text/plain; charset=UTF-8\"}";
ArrayNode diff = reserializeAndDiff(mimeTypeStr, Attachment.class);
assertEquals(0, diff.size());
}

public void testLangTag() throws JsonProcessingException {
String langTagStr = "{\"en-us\": \"foo\"}";
ArrayNode diff = reserializeAndDiff(langTagStr, LangMap.class);
assertEquals(0, diff.size());
}

public void testVersion() throws JsonProcessingException {
String versionStr = "{\"version\": \"1.0.0\"}";
ArrayNode diff = reserializeAndDiff(versionStr, Statement.class);
assertEquals(0, diff.size());
}

public void testSHA1() throws JsonProcessingException {
String sha1Str = "{\"mbox_sha1sum\": \"767e74eab7081c41e0b83630511139d130249666\"}";
ArrayNode diff = reserializeAndDiff(sha1Str, Agent.class);
assertEquals(0, diff.size());
}

public void testSHA2() throws JsonProcessingException {
String sha2Str = "{\"sha2\": \"321ba197033e81286fedb719d60d4ed5cecaed170733cb4a92013811afc0e3b6\"}";
ArrayNode diff = reserializeAndDiff(sha2Str, Attachment.class);
assertEquals(0, diff.size());
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/yetanalytics/XapiDeserializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testBasicStatement() throws StreamReadException, DatabindException,
assertEquals(stmt.getTimestamp().format(DateTimeFormatter.ISO_INSTANT), "2023-10-27T09:03:21.723Z");
assertEquals(stmt.getStored().format(DateTimeFormatter.ISO_INSTANT), "2023-10-27T09:03:21.722Z");
assertEquals(stmt.getId(), UUID.fromString("6fbd600f-d87c-4c74-801a-2ec2e53231c8"));
assertEquals(stmt.getVersion(), "1.0.3");
assertEquals(stmt.getVersion().toString(), "1.0.3");

Agent actor = (Agent) stmt.getActor();
assertEquals(actor.getName(), "Cliff Casey");
Expand Down Expand Up @@ -91,7 +91,7 @@ public void testAttachments() throws StreamReadException, DatabindException, IOE
assertEquals(att1.getUsageType(), URI.create("https://www.yetanalytics.com/usagetypes/1"));
assertEquals(att1.getDisplay().get(LangTag.parse("en-us")), "Attachment 1");
assertEquals(att1.getDescription().get(LangTag.parse("en-us")), "The First Attachment");
assertEquals(att1.getContentType(), "application/json");
assertEquals(att1.getContentType().toString(), "application/json");
assertEquals(att1.getLength(), Integer.valueOf(450));
assertEquals(att1.getSha2(), "426cf3a8b2864dd91201b989ba5728181da52bfff9a0489670e54cd8ec8b3a50");
assertEquals(att1.getFileUrl(), URI.create("https://www.yetanalytics.com/files/file1.json"));
Expand Down
6 changes: 4 additions & 2 deletions src/test/java/com/yetanalytics/XapiSerializationTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.yetanalytics;

import java.io.IOException;
import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.flipkart.zjsonpatch.JsonDiff;
import com.yetanalytics.util.TestFileUtils;
import com.yetanalytics.xapi.model.*;
import com.yetanalytics.xapi.model.Statement;
import com.yetanalytics.xapi.model.StatementResult;
import com.yetanalytics.xapi.util.Mapper;

import junit.framework.Test;
Expand All @@ -35,6 +36,7 @@ public <T> ArrayNode reserializeAndDiff(File original, Class<T> toConvert) throw
T stmt = mapper.readValue(original, toConvert);
//Reserialize
String reserialized = mapper.writeValueAsString(stmt);
System.out.println(reserialized);

JsonNode before = mapper.readTree(original);
JsonNode after = mapper.readTree(reserialized);
Expand Down
Loading