From 37ef68d0915bdc4dd9554d0645677705ccc7d6a2 Mon Sep 17 00:00:00 2001 From: lou wolford Date: Mon, 17 Nov 2014 13:24:05 -0800 Subject: [PATCH 1/4] uses android base64 for auth --- .gitignore | 4 ++++ pom.xml | 6 ++++++ src/main/java/gov/adlnet/xapi/client/BaseClient.java | 6 ++---- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 9bb7bf1..8c1f93d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,7 @@ target/ .classpath .project .settings/ + +# IntelliJ additions +*.iml +.idea/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index e6cbba2..6733135 100644 --- a/pom.xml +++ b/pom.xml @@ -152,6 +152,12 @@ + + com.google.android + android + 4.1.1.4 + provided + junit junit diff --git a/src/main/java/gov/adlnet/xapi/client/BaseClient.java b/src/main/java/gov/adlnet/xapi/client/BaseClient.java index 056d91d..3120ac0 100644 --- a/src/main/java/gov/adlnet/xapi/client/BaseClient.java +++ b/src/main/java/gov/adlnet/xapi/client/BaseClient.java @@ -11,13 +11,11 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; -import java.net.Authenticator; import java.net.HttpURLConnection; import java.net.MalformedURLException; -import java.net.PasswordAuthentication; import java.net.URL; -import javax.xml.bind.DatatypeConverter; +import android.util.Base64; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -52,7 +50,7 @@ protected void init(URL uri, String user, String password) { this._host = uri; this.username = user; this.password = password; - this.authString = "Basic " + DatatypeConverter.printBase64Binary((this.username + ":" + this.password).getBytes()); + this.authString = "Basic " + Base64.encodeToString((this.username + ":" + this.password).getBytes(), Base64.DEFAULT); } protected String readFromConnection(HttpURLConnection conn) From 29e7ff8258893983c8314f2ff09ecb713d43f18b Mon Sep 17 00:00:00 2001 From: lou wolford Date: Thu, 18 Dec 2014 06:10:09 -0800 Subject: [PATCH 2/4] moved state api to activity-updated tests --- pom.xml | 12 +- src/main/java/gov/adlnet/xapi/App.java | 2 +- .../adlnet/xapi/client/ActivityClient.java | 112 +++++++++++++-- .../gov/adlnet/xapi/client/AgentClient.java | 54 ++++++-- .../gov/adlnet/xapi/client/BaseClient.java | 71 +++++++++- .../gov/adlnet/xapi/client/StateClient.java | 92 ------------- .../adlnet/xapi/client/StatementClient.java | 10 +- .../java/gov/adlnet/xapi/model/Person.java | 45 ++++++ .../java/gov/adlnet/xapi/ActivityTest.java | 128 +++++++++++++++--- src/test/java/gov/adlnet/xapi/AgentTest.java | 81 +++++++++-- src/test/java/gov/adlnet/xapi/AppTest.java | 2 +- src/test/java/gov/adlnet/xapi/StateTest.java | 48 ------- 12 files changed, 447 insertions(+), 210 deletions(-) delete mode 100644 src/main/java/gov/adlnet/xapi/client/StateClient.java create mode 100644 src/main/java/gov/adlnet/xapi/model/Person.java delete mode 100644 src/test/java/gov/adlnet/xapi/StateTest.java diff --git a/pom.xml b/pom.xml index 6733135..9f07cf1 100644 --- a/pom.xml +++ b/pom.xml @@ -152,12 +152,12 @@ - - com.google.android - android - 4.1.1.4 - provided - + + + + + + junit junit diff --git a/src/main/java/gov/adlnet/xapi/App.java b/src/main/java/gov/adlnet/xapi/App.java index 78bd23a..34fe734 100644 --- a/src/main/java/gov/adlnet/xapi/App.java +++ b/src/main/java/gov/adlnet/xapi/App.java @@ -8,7 +8,7 @@ public class App { private static final String LRS_URI = "https://lrs.adlnet.gov/xAPI/"; - private static final String USERNAME = "Walt Grata"; + private static final String USERNAME = "jXAPI"; private static final String PASSWORD = "password"; public static void main(String[] args) throws IOException { StatementClient client = new StatementClient(LRS_URI, USERNAME, diff --git a/src/main/java/gov/adlnet/xapi/client/ActivityClient.java b/src/main/java/gov/adlnet/xapi/client/ActivityClient.java index 7f3f231..565ce5d 100644 --- a/src/main/java/gov/adlnet/xapi/client/ActivityClient.java +++ b/src/main/java/gov/adlnet/xapi/client/ActivityClient.java @@ -1,6 +1,7 @@ package gov.adlnet.xapi.client; import gov.adlnet.xapi.model.Activity; +import gov.adlnet.xapi.model.Agent; import java.io.IOException; import java.net.MalformedURLException; @@ -8,6 +9,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; +import com.google.gson.JsonObject; public class ActivityClient extends BaseClient { @@ -27,7 +29,23 @@ public Activity getActivity(String activityId) String result = issueGet(path); return getDecoder().fromJson(result, Activity.class); } - public JsonArray getActivities(String activityId, String since) throws MalformedURLException, IOException { + + private String createProfilePath(String activityId, String profileId){ + StringBuilder sb = new StringBuilder(); + sb.append("/xAPI/activities/profile?activityId="); + sb.append(activityId); + sb.append("&profileId="); + sb.append(profileId); + return sb.toString(); + } + + public JsonObject getActivityProfile(String activityId, String profileId) + throws MalformedURLException, IOException{ + String result = issueGet(createProfilePath(activityId, profileId)); + return getDecoder().fromJson(result, JsonObject.class); + } + + public JsonArray getActivityProfiles(String activityId, String since) throws MalformedURLException, IOException { String path = "/xapi/activities/profile?activityId=" + activityId; if (since != null && since.length() > 0){ path += ("&since=" + since); @@ -35,19 +53,87 @@ public JsonArray getActivities(String activityId, String since) throws Malformed String result = issueGet(path); return getDecoder().fromJson(result, JsonArray.class); } - private String createProfilePath(String activityId, String profileId){ - StringBuilder sb = new StringBuilder(); - sb.append("/xAPI/activities/profile?activityId="); - sb.append(activityId); - sb.append("&profileId="); - sb.append(profileId); - return sb.toString(); - } - public boolean publishActivityProfile(String activityId, String profileId, - JsonElement obj) throws IOException { - String json = getDecoder().toJson(obj); + + public boolean postActivityProfile(String activityId, String profileId, + JsonElement profile) throws IOException { + String json = getDecoder().toJson(profile); String response = issuePost(createProfilePath(activityId, profileId), json); - return response != null; + return response.isEmpty(); } + + public boolean putActivityProfile(String activityId, String profileId, + JsonElement profile) throws IOException { + String json = getDecoder().toJson(profile); + String response = issuePut(createProfilePath(activityId, profileId), + json); + return response.isEmpty(); + } + + public boolean deleteActivityProfile(String activityId, String profileId) throws IOException { + String response = issueDelete(createProfilePath(activityId, profileId)); + return response.isEmpty(); + } + + private String createStatePath(String activityId, Agent agent, String registration, String stateId){ + String path = String.format("/xapi/activities/state?activityId=%s&agent=%s&stateId=%s", activityId, + getDecoder().toJson(agent.serialize()), stateId); + if (registration != null && registration.length() > 0){ + path += ("®istration=" + registration); + } + return path; + } + + public JsonObject getActivityState(String activityId, Agent agent, String registration, String stateId) + throws MalformedURLException, IOException{ + String result = issueGet(createStatePath(activityId, agent, registration, stateId)); + return getDecoder().fromJson(result, JsonObject.class); + } + + public boolean postActivityState(String activityId, Agent agent, String registration, String stateId, + JsonElement state) + throws MalformedURLException, IOException{ + String json = getDecoder().toJson((state)); + String result = issuePost(createStatePath(activityId, agent, registration, stateId), json); + return result.isEmpty(); + } + + public boolean putActivityState(String activityId, Agent agent, String registration, String stateId, + JsonElement state) + throws MalformedURLException, IOException{ + String json = getDecoder().toJson((state)); + String result = issuePut(createStatePath(activityId, agent, registration, stateId), json); + return result.isEmpty(); + } + + public boolean deleteActivityState(String activityId, Agent agent, String registration, String stateId) + throws MalformedURLException, IOException{ + String result = issueDelete(createStatePath(activityId, agent, registration, stateId)); + return result.isEmpty(); + } + + public JsonArray getActivityStates(String activityId, Agent agent, String registration, String since) + throws MalformedURLException, IOException{ + String path = String.format("/xapi/activities/state?activityId=%s&agent=%s", activityId, + getDecoder().toJson(agent.serialize())); + if (registration != null && registration.length() > 0){ + path += ("®istration=" + registration); + } + if (since != null && since.length() > 0){ + path += ("&since=" + since); + } + String result = issueGet(path); + return getDecoder().fromJson(result, JsonArray.class); + } + + public boolean deleteActivityStates(String activityId, Agent agent, String registration) + throws MalformedURLException, IOException{ + String path = String.format("/xapi/activities/state?activityId=%s&agent=%s", activityId, + getDecoder().toJson(agent.serialize())); + if (registration != null && registration.length() > 0){ + path += ("®istration=" + registration); + } + String result = issueDelete(path); + return result.isEmpty(); + } } diff --git a/src/main/java/gov/adlnet/xapi/client/AgentClient.java b/src/main/java/gov/adlnet/xapi/client/AgentClient.java index 9d935be..83eea06 100644 --- a/src/main/java/gov/adlnet/xapi/client/AgentClient.java +++ b/src/main/java/gov/adlnet/xapi/client/AgentClient.java @@ -1,11 +1,13 @@ package gov.adlnet.xapi.client; import gov.adlnet.xapi.model.Agent; +import gov.adlnet.xapi.model.Person; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; +import com.google.gson.JsonArray; import com.google.gson.JsonElement; public class AgentClient extends BaseClient { @@ -20,25 +22,59 @@ public AgentClient(URL uri, String username, String password) super(uri, username, password); } - private String formatProfilePath(Agent a, String profileId) { + public Person getPerson(Agent a) + throws MalformedURLException, IOException { + String path = "/xapi/agents?agent=" + getDecoder().toJson(a.serialize()); + String result = issueGet(path); + return getDecoder().fromJson(result, Person.class); + } + + private String formatProfilePath(Agent a, String profileId) { String agentJson = getDecoder().toJson(a.serialize()); String path = "/xAPI/agents/profile?agent=" + agentJson + "&profileId=" + profileId; return path; } - public boolean publishAgentProfile(Agent a, String profileId, - JsonElement profile) throws IOException { - String requestBody = getDecoder().toJson(profile); - String path = formatProfilePath(a, profileId); - String result = issuePost(path, requestBody); - return result != null; - } - public JsonElement getAgentProfile(Agent a, String profileId) throws MalformedURLException, IOException { String path = formatProfilePath(a, profileId); String result = issueGet(path); return getDecoder().fromJson(result, JsonElement.class); } + + public boolean putAgentProfile(Agent a, String profileId, JsonElement profile) + throws MalformedURLException, IOException { + String path = formatProfilePath(a, profileId); + String json = getDecoder().toJson((profile)); + String result = issuePut(path, json); + return result.isEmpty(); + } + + public boolean postAgentProfile(Agent a, String profileId, JsonElement profile) + throws MalformedURLException, IOException { + String path = formatProfilePath(a, profileId); + String json = getDecoder().toJson((profile)); + String result = issuePost(path, json); + return result.isEmpty(); + } + + public boolean deleteAgentProfile(Agent a, String profileId) + throws MalformedURLException, IOException { + String path = formatProfilePath(a, profileId); + String result = issueDelete(path); + return result.isEmpty(); + } + + public JsonArray getAgentProfiles(Agent a, String since) + throws MalformedURLException, IOException { + String agentJson = getDecoder().toJson(a.serialize()); + String path = "/xAPI/agents/profile?agent=" + agentJson; + if (since != null && since.length() > 0){ + path += ("&since=" + since); + } + String result = issueGet(path); + return getDecoder().fromJson(result, JsonArray.class); + } + } diff --git a/src/main/java/gov/adlnet/xapi/client/BaseClient.java b/src/main/java/gov/adlnet/xapi/client/BaseClient.java index 3120ac0..486641b 100644 --- a/src/main/java/gov/adlnet/xapi/client/BaseClient.java +++ b/src/main/java/gov/adlnet/xapi/client/BaseClient.java @@ -15,7 +15,9 @@ import java.net.MalformedURLException; import java.net.URL; -import android.util.Base64; +//import android.util.Base64; +import javax.xml.bind.DatatypeConverter; +//import org.apache.commons.codec.binary.Base64; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -50,8 +52,10 @@ protected void init(URL uri, String user, String password) { this._host = uri; this.username = user; this.password = password; - this.authString = "Basic " + Base64.encodeToString((this.username + ":" + this.password).getBytes(), Base64.DEFAULT); - } +// this.authString = "Basic " + Base64.encodeToString((this.username + ":" + this.password).getBytes(), Base64.DEFAULT); + this.authString = "Basic " + DatatypeConverter.printBase64Binary((this.username + ":" + this.password).getBytes()); +// this.authString = "Basic " + Base64.encodeBase64((this.username + ":" + this.password).getBytes()).toString(); + } protected String readFromConnection(HttpURLConnection conn) throws java.io.IOException { @@ -121,6 +125,67 @@ protected String issuePost(String path, String data) } } + protected String issuePut(String path, String data) + throws java.io.IOException { + URL url = new URL(this._host.getProtocol(), this._host.getHost(), path); + HttpURLConnection conn = initializePOSTConnection(url); + conn.setRequestMethod("PUT"); + OutputStreamWriter writer = new OutputStreamWriter( + conn.getOutputStream()); + try { + writer.write(data); + } catch (IOException ex) { + InputStream s = conn.getErrorStream(); + InputStreamReader isr = new InputStreamReader(s); + BufferedReader br = new BufferedReader(isr); + try { + String line = ""; + while((line = br.readLine()) != null){ + System.out.print(line); + } + System.out.println(); + } finally { + s.close(); + } + throw ex; + } finally { + writer.close(); + } + try { + return readFromConnection(conn); + } finally { + conn.disconnect(); + } + } + + protected String issueDelete(String path) + throws java.io.IOException { + URL url = new URL(this._host.getProtocol(), this._host.getHost(), path); + HttpURLConnection conn = initializeConnection(url); + conn.setRequestMethod("DELETE"); + try{ + return readFromConnection(conn); + } + catch (IOException ex){ + InputStream s = conn.getErrorStream(); + InputStreamReader isr = new InputStreamReader(s); + BufferedReader br = new BufferedReader(isr); + try { + String line = ""; + while((line = br.readLine()) != null){ + System.out.print(line); + } + System.out.println(); + } finally { + s.close(); + } + throw ex; + } + finally{ + conn.disconnect(); + } + } + protected String issueGet(String path) throws java.io.IOException, java.net.MalformedURLException { URL url = new URL(this._host.getProtocol(), this._host.getHost(), path); diff --git a/src/main/java/gov/adlnet/xapi/client/StateClient.java b/src/main/java/gov/adlnet/xapi/client/StateClient.java deleted file mode 100644 index 9040ff1..0000000 --- a/src/main/java/gov/adlnet/xapi/client/StateClient.java +++ /dev/null @@ -1,92 +0,0 @@ -package gov.adlnet.xapi.client; - -import gov.adlnet.xapi.model.Agent; - -import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; - -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - -public class StateClient extends BaseClient { - - public StateClient(String uri, String username, String password) - throws MalformedURLException { - super(new URL(uri), username, password); - } - - public StateClient(URL uri, String username, String password) - throws MalformedURLException { - super(uri, username, password); - } - - private String createStatePath(String activityId, Agent agent, - String stateId) { - - StringBuilder path = new StringBuilder(); - path.append("/xapi/activities/state"); - path.append("?"); - path.append("activityId="); - path.append(activityId); - path.append("&"); - path.append("agent="); - path.append(getDecoder().toJson(agent.serialize())); - path.append("&"); - path.append("stateId="); - path.append(stateId); - return path.toString(); - } - - public boolean publishState(String activityId, Agent agent, String stateId, - JsonObject state) throws IOException { - Gson gson = getDecoder(); - String json = gson.toJson(state); - String path = createStatePath(activityId, agent, stateId); - String result = issuePost(path, json); - return result != null; - } - - public JsonObject getState(String activityId, Agent agent, String stateId) - throws IOException { - String path = createStatePath(activityId, agent, stateId); - URL url = new URL(this._host.getProtocol(), this._host.getHost(), path); - HttpURLConnection conn = initializeConnection(url); - String response = readFromConnection(conn); - return getDecoder().fromJson(response, JsonObject.class); - } - - private String createStatesPath(String activityId, Agent agent, - String registration, String since) { - StringBuilder path = new StringBuilder(); - path.append("/xapi/activities/state"); - path.append("?"); - path.append("activityId="); - path.append(activityId); - path.append("&"); - path.append("agent="); - path.append(getDecoder().toJson(agent.serialize())); - if (registration != null) { - path.append("&"); - path.append("registration="); - path.append(registration); - } - if (since != null) { - path.append("&"); - path.append("since="); - path.append(since); - } - return path.toString(); - } - - public JsonArray getStates(String activityId, Agent agent, - String registration, String since) throws IOException { - String path = createStatesPath(activityId, agent, registration, since); - URL url = new URL(this._host.getProtocol(), this._host.getHost(), path); - HttpURLConnection conn = initializeConnection(url); - String response = readFromConnection(conn); - return getDecoder().fromJson(response, JsonArray.class); - } -} diff --git a/src/main/java/gov/adlnet/xapi/client/StatementClient.java b/src/main/java/gov/adlnet/xapi/client/StatementClient.java index 02f4535..b5dd8a3 100644 --- a/src/main/java/gov/adlnet/xapi/client/StatementClient.java +++ b/src/main/java/gov/adlnet/xapi/client/StatementClient.java @@ -5,8 +5,10 @@ import gov.adlnet.xapi.model.StatementResult; import gov.adlnet.xapi.model.Verb; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLEncoder; import java.util.Map.Entry; import java.util.TreeMap; @@ -90,16 +92,16 @@ private StatementClient addFilter(String key, String value) { } } - public StatementClient filterByVerb(Verb v) { - return addFilter("verb", v.getId()); + public StatementClient filterByVerb(Verb v) throws UnsupportedEncodingException { + return addFilter("verb", URLEncoder.encode(v.getId(), "UTF-8")); } public StatementClient filterByVerb(String verbId) { return addFilter("verb", verbId); } - public StatementClient filterByActor(Actor a) { - return addFilter("agent", getDecoder().toJson(a)); + public StatementClient filterByActor(Actor a) throws UnsupportedEncodingException{ + return addFilter("agent", URLEncoder.encode(getDecoder().toJson(a.serialize()), "UTF-8")); } public StatementClient filterByActivity(String activityId) { diff --git a/src/main/java/gov/adlnet/xapi/model/Person.java b/src/main/java/gov/adlnet/xapi/model/Person.java new file mode 100644 index 0000000..b6be5e0 --- /dev/null +++ b/src/main/java/gov/adlnet/xapi/model/Person.java @@ -0,0 +1,45 @@ +package gov.adlnet.xapi.model; + +/** + * Created by lou on 12/15/14. + */ +public class Person { + private static final String OBJECTTYPE = "Person"; + private String[] name; + private String[] mbox; + private String[] mbox_sha1sum; + private String[] openid; + private Account[] account; + + public String getObjectType() { + return OBJECTTYPE; + } + + public String toString() { + String ret = getObjectType() + " ; "; + if (name != null && name.length != 0){ + ret += "name: " + name.toString() + " ; "; + } + if (mbox != null && mbox.length != 0){ + ret += "mbox: " + mbox.toString() + " ; "; + } + if (mbox_sha1sum != null && mbox_sha1sum.length != 0){ + ret += "mbox_sha1sum: " + mbox_sha1sum.toString() + " ; "; + } + if (openid != null && openid.length != 0){ + ret += "openid: " + openid.toString() + " ; "; + } + if (account != null && account.length != 0){ + ret += "account: "; + String accts = ""; + for(Account acc: account){ + if (accts.length() != 0){ + accts += " , "; + } + accts += acc.toString() + " "; + } + ret += accts + " ; "; + } + return ret; + } +} diff --git a/src/test/java/gov/adlnet/xapi/ActivityTest.java b/src/test/java/gov/adlnet/xapi/ActivityTest.java index c1b5be0..96dc6bf 100644 --- a/src/test/java/gov/adlnet/xapi/ActivityTest.java +++ b/src/test/java/gov/adlnet/xapi/ActivityTest.java @@ -1,7 +1,11 @@ package gov.adlnet.xapi; import gov.adlnet.xapi.client.ActivityClient; +import gov.adlnet.xapi.client.StatementClient; import gov.adlnet.xapi.model.Activity; +import gov.adlnet.xapi.model.Agent; +import gov.adlnet.xapi.model.Statement; +import gov.adlnet.xapi.model.Verb; import java.io.IOException; import java.util.UUID; @@ -11,14 +15,19 @@ import junit.framework.TestSuite; import com.google.gson.JsonArray; +import com.google.gson.JsonElement; import com.google.gson.JsonObject; public class ActivityTest extends TestCase { - private static final String PROFILE_ID = UUID.randomUUID().toString(); - private static final String ACTIVITY_ID = "http://example.com"; + private String PUT_PROFILE_ID; + private String POST_PROFILE_ID; + private String PUT_STATE_ID; + private String POST_STATE_ID; + private String ACTIVITY_ID; private static final String LRS_URI = "https://lrs.adlnet.gov/xAPI/"; - private static final String USERNAME = "Walt Grata"; + private static final String USERNAME = "jXAPI"; private static final String PASSWORD = "password"; + private static final String MBOX = "mailto:test@example.com"; /** * Create the test case @@ -40,26 +49,109 @@ public static Test suite() { return new TestSuite(ActivityTest.class); } + public void setUp() throws IOException{ + PUT_PROFILE_ID = UUID.randomUUID().toString(); + POST_PROFILE_ID = UUID.randomUUID().toString(); + PUT_STATE_ID = UUID.randomUUID().toString(); + POST_STATE_ID = UUID.randomUUID().toString(); + ACTIVITY_ID = "http://example.com"; + Agent a = new Agent(); + a.setMbox(MBOX); + + ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); + JsonObject puobj = new JsonObject(); + puobj.addProperty("putproftest", "putproftest"); + assertTrue(_client.putActivityProfile(ACTIVITY_ID, PUT_PROFILE_ID, puobj)); + + JsonObject pobj = new JsonObject(); + pobj.addProperty("postproftest", "postproftest"); + assertTrue(_client.postActivityProfile(ACTIVITY_ID, POST_PROFILE_ID, pobj)); + + JsonObject pusobj = new JsonObject(); + pusobj.addProperty("putstatetest", "putstatetest"); + assertTrue(_client.putActivityState(ACTIVITY_ID, a, null, PUT_STATE_ID, pusobj)); + + JsonObject posbj = new JsonObject(); + posbj.addProperty("poststatetest", "poststatetest"); + assertTrue(_client.postActivityState(ACTIVITY_ID, a, null, POST_STATE_ID, posbj)); + } + + public void tearDown() throws IOException{ + ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + assertTrue(_client.deleteActivityProfile(ACTIVITY_ID, PUT_PROFILE_ID)); + assertTrue(_client.deleteActivityProfile(ACTIVITY_ID, POST_PROFILE_ID)); + assertTrue(_client.deleteActivityState(ACTIVITY_ID, a, null, PUT_STATE_ID)); + assertTrue(_client.deleteActivityStates(ACTIVITY_ID, a, null)); + } + public void testGetActivity() throws IOException { - ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); - Activity a = _client.getActivity(ACTIVITY_ID); - assertNotNull(a); - assertEquals(a.getId(), ACTIVITY_ID); - } + StatementClient sc = new StatementClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + Verb v = new Verb("http://example.com/tested"); + Activity act = new Activity(ACTIVITY_ID); + Statement st = new Statement(a, v, act); + String stID = sc.publishStatement(st); - public void testSaveActivityProfile() throws IOException { - ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); - JsonObject obj = new JsonObject(); - assertTrue(_client.publishActivityProfile(ACTIVITY_ID, PROFILE_ID, obj)); + ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); + Activity returnAct = _client.getActivity(ACTIVITY_ID); + assertNotNull(returnAct); + assertEquals(returnAct.getId(), ACTIVITY_ID); } - public void testGetActivities() throws IOException{ + + public void testGetActivityProfile() throws IOException{ + ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); + JsonElement putProfile = _client.getActivityProfile(ACTIVITY_ID, PUT_PROFILE_ID); + assertNotNull(putProfile); + assertTrue(putProfile.isJsonObject()); + JsonObject obj = (JsonObject)putProfile; + assertEquals(obj.getAsJsonPrimitive("putproftest").getAsString(), "putproftest"); + + JsonElement postProfile = _client.getActivityProfile(ACTIVITY_ID, POST_PROFILE_ID); + assertNotNull(postProfile); + assertTrue(postProfile.isJsonObject()); + JsonObject pobj = (JsonObject)postProfile; + assertEquals(pobj.getAsJsonPrimitive("postproftest").getAsString(), "postproftest"); + } + + public void testGetActivityProfiles() throws IOException{ ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); - JsonArray a = _client.getActivities(ACTIVITY_ID, null); - assertNotNull(a); + JsonArray a = _client.getActivityProfiles(ACTIVITY_ID, null); + assertNotNull(a); + assertTrue(a.size() >= 2); } - public void testGetActivitesWithSince() throws IOException { + + public void testGetActivityProfilesWithSince() throws IOException { ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); - JsonArray a = _client.getActivities(ACTIVITY_ID, "2014-05-02T17:28:47.00Z"); - assertNotNull(a); + JsonArray a = _client.getActivityProfiles(ACTIVITY_ID, "2014-05-02T17:28:47.00Z"); + assertNotNull(a); + assertTrue(a.size() >= 2); } + public void testGetActivityState() throws IOException{ + ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + JsonElement putState = _client.getActivityState(ACTIVITY_ID, a, null, PUT_STATE_ID); + assertNotNull(putState); + assertTrue(putState.isJsonObject()); + JsonObject obj = (JsonObject)putState; + assertEquals(obj.getAsJsonPrimitive("putstatetest").getAsString(), "putstatetest"); + + JsonElement postState = _client.getActivityState(ACTIVITY_ID, a, null, POST_STATE_ID); + assertNotNull(postState); + assertTrue(postState.isJsonObject()); + JsonObject pobj = (JsonObject)postState; + assertEquals(pobj.getAsJsonPrimitive("poststatetest").getAsString(), "poststatetest"); + } + + public void testGetActivityStates() throws IOException{ + ActivityClient _client = new ActivityClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + JsonArray arr = _client.getActivityStates(ACTIVITY_ID, a, null, null); + assertNotNull(arr); + assertEquals(2, arr.size()); + } } diff --git a/src/test/java/gov/adlnet/xapi/AgentTest.java b/src/test/java/gov/adlnet/xapi/AgentTest.java index f0a500c..a6c0c3e 100644 --- a/src/test/java/gov/adlnet/xapi/AgentTest.java +++ b/src/test/java/gov/adlnet/xapi/AgentTest.java @@ -7,32 +7,83 @@ import gov.adlnet.xapi.client.AgentClient; import gov.adlnet.xapi.model.Agent; +import gov.adlnet.xapi.model.Person; + import junit.framework.TestCase; public class AgentTest extends TestCase { private static final String LRS_URI = "https://lrs.adlnet.gov/xAPI/"; - private static final String USERNAME = "Walt Grata"; + private static final String USERNAME = "jXAPI"; private static final String PASSWORD = "password"; private static final String MBOX = "mailto:test@example.com"; - private static final String PROFILE_ID = UUID.randomUUID().toString(); + private String PUT_PROFILE_ID; + private String POST_PROFILE_ID; - public void testPublishProfile() throws IOException { - AgentClient client = new AgentClient(LRS_URI, USERNAME, PASSWORD); - Agent a = new Agent(); - a.setMbox(MBOX); - JsonObject obj = new JsonObject(); - obj.addProperty("test", "test"); - client.publishAgentProfile(a, PROFILE_ID, obj); - } + public void setUp() throws IOException{ + PUT_PROFILE_ID = UUID.randomUUID().toString(); + POST_PROFILE_ID = UUID.randomUUID().toString(); + + AgentClient client = new AgentClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + JsonObject puobj = new JsonObject(); + puobj.addProperty("puttest", "puttest"); + assertTrue(client.putAgentProfile(a, PUT_PROFILE_ID, puobj)); + JsonObject pobj = new JsonObject(); + pobj.addProperty("posttest", "posttest"); + assertTrue(client.postAgentProfile(a, POST_PROFILE_ID, pobj)); + } + + public void tearDown() throws IOException{ + AgentClient client = new AgentClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + boolean putResp = client.deleteAgentProfile(a, PUT_PROFILE_ID); + assertTrue(putResp); + boolean postResp = client.deleteAgentProfile(a, POST_PROFILE_ID); + assertTrue(postResp); + } public void testGetProfile() throws IOException { AgentClient client = new AgentClient(LRS_URI, USERNAME, PASSWORD); Agent a = new Agent(); a.setMbox(MBOX); - JsonElement profile = client.getAgentProfile(a, PROFILE_ID); - assertNotNull(profile); - assertTrue(profile.isJsonObject()); - JsonObject obj = (JsonObject)profile; - assertEquals(obj.getAsJsonPrimitive("test").getAsString(), "test"); + JsonElement putProfile = client.getAgentProfile(a, PUT_PROFILE_ID); + assertNotNull(putProfile); + assertTrue(putProfile.isJsonObject()); + JsonObject obj = (JsonObject)putProfile; + assertEquals(obj.getAsJsonPrimitive("puttest").getAsString(), "puttest"); + + JsonElement postProfile = client.getAgentProfile(a, POST_PROFILE_ID); + assertNotNull(postProfile); + assertTrue(postProfile.isJsonObject()); + JsonObject pobj = (JsonObject)postProfile; + assertEquals(pobj.getAsJsonPrimitive("posttest").getAsString(), "posttest"); } + + public void testGetProfiles() throws IOException{ + AgentClient client = new AgentClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + JsonArray profiles = client.getAgentProfiles(a, ""); + assertNotNull(profiles); + assertTrue(profiles.size() >= 2); + } + + public void testGetProfilesWithSince() throws IOException{ + AgentClient client = new AgentClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + JsonArray profiles = client.getAgentProfiles(a, "2014-05-02T17:28:47.00Z"); + assertNotNull(profiles); + assertTrue(profiles.size() >= 2); + } + + public void testGetPerson() throws IOException{ + AgentClient client = new AgentClient(LRS_URI, USERNAME, PASSWORD); + Agent a = new Agent(); + a.setMbox(MBOX); + Person p = client.getPerson(a); + assertNotNull(p); + } } diff --git a/src/test/java/gov/adlnet/xapi/AppTest.java b/src/test/java/gov/adlnet/xapi/AppTest.java index fcb2ccb..b8baf4a 100644 --- a/src/test/java/gov/adlnet/xapi/AppTest.java +++ b/src/test/java/gov/adlnet/xapi/AppTest.java @@ -61,7 +61,7 @@ public static Calendar toCalendar(final String iso8601string) } private static final String LRS_URI = "https://lrs.adlnet.gov/xAPI/"; - private static final String USERNAME = "Walt Grata"; + private static final String USERNAME = "jXAPI"; private static final String PASSWORD = "password"; /** diff --git a/src/test/java/gov/adlnet/xapi/StateTest.java b/src/test/java/gov/adlnet/xapi/StateTest.java deleted file mode 100644 index 00012e6..0000000 --- a/src/test/java/gov/adlnet/xapi/StateTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package gov.adlnet.xapi; - -import gov.adlnet.xapi.client.StateClient; -import gov.adlnet.xapi.model.Agent; - -import java.io.IOException; - -import junit.framework.TestCase; - -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - -public class StateTest extends TestCase { - private static final String STATE_ID = "questionState"; - private static final String ACTIVITY_ID = "http://example.com"; - private static final String LRS_URI = "https://lrs.adlnet.gov/xAPI/"; - private static final String USERNAME = "Walt Grata"; - private static final String PASSWORD = "password"; - - public StateTest(String testName) throws java.net.URISyntaxException, - java.io.UnsupportedEncodingException { - super(testName); - } - - public void testPublishState() throws IOException { - StateClient client = new StateClient(LRS_URI, USERNAME, PASSWORD); - Agent a = new Agent(); - a.setMbox("mailto:test@example.com"); - JsonObject obj = new JsonObject(); - obj.addProperty("test", "test"); - assertTrue(client.publishState(ACTIVITY_ID, a, - STATE_ID, obj)); - } - - public void testGetState() throws IOException { - StateClient client = new StateClient(LRS_URI, USERNAME, PASSWORD); - Agent a = new Agent(); - a.setMbox("mailto:test@example.com"); - assertNotNull(client.getState(ACTIVITY_ID, a, STATE_ID)); - } - public void testGetStates() throws IOException { - StateClient client = new StateClient(LRS_URI, USERNAME, PASSWORD); - Agent a = new Agent(); - a.setMbox("mailto:test@example.com"); - JsonArray stuff = client.getStates(ACTIVITY_ID, a, null, null); - assertNotNull(stuff); - } -} From 8062fd7a8c12231c8cbb65f5824144c9d2a05640 Mon Sep 17 00:00:00 2001 From: lou wolford Date: Thu, 18 Dec 2014 06:12:47 -0800 Subject: [PATCH 3/4] removed comment --- src/main/java/gov/adlnet/xapi/client/BaseClient.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/gov/adlnet/xapi/client/BaseClient.java b/src/main/java/gov/adlnet/xapi/client/BaseClient.java index 486641b..90a8c30 100644 --- a/src/main/java/gov/adlnet/xapi/client/BaseClient.java +++ b/src/main/java/gov/adlnet/xapi/client/BaseClient.java @@ -17,7 +17,6 @@ //import android.util.Base64; import javax.xml.bind.DatatypeConverter; -//import org.apache.commons.codec.binary.Base64; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -54,7 +53,6 @@ protected void init(URL uri, String user, String password) { this.password = password; // this.authString = "Basic " + Base64.encodeToString((this.username + ":" + this.password).getBytes(), Base64.DEFAULT); this.authString = "Basic " + DatatypeConverter.printBase64Binary((this.username + ":" + this.password).getBytes()); -// this.authString = "Basic " + Base64.encodeBase64((this.username + ":" + this.password).getBytes()).toString(); } protected String readFromConnection(HttpURLConnection conn) From e80bc12b18bdf1e25e57e4978bf735e3d5e2cba4 Mon Sep 17 00:00:00 2001 From: lou wolford Date: Thu, 18 Dec 2014 06:19:47 -0800 Subject: [PATCH 4/4] removed comments --- pom.xml | 6 ------ src/main/java/gov/adlnet/xapi/client/BaseClient.java | 2 -- 2 files changed, 8 deletions(-) diff --git a/pom.xml b/pom.xml index 9f07cf1..e6cbba2 100644 --- a/pom.xml +++ b/pom.xml @@ -152,12 +152,6 @@ - - - - - - junit junit diff --git a/src/main/java/gov/adlnet/xapi/client/BaseClient.java b/src/main/java/gov/adlnet/xapi/client/BaseClient.java index 90a8c30..64a5692 100644 --- a/src/main/java/gov/adlnet/xapi/client/BaseClient.java +++ b/src/main/java/gov/adlnet/xapi/client/BaseClient.java @@ -15,7 +15,6 @@ import java.net.MalformedURLException; import java.net.URL; -//import android.util.Base64; import javax.xml.bind.DatatypeConverter; import com.google.gson.Gson; @@ -51,7 +50,6 @@ protected void init(URL uri, String user, String password) { this._host = uri; this.username = user; this.password = password; -// this.authString = "Basic " + Base64.encodeToString((this.username + ":" + this.password).getBytes(), Base64.DEFAULT); this.authString = "Basic " + DatatypeConverter.printBase64Binary((this.username + ":" + this.password).getBytes()); }