HerdFileUtils.verifyFileExistsAndReadable(realFile);
/** * Reads a JSON manifest file into a JSON manifest object. * * @param jsonManifestFile the JSON manifest file. * * @return the manifest object. * @throws java.io.IOException if any errors were encountered reading the JSON file. * @throws IllegalArgumentException if the manifest file has validation errors. */ public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException { // Verify that the file exists and can be read. HerdFileUtils.verifyFileExistsAndReadable(jsonManifestFile); // Deserialize the JSON manifest. BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile)); BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8)); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); M manifest = getManifestFromReader(reader, objectMapper); // Validate the manifest and return it. validateManifest(manifest); return manifest; }
/** * Reads a JSON manifest file into a JSON manifest object. * * @param jsonManifestFile the JSON manifest file. * * @return the manifest object. * @throws java.io.IOException if any errors were encountered reading the JSON file. * @throws IllegalArgumentException if the manifest file has validation errors. */ public M readJsonManifest(File jsonManifestFile) throws IOException, IllegalArgumentException { // Verify that the file exists and can be read. HerdFileUtils.verifyFileExistsAndReadable(jsonManifestFile); // Deserialize the JSON manifest. BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(jsonManifestFile)); BufferedReader reader = new BufferedReader(new InputStreamReader(buffer, Charsets.UTF_8)); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY); objectMapper.setVisibility(PropertyAccessor.GETTER, Visibility.NONE); objectMapper.setVisibility(PropertyAccessor.SETTER, Visibility.NONE); M manifest = getManifestFromReader(reader, objectMapper); // Validate the manifest and return it. validateManifest(manifest); return manifest; }
@Test public void testVerifyFileExistsAndReadable() throws IOException { File testFile = createLocalFile(localTempPath.toString(), "SOME_FILE", FILE_SIZE_1_KB); HerdFileUtils.verifyFileExistsAndReadable(testFile); }
@Test public void testVerifyFileExistsAndReadableFileNoExists() throws IOException { File testFile = new File("I_DO_NOT_EXIST"); try { HerdFileUtils.verifyFileExistsAndReadable(testFile); fail("Should throw an IllegalArgumentException when file does not exist."); } catch (IllegalArgumentException e) { assertEquals(String.format("File \"%s\" doesn't exist.", testFile.getName()), e.getMessage()); } }
@Test public void testVerifyFileExistsAndReadableFileIsDirectory() throws IOException { File testDirectory = localTempPath.toFile(); try { HerdFileUtils.verifyFileExistsAndReadable(testDirectory); fail("Should throw an IllegalArgumentException when argument is a directory."); } catch (IllegalArgumentException e) { assertEquals(String.format("File \"%s\" is not a valid file that can be read as a manifest. Is it a directory?", testDirectory.getName()), e.getMessage()); } }
@Test public void testVerifyFileExistsAndReadableFileNotReadable() throws IOException { File testFile = createLocalFile(localTempPath.toString(), "SOME_FILE", FILE_SIZE_1_KB); if (testFile.setReadable(false)) { try { HerdFileUtils.verifyFileExistsAndReadable(testFile); fail("Should throw an IllegalArgumentException when file is not readable."); } catch (IllegalArgumentException e) { assertEquals(String.format("Unable to read file \"%s\". Check permissions.", testFile.getName()), e.getMessage()); } } }