LogTailDelegator extends BaseDelegator implements ILogTailDelegator { @Override public ILogTail getLogTail(final LogTailOptions opts) throws P4JavaException { List<Map<String, Object>> resultMaps = execMapCmdList(LOGTAIL, processParameters(opts, server), null); String logFile = null; long offset = -1; List<String> data = new ArrayList<>(); if (nonNull(resultMaps)) { for (Map<String, Object> map : resultMaps) { if (nonNull(map)) { ResultMapParser.handleErrors(ResultMapParser.toServerMessage(map)); try { if (map.containsKey(FILE)) { logFile = parseString(map, FILE); } if (map.containsKey(DATA)) { data.add(parseString(map, DATA)); } if (map.containsKey(OFFSET)) { offset = parseLong(map, OFFSET); } } catch (Throwable thr) { Log.exception(thr); } } } } if (isNotBlank(logFile) && !data.isEmpty() && offset > -1) { return new LogTail(logFile, offset, data); } return null; } LogTailDelegator(final IOptionsServer server); @Override ILogTail getLogTail(final LogTailOptions opts);  }
@Test(expected = ConnectionException.class) public void testLogTailConnectionException() throws P4JavaException { setUpException(ConnectionException.class); logTailDelegator.getLogTail(new LogTailOptions()); }
@Test(expected = AccessException.class) public void testLogTailAccessException() throws P4JavaException { setUpException(AccessException.AccessExceptionForTests.class); logTailDelegator.getLogTail(new LogTailOptions()); }
@Test(expected = RequestException.class) public void testLogTailRequestException() throws P4JavaException { setUpException(RequestException.class); logTailDelegator.getLogTail(new LogTailOptions()); }
@Test(expected = P4JavaException.class) public void testLogTailP4JavaException() throws P4JavaException { setUpException(P4JavaException.class); logTailDelegator.getLogTail(new LogTailOptions()); }
@Test public void testLogTail() throws P4JavaException { when(server.execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null))) .thenReturn(buildValidResultMap(OFFSET)); ILogTail logTail = logTailDelegator.getLogTail(new LogTailOptions()); verify(server).execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null)); assertLogTail(logTail); }
@Test public void testLogTailOptions() throws P4JavaException { LogTailOptions options = new LogTailOptions().setBlockSize(1L).setMaxBlocks(1).setStartingOffset(2L); when(server.execMapCmdList(eq(LOGTAIL.toString()), argThat(OPTION_MATCHER), eq(null))) .thenReturn(buildValidResultMap(OFFSET)); ILogTail logTail = logTailDelegator.getLogTail(options); verify(server).execMapCmdList(eq(LOGTAIL.toString()), argThat(OPTION_MATCHER), eq(null)); assertLogTail(logTail); }
@Test(expected = RequestException.class) public void testLogTailError() throws P4JavaException { when(server.execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null))) .thenReturn(buildErrorResultMap()); ILogTail logTail = logTailDelegator.getLogTail(new LogTailOptions()); verify(server).execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null)); assertLogTail(logTail); }
@Test public void testLogTailLowOffset() throws P4JavaException { when(server.execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null))) .thenReturn(buildValidResultMap(LOW_OFFSET)); ILogTail logTail = logTailDelegator.getLogTail(new LogTailOptions()); verify(server).execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null)); assertNull(logTail); }
@Test public void testLogTailNullResults() throws P4JavaException { when(server.execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null))) .thenReturn(null); ILogTail logTail = logTailDelegator.getLogTail(new LogTailOptions()); verify(server).execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null)); assertNull(logTail); }
@Test public void testLogTailEmptyResults() throws P4JavaException { when(server.execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null))) .thenReturn(new ArrayList<>()); ILogTail logTail = logTailDelegator.getLogTail(new LogTailOptions()); verify(server).execMapCmdList(eq(LOGTAIL.toString()), argThat(EMPTY_MATCHER), eq(null)); assertNull(logTail); }