/** * Spy on the {@link LongTermArchivingHFileCleaner} to ensure we can catch when the cleaner has * seen all the files * @return a {@link CountDownLatch} to wait on that releases when the cleaner has been called at * least the expected number of times. */ private CountDownLatch setupCleanerWatching(LongTermArchivingHFileCleaner cleaner, List<BaseHFileCleanerDelegate> cleaners, final int expected) { // replace the cleaner with one that we can can check BaseHFileCleanerDelegate delegateSpy = Mockito.spy(cleaner); final int[] counter = new int[] { 0 }; final CountDownLatch finished = new CountDownLatch(1); Mockito.doAnswer(new Answer<Iterable<FileStatus>>() { @Override public Iterable<FileStatus> answer(InvocationOnMock invocation) throws Throwable { counter[0]++; LOG.debug(counter[0] + "/ " + expected + ") Wrapping call to getDeletableFiles for files: " + invocation.getArgument(0)); @SuppressWarnings("unchecked") Iterable<FileStatus> ret = (Iterable<FileStatus>) invocation.callRealMethod(); if (counter[0] >= expected) finished.countDown(); return ret; } }).when(delegateSpy).getDeletableFiles(Mockito.anyListOf(FileStatus.class)); cleaners.set(0, delegateSpy); return finished; }
public void convertRecordTest() { this.hivePurgerConverterMock.convertRecord(this.schemaMock, this.datasetMock, this.stateMock); Mockito.verify(this.datasetMock).setPurgeQueries(Mockito.anyListOf(String.class)); } }
@Test public void onUserResponse() throws Exception { Map<Integer, EndpointAccessToken> attachingEPs = new HashMap<>(); attachingEPs.put(REQUEST_ID_1, new EndpointAccessToken("token1")); attachingEPs.put(REQUEST_ID_2, new EndpointAccessToken("token2")); Map<Integer, EndpointKeyHash> dettachingEPs = new HashMap<>(); dettachingEPs.put(REQUEST_ID_1, new EndpointKeyHash("keyhash1")); dettachingEPs.put(REQUEST_ID_2, new EndpointKeyHash("keyhash2")); KaaClientState clientState = Mockito.mock(KaaClientState.class); EndpointRegistrationProcessor processor = Mockito.mock(EndpointRegistrationProcessor.class); Mockito.when(processor.getAttachEndpointRequests()).thenReturn(attachingEPs); Mockito.when(processor.getDetachEndpointRequests()).thenReturn(dettachingEPs); UserTransport transport = new DefaultUserTransport(); UserSyncResponse response1 = new UserSyncResponse(); response1.setEndpointAttachResponses(Arrays.asList( new EndpointAttachResponse(REQUEST_ID_1, "keyhash1", SyncResponseResultType.SUCCESS), new EndpointAttachResponse(REQUEST_ID_2, "keyhash2", SyncResponseResultType.SUCCESS), new EndpointAttachResponse(REQUEST_ID_1 + 1, "keyhash2", SyncResponseResultType.FAILURE))); response1.setEndpointDetachResponses(Arrays.asList( new EndpointDetachResponse(REQUEST_ID_1, SyncResponseResultType.SUCCESS), new EndpointDetachResponse(REQUEST_ID_1 + 2, SyncResponseResultType.FAILURE))); transport.setEndpointRegistrationProcessor(processor); transport.setClientState(clientState); transport.onUserResponse(response1); Mockito.verify(processor, Mockito.times(1)).onUpdate(Mockito.anyListOf(EndpointAttachResponse.class), Mockito.anyListOf(EndpointDetachResponse.class), Mockito.any(UserAttachResponse.class), Mockito.any(UserAttachNotification.class), Mockito.any(UserDetachNotification.class)); Mockito.verify(clientState, Mockito.times(1)).setAttachedEndpointsList(Mockito.anyMap()); }
lDrvInp.abort(); LockException lEx = new LockException(ErrorMsg.LOCK_ACQUIRE_CANCELLED.getMsg()); when(mockLockManager.lock(anyListOf(HiveLockObj.class), eq(false), eq(lDrvState))).thenReturn(expectedLocks); when(mockLockManager.lock(anyListOf(HiveLockObj.class), eq(false), eq(lDrvInp))).thenThrow(lEx); doNothing().when(mockLockManager).setContext(any(HiveLockManagerCtx.class)); doNothing().when(mockLockManager).close();
@Test public void testDeleteAsyncWithSkipCustomListeners() { when(runtimeServiceMock.deleteProcessInstancesAsync(anyListOf(String.class), any(ProcessInstanceQuery.class), anyString(), anyBoolean(), anyBoolean())).thenReturn(new BatchEntity()); Map<String, Object> messageBodyJson = new HashMap<String, Object>(); messageBodyJson.put("deleteReason", TEST_DELETE_REASON); messageBodyJson.put("processInstanceIds", Arrays.asList("processInstanceId1", "processInstanceId2")); messageBodyJson.put("skipCustomListeners", true); given() .contentType(ContentType.JSON).body(messageBodyJson) .then().expect() .statusCode(Status.OK.getStatusCode()) .when().post(DELETE_PROCESS_INSTANCES_ASYNC_URL); verify(runtimeServiceMock).deleteProcessInstancesAsync(anyListOf(String.class), Mockito.any(ProcessInstanceQuery.class), Mockito.eq(TEST_DELETE_REASON), Mockito.eq(true), Mockito.eq(false)); }
@Test public void testSubmitAllRecordsWithPartialFailures() { for (int i = 0; i < 10; i++) { recorder.saveRecord(randomBytes(1024), STREAM_NAME); } Mockito.when(sender.sendBatch(Mockito.anyString(), Mockito.anyListOf(byte[].class))) // one of the records fails, but succeeds the next time .thenReturn(Arrays.asList(randomBytes(1024))) .thenReturn(new ArrayList<byte[]>()); recorder.submitAllRecords(); assertEquals("records removed", 0, recorder.getDiskBytesUsed()); }
@Test public void testDeleteAsyncWithQuery() { Map<String, Object> messageBodyJson = new HashMap<String, Object>(); messageBodyJson.put("deleteReason", TEST_DELETE_REASON); ProcessInstanceQueryDto query = new ProcessInstanceQueryDto(); messageBodyJson.put("processInstanceQuery", query); when(runtimeServiceMock.deleteProcessInstancesAsync(anyListOf(String.class), any(ProcessInstanceQuery.class), anyString(), anyBoolean(), anyBoolean())).thenReturn(new BatchEntity()); given() .contentType(ContentType.JSON).body(messageBodyJson) .then().expect() .statusCode(Status.OK.getStatusCode()) .when().post(DELETE_PROCESS_INSTANCES_ASYNC_URL); verify(runtimeServiceMock, times(1)).deleteProcessInstancesAsync( anyListOf(String.class), Mockito.any(ProcessInstanceQuery.class), Mockito.eq(TEST_DELETE_REASON), Mockito.eq(false), Mockito.eq(false)); }
@Test public void shouldBackupMultipleTypes() throws Exception { mockFetch(SMS, 1); mockFetch(MMS, 2); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenReturn(folder); when(converter.convertMessages(any(Cursor.class), any(DataType.class))).thenReturn(result(SMS, 1)); BackupState finalState = task.doInBackground(getBackupConfig(EnumSet.of(SMS, MMS))); assertThat(finalState.currentSyncedItems).isEqualTo(3); verify(folder, times(3)).appendMessages(anyListOf(Message.class)); }
@Test public void testDeleteAsyncWithSkipSubprocesses() { when(runtimeServiceMock.deleteProcessInstancesAsync(anyListOf(String.class), any(ProcessInstanceQuery.class), anyString(), anyBoolean(), anyBoolean())).thenReturn(new BatchEntity()); Map<String, Object> messageBodyJson = new HashMap<String, Object>(); messageBodyJson.put("deleteReason", TEST_DELETE_REASON); messageBodyJson.put("processInstanceIds", Arrays.asList("processInstanceId1", "processInstanceId2")); messageBodyJson.put("skipSubprocesses", true); given() .contentType(ContentType.JSON).body(messageBodyJson) .then().expect() .statusCode(Status.OK.getStatusCode()) .when().post(DELETE_PROCESS_INSTANCES_ASYNC_URL); verify(runtimeServiceMock).deleteProcessInstancesAsync( anyListOf(String.class), Mockito.any(ProcessInstanceQuery.class), Mockito.eq(TEST_DELETE_REASON), Mockito.eq(false), Mockito.eq(true)); }
@Test public void shouldBackupItems() throws Exception { mockFetch(SMS, 1); when(converter.convertMessages(any(Cursor.class), eq(SMS))).thenReturn(result(SMS, 1)); when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenReturn(folder); BackupState finalState = task.doInBackground(config); verify(folder).appendMessages(anyListOf(Message.class)); verify(service).transition(SmsSyncState.LOGIN, null); verify(service).transition(SmsSyncState.CALC, null); assertThat(finalState).isNotNull(); assertThat(finalState.isFinished()).isTrue(); assertThat(finalState.currentSyncedItems).isEqualTo(1); assertThat(finalState.itemsToSync).isEqualTo(1); assertThat(finalState.backupType).isEqualTo(config.backupType); }
@Test public void testDeleteAsync() { List<String> ids = Arrays.asList(MockProvider.EXAMPLE_PROCESS_INSTANCE_ID); when(runtimeServiceMock.deleteProcessInstancesAsync(anyListOf(String.class), any(ProcessInstanceQuery.class), anyString(), anyBoolean(), anyBoolean())).thenReturn(new BatchEntity()); Map<String, Object> messageBodyJson = new HashMap<String, Object>(); messageBodyJson.put("processInstanceIds", ids); messageBodyJson.put("deleteReason", TEST_DELETE_REASON); given() .contentType(ContentType.JSON).body(messageBodyJson) .then().expect() .statusCode(Status.OK.getStatusCode()) .when().post(DELETE_PROCESS_INSTANCES_ASYNC_URL); verify(runtimeServiceMock, times(1)).deleteProcessInstancesAsync(ids, null, TEST_DELETE_REASON, false, false); }
@Test public void testSubmitAllRecordsWithPartialFailuresExceedsMaxRetry() { for (int i = 0; i < 10; i++) { recorder.saveRecord(randomBytes(1024), STREAM_NAME); } Mockito.when(sender.sendBatch(Mockito.anyString(), Mockito.anyListOf(byte[].class))) // one of the records always failes .thenReturn(Arrays.asList(randomBytes(1024))); recorder.submitAllRecords(); assertTrue("records not removed", recorder.getDiskBytesUsed() > 0); }
@Test public void testSetRetriesByProcessAsyncHistoricQueryBasedWithProcessInstanceIds() { Batch batchEntity = MockProvider.createMockBatch(); when(mockManagementService.setJobRetriesAsync( anyListOf(String.class), eq((ProcessInstanceQuery) null), anyInt()) ).thenReturn(batchEntity); SetJobRetriesByProcessDto body = new SetJobRetriesByProcessDto(); body.setRetries(MockProvider.EXAMPLE_JOB_RETRIES); body.setProcessInstances(Arrays.asList(MockProvider.EXAMPLE_PROCESS_INSTANCE_ID)); given() .contentType(ContentType.JSON).body(body) .then().expect() .statusCode(Status.OK.getStatusCode()) .when().post(SET_JOB_RETRIES_ASYNC_HIST_QUERY_URL); verify(mockManagementService, times(1)).setJobRetriesAsync( eq(Arrays.asList(MockProvider.EXAMPLE_PROCESS_INSTANCE_ID)), eq((ProcessInstanceQuery) null), eq(MockProvider.EXAMPLE_JOB_RETRIES)); }
@Test public void testSetRetriesByProcessAsyncWithQuery() { Batch batchEntity = MockProvider.createMockBatch(); when(mockManagementService.setJobRetriesAsync( anyListOf(String.class), any(ProcessInstanceQuery.class), anyInt()) ).thenReturn(batchEntity); Map<String, Object> messageBodyJson = new HashMap<String, Object>(); messageBodyJson.put(RETRIES, 5); HistoricProcessInstanceQueryDto query = new HistoricProcessInstanceQueryDto(); messageBodyJson.put("processInstanceQuery", query); Response response = given() .contentType(ContentType.JSON).body(messageBodyJson) .then().expect() .statusCode(Status.OK.getStatusCode()) .when().post(SET_JOB_RETRIES_ASYNC_URL); verifyBatchJson(response.asString()); verify(mockManagementService, times(1)).setJobRetriesAsync( eq((List<String>) null), any(ProcessInstanceQuery.class), Mockito.eq(5)); }
@Test public void testSetRetriesByProcessWithNegativeRetries() { doThrow(new BadUserRequestException("retries are negative")) .when(mockManagementService).setJobRetriesAsync( anyListOf(String.class), any(ProcessInstanceQuery.class), eq(-1)); Map<String, Object> messageBodyJson = new HashMap<String, Object>(); messageBodyJson.put(RETRIES, -1); HistoricProcessInstanceQueryDto query = new HistoricProcessInstanceQueryDto(); messageBodyJson.put("processInstanceQuery", query); given() .contentType(ContentType.JSON).body(messageBodyJson) .then().expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .when().post(SET_JOB_RETRIES_ASYNC_URL); }
@Test public void testSubmitAllRecords() { for (int i = 0; i < 10; i++) { recorder.saveRecord(randomBytes(1024), STREAM_NAME); } String anotherStream = "another_stream"; for (int i = 0; i < 10; i++) { recorder.saveRecord(randomBytes(1024), anotherStream); } Mockito.when(sender.sendBatch(Mockito.anyString(), Mockito.anyListOf(byte[].class))) .thenReturn(new ArrayList<byte[]>()); recorder.submitAllRecords(); assertEquals("no records after submitAllRecords", 0, recorder.getDiskBytesUsed()); }
@Test public void testSubmitAllRecordsWithNonRecoverableFailures() { for (int i = 0; i < 10; i++) { recorder.saveRecord(randomBytes(1024), STREAM_NAME); } AmazonServiceException ase = new AmazonServiceException("some failures"); Mockito.when(sender.sendBatch(Mockito.anyString(), Mockito.anyListOf(byte[].class))) .thenThrow(ase); Mockito.when(sender.isRecoverable(ase)).thenReturn(false); try { recorder.submitAllRecords(); fail("Should throw exception"); } catch (AmazonClientException ace) { assertSame("same exception", ase, ace); } assertEquals("records removed", 0, recorder.getDiskBytesUsed()); }
@Test public void testSetRetriesByProcessAsyncHistoricQueryBasedWithNegativeRetries() { doThrow(new BadUserRequestException("retries are negative")) .when(mockManagementService).setJobRetriesAsync( anyListOf(String.class), eq((ProcessInstanceQuery) null), eq(MockProvider.EXAMPLE_NEGATIVE_JOB_RETRIES)); HistoricProcessInstanceQuery mockedHistoricProcessInstanceQuery = mock(HistoricProcessInstanceQuery.class); when(historyServiceMock.createHistoricProcessInstanceQuery()).thenReturn(mockedHistoricProcessInstanceQuery); List<HistoricProcessInstance> historicProcessInstances = MockProvider.createMockRunningHistoricProcessInstances(); when(mockedHistoricProcessInstanceQuery.list()).thenReturn(historicProcessInstances); SetJobRetriesByProcessDto body = new SetJobRetriesByProcessDto(); body.setRetries(MockProvider.EXAMPLE_NEGATIVE_JOB_RETRIES); body.setHistoricProcessInstanceQuery(new HistoricProcessInstanceQueryDto()); given() .contentType(ContentType.JSON).body(body) .then().expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .when().post(SET_JOB_RETRIES_ASYNC_HIST_QUERY_URL); }
@Test public void testDeleteAsyncHistoricQueryBasedWithoutQueryAndWithoutProcessInstanceIds() { doThrow(new BadUserRequestException("processInstanceIds is empty")) .when(runtimeServiceMock).deleteProcessInstancesAsync( anyListOf(String.class), eq((ProcessInstanceQuery) null), anyString(), anyBoolean(), anyBoolean()); given() .contentType(ContentType.JSON).body(new DeleteProcessInstancesDto()) .then().expect() .statusCode(Status.BAD_REQUEST.getStatusCode()) .when().post(DELETE_PROCESS_INSTANCES_ASYNC_HIST_QUERY_URL); verify(runtimeServiceMock, times(1)).deleteProcessInstancesAsync( new ArrayList<String>(), null, null, false, false); }
@Test public void testSubmitAllRecordsWithRecoverableFailures() { for (int i = 0; i < 10; i++) { recorder.saveRecord(randomBytes(1024), STREAM_NAME); } long size = recorder.getDiskBytesUsed(); AmazonServiceException ase = new AmazonServiceException("some failures"); Mockito.when(sender.sendBatch(Mockito.anyString(), Mockito.anyListOf(byte[].class))) .thenThrow(ase); Mockito.when(sender.isRecoverable(ase)).thenReturn(true); try { recorder.submitAllRecords(); fail("Should throw exception"); } catch (AmazonClientException ace) { assertSame("same exception", ase, ace); } assertEquals("no records sent", size, recorder.getDiskBytesUsed()); }