AuditLogReaderImpl implements AuditLogReader { @Override public InputStream getAuditLog(final String account, final String storeId, final String spaceId) throws AuditLogReaderException { checkEnabled(); this.storageProvider = getStorageProvider(); final String auditBucket = auditConfig.getAuditLogSpaceId(); String prefix = MessageFormat.format("{0}/{1}/{2}/", account, storeId, spaceId); final PipedInputStream is = new PipedInputStream(10 * 1024); final PipedOutputStream os; try { os = new PipedOutputStream(is); } catch (IOException e) { throw new AuditLogReaderException(e); } try { final Iterator<String> it = this.storageProvider.getSpaceContents(auditBucket, prefix); if (!it.hasNext()) { os.write((AuditLogUtil.getHeader() + "\n").getBytes()); os.close(); } new Thread(new Runnable() { @Override public void run() { try { int count = 0; while (it.hasNext()) { String contentId = it.next(); writeToOutputStream(auditBucket, storageProvider, os, count, contentId); count++; } os.close(); } catch (ContentStoreException | IOException ex) { log.error(MessageFormat.format("failed to complete audit log read routine " + "for space: storeId={0}, spaceId={1}", storeId, spaceId), ex); } } }).start(); } catch (StorageException | IOException e) { throw new AuditLogReaderException(e); } return is; } AuditLogReaderImpl(); @Override void initialize(AuditConfig auditConfig); @Override InputStream getAuditLog(final String account, final String storeId, final String spaceId);  }
@Test public void testGetAuditLog() throws IOException, ContentStoreException { String[] file1Lines = {"a", "b", "c", "d"}; String[] file2Lines = {"e", "f", "g", "h"}; String prefix = getPrefix(); final StorageProvider storageProvider = createMock(StorageProvider.class); String contentId1 = "log1"; String contentId2 = "log2"; Iterator<String> it = Arrays.asList(new String[] {prefix + "/" + contentId1, prefix + "/" + contentId2}).iterator(); expect(storageProvider.getSpaceContents(eq(globalAuditSpaceId), eq(prefix))).andReturn(it); AuditConfig config = createMock(AuditConfig.class); mockCheckEnabled(config); expect(config.getAuditLogSpaceId()).andReturn(globalAuditSpaceId); setupGetContentCall(prefix, storageProvider, contentId1, file1Lines); setupGetContentCall(prefix, storageProvider, contentId2, file2Lines); replayAll(); AuditLogReaderImpl auditReader = createAuditLogReader(storageProvider, config); InputStream is = auditReader.getAuditLog(account, storeId, spaceId); assertNotNull(is); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String line = reader.readLine(); assertEquals(file1Lines[0], line); int count = 1; int totalCount = 1; String[] lines = file1Lines; while (true) { line = reader.readLine(); assertEquals(lines[count], line); count++; totalCount++; if (totalCount >= file1Lines.length + file2Lines.length - 1) { break; } else if (lines == file1Lines && count >= file1Lines.length) { count = 1; lines = file2Lines; } } assertNull(reader.readLine()); assertEquals(file1Lines.length + file2Lines.length - 1, totalCount); }
@Test public void testGetLogNotFound() throws IOException, StorageException { String prefix = getPrefix(); expect(storageProvider.getSpaceContents(eq(globalAuditSpaceId), eq(prefix))) .andThrow(new NotFoundException("not found")); expect(config.getAuditLogSpaceId()).andReturn(globalAuditSpaceId); mockCheckEnabled(config); replayAll(); AuditLogReaderImpl auditReader = createAuditLogReader(storageProvider, config); try { auditReader.getAuditLog(account, storeId, spaceId); fail("expected to fail with empty log exception"); } catch (AuditLogReaderException e) { } }
@Test public void testEmptyLog() throws IOException, StorageException { String prefix = getPrefix(); Iterator<String> it = Arrays.asList(new String[] {}).iterator(); expect(storageProvider.getSpaceContents(eq(globalAuditSpaceId), eq(prefix))).andReturn(it); expect(config.getAuditLogSpaceId()).andReturn(globalAuditSpaceId); mockCheckEnabled(config); replayAll(); AuditLogReaderImpl auditReader = createAuditLogReader(storageProvider, config); try { InputStream is = auditReader.getAuditLog(account, storeId, spaceId); BufferedReader buf = new BufferedReader(new InputStreamReader(is)); String line = buf.readLine(); assertEquals(AuditLogUtil.getHeader(), line); assertEquals(-1, buf.read()); } catch (Exception e) { fail("unexpected to fail with empty log exception"); } }
@Test public void testContentFailure() throws IOException, ContentStoreException { String[] file1Lines = {"a", "b"}; String prefix = getPrefix(); String contentId1 = "log1"; String contentId2 = "log2"; Iterator<String> it = Arrays.asList(new String[] {prefix + "/" + contentId1, prefix + "/" + contentId2,}).iterator(); expect(storageProvider.getSpaceContents(eq(globalAuditSpaceId), eq(prefix))).andReturn(it); expect(config.getAuditLogSpaceId()).andReturn(globalAuditSpaceId); setupGetContentCall(prefix, storageProvider, contentId1, file1Lines); setupGetContentCallFailure(prefix, storageProvider, contentId2, null); mockCheckEnabled(config); replayAll(); AuditLogReaderImpl auditReader = createAuditLogReader(storageProvider, config); try { InputStream is = auditReader.getAuditLog(account, storeId, spaceId); sleep(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); assertNotNull(reader.readLine()); assertNotNull(reader.readLine()); reader.readLine(); fail("expected reader exception"); } catch (AuditLogReaderException ex) { fail("did not expect empty log exception"); } catch (IOException ex) { } }