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

Logging #9

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@
<artifactId>json-path</artifactId>
<version>2.9.0</version>
</dependency>
<!-- logging -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.16</version>
</dependency>
<!-- testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/yetanalytics/xapi/Deserializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.yetanalytics.xapi;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yetanalytics.xapi.util.Mapper;

public class Deserializer {

private final ObjectMapper mapper;
private final Logger logger;

public Deserializer() {
mapper = Mapper.getMapper();
logger = LoggerFactory.getLogger(Serializer.class);
}

public <T> T deserialize(File file, Class<T> clazz) {
try {
return mapper.readValue(file, clazz);
} catch (JsonProcessingException e) {
logger.warn("Cannot parse JSON data!");
logger.warn(e.getOriginalMessage());
return null;
} catch (IOException e) {
logger.error("Cannot read JSON file!");
logger.error(e.getMessage());
return null;
}
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/yetanalytics/xapi/Serializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.yetanalytics.xapi;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yetanalytics.xapi.util.Mapper;

public class Serializer {
private final ObjectMapper mapper;
private final Logger logger;

public Serializer() {
mapper = Mapper.getMapper();
logger = LoggerFactory.getLogger(Serializer.class);
}

public <T> String serialize(T data) {
try {
return mapper.writeValueAsString(data);
} catch (JsonProcessingException e) {
logger.warn("Cannot write JSON data!");
logger.warn(e.getOriginalMessage());
return null;
}
}
}
10 changes: 10 additions & 0 deletions src/main/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
47 changes: 36 additions & 11 deletions src/test/java/com/yetanalytics/XapiDeserializationTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.yetanalytics;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.io.File;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -12,8 +12,23 @@
import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.databind.DatabindException;
import com.yetanalytics.util.TestFileUtils;
import com.yetanalytics.xapi.model.*;
import com.yetanalytics.xapi.util.Mapper;
import com.yetanalytics.xapi.Deserializer;
import com.yetanalytics.xapi.model.AbstractActor;
import com.yetanalytics.xapi.model.Activity;
import com.yetanalytics.xapi.model.ActivityDefinition;
import com.yetanalytics.xapi.model.Agent;
import com.yetanalytics.xapi.model.Attachment;
import com.yetanalytics.xapi.model.Context;
import com.yetanalytics.xapi.model.ContextActivities;
import com.yetanalytics.xapi.model.Extensions;
import com.yetanalytics.xapi.model.Group;
import com.yetanalytics.xapi.model.InteractionComponent;
import com.yetanalytics.xapi.model.InteractionType;
import com.yetanalytics.xapi.model.Result;
import com.yetanalytics.xapi.model.Score;
import com.yetanalytics.xapi.model.Statement;
import com.yetanalytics.xapi.model.StatementResult;
import com.yetanalytics.xapi.model.Verb;

import junit.framework.Test;
import junit.framework.TestCase;
Expand All @@ -32,8 +47,9 @@ public static Test suite()
}

public void testBasicStatement() throws StreamReadException, DatabindException, IOException {
Deserializer deserializer = new Deserializer();
File testFile = TestFileUtils.getJsonTestFile("basic");
Statement stmt = Mapper.getMapper().readValue(testFile, Statement.class);
Statement stmt = deserializer.deserialize(testFile, Statement.class);

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");
Expand Down Expand Up @@ -67,8 +83,10 @@ public void testBasicStatement() throws StreamReadException, DatabindException,
}

public void testAttachments() throws StreamReadException, DatabindException, IOException {
Deserializer deserializer = new Deserializer();
File testFile = TestFileUtils.getJsonTestFile("attachments");
Statement stmt = Mapper.getMapper().readValue(testFile, Statement.class);
Statement stmt = deserializer.deserialize(testFile, Statement.class);

assertEquals(stmt.getAttachments().size(), 1);
Attachment att1 = stmt.getAttachments().get(0);
assertEquals(att1.getUsageType(), "https://www.yetanalytics.com/usagetypes/1");
Expand All @@ -81,8 +99,9 @@ public void testAttachments() throws StreamReadException, DatabindException, IOE
}

public void testExtensions() throws StreamReadException, DatabindException, IOException {
Deserializer deserializer = new Deserializer();
File testFile = TestFileUtils.getJsonTestFile("extensions");
Statement stmt = Mapper.getMapper().readValue(testFile, Statement.class);
Statement stmt = deserializer.deserialize(testFile, Statement.class);

Activity object = (Activity) stmt.getObject();
assertEquals(object.getId(), "https://www.yetanalytics.com/profiles/thing/1.0/concepts/activities/act1");
Expand Down Expand Up @@ -114,8 +133,9 @@ public void testExtensions() throws StreamReadException, DatabindException, IOEx
}

public void testResult() throws StreamReadException, DatabindException, IOException {
Deserializer deserializer = new Deserializer();
File testFile = TestFileUtils.getJsonTestFile("result");
Statement stmt = Mapper.getMapper().readValue(testFile, Statement.class);
Statement stmt = deserializer.deserialize(testFile, Statement.class);

Result res = stmt.getResult();
assertTrue(res.getCompletion());
Expand All @@ -131,8 +151,9 @@ public void testResult() throws StreamReadException, DatabindException, IOExcept
}

public void testContext() throws StreamReadException, DatabindException, IOException {
Deserializer deserializer = new Deserializer();
File testFile = TestFileUtils.getJsonTestFile("context");
Statement stmt = Mapper.getMapper().readValue(testFile, Statement.class);
Statement stmt = deserializer.deserialize(testFile, Statement.class);

Context ctx = stmt.getContext();
assertEquals(ctx.getRegistration(), UUID.fromString("6fbd600f-d17c-4c74-801a-2ec2e53231c7"));
Expand All @@ -154,8 +175,9 @@ public void testContext() throws StreamReadException, DatabindException, IOExcep
}

public void testInteractionActivity() throws StreamReadException, DatabindException, IOException {
Deserializer deserializer = new Deserializer();
File testFile = TestFileUtils.getJsonTestFile("interaction-activity");
Statement stmt = Mapper.getMapper().readValue(testFile, Statement.class);
Statement stmt = deserializer.deserialize(testFile, Statement.class);

Activity act = (Activity) stmt.getObject();
assertEquals(act.getId(), "https://www.yetanalytics.com/activities/act1/question1");
Expand All @@ -171,8 +193,9 @@ public void testInteractionActivity() throws StreamReadException, DatabindExcept
}

public void testGroupActor() throws StreamReadException, DatabindException, IOException {
Deserializer deserializer = new Deserializer();
File testFile = TestFileUtils.getJsonTestFile("group-actor");
Statement stmt = Mapper.getMapper().readValue(testFile, Statement.class);
Statement stmt = deserializer.deserialize(testFile, Statement.class);

Group group = (Group) stmt.getActor();
assertEquals(group.getMbox(), "mailto:[email protected]");
Expand All @@ -182,8 +205,10 @@ public void testGroupActor() throws StreamReadException, DatabindException, IOEx
}

public void testStatementResults() throws StreamReadException, DatabindException, IOException {
Deserializer deserializer = new Deserializer();
File testFile = TestFileUtils.getJsonTestFile("statementresults");
StatementResult stmtRes = Mapper.getMapper().readValue(testFile, StatementResult.class);
StatementResult stmtRes = deserializer.deserialize(testFile, StatementResult.class);

assertEquals(stmtRes.getMore(), "/xapi/statements?limit=2&from=6fbd600f-d17c-4c74-801a-2ec2e53231c9");

assertEquals(stmtRes.getStatements().get(0).getVerb().getId(),
Expand Down
13 changes: 9 additions & 4 deletions src/test/java/com/yetanalytics/XapiSerializationTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
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.Deserializer;
import com.yetanalytics.xapi.Serializer;
import com.yetanalytics.xapi.model.Statement;
import com.yetanalytics.xapi.model.StatementResult;
import com.yetanalytics.xapi.util.Mapper;

import junit.framework.Test;
Expand All @@ -31,10 +34,12 @@ public static Test suite()
public <T> ArrayNode reserializeAndDiff(File original, Class<T> toConvert) throws IOException {

ObjectMapper mapper = Mapper.getMapper();
Deserializer deserializer = new Deserializer();
Serializer serializer = new Serializer();
//Deserialize
T stmt = mapper.readValue(original, toConvert);
T stmt = deserializer.deserialize(original, toConvert);
//Reserialize
String reserialized = mapper.writeValueAsString(stmt);
String reserialized = serializer.serialize(stmt);

JsonNode before = mapper.readTree(original);
JsonNode after = mapper.readTree(reserialized);
Expand Down
10 changes: 10 additions & 0 deletions src/test/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="warn">
<appender-ref ref="STDOUT" />
</root>
</configuration>
Loading