Skip to content

Commit

Permalink
Modify parameterized constructors, Statement(StringOfJSON), of Statement
Browse files Browse the repository at this point in the history
  • Loading branch information
okabe_JYSSH26 committed Dec 7, 2015
1 parent 293db93 commit ac35d36
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ target/test-classes/
.classpath
.project
.settings/
/target/
23 changes: 11 additions & 12 deletions src/main/java/com/rusticisoftware/tincan/Statement.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.List;
import java.util.UUID;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
Expand All @@ -34,6 +29,10 @@
import com.rusticisoftware.tincan.internal.StatementBase;
import com.rusticisoftware.tincan.json.StringOfJSON;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
* Statement Class
*/
Expand All @@ -45,7 +44,7 @@ public class Statement extends StatementBase {
private DateTime stored;
private Agent authority;
private TCAPIVersion version;

@Deprecated
private Boolean voided;

Expand All @@ -66,20 +65,20 @@ public Statement(JsonNode jsonNode) throws URISyntaxException, MalformedURLExcep
if (! authorityNode.isMissingNode()) {
this.setAuthority(Agent.fromJson(authorityNode));
}

JsonNode voidedNode = jsonNode.path("voided");
if (! voidedNode.isMissingNode()) {
this.setVoided(voidedNode.asBoolean());
}

JsonNode versionNode = jsonNode.path("version");
if (! versionNode.isMissingNode()) {
this.setVersion(TCAPIVersion.fromString(versionNode.textValue()));
}
}

public Statement(StringOfJSON jsonStr) throws IOException, URISyntaxException {
super(jsonStr);
this(jsonStr.toJSONNode());
}

public Statement(Agent actor, Verb verb, StatementTarget object, Result result, Context context) {
Expand All @@ -104,21 +103,21 @@ public ObjectNode toJSONNode(TCAPIVersion version) {
if (this.authority != null) {
node.put("authority", this.getAuthority().toJSONNode(version));
}

//Include 0.95 specific fields if asking for 0.95 version
if (TCAPIVersion.V095.equals(version)) {
if (this.getVoided() != null) {
node.put("voided", this.getVoided());
}
}

//Include 1.0.x specific fields if asking for 1.0.x version
if (version.ordinal() <= TCAPIVersion.V100.ordinal()) {
if (this.getVersion() != null) {
node.put("version", this.getVersion().toString());
}
}

return node;
}

Expand Down
79 changes: 67 additions & 12 deletions src/test/java/com/rusticisoftware/tincan/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/
package com.rusticisoftware.tincan;

import static com.rusticisoftware.tincan.TestUtils.assertSerializeDeserialize;
import static com.rusticisoftware.tincan.TestUtils.getAgent;
import static com.rusticisoftware.tincan.TestUtils.*;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -25,51 +26,105 @@
import org.joda.time.DateTime;
import org.junit.Test;

import com.rusticisoftware.tincan.json.StringOfJSON;

/**
* Description
*/
public class StatementTest {

@Test
public void serializeDeserialize() throws Exception {

List<StatementTarget> statementTargets = new ArrayList<StatementTarget>();
statementTargets.add(new Activity("http://example.com/activity"));
statementTargets.add(getAgent("Target", "mbox", "mailto:[email protected]"));
statementTargets.add(new StatementRef(UUID.randomUUID()));

SubStatement sub = new SubStatement();
sub.setActor(getAgent("Sub", "mbox", "mailto:[email protected]"));
sub.setVerb(new Verb("http://example.com/verb"));
sub.setObject(new Activity("http://example.com/sub-activity"));
statementTargets.add(sub);


Statement st = new Statement();
st.setActor(getAgent("Joe", "mbox", "mailto:[email protected]"));

st.setAttachments(new ArrayList<Attachment>());
Attachment att = new Attachment();
att.setSha2("abc");
st.getAttachments().add(att);

st.setAuthority(getAgent("Authority", "mbox", "mailto:[email protected]"));

st.setContext(new Context());
st.getContext().setLanguage("en-US");

st.setId(UUID.randomUUID());

st.setResult(new Result());
st.getResult().setCompletion(true);

st.setStored(new DateTime());
st.setTimestamp(new DateTime());
st.setVerb(new Verb("http://example.com/verb"));

for (StatementTarget target : statementTargets) {
st.setObject(target);
assertSerializeDeserialize(st);
}
}


/**
* To assert that {@Link Statement(StringOfJSON)} converts all textbased-elements in StringOfJSON to {@Link Statement}
*
* @throws Exception
*/
@Test
public void StatementConstructorTest() throws Exception {

List<StatementTarget> statementTargets = new ArrayList<StatementTarget>();
statementTargets.add(new Activity("http://example.com/activity"));
statementTargets.add(getAgent("Target", "mbox", "mailto:[email protected]"));
statementTargets.add(new StatementRef(UUID.randomUUID()));

SubStatement sub = new SubStatement();
sub.setActor(getAgent("Sub", "mbox", "mailto:[email protected]"));
sub.setVerb(new Verb("http://example.com/verb"));
sub.setObject(new Activity("http://example.com/sub-activity"));
statementTargets.add(sub);

Statement st = new Statement();
st.setActor(getAgent("Joe", "mbox", "mailto:[email protected]"));

st.setAttachments(new ArrayList<Attachment>());
Attachment att = new Attachment();
att.setSha2("abc");
st.getAttachments().add(att);

st.setAuthority(getAgent("Authority", "mbox", "mailto:[email protected]"));

st.setContext(new Context());
st.getContext().setLanguage("en-US");

st.setId(UUID.randomUUID());

st.setResult(new Result());
st.getResult().setCompletion(true);

st.setStored(new DateTime());
st.setTimestamp(new DateTime());
st.setVerb(new Verb("http://example.com/verb"));


for (StatementTarget target : statementTargets) {
st.setObject(target);

Statement tmpStatement = new Statement(new StringOfJSON(st.toJSON()));

assertThat(st.toJSON(), is(tmpStatement.toJSON()));
}
}
}

0 comments on commit ac35d36

Please sign in to comment.