Skip to content

Commit 60e7414

Browse files
committed
Add binary attachment unit test (failing)
1 parent bbd5a9a commit 60e7414

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

src/main/java/com/rusticisoftware/tincan/Attachment.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@
2929
import com.rusticisoftware.tincan.http.HTTPPart;
3030
import com.rusticisoftware.tincan.json.JSONBase;
3131
import com.rusticisoftware.tincan.json.Mapper;
32+
import org.apache.commons.codec.binary.Hex;
3233

3334
import lombok.Data;
3435
import lombok.EqualsAndHashCode;
3536
import lombok.NoArgsConstructor;
36-
import org.apache.commons.codec.binary.Hex;
3737

3838
/**
3939
* Attachment Class
@@ -96,7 +96,6 @@ public Attachment(JsonNode jsonNode, byte[] content) throws URISyntaxException,
9696
}
9797
}
9898

99-
10099
public void setContent(byte[] content) throws NoSuchAlgorithmException {
101100
this.content = Arrays.copyOf(content, content.length);
102101
this.setLength(content.length);

src/main/java/com/rusticisoftware/tincan/RemoteLRS.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@
1515
*/
1616
package com.rusticisoftware.tincan;
1717

18-
import javax.servlet.MultipartConfigElement;
19-
import javax.servlet.http.Part;
2018
import org.apache.commons.codec.binary.Base64;
2119
import com.fasterxml.jackson.core.JsonProcessingException;
2220
import com.fasterxml.jackson.databind.JsonNode;
2321
import com.fasterxml.jackson.databind.node.ArrayNode;
24-
import com.fasterxml.jackson.databind.ObjectMapper;
2522
import com.rusticisoftware.tincan.documents.ActivityProfileDocument;
2623
import com.rusticisoftware.tincan.documents.AgentProfileDocument;
2724
import com.rusticisoftware.tincan.documents.Document;
@@ -37,10 +34,7 @@
3734
import com.rusticisoftware.tincan.v10x.StatementsQuery;
3835
import org.eclipse.jetty.client.api.ContentResponse;
3936
import org.eclipse.jetty.client.api.Request;
40-
import org.eclipse.jetty.client.api.Response;
4137
import org.eclipse.jetty.client.HttpClient;
42-
import org.eclipse.jetty.client.api.Response.CompleteListener;
43-
import org.eclipse.jetty.client.api.Response.Listener;
4438
import org.eclipse.jetty.client.util.*;
4539
import org.eclipse.jetty.http.HttpField;
4640
import org.eclipse.jetty.http.HttpMethod;
@@ -53,9 +47,7 @@
5347
import java.net.MalformedURLException;
5448
import java.net.URL;
5549
import java.net.URLEncoder;
56-
import java.nio.ByteBuffer;
5750
import java.util.*;
58-
import java.util.concurrent.TimeUnit;
5951

6052
/**
6153
* Class used to communicate with a TCAPI endpoint synchronously

src/test/java/com/rusticisoftware/tincan/RemoteLRSTest.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616
package com.rusticisoftware.tincan;
1717

18-
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.IOException;
1920
import java.io.InputStream;
2021
import java.net.MalformedURLException;
2122
import java.net.URI;
@@ -30,10 +31,8 @@
3031

3132
import lombok.extern.java.Log;
3233

33-
import com.fasterxml.jackson.databind.JsonNode;
3434
import com.fasterxml.jackson.databind.ObjectMapper;
3535
import com.fasterxml.jackson.databind.node.ObjectNode;
36-
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
3736

3837
import org.apache.commons.codec.binary.Hex;
3938
import 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();

src/test/resources/files/image.jpg

49.6 KB
Loading

0 commit comments

Comments
 (0)