Commits

Sam Adams committed 44fb9e8

Extracted ETagFactory from FileResource

  • Participants
  • Parent commits 7d1267f

Comments (0)

Files changed (3)

File chempound-webapp/src/main/java/net/chempound/webapp/content/ETagFactory.java

+package net.chempound.webapp.content;
+
+import net.chempound.storage.LocalResource;
+
+import javax.inject.Singleton;
+
+/**
+ * @author Sam Adams
+ */
+@Singleton
+public class ETagFactory {
+
+    public String createETag(final LocalResource resource) {
+        final long length = resource.getLength();
+        final long modified = resource.getLastModified().getTime();
+        return Long.toHexString(modified) + '-' + Long.toHexString(length);
+    }
+
+}

File chempound-webapp/src/main/java/net/chempound/webapp/content/FileResource.java

 
     private final TripleStore tripleStore;
     private final ResourceStore resourceStore;
+    private final ETagFactory tagFactory;
 
     private LocalResource resource;
     private MediaType mediaType;
 
     @Inject
-    public FileResource(final TripleStore tripleStore, final ResourceStore resourceStore) {
+    public FileResource(final TripleStore tripleStore, final ResourceStore resourceStore, final ETagFactory tagFactory) {
         this.tripleStore = tripleStore;
         this.resourceStore = resourceStore;
+        this.tagFactory = tagFactory;
     }
 
     @Override
         }
     }
 
+    @Override
+    protected Representation get() {
+        final ResourceRepresentation representation = new ResourceRepresentation(resource, mediaType);
+        representation.setTag(new Tag(tagFactory.createETag(resource)));
+        return representation;
+    }
+
     private MediaType getMediaType() {
         final URI uri = getReference().toUri();
         final String mediaType = tripleStore.getMediaType(uri);
         }
         return MediaType.valueOf(mediaType);
     }
-
-    @Override
-    protected Representation get() {
-        final ResourceRepresentation representation = new ResourceRepresentation(resource, mediaType);
-        final Tag tag = createTag(resource);
-        representation.setTag(tag);
-        return representation;
-    }
-
-    private Tag createTag(final LocalResource resource) {
-        final long length = resource.getLength();
-        final long modified = resource.getLastModified().getTime();
-        final String tag = Long.toHexString(modified) + Long.toHexString(length);
-        return new Tag(tag);
-    }
-
 }

File chempound-webapp/src/test/java/net/chempound/webapp/content/FileResourceTest.java

  */
 public class FileResourceTest {
 
-    private TripleStore tripleStore;
-    private ResourceStore resourceStore;
+    private static final String URL = "http://repo.example.com/content/foo/bar.txt";
+    private static final String CONTENT = "A hollow voice says 'plugh'.";
+    private static final MimeType MIME_TYPE = MimeType.TEXT_PLAIN;
+    private static final MediaType MEDIA_TYPE = MediaType.TEXT_PLAIN;
+    private static final String TAG_STRING = "123-456";
+    private static final Tag ETAG = new Tag(TAG_STRING);
+
+    private FileResource fileResource;
 
     @Before
     public void setUp() throws IOException {
-        InMemoryResource resource = new InMemoryResource("http://repo.example.com/content/foo/bar.txt", MimeType.TEXT_PLAIN, "A hollow voice says 'plugh'.");
-        resourceStore = mock(ResourceStore.class);
-        when(resourceStore.retrieveItem("http://repo.example.com/content/foo/bar.txt")).thenReturn(resource);
-        tripleStore = mock(TripleStore.class);
+        InMemoryResource resource = new InMemoryResource(URL, MIME_TYPE, CONTENT);
+
+        ResourceStore resourceStore = mock(ResourceStore.class);
+        when(resourceStore.retrieveItem(URL)).thenReturn(resource);
+
+        TripleStore tripleStore = mock(TripleStore.class);
+
+        ETagFactory tagFactory = mock(ETagFactory.class);
+        when(tagFactory.createETag(resource)).thenReturn(TAG_STRING);
+
+        fileResource = new FileResource(tripleStore, resourceStore, tagFactory);
     }
 
     @Test
     public void testGetResource() throws IOException {
-        Request request = new Request(Method.GET, "http://repo.example.com/content/foo/bar.txt");
+        Request request = new Request(Method.GET, URL);
         Response response = handle(request);
 
         assertEquals(Status.SUCCESS_OK, response.getStatus());
-        assertEquals(MediaType.TEXT_PLAIN, response.getEntity().getMediaType());
-        assertEquals("A hollow voice says 'plugh'.", response.getEntity().getText());
+        assertEquals(MEDIA_TYPE, response.getEntity().getMediaType());
+        assertEquals(CONTENT, response.getEntity().getText());
     }
 
     @Test
     public void testLastModified() throws IOException {
-        Request request1 = new Request(Method.GET, "http://repo.example.com/content/foo/bar.txt");
+        Request request1 = new Request(Method.GET, URL);
         Response response1 = handle(request1);
 
         assertEquals(Status.SUCCESS_OK, response1.getStatus());
         Date lastModified = response1.getEntity().getModificationDate();
         assertNotNull(lastModified);
 
-
-        Request request2 = new Request(Method.GET, "http://repo.example.com/content/foo/bar.txt");
+        Request request2 = new Request(Method.GET, URL);
         request2.getConditions().setModifiedSince(lastModified);
 
         Response response2 = handle(request2);
     }
 
     @Test
-    public void testETag() throws IOException {
-        Request request1 = new Request(Method.GET, "http://repo.example.com/content/foo/bar.txt");
-        Response response1 = handle(request1);
+    public void testResponseIncludesETag() throws IOException {
+        Request request = new Request(Method.GET, URL);
+        Response response = handle(request);
 
-        assertEquals(Status.SUCCESS_OK, response1.getStatus());
-        Tag tag = response1.getEntity().getTag();
-        assertNotNull(tag);
+        assertEquals(Status.SUCCESS_OK, response.getStatus());
+        assertEquals(ETAG, response.getEntity().getTag());
+    }
 
+    @Test
+    public void testMatchingETagReturnsNotModified() {
+        Request request = new Request(Method.GET, URL);
+        request.getConditions().getNoneMatch().add(ETAG);
 
-        Request request2 = new Request(Method.GET, "http://repo.example.com/content/foo/bar.txt");
-        request2.getConditions().getNoneMatch().add(tag);
-
-        Response response2 = handle(request2);
-        assertEquals(Status.REDIRECTION_NOT_MODIFIED, response2.getStatus());
+        Response response = handle(request);
+        assertEquals(Status.REDIRECTION_NOT_MODIFIED, response.getStatus());
     }
 
     private Response handle(Request request) {
         Response response = new Response(request);
-
-        FileResource resource = new FileResource(tripleStore, resourceStore);
-        resource.init(null, request, response);
-        resource.handle();
+        fileResource.init(null, request, response);
+        fileResource.handle();
         return response;
     }
 
 }
-
-