private Dispatcher createDispatcher(ExecutorService service, boolean scansNetworkChanges) { when(connectivityManager.getActiveNetworkInfo()).thenReturn(scansNetworkChanges ? mock(NetworkInfo.class) : null); when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager); when(context.checkCallingOrSelfPermission(anyString())).thenReturn( scansNetworkChanges ? PERMISSION_GRANTED : PERMISSION_DENIED); return new Dispatcher(context, service, new Handler(Looper.getMainLooper()), cache, stats); }
HtmlProcessor createFakeHtmlProcessor() { HtmlProcessor htmlProcessor = mock(HtmlProcessor.class); when(htmlProcessor.processForDisplay(anyString())).thenAnswer(new Answer<String>() { @Override public String answer(InvocationOnMock invocation) throws Throwable { return (String) invocation.getArguments()[0]; } }); return htmlProcessor; }
@Test public void getTimestamp_Test() throws Exception { ValueMetaTimestamp v = new ValueMetaTimestamp( "TS" ); RowMetaInterface row = mock( RowMetaInterface.class ); doReturn( v ).when( row ).searchValueMeta( anyString() ); doReturn( 0 ).when( row ).indexOfValue( anyString() ); assertEquals( Timestamp.valueOf( "2018-07-23 12:40:55" ), new FieldHelper( row, "TS" ).getTimestamp( new Object[] { Timestamp.valueOf( "2018-07-23 12:40:55" ) } ) ); }
@Test public void shouldGracefullyHandleProtocolExceptionForInvalidMethod() throws Throwable { doThrow(ProtocolException.class).when(mockHttpURLConnection).setRequestMethod(anyString()); assertThat(httpFunction().apply(HTTP_DELETE_REQUEST).getFailure(), instanceOf(ProtocolException.class)); verify(mockHttpURLConnection).disconnect(); }
@Test public void testReplace() { String input = "\\\"Name\""; String[] from = new String[] { "\"" }; String[] to = new String[] { "\\\"" }; doCallRealMethod().when( ingresVectorwiseLoaderMock ).replace( anyString(), any( String[].class ), any( String[].class ) ); String actual = ingresVectorwiseLoaderMock.replace( input, from, to ); String expected = "\\\\\"Name\\\""; assertEquals( actual, expected ); }
@Before public void setUp() throws Exception { reset(requestHandler); when(requestHandler.cancel(anyString(), anyString())).thenReturn(EurekaHttpResponse.status(200)); when(requestHandler.getDelta()).thenReturn( anEurekaHttpResponse(200, new Applications()).type(MediaType.APPLICATION_JSON_TYPE).build() ); }
private void mockCustomTabsAllowed(final boolean allowed) { final FetchedAppSettings settings = mock(FetchedAppSettings.class); when(settings.getCustomTabsEnabled()).thenReturn(allowed); mockStatic(FetchedAppSettingsManager.class); when(FetchedAppSettingsManager.getAppSettingsWithoutQuery(anyString())).thenReturn(settings); }
private void verifyNoBackingStoreReads() { verify(mBackingStore, times(0)).getChild(anyLong(), anyString()); verify(mBackingStore, times(0)).getChildId(anyLong(), anyString()); verify(mBackingStore, times(0)).getChildren(anyLong()); verify(mBackingStore, times(0)).getChildIds(anyLong()); verify(mBackingStore, times(0)).get(anyLong()); verify(mBackingStore, times(0)).getMutable(anyLong()); } }
@Test public void testConflictResponseReturnsTheInstanceInfoInTheResponseEntity() throws Exception { when(instanceResource.renewLease(anyString(), anyString(), anyString(), anyString())).thenReturn(Response.status(Status.CONFLICT).entity(instanceInfo).build()); ReplicationInstance replicationInstance = newReplicationInstanceOf(Action.Heartbeat, instanceInfo); Response response = peerReplicationResource.batchReplication(new ReplicationList(replicationInstance)); assertStatusIsConflict(response); assertResponseEntityExist(response); }
@Test public void setValue_InetAddress() throws Exception { ValueMetaInternetAddress v = new ValueMetaInternetAddress( "IP" ); RowMetaInterface row = mock( RowMetaInterface.class ); doReturn( v ).when( row ).searchValueMeta( anyString() ); doReturn( 0 ).when( row ).indexOfValue( anyString() ); Object[] data = new Object[1]; new FieldHelper( row, "IP" ).setValue( data, InetAddress.getLoopbackAddress() ); assertEquals( InetAddress.getLoopbackAddress(), data[0] ); }
/** * If nimbusDetails are null, the method returns without any Zookeeper calls. */ @Test public void testUpdateKeyForBlobStore_nullNimbusInfo() { BlobStoreUtils.updateKeyForBlobStore(conf, blobStore, zkClientBuilder.build(), KEY, null); zkClientBuilder.verifyExists(false); zkClientBuilder.verifyGetChildren(false); verify(nimbusDetails, never()).getHost(); verify(conf, never()).get(anyString()); }
@Test public void testGetAttributesXml_CustomTag_EmptyMap() { PowerMockito.when( AttributesUtil.getAttributesXml( any( Map.class ), anyString() ) ).thenCallRealMethod(); Map<String, Map<String, String>> attributesMap = new HashMap<>(); String attributesXml = AttributesUtil.getAttributesXml( attributesMap, CUSTOM_TAG ); assertNotNull( attributesXml ); // Check that it's not an empty XML fragment assertTrue( attributesXml.contains( CUSTOM_TAG ) ); }
@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 getBinary_Test() throws Exception { ValueMetaBinary v = new ValueMetaBinary( "Data" ); RowMetaInterface row = mock( RowMetaInterface.class ); doReturn( v ).when( row ).searchValueMeta( anyString() ); doReturn( 0 ).when( row ).indexOfValue( anyString() ); assertArrayEquals( new byte[] { 0, 1, 2 }, new FieldHelper( row, "Data" ).getBinary( new Object[] { new byte[] { 0, 1, 2 } } ) ); }
/** * If the node doesn't exist, the method returns before attempting to fetch children. */ @Test public void testUpdateKeyForBlobStore_missingNode() { zkClientBuilder.withExists(BLOBSTORE_KEY, false); BlobStoreUtils.updateKeyForBlobStore(conf, blobStore, zkClientBuilder.build(), KEY, nimbusDetails); zkClientBuilder.verifyExists(true); zkClientBuilder.verifyGetChildren(false); verify(nimbusDetails, never()).getHost(); verify(conf, never()).get(anyString()); }
@Test public void testGetAttributesXml_CustomTag_NullParameter() { PowerMockito.when( AttributesUtil.getAttributesXml( any( Map.class ), anyString() ) ).thenCallRealMethod(); String attributesXml = AttributesUtil.getAttributesXml( null, CUSTOM_TAG ); assertNotNull( attributesXml ); // Check that it's not an empty XML fragment assertTrue( attributesXml.contains( CUSTOM_TAG ) ); }
@Test public void testMultiRowPut() throws Exception { testTimedOutCall(new CallExecutor() { @Override public void run() throws Exception { Put[] puts = { new Put(Bytes.toBytes("Row1")), new Put(Bytes.toBytes("Row2")) }; remoteTable.put(Arrays.asList(puts)); } }); verify(client, times(RETRIES)).put(anyString(), anyString(), any()); }
@Test public void setValue_ValueMetaBinary() throws Exception { ValueMetaBinary v = new ValueMetaBinary( "Data" ); RowMetaInterface row = mock( RowMetaInterface.class ); doReturn( v ).when( row ).searchValueMeta( anyString() ); doReturn( 0 ).when( row ).indexOfValue( anyString() ); Object[] data = new Object[1]; new FieldHelper( row, "Data" ).setValue( data, new byte[] { 0, 1, 2 } ); assertArrayEquals( new byte[] { 0, 1, 2 }, (byte[]) data[0] ); } }
@Test public void warnWhenExecutionTakesLongerThanInterval() throws Exception { SleepingTimer timer = new SleepingTimer(THREAD_NAME, INTERVAL_MS, mMockLogger, mFakeClock, mMockSleeper); timer.tick(); mFakeClock.addTimeMs(5 * INTERVAL_MS); timer.tick(); verify(mMockLogger).warn(anyString(), anyString(), Mockito.anyLong(), Mockito.anyLong()); }