1515*/
1616package com .rusticisoftware .tincan ;
1717
18-
18+ import java .io .ByteArrayOutputStream ;
19+ import java .io .IOException ;
1920import java .io .InputStream ;
2021import java .net .MalformedURLException ;
2122import java .net .URI ;
3031
3132import lombok .extern .java .Log ;
3233
33- import com .fasterxml .jackson .databind .JsonNode ;
3434import com .fasterxml .jackson .databind .ObjectMapper ;
3535import com .fasterxml .jackson .databind .node .ObjectNode ;
36- import com .fasterxml .jackson .databind .node .JsonNodeFactory ;
3736
3837import org .apache .commons .codec .binary .Hex ;
3938import org .joda .time .Period ;
@@ -57,6 +56,7 @@ public class RemoteLRSTest {
5756 private static SubStatement subStatement ;
5857 private static Attachment attachment1 ;
5958 private static Attachment attachment2 ;
59+ private static Attachment attachment3 ;
6060
6161 private static Properties config = new Properties ();
6262
@@ -142,6 +142,35 @@ public static void Init() throws Exception {
142142 attachment2 .setDisplay (new LanguageMap ());
143143 attachment2 .getDisplay ().put ("en-US" , "Test Display 2" );
144144 attachment2 .setUsageType (new URI ("http://id.tincanapi.com/attachment/supporting_media" ));
145+
146+
147+ attachment3 = new Attachment ();
148+ attachment3 .setContent (getResourceAsByteArray ("/files/image.jpg" ));
149+ attachment3 .setContentType ("image/jpeg" );
150+ attachment3 .setDescription (new LanguageMap ());
151+ attachment3 .getDescription ().put ("en-US" , "Test Description 3" );
152+ attachment3 .setDisplay (new LanguageMap ());
153+ attachment3 .getDisplay ().put ("en-US" , "Test Display 3" );
154+ attachment3 .setUsageType (new URI ("http://id.tincanapi.com/attachment/supporting_media" ));
155+ }
156+
157+ //
158+ // see http://stackoverflow.com/a/1264737/1464957
159+ //
160+ private static byte [] getResourceAsByteArray (String resourcePath ) throws IOException {
161+ InputStream resourceIs = RemoteLRSTest .class .getResourceAsStream (resourcePath );
162+ ByteArrayOutputStream resourceData = new ByteArrayOutputStream ();
163+
164+ int nRead ;
165+ byte [] buffer = new byte [16384 ];
166+
167+ while ((nRead = resourceIs .read (buffer , 0 , buffer .length )) != -1 ) {
168+ resourceData .write (buffer , 0 , nRead );
169+ }
170+
171+ resourceData .flush ();
172+
173+ return resourceData .toByteArray ();
145174 }
146175
147176 @ Test
@@ -446,6 +475,33 @@ public void testRetrieveStatementWithAttachment() throws Exception {
446475 Assert .assertEquals (hash1 , hash2 );
447476 }
448477
478+ @ Test
479+ public void testRetrieveStatementWithBinaryAttachment () throws Exception {
480+ Statement statement = new Statement ();
481+ statement .setActor (agent );
482+ statement .setVerb (verb );
483+ statement .setObject (activity );
484+ statement .addAttachment (attachment3 );
485+
486+ StatementLRSResponse saveRes = lrs .saveStatement (statement );
487+ Assert .assertTrue (saveRes .getSuccess ());
488+
489+ StatementLRSResponse retRes = lrs .retrieveStatement (saveRes .getContent ().getId ().toString (), true );
490+ Assert .assertTrue (retRes .getSuccess ());
491+
492+ String hash1 , hash2 ;
493+ MessageDigest digest = MessageDigest .getInstance ("SHA-256" );
494+ digest .update (attachment3 .getContent ());
495+ byte [] hash = digest .digest ();
496+ hash1 = new String (Hex .encodeHex (hash ));
497+
498+ digest .update (retRes .getContent ().getAttachments ().get (0 ).getContent ());
499+ hash = digest .digest ();
500+ hash2 = new String (Hex .encodeHex (hash ));
501+
502+ Assert .assertEquals (hash1 , hash2 );
503+ }
504+
449505 @ Test
450506 public void testQueryStatements () throws Exception {
451507 StatementsQuery query = new StatementsQuery ();
0 commit comments