-
Notifications
You must be signed in to change notification settings - Fork 0
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
kelvinqian00
wants to merge
9
commits into
lang-tags
Choose a base branch
from
other-strings
base: lang-tags
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b018935
Add Jakarta Activation API for MIME types
kelvinqian00 6145e99
Implement Attachment contextTypes as MIME types
kelvinqian00 1224c32
Add latest Semver4j dep
kelvinqian00 e3a2405
Use Semver for Statement version
kelvinqian00 5d001c8
Add TODOs for SHA validation
kelvinqian00 8e8b497
Add value serialization tests to test special strings
kelvinqian00 98f75cc
Add docstrings
kelvinqian00 d02eac8
Add test for internationalized IRIs
kelvinqian00 15367df
Remove stray println
kelvinqian00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/com/yetanalytics/xapi/model/deserializers/MimeTypeDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/yetanalytics/xapi/model/serializers/SemverSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
111
src/test/java/com/yetanalytics/ValueSerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.