private HttpMessageWriter<String> getWriter(Flux<DataBuffer> encodedStream, MimeType... mimeTypes) { List<MimeType> typeList = Arrays.asList(mimeTypes); when(this.encoder.getEncodableMimeTypes()).thenReturn(typeList); when(this.encoder.encode(any(), any(), any(), this.mediaTypeCaptor.capture(), any())).thenReturn(encodedStream); return new EncoderHttpMessageWriter<>(this.encoder); }
@Test public void initShouldCreateThreadPool() throws Throwable { ExecutorFactory mockExecutorFactory = mock( ExecutorFactory.class ); when( mockExecutorFactory.create( anyInt(), anyInt(), any(), anyInt(), anyBoolean(), any() ) ).thenReturn( Executors.newCachedThreadPool() ); ExecutorBoltScheduler scheduler = new ExecutorBoltScheduler( CONNECTOR_KEY, mockExecutorFactory, jobScheduler, logService, 0, 10, Duration.ofMinutes( 1 ), 0, ForkJoinPool.commonPool() ); scheduler.start(); verify( jobScheduler ).threadFactory( Group.BOLT_WORKER ); verify( mockExecutorFactory, times( 1 ) ).create( anyInt(), anyInt(), any( Duration.class ), anyInt(), anyBoolean(), any( ThreadFactory.class ) ); }
/** * Mocks a subscriber and prepares it to request Long.MAX_VALUE. * @param <T> the value type * @return the mocked subscriber */ @SuppressWarnings("unchecked") public static <T> FlowableSubscriber<T> mockSubscriber() { FlowableSubscriber<T> w = mock(FlowableSubscriber.class); Mockito.doAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock a) throws Throwable { Subscription s = a.getArgument(0); s.request(Long.MAX_VALUE); return null; } }).when(w).onSubscribe((Subscription)any()); return w; }
@Test public void testExpressionInStringArray() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); BeanExpressionResolver beanExpressionResolver = mock(BeanExpressionResolver.class); when(beanExpressionResolver.evaluate(eq("#{foo}"), ArgumentMatchers.any(BeanExpressionContext.class))) .thenReturn("classpath:/org/springframework/beans/factory/xml/util.properties"); bf.setBeanExpressionResolver(beanExpressionResolver); RootBeanDefinition rbd = new RootBeanDefinition(PropertiesFactoryBean.class); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("locations", new String[]{"#{foo}"}); rbd.setPropertyValues(pvs); bf.registerBeanDefinition("myProperties", rbd); Properties properties = (Properties) bf.getBean("myProperties"); assertEquals("bar", properties.getProperty("foo")); }
@Test public void testNextTupleEmitsAtMostOneTuple() { //The spout should emit at most one message per call to nextTuple //This is necessary for Storm to be able to throttle the spout according to maxSpoutPending KafkaSpout<String, String> spout = SpoutWithMockedConsumerSetupHelper.setupSpout(spoutConfig, conf, contextMock, collectorMock, consumerMock, partition); Map<TopicPartition, List<ConsumerRecord<String, String>>> records = new HashMap<>(); records.put(partition, SpoutWithMockedConsumerSetupHelper.createRecords(partition, 0, 10)); when(consumerMock.poll(anyLong())) .thenReturn(new ConsumerRecords<>(records)); spout.nextTuple(); verify(collectorMock, times(1)).emit(anyString(), anyList(), any(KafkaSpoutMessageId.class)); }
private ProduceRequestResult mockProduceRequestResult() throws InterruptedException { ProduceRequestResult mockProduceRequestResult = mock(ProduceRequestResult.class); when(mockProduceRequestResult.await(anyLong(), any(TimeUnit.class))).thenReturn(true); return mockProduceRequestResult; } }
private static TransactionStateMachineV1SPI newTransactionStateMachineSPI( KernelTransaction transaction ) throws KernelException { BoltResultHandle resultHandle = newResultHandle(); TransactionStateMachineV1SPI stateMachineSPI = mock( TransactionStateMachineV1SPI.class ); when( stateMachineSPI.beginTransaction( any(), any(), any() ) ).thenReturn( transaction ); when( stateMachineSPI.executeQuery( any(), anyString(), any(), any(), any() ) ).thenReturn( resultHandle ); return stateMachineSPI; }
@Test public void testTakeLastEmpty() { Observable<String> w = Observable.empty(); Observable<String> take = w.takeLast(2); Observer<String> observer = TestHelper.mockObserver(); take.subscribe(observer); verify(observer, never()).onNext(any(String.class)); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@Test public void shouldCallIndexSeek() throws KernelException { // GIVEN Read read = mock( Read.class ); IndexReference index = mock( IndexReference.class ); when( index.properties() ).thenReturn( new int[]{42} ); // WHEN CompiledIndexUtils.indexSeek( read, mock( CursorFactory.class ), index, "hello" ); // THEN verify( read, times( 1 ) ).nodeIndexSeek( any(), any(), any(), anyBoolean(), any() ); }
@Test public void testSkipLastEmpty() { Observable<String> o = Observable.<String> empty().skipLast(2); Observer<String> observer = TestHelper.mockObserver(); o.subscribe(observer); verify(observer, never()).onNext(any(String.class)); verify(observer, never()).onError(any(Throwable.class)); verify(observer, times(1)).onComplete(); }
@SuppressWarnings("unchecked") @Test public void shouldCallOnNextAndOnCompleted() throws Exception { Callable<String> func = mock(Callable.class); when(func.call()).thenReturn("test_value"); Observable<String> fromCallableObservable = Observable.fromCallable(func); Observer<Object> observer = TestHelper.mockObserver(); fromCallableObservable.subscribe(observer); verify(observer).onNext("test_value"); verify(observer).onComplete(); verify(observer, never()).onError(any(Throwable.class)); }
@SuppressWarnings("unchecked") @Test public void shouldCallOnError() throws Exception { Callable<Object> func = mock(Callable.class); Throwable throwable = new IllegalStateException("Test exception"); when(func.call()).thenThrow(throwable); Observable<Object> fromCallableObservable = Observable.fromCallable(func); Observer<Object> observer = TestHelper.mockObserver(); fromCallableObservable.subscribe(observer); verify(observer, never()).onNext(any()); verify(observer, never()).onComplete(); verify(observer).onError(throwable); }
@Test public void testList() { Observable<String> w = Observable.fromIterable(Arrays.asList("one", "two", "three")); Single<List<String>> single = w.toList(); SingleObserver<List<String>> observer = TestHelper.mockSingleObserver(); single.subscribe(observer); verify(observer, times(1)).onSuccess(Arrays.asList("one", "two", "three")); verify(observer, Mockito.never()).onError(any(Throwable.class)); }
@SuppressWarnings("unchecked") @Test public void shouldCallOnNextAndOnCompleted() throws Exception { Callable<String> func = mock(Callable.class); when(func.call()).thenReturn("test_value"); Flowable<String> fromCallableFlowable = Flowable.fromCallable(func); Subscriber<String> subscriber = TestHelper.mockSubscriber(); fromCallableFlowable.subscribe(subscriber); verify(subscriber).onNext("test_value"); verify(subscriber).onComplete(); verify(subscriber, never()).onError(any(Throwable.class)); }
@Test public void testListViaObservable() { Observable<String> w = Observable.fromIterable(Arrays.asList("one", "two", "three")); Single<List<String>> single = w.toList(); SingleObserver<List<String>> observer = TestHelper.mockSingleObserver(); single.subscribe(observer); verify(observer, times(1)).onSuccess(Arrays.asList("one", "two", "three")); verify(observer, Mockito.never()).onError(any(Throwable.class)); }
@Test public void shouldOnlyReadOnceIfAllBytesAreRead() throws Exception { // GIVEN Socket socket = mock( Socket.class ); InputStream stream = mock( InputStream.class ); when(socket.getInputStream()).thenReturn( stream ); when(stream.read( any(byte[].class), anyInt(), anyInt() )).thenReturn( 4 ); SocketConnection connection = new SocketConnection( socket ); connection.connect( new HostnamePort( "my.domain", 1234 ) ); // WHEN connection.recv( 4 ); // THEN verify(stream, times(1)).read( any(byte[].class), anyInt(), anyInt() ); }
@Test public void testListViaFlowable() { Flowable<String> w = Flowable.fromIterable(Arrays.asList("one", "two", "three")); Single<List<String>> single = w.toList(); SingleObserver<List<String>> observer = TestHelper.mockSingleObserver(); single.subscribe(observer); verify(observer, times(1)).onSuccess(Arrays.asList("one", "two", "three")); verify(observer, Mockito.never()).onError(any(Throwable.class)); }