private static ClusterClient<String> createClusterClient(String expectedResponse) throws Exception { final ClusterClient<String> clusterClient = mock(ClusterClient.class); when(clusterClient.triggerSavepoint(any(JobID.class), nullable(String.class))) .thenReturn(CompletableFuture.completedFuture(expectedResponse)); return clusterClient; }
private OutputCommitter setupOutputCommitter(boolean needsTaskCommit) throws IOException { OutputCommitter outputCommitter = Mockito.mock(OutputCommitter.class); when(outputCommitter.needsTaskCommit(nullable(TaskAttemptContext.class))).thenReturn(needsTaskCommit); doNothing().when(outputCommitter).commitTask(any(TaskAttemptContext.class)); return outputCommitter; }
private static ClusterClient<String> createFailingClusterClient(Exception expectedException) throws Exception { final ClusterClient<String> clusterClient = mock(ClusterClient.class); when(clusterClient.triggerSavepoint(any(JobID.class), nullable(String.class))) .thenReturn(FutureUtils.completedExceptionally(expectedException)); return clusterClient; } }
private void setupNicknameContactProvider(String[]... contactsWithNickname) { MatrixCursor cursor = new MatrixCursor(PROJECTION_NICKNAME); for (String[] contact : contactsWithNickname) { cursor.addRow(contact); } when(contentResolver .query(eq(ContactsContract.Data.CONTENT_URI), aryEq(PROJECTION_NICKNAME), nullable(String.class), nullable(String[].class), nullable(String.class))).thenReturn(cursor); }
@Test public void testCloseWithTaskCommit() throws Exception { OutputFormat<String, Long> dummyOutputFormat = mock(DummyOutputFormat.class); DummyOutputCommitter outputCommitter = mock(DummyOutputCommitter.class); when(outputCommitter.needsTaskCommit(nullable(TaskAttemptContext.class))).thenReturn(true); DummyRecordWriter recordWriter = mock(DummyRecordWriter.class); JobConf jobConf = mock(JobConf.class); HadoopOutputFormat<String, Long> outputFormat = new HadoopOutputFormat<>(dummyOutputFormat, jobConf); outputFormat.recordWriter = recordWriter; outputFormat.outputCommitter = outputCommitter; outputFormat.close(); verify(recordWriter, times(1)).close(nullable(Reporter.class)); verify(outputCommitter, times(1)).commitTask(nullable(TaskAttemptContext.class)); }
@Test public void testOpen() throws Exception { OutputFormat<String, Long> dummyOutputFormat = mock(DummyOutputFormat.class); DummyOutputCommitter outputCommitter = mock(DummyOutputCommitter.class); JobConf jobConf = Mockito.spy(new JobConf()); when(jobConf.getOutputCommitter()).thenReturn(outputCommitter); HadoopOutputFormat<String, Long> outputFormat = new HadoopOutputFormat<>(dummyOutputFormat, jobConf); outputFormat.open(1, 1); verify(jobConf, times(2)).getOutputCommitter(); verify(outputCommitter, times(1)).setupJob(any(JobContext.class)); verify(dummyOutputFormat, times(1)).getRecordWriter(nullable(FileSystem.class), any(JobConf.class), anyString(), any(Progressable.class)); }
@Test public void testOpen() throws Exception { DummyInputFormat inputFormat = mock(DummyInputFormat.class); when(inputFormat.createRecordReader(nullable(InputSplit.class), any(TaskAttemptContext.class))).thenReturn(new DummyRecordReader()); HadoopInputSplit inputSplit = mock(HadoopInputSplit.class); HadoopInputFormat<String, Long> hadoopInputFormat = setupHadoopInputFormat(inputFormat, Job.getInstance(), null); hadoopInputFormat.open(inputSplit); verify(inputFormat, times(1)).createRecordReader(nullable(InputSplit.class), any(TaskAttemptContext.class)); assertThat(hadoopInputFormat.fetched, is(false)); }
private Message buildLargeNewMessage() { Message message = mock(Message.class); when(message.olderThan(nullable(Date.class))).thenReturn(false); when(message.getSize()).thenReturn((long) (MAXIMUM_SMALL_MESSAGE_SIZE + 1)); return message; }
private Message buildSmallNewMessage() { Message message = mock(Message.class); when(message.olderThan(nullable(Date.class))).thenReturn(false); when(message.getSize()).thenReturn((long) MAXIMUM_SMALL_MESSAGE_SIZE); return message; }
@Test public void testCloseWithNeedsTaskCommitFalse() throws Exception { RecordWriter<String, Long> recordWriter = Mockito.mock(DummyRecordWriter.class); OutputCommitter outputCommitter = setupOutputCommitter(false); HadoopOutputFormat<String, Long> hadoopOutputFormat = setupHadoopOutputFormat(new DummyOutputFormat(), Job.getInstance(), recordWriter, outputCommitter, new Configuration()); hadoopOutputFormat.close(); verify(outputCommitter, times(0)).commitTask(nullable(TaskAttemptContext.class)); verify(recordWriter, times(1)).close(nullable(TaskAttemptContext.class)); }
@Test public void testFetchNext() throws IOException { DummyRecordReader recordReader = mock(DummyRecordReader.class); when(recordReader.next(nullable(String.class), nullable(Long.class))).thenReturn(true); DummyInputFormat inputFormat = mock(DummyInputFormat.class); when(inputFormat.getRecordReader(any(InputSplit.class), any(JobConf.class), any(Reporter.class))).thenReturn(recordReader); HadoopInputFormat<String, Long> hadoopInputFormat = new HadoopInputFormat<>(inputFormat, String.class, Long.class, new JobConf()); hadoopInputFormat.open(getHadoopInputSplit()); hadoopInputFormat.fetchNext(); verify(recordReader, times(1)).next(nullable(String.class), anyLong()); assertThat(hadoopInputFormat.hasNext, is(true)); assertThat(hadoopInputFormat.fetched, is(true)); }
@Test public void testWriteRecord() throws Exception { RecordWriter<String, Long> recordWriter = mock(DummyRecordWriter.class); HadoopOutputFormat<String, Long> hadoopOutputFormat = setupHadoopOutputFormat(new DummyOutputFormat(), Job.getInstance(), recordWriter, null, new Configuration()); hadoopOutputFormat.writeRecord(new Tuple2<String, Long>()); verify(recordWriter, times(1)).write(nullable(String.class), nullable(Long.class)); }
@Test public void testCloseWithNeedsTaskCommitTrue() throws Exception { RecordWriter<String, Long> recordWriter = Mockito.mock(DummyRecordWriter.class); OutputCommitter outputCommitter = setupOutputCommitter(true); HadoopOutputFormat<String, Long> hadoopOutputFormat = setupHadoopOutputFormat(new DummyOutputFormat(), Job.getInstance(), recordWriter, outputCommitter, new Configuration()); hadoopOutputFormat.close(); verify(outputCommitter, times(1)).commitTask(nullable(TaskAttemptContext.class)); verify(recordWriter, times(1)).close(nullable(TaskAttemptContext.class)); }
@Test(expected = SQLException.class) public void testInsertFails() { SQLiteDatabase database = mock(SQLiteDatabase.class); when(database.insert(anyString(), nullable(String.class), any(ContentValues.class))).thenReturn(-1L); when(database.insertWithOnConflict(anyString(), nullable(String.class), any(ContentValues.class), anyInt())).thenThrow(SQLException.class); manager().getJobStorage().injectDatabase(database); DummyJobs.createOneOff().schedule(); }
@Test public void searchRemoteMessagesSynchronous_shouldNotifyOnFailure() throws Exception { setupRemoteSearch(); when(backend.search(anyString(), anyString(), nullable(Set.class), nullable(Set.class))) .thenThrow(new MessagingException("Test")); controller.searchRemoteMessagesSynchronous(ACCOUNT_UUID, FOLDER_NAME, "query", reqFlags, forbiddenFlags, listener); verify(listener).remoteSearchFailed(null, "Test"); }
@Test public void sync_shouldFetchUnsynchronizedMessagesListAndFlags() throws Exception { messageCountInRemoteFolder(1); hasUnsyncedRemoteMessage(); when(remoteFolder.supportsFetchingFlags()).thenReturn(true); imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); verify(remoteFolder, atLeastOnce()).fetch(any(List.class), fetchProfileCaptor.capture(), nullable(MessageRetrievalListener.class)); assertTrue(fetchProfileCaptor.getAllValues().get(0).contains(FetchProfile.Item.FLAGS)); assertTrue(fetchProfileCaptor.getAllValues().get(0).contains(FetchProfile.Item.ENVELOPE)); assertEquals(2, fetchProfileCaptor.getAllValues().get(0).size()); }
@Test public void fetch_withBodySaneFetchProfile_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.BODY_SANE); when(storeConfig.getMaximumAutoDownloadMessageSize()).thenReturn(4096); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[]<0.4096>)", false); }
@Test public void fetch_withBodySaneFetchProfileAndNoMaximumDownloadSize_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.BODY_SANE); when(storeConfig.getMaximumAutoDownloadMessageSize()).thenReturn(0); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[])", false); }
@Test public void fetch_withStructureFetchProfile_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.STRUCTURE); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODYSTRUCTURE)", false); }
@Test public void fetchPart_withNonTextSection_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); ImapMessage message = createImapMessage("1"); Part part = createPart("1.1"); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); folder.fetchPart(message, part, null, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[1.1])", false); }