FileStorageFactory { public static FileStorage getFileStorage(@Nonnull String base, boolean compress) { String key = base + compress; FileStorage ret = storageMap.get(key); if (ret == null) { if (base.startsWith("s3:")) { String s = StringUtils.removeStart(FilenameUtils.normalizeNoEndSeparator(base.substring(3)), "/"); ret = new S3FileStorage(s, compress); } else { ret = new FileSystemFileStorage(base, compress); } storageMap.put(key, ret); } return ret; }  static FileStorage getFileStorage(@Nonnull String base, boolean compress);  }
@Test @Tag(TestGroups.FUNCTIONAL) public void testFileStorage() throws Exception { String s = "This is a test"; File f = new File("target/storage"); if (f.exists() && f.isDirectory()) { FileUtils.deleteDirectory(f); } ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes()); FileStorage storage = FileStorageFactory.getFileStorage(f.getAbsolutePath(), false); FileData fd = new FileData("", "test.txt"); storage.storeFileData(fd, bis); File file = getFile(f.getAbsolutePath(), fd); String fromFile = FileUtils.readFileToString(file, StandardCharsets.UTF_8); assertEquals(s, fromFile); try (InputStream in = storage.readFileData(fd) ) { String fromService = IOUtils.toString(in, StandardCharsets.UTF_8); assertEquals(s, fromService); } assertTrue(storage.exists(fd)); storage.delete(fd); assertTrue(!storage.exists(fd)); }
@Test @Tag(TestGroups.FUNCTIONAL) public void testFileStorageCompressed() throws Exception { String s = "This is a test"; File f = new File("target/storage"); if (f.exists() && f.isDirectory()) { FileUtils.deleteDirectory(f); } FileStorage storage = FileStorageFactory.getFileStorage(f.getAbsolutePath(), true); ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes()); FileData fd = new FileData("", "test.gz"); storage.storeFileData(fd, bis); File file = getFile(f.getAbsolutePath(), fd); try ( InputStream in = new GZIPInputStream(new FileInputStream(file)) ) { String fromFile = IOUtils.toString(in, StandardCharsets.UTF_8); assertEquals(s, fromFile); } try ( InputStream in = storage.readFileData(fd) ) { String fromService = IOUtils.toString(in,StandardCharsets.UTF_8); assertEquals(s, fromService); } assertTrue(storage.exists(fd)); storage.delete(fd); assertTrue(!storage.exists(fd)); }
@Test @Tag(TestGroups.EXPERIMENTAL) public void testS3FileStorage() throws Exception { String s = "This is a test"; ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes()); FileStorage storage = FileStorageFactory.getFileStorage("s3:systemstorage", false); FileData fd = new FileData("", "test.txt"); storage.storeFileData(fd, bis); try ( InputStream in = storage.readFileData(fd) ) { String fromService = IOUtils.toString(in, StandardCharsets.UTF_8); assertEquals(s, fromService); } assertTrue(storage.exists(fd)); storage.delete(fd); assertTrue(!storage.exists(fd)); }
@Test @Tag(TestGroups.EXPERIMENTAL) public void testS3StorageCompressed() throws Exception { String s = "This is a test"; ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes()); FileStorage storage = FileStorageFactory.getFileStorage("s3:systemstorage", true); FileData fd = new FileData("", "test.gz"); storage.storeFileData(fd, bis); try ( InputStream in = storage.readFileData(fd) ) { String fromService = IOUtils.toString(in, StandardCharsets.UTF_8); assertEquals(s, fromService); } assertTrue(storage.exists(fd)); storage.delete(fd); assertTrue(!storage.exists(fd)); }
@Test @Tag(TestGroups.EXPERIMENTAL) public void testS3List() throws Exception { String s = "This is a test"; FileStorage storage = FileStorageFactory.getFileStorage("s3:systemstorage", false); FileData fd = new FileData("", "test1.txt"); ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes()); storage.storeFileData(fd, bis); FileData fd1 = new FileData("", "test2.txt"); bis = new ByteArrayInputStream(s.getBytes()); storage.storeFileData(fd1, bis); FileData fd2 = new FileData("/folder", "test3.txt"); bis = new ByteArrayInputStream(s.getBytes()); storage.storeFileData(fd2, bis); List<FileData> listFileData = storage.listFileData(""); List<FileData> listFileData1 = storage.listFileData("/folder"); storage.delete(fd); storage.delete(fd1); storage.delete(fd2); assertTrue(listFileData.contains(fd)); assertTrue(listFileData.contains(fd1)); assertTrue(!listFileData.contains(fd2)); assertTrue(!listFileData1.contains(fd)); assertTrue(!listFileData1.contains(fd1)); assertTrue(listFileData1.contains(fd2)); assertTrue(!storage.exists(fd)); assertTrue(!storage.exists(fd1)); assertTrue(!storage.exists(fd2)); }
@Test @Tag(TestGroups.EXPERIMENTAL) public void testFileList() throws Exception { String s = "This is a test"; File f = new File("target/storage"); FileUtils.deleteDirectory(f); FileStorage storage = FileStorageFactory.getFileStorage(f.getAbsolutePath(), true); FileData fd = new FileData("", "test1.txt"); ByteArrayInputStream bis = new ByteArrayInputStream(s.getBytes()); storage.storeFileData(fd, bis); FileData fd1 = new FileData("", "test2.txt"); bis = new ByteArrayInputStream(s.getBytes()); storage.storeFileData(fd1, bis); FileData fd2 = new FileData("/folder", "test3.txt"); bis = new ByteArrayInputStream(s.getBytes()); storage.storeFileData(fd2, bis); List<FileData> listFileData = storage.listFileData(""); List<FileData> listFileData1 = storage.listFileData("/folder"); storage.delete(fd); storage.delete(fd1); storage.delete(fd2); assertTrue(listFileData.contains(fd)); assertTrue(listFileData.contains(fd1)); assertTrue(!listFileData.contains(fd2)); assertTrue(!listFileData1.contains(fd)); assertTrue(!listFileData1.contains(fd1)); assertTrue(listFileData1.contains(fd2)); assertTrue(!storage.exists(fd)); assertTrue(!storage.exists(fd1)); assertTrue(!storage.exists(fd2)); }