Skip to content
This repository has been archived by the owner on Oct 23, 2020. It is now read-only.

Commit

Permalink
Merge pull request #25 from janeklb/fix-24
Browse files Browse the repository at this point in the history
fixes #24: sending sha2 with fileUrl attachment in test
  • Loading branch information
ljwolford authored Dec 22, 2016
2 parents 7872500 + d8342f2 commit d5449a3
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/test/java/gov/adlnet/xapi/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public void testSettingMultipeInverseFunctionProperties()
}

public void testPublishStatementWithAttachmentFileURL()
throws URISyntaxException, IOException{
throws URISyntaxException, IOException, NoSuchAlgorithmException{
StatementClient _client = new StatementClient(LRS_URI, USERNAME,
PASSWORD);
Statement statement = new Statement();
Expand Down Expand Up @@ -232,6 +232,7 @@ public void testPublishStatementWithAttachmentFileURL()
att.setContentType("application/json");
att.setLength(45);
att.setFileUrl(new URI("http://test/attachment/url"));
att.setSha2(generateSha2("http://test/attachment/url".getBytes()));

ArrayList<Attachment> attList = new ArrayList<Attachment>();
attList.add(att);
Expand Down Expand Up @@ -296,9 +297,7 @@ public void testPublishStatementWithAttachmentFile()
att.setContentType(contentType);
att.setLength((int)testFile.length());
byte[] arr = fileToByteArray(testFile);
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(arr);
att.setSha2(new String(Hex.encode(md.digest())));
att.setSha2(generateSha2(arr));

ArrayList<Attachment> attList = new ArrayList<Attachment>();
attList.add(att);
Expand Down Expand Up @@ -334,6 +333,12 @@ private byte[] fileToByteArray(File file) throws IOException {
return buffer;
}

public String generateSha2(byte[] bytes) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(bytes);
return new String(Hex.encode(md.digest()));
}

public void testQueryByVerb() throws java.net.URISyntaxException,
java.io.IOException {
StatementClient _client = new StatementClient(LRS_URI, USERNAME,
Expand Down

0 comments on commit d5449a3

Please sign in to comment.