FileLocator { Optional<FileResult> find(String path, List<String> accept) { Optional<FileResult> result = findExactMatch(path); if (!result.isPresent()) { result = findWithExtension(path, accept); } return result; } FileLocator(File rootDir, Map<String, String> mimeByFileExtension);   }
@Test public void canFindFileExactMatch() { Optional<FileLocator.FileResult> result = fileLocator.find("hello", emptyList()); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("hello").toFile(), result.get().file); assertEquals(singletonList("application/octet-stream"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); }
@Test public void canFindFileExactMatchInSubDirectory() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/p2/hello", emptyList()); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/p2/hello").toFile(), result.get().file); assertEquals(singletonList("application/octet-stream"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); }
@Test public void canFindFileExactMatchWithExtensionInSubDirectory() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/hello.json", emptyList()); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/hello.json").toFile(), result.get().file); assertEquals(singletonList("application/json"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); result = fileLocator.find("p1/hello.xml", emptyList()); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/hello.xml").toFile(), result.get().file); assertEquals(singletonList("text/xml"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); }
@Test public void canFindFileWithNonExactMatch() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/hello", emptyList()); assertTrue(result.isPresent()); File jsonFile = rootDir.resolve("p1/hello.json").toFile(); File xmlFile = rootDir.resolve("p1/hello.xml").toFile(); File actualFile = result.get().file; if (actualFile.equals(jsonFile)) { assertEquals(singletonList("application/json"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); } else if (actualFile.equals(xmlFile)) { assertEquals(singletonList("text/xml"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); } else { fail("Found file is not as expected: " + actualFile); } }
@Test public void canFindFileUsingAcceptHeader() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/hello", singletonList("application/json")); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/hello.json").toFile(), result.get().file); assertEquals(singletonList("application/json"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); result = fileLocator.find("p1/hello", singletonList("text/xml")); assertTrue(result.isPresent()); assertEquals(rootDir.resolve("p1/hello.xml").toFile(), result.get().file); assertEquals(singletonList("text/xml"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); }
@Test public void resourceIsReturnedIfAvailableEvenIfNotMatchingAcceptHeader() { Optional<FileLocator.FileResult> result = fileLocator.find("p1/hello", singletonList("image/gif")); assertTrue(result.isPresent()); File jsonFile = rootDir.resolve("p1/hello.json").toFile(); File xmlFile = rootDir.resolve("p1/hello.xml").toFile(); File actualFile = result.get().file; if (actualFile.equals(jsonFile)) { assertEquals(singletonList("application/json"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); } else if (actualFile.equals(xmlFile)) { assertEquals(singletonList("text/xml"), result.get().fileHttpHeaders.get("Content-Type")); assertTimestampIsWithinSecondsAgo(result.get().fileHttpHeaders.getFirst("Last-Modified").orElse("none")); } else { fail("Found file is not as expected: " + actualFile); } }
@Test public void cannotFindFileThatDoesNotMatch() { Optional<FileLocator.FileResult> result = fileLocator.find("hello.json", emptyList()); assertFalse(result.isPresent()); result = fileLocator.find("p1/hello.json.json", emptyList()); assertFalse(result.isPresent()); result = fileLocator.find("p1/hello.xml.json", emptyList()); assertFalse(result.isPresent()); result = fileLocator.find("does/not/exist", emptyList()); assertFalse(result.isPresent()); }