Skip to content

Commit

Permalink
Convert Extension keys to URIs
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinqian00 committed Dec 10, 2024
1 parent 19dba4c commit f52049f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
19 changes: 10 additions & 9 deletions src/main/java/com/yetanalytics/xapi/model/Extensions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yetanalytics.xapi.model;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand All @@ -24,9 +25,9 @@
@JsonSerialize(using = ExtensionSerializer.class)
public class Extensions {

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

public Extensions(Map<String, Object> input) {
public Extensions(Map<URI, Object> input) {
extMap = input;
}

Expand All @@ -35,7 +36,7 @@ public Extensions(Map<String, Object> input) {
* @param key the IRI key of the extension
* @param value The Collections API representation of the JSON Data
*/
public void put(String key, Object value) {
public void put(URI key, Object value) {
extMap.put(key, value);
}

Expand All @@ -44,7 +45,7 @@ public void put(String key, Object value) {
* @param key The IRI of the extension
* @return The Collections API representation of the JSON Data
*/
public Object get(String key) {
public Object get(URI key) {
return extMap.get(key);
}

Expand All @@ -57,7 +58,7 @@ public Object get(String key) {
* @return Object of type T that is the result of deserialization from the query
*/
@SuppressWarnings("unchecked")
public <T> T read(String key, String jsonPathExpression, Class<T> typeKey) {
public <T> T read(URI key, String jsonPathExpression, Class<T> typeKey) {
try {
Object jsonObject = extMap.get(key);
if (jsonObject == null) return null;
Expand All @@ -76,22 +77,22 @@ public <T> T read(String key, String jsonPathExpression, Class<T> typeKey) {
* Remove an extension by IRI key
* @param key the IRI of the extension to remove
*/
public void remove(String key) {
public void remove(URI key) {
extMap.remove(key);
}

/**
* Returns a set of all IRI Extension keys
* @return Set of IRI keys
*/
public Set<String> getKeys() {
public Set<URI> getKeys() {
return extMap.keySet();
}
/**
* Returns the full raw Extension Map as a HashMap&lt;String, Object&gt;
* Returns the full raw Extension Map as a HashMap&lt;URI, Object&gt;
* @return The raw Extensions Map
*/
public Map<String, Object> getMap() {
public Map<URI, Object> getMap() {
return extMap;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yetanalytics.xapi.model.deserializers;

import java.net.URI;
import java.util.HashMap;

import com.fasterxml.jackson.core.JsonParser;
Expand All @@ -26,8 +27,8 @@ public ExtensionDeserializer(final Class<?> vc) {
@Override
public Extensions deserialize(final JsonParser jp, final DeserializationContext context) {
try {
TypeReference<HashMap<String, Object>> typeRef
= new TypeReference<HashMap<String,Object>>() {};
TypeReference<HashMap<URI, Object>> typeRef
= new TypeReference<HashMap<URI,Object>>() {};

JsonNode node = Mapper.getMapper().readTree(jp);

Expand Down
9 changes: 5 additions & 4 deletions src/test/java/com/yetanalytics/XapiDeserializationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void testExtensions() throws StreamReadException, DatabindException, IOEx
assertEquals(object.getId(), URI.create("https://www.yetanalytics.com/profiles/thing/1.0/concepts/activities/act1"));

Extensions ext = object.getDefinition().getExtensions();
String extKey = "http://www.yetanalytics.com/profiles/thing/1.0/concepts/extensions/ext1";
URI extKey = URI.create("http://www.yetanalytics.com/profiles/thing/1.0/concepts/extensions/ext1");

//collections API
@SuppressWarnings("unchecked")
Expand All @@ -125,8 +125,9 @@ public void testExtensions() throws StreamReadException, DatabindException, IOEx
assertNull(nullEntry);
String miss = ext.read(extKey, "$.miss", String.class);
assertNull(miss);
String badKey = ext.read("badKey", "$.doesnt.matter", String.class);
assertNull(badKey);
URI badKey = URI.create("http://bad.key");
String badKeyMiss = ext.read(badKey, "$.doesnt.matter", String.class);
assertNull(badKeyMiss);
}

public void testResult() throws StreamReadException, DatabindException, IOException {
Expand Down Expand Up @@ -159,7 +160,7 @@ public void testContext() throws StreamReadException, DatabindException, IOExcep
assertEquals(ctx.getLanguage(), "en-us");
assertEquals(ctx.getPlatform(), "JUnit Testing");
assertEquals(ctx.getStatement().getId(), UUID.fromString("6fbd600f-d17c-4c74-801a-2ec2e53231c6"));
String extKey = "https://www.yetanalytics.com/extensions/ext3";
URI extKey = URI.create("https://www.yetanalytics.com/extensions/ext3");
assertEquals(ctx.getExtensions().read(extKey, "$.thing", String.class), "stuff");
ContextActivities ctxActs = ctx.getContextActivities();
assertEquals(ctxActs.getParent().get(1).getId(), URI.create("https://www.yetanalytics.com/activities/parent2"));
Expand Down

0 comments on commit f52049f

Please sign in to comment.