MARMOTTA-449: Added test for binary resource, implementation is now functional.


Project: http://git-wip-us.apache.org/repos/asf/marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/marmotta/commit/761a4a08
Tree: http://git-wip-us.apache.org/repos/asf/marmotta/tree/761a4a08
Diff: http://git-wip-us.apache.org/repos/asf/marmotta/diff/761a4a08

Branch: refs/heads/develop
Commit: 761a4a08ef8cb71701b936d1bf72019063eae81e
Parents: 22d6735
Author: Jakob Frank <[email protected]>
Authored: Mon Mar 10 17:13:47 2014 +0100
Committer: Jakob Frank <[email protected]>
Committed: Mon Mar 10 17:13:47 2014 +0100

----------------------------------------------------------------------
 .../apache/marmotta/commons/util/HashUtils.java |  8 +--
 .../marmotta/platform/ldp/api/LdpService.java   |  5 +-
 .../platform/ldp/services/LdpServiceImpl.java   |  9 ++-
 .../marmotta/platform/ldp/util/LdpUtils.java    | 27 +++++++--
 .../platform/ldp/webservices/LdpWebService.java |  4 +-
 .../ldp/webservices/LdpWebServiceTest.java      | 59 +++++++++++++++++---
 6 files changed, 84 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/marmotta/blob/761a4a08/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/util/HashUtils.java
----------------------------------------------------------------------
diff --git 
a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/util/HashUtils.java
 
b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/util/HashUtils.java
index 5f2f83d..37a0e6c 100644
--- 
a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/util/HashUtils.java
+++ 
b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/util/HashUtils.java
@@ -17,12 +17,7 @@
  */
 package org.apache.marmotta.commons.util;
 
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
 import java.math.BigInteger;
 import java.nio.file.Path;
 import java.security.DigestInputStream;
@@ -100,6 +95,7 @@ public class HashUtils {
     public static final String sha512(byte[] bytes) {
         return calcHash(bytes, "SHA-512");
     }
+
     private static String calcHash(String string, String algorithm) {
         try {
             return calcHash(string.getBytes("UTF-8"), algorithm);

http://git-wip-us.apache.org/repos/asf/marmotta/blob/761a4a08/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java
----------------------------------------------------------------------
diff --git 
a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java
 
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java
index 01c4db4..e099bb6 100644
--- 
a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java
+++ 
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/api/LdpService.java
@@ -29,7 +29,6 @@ import org.openrdf.rio.RDFHandlerException;
 import org.openrdf.rio.RDFParseException;
 
 import javax.ws.rs.core.EntityTag;
-import javax.ws.rs.core.MediaType;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -63,7 +62,7 @@ public interface LdpService {
      * @throws IOException
      * @throws RDFParseException
      */
-    String addResource(RepositoryConnection connection, String container, 
String resource, MediaType type, InputStream stream) throws 
RepositoryException, IOException, RDFParseException;
+    String addResource(RepositoryConnection connection, String container, 
String resource, String type, InputStream stream) throws RepositoryException, 
IOException, RDFParseException;
 
     /**
      * Add a LDP resource
@@ -78,7 +77,7 @@ public interface LdpService {
      * @throws IOException
      * @throws RDFParseException
      */
-    String addResource(RepositoryConnection connection, URI container, URI 
resource, MediaType type, InputStream stream) throws RepositoryException, 
IOException, RDFParseException;
+    String addResource(RepositoryConnection connection, URI container, URI 
resource, String type, InputStream stream) throws RepositoryException, 
IOException, RDFParseException;
 
     List<Statement> getLdpTypes(RepositoryConnection connection, String 
resource) throws RepositoryException;
 

http://git-wip-us.apache.org/repos/asf/marmotta/blob/761a4a08/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java
 
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java
index f1af941..1d2ce22 100644
--- 
a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java
+++ 
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/services/LdpServiceImpl.java
@@ -46,7 +46,6 @@ import org.slf4j.LoggerFactory;
 import javax.enterprise.context.ApplicationScoped;
 import javax.inject.Inject;
 import javax.ws.rs.core.EntityTag;
-import javax.ws.rs.core.MediaType;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -167,12 +166,12 @@ public class LdpServiceImpl implements LdpService {
     }
 
     @Override
-    public String addResource(RepositoryConnection connection, String 
container, String resource, MediaType type, InputStream stream) throws 
RepositoryException, IOException, RDFParseException {
+    public String addResource(RepositoryConnection connection, String 
container, String resource, String type, InputStream stream) throws 
RepositoryException, IOException, RDFParseException {
         return addResource(connection, buildURI(container), 
buildURI(resource), type, stream);
     }
 
     @Override
-    public String addResource(RepositoryConnection connection, URI container, 
URI resource, MediaType type, InputStream stream) throws RepositoryException, 
IOException, RDFParseException {
+    public String addResource(RepositoryConnection connection, URI container, 
URI resource, String type, InputStream stream) throws RepositoryException, 
IOException, RDFParseException {
         ValueFactory valueFactory = connection.getValueFactory();
 
         // Add container triples (Sec. 6.4.3)
@@ -195,10 +194,10 @@ public class LdpServiceImpl implements LdpService {
         connection.add(resource, DCTERMS.modified, now, ldpContext);
 
         // Add the bodyContent
-        final RDFFormat rdfFormat = 
Rio.getParserFormatForMIMEType(type.toString());
+        final RDFFormat rdfFormat = Rio.getParserFormatForMIMEType(type);
         if (rdfFormat == null) {
             log.debug("POST creates new LDP-NR, because no suitable RDF parser 
found for type {}", type);
-            Literal format = valueFactory.createLiteral(type.toString());
+            Literal format = valueFactory.createLiteral(type);
             URI binaryResource = valueFactory.createURI(resource.stringValue() 
+ LdpUtils.getExtension(type));
 
             connection.add(binaryResource, DCTERMS.created, now, ldpContext);

http://git-wip-us.apache.org/repos/asf/marmotta/blob/761a4a08/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpUtils.java
----------------------------------------------------------------------
diff --git 
a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpUtils.java
 
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpUtils.java
index d16035e..ceba28d 100644
--- 
a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpUtils.java
+++ 
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/util/LdpUtils.java
@@ -20,7 +20,6 @@ package org.apache.marmotta.platform.ldp.util;
 import info.aduna.iteration.CloseableIteration;
 import org.apache.marmotta.commons.vocabulary.LDP;
 import org.apache.marmotta.commons.vocabulary.XSD;
-import org.apache.tika.mime.MimeType;
 import org.apache.tika.mime.MimeTypeException;
 import org.apache.tika.mime.MimeTypes;
 import org.openrdf.model.Statement;
@@ -55,20 +54,38 @@ public class LdpUtils {
      *
      * @param mediaType content type
      * @return file extension (already including '.')
-     * @throws MimeTypeException
      */
     public static String getExtension(MediaType mediaType) {
-        String contentType = String.format("%s/%s", mediaType.getType(), 
mediaType.getSubtype());
+        String contentType = getMimeType(mediaType);
+        return getExtension(contentType);
+    }
+
+    /**
+     * Get the preferred file extension for the content type
+     *
+     * @param mimeType mimeType
+     * @return file extension (already including '.')
+     */
+    public static String getExtension(String mimeType) {
         MimeTypes allTypes = MimeTypes.getDefaultMimeTypes();
         try {
-            MimeType mimeType = allTypes.forName(contentType);
-            return mimeType.getExtension();
+            return allTypes.forName(mimeType).getExtension();
         } catch (MimeTypeException e) {
             return null; //FIXME
         }
     }
 
     /**
+     * Get <b>only</b> the mimeType from the {@link javax.ws.rs.core.MediaType}
+     *
+     * @param mediaType the mediaType
+     * @return the mimeType
+     */
+    public static String getMimeType(MediaType mediaType) {
+        return String.format("%s/%s", mediaType.getType(), 
mediaType.getSubtype());
+    }
+
+    /**
      * LDP-Style to serialize a resource.
      *
      * @param writer the writer to serialize to

http://git-wip-us.apache.org/repos/asf/marmotta/blob/761a4a08/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java
----------------------------------------------------------------------
diff --git 
a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java
 
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java
index 38ff9b0..d344a86 100644
--- 
a/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java
+++ 
b/platform/marmotta-ldp/src/main/java/org/apache/marmotta/platform/ldp/webservices/LdpWebService.java
@@ -225,10 +225,10 @@ public class LdpWebService {
             }
 
             log.debug("POST to <{}> will create new LDP-R <{}>", container, 
newResource);
-
+            final String mimeType = LdpUtils.getMimeType(type);
             //checking if resource (container) hasType is done later in the 
service
             try {
-                String location = ldpService.addResource(conn, container, 
newResource, type, postBody);
+                String location = ldpService.addResource(conn, container, 
newResource, mimeType, postBody);
                 final Response.ResponseBuilder response = createResponse(conn, 
Response.Status.CREATED, container).location(java.net.URI.create(location));
                 if (newResource.compareTo(location) != 0) {
                     response.link(newResource, "describedby"); //Sec. 
6.2.3.12, see also http://www.w3.org/2012/ldp/track/issues/15

http://git-wip-us.apache.org/repos/asf/marmotta/blob/761a4a08/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
----------------------------------------------------------------------
diff --git 
a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
 
b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
index 73497e0..1ebcc91 100644
--- 
a/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
+++ 
b/platform/marmotta-ldp/src/test/java/org/apache/marmotta/platform/ldp/webservices/LdpWebServiceTest.java
@@ -20,6 +20,7 @@ package org.apache.marmotta.platform.ldp.webservices;
 import com.jayway.restassured.RestAssured;
 import org.apache.commons.io.IOUtils;
 import org.apache.marmotta.commons.sesame.test.SesameMatchers;
+import org.apache.marmotta.commons.util.HashUtils;
 import org.apache.marmotta.commons.vocabulary.LDP;
 import org.apache.marmotta.platform.core.exception.io.MarmottaImportException;
 import org.apache.marmotta.platform.core.test.base.JettyMarmotta;
@@ -28,6 +29,7 @@ import org.hamcrest.CoreMatchers;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test;
+import org.openrdf.model.impl.LiteralImpl;
 import org.openrdf.model.impl.URIImpl;
 import org.openrdf.model.vocabulary.DCTERMS;
 import org.openrdf.model.vocabulary.RDF;
@@ -39,6 +41,8 @@ import javax.ws.rs.core.UriBuilder;
 import java.io.IOException;
 import java.net.URISyntaxException;
 
+import static org.junit.Assert.assertEquals;
+
 /**
  * Testing LDP web services
  *
@@ -105,7 +109,7 @@ public class LdpWebServiceTest {
                 .header("Accept", mimeType)
             .expect()
                 .statusCode(200)
-                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIST header...
+                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIRST header...
                         
HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport";,
 "describedby"),
                         
HeaderMatchers.isLink(LDP.BasicContainer.stringValue(), "type"))
                 )
@@ -123,7 +127,7 @@ public class LdpWebServiceTest {
                 .header("Accept", mimeType)
             .expect()
                 .statusCode(200)
-                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIST header...
+                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIRST header...
                         
HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport";,
 "describedby"),
                         HeaderMatchers.isLink(LDP.Resource.stringValue(), 
"type"))
                 )
@@ -176,7 +180,7 @@ public class LdpWebServiceTest {
             .expect()
                 .statusCode(201)
                 .header("Location", baseUrl + newResource + ".png")
-                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIST header...
+                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIRST header...
                         HeaderMatchers.isLink(baseUrl + newResource, 
"describedby"),
                         
HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport";,
 "describedby"),
                         
HeaderMatchers.isLink(LDP.BasicContainer.stringValue(), "type"))
@@ -189,7 +193,7 @@ public class LdpWebServiceTest {
                 .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
             .expect()
                 .statusCode(200)
-                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIST header...
+                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIRST header...
                         
HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport";,
 "describedby"),
                         
HeaderMatchers.isLink(LDP.BasicContainer.stringValue(), "type"))
                 )
@@ -209,20 +213,61 @@ public class LdpWebServiceTest {
                 .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
             .expect()
                 .statusCode(200)
-                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIST header...
+                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIRST header...
                         
HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport";,
 "describedby"),
                         HeaderMatchers.isLink(LDP.Resource.stringValue(), 
"type"),
-                        
HeaderMatchers.isLink(LDP.NonRdfResource.stringValue(), "type"))
+                        HeaderMatchers.isLink(LDP.RdfResource.stringValue(), 
"type"))
                 )
                 .header("ETag", HeaderMatchers.hasEntityTag(true)) // FIXME: 
be more specific here
                 .contentType(RDFFormat.TURTLE.getDefaultMIMEType())
                 
.body(SesameMatchers.rdfStringMatches(RDFFormat.TURTLE.getDefaultMIMEType(), 
baseUrl + newResource,
                         SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource), RDF.TYPE, LDP.Resource),
-                        SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource), RDF.TYPE, LDP.NonRdfResource),
+                        SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource), RDF.TYPE, LDP.RdfResource),
                         SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource), DCTERMS.MODIFIED, null),
                         SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource), DCTERMS.HAS_FORMAT, new URIImpl(baseUrl + newResource + ".png"))
                 ))
             .get(newResource);
+
+        // now the resource hasType
+        RestAssured
+                .given()
+                .header("Accept", RDFFormat.TURTLE.getDefaultMIMEType())
+                .expect()
+                .statusCode(200)
+                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIRST header...
+                        
HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport";,
 "describedby"),
+                        HeaderMatchers.isLink(LDP.Resource.stringValue(), 
"type"),
+                        
HeaderMatchers.isLink(LDP.NonRdfResource.stringValue(), "type"))
+                )
+                .header("ETag", HeaderMatchers.hasEntityTag(true)) // FIXME: 
be more specific here
+                .contentType(RDFFormat.TURTLE.getDefaultMIMEType())
+                
.body(SesameMatchers.rdfStringMatches(RDFFormat.TURTLE.getDefaultMIMEType(), 
baseUrl + newResource+".png",
+                        SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource+".png"), RDF.TYPE, LDP.Resource),
+                        SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource+".png"), RDF.TYPE, LDP.NonRdfResource),
+                        SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource+".png"), DCTERMS.MODIFIED, null),
+                        SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource+".png"), DCTERMS.FORMAT, new LiteralImpl(mimeType)),
+                        SesameMatchers.hasStatement(new URIImpl(baseUrl + 
newResource+".png"), DCTERMS.IS_FORMAT_OF, new URIImpl(baseUrl + newResource))
+                ))
+                .get(newResource + ".png");
+
+        // now check that the data is really there
+        final String expectedMD5 = 
HashUtils.md5sum(LdpWebServiceTest.class.getResourceAsStream("/test.png"));
+        final byte[] data = RestAssured
+            .given()
+                .header("Accept", mimeType)
+            .expect()
+                .statusCode(200)
+                .header("Link", CoreMatchers.anyOf( //TODO: RestAssured only 
checks the FIRST header...
+                        
HeaderMatchers.isLink("http://wiki.apache.org/marmotta/LDPImplementationReport";,
 "describedby"),
+                        HeaderMatchers.isLink(LDP.Resource.stringValue(), 
"type"),
+                        HeaderMatchers.isLink(LDP.RdfResource.stringValue(), 
"type"))
+                )
+                .header("ETag", HeaderMatchers.hasEntityTag(true)) // FIXME: 
be more specific here
+                .contentType(mimeType)
+            .get(newResource + ".png")
+                .body().asByteArray();
+
+        assertEquals("md5sum",expectedMD5, HashUtils.md5sum(data));
     }
 
    @AfterClass

Reply via email to