public static <Solution_> InnerScoreDirector<Solution_> mockScoreDirector(SolutionDescriptor<Solution_> solutionDescriptor) { EasyScoreDirectorFactory<Solution_> scoreDirectorFactory = new EasyScoreDirectorFactory<>(solutionDescriptor, (EasyScoreCalculator<Solution_>) (solution_) -> SimpleScore.of(0)); scoreDirectorFactory.setInitializingScoreTrend( InitializingScoreTrend.buildUniformTrend(InitializingScoreTrendLevel.ONLY_DOWN, 1)); return mock(InnerScoreDirector.class, AdditionalAnswers.delegatesTo(scoreDirectorFactory.buildScoreDirector(false, false))); }
public HTable asHTable() { return Mockito.mock(HTable.class, delegatesTo(this)); }
@Override protected ConcurrentMap<StatementMethod, StatementHolder> buildStatementCache(int maxSize) { ConcurrentMap<StatementMethod, StatementHolder> mockedStatementCache = mock(ConcurrentMap.class, delegatesTo(super.buildStatementCache(maxSize))); holder[0] = mockedStatementCache; return mockedStatementCache; } });
@Override public void reifyTest(Set< FieldDescriptor> fieldDescriptors) { doPrivileged((PrivilegedAction<Object>) () -> { fieldDescriptors .parallelStream() .forEach(p -> { try { Field field = p.getField(); Type fieldType = p.getGenericType(); Set<? extends Annotation> fieldAnnotations = p.getAnnotations(); Optional<Real> real = p.getAnnotation(Real.class ); Object instance = locator.getService(fieldType, fieldAnnotations); if (instance == null) { return; } if (real.isPresent() && real.get().value()) { instance = mock(p.getType(), delegatesTo(instance)); } field.setAccessible(true); field.set(testInstance, instance); } catch (SecurityException | IllegalAccessException | IllegalArgumentException e) { throw new IllegalStateException(e); } }); return null; }); }
@Override public void reifyTest(Set< FieldDescriptor> fieldDescriptors) { doPrivileged((PrivilegedAction<Object>) () -> { fieldDescriptors .parallelStream() .forEach(p -> { try { Field field = p.getField(); Type fieldType = p.getGenericType(); Set<? extends Annotation> fieldAnnotations = p.getAnnotations(); Optional<Real> real = p.getAnnotation(Real.class ); Object instance = locator.getService(fieldType, fieldAnnotations); if (instance == null) { return; } if (real.isPresent() && real.get().value()) { instance = mock(p.getType(), delegatesTo(instance)); } field.setAccessible(true); field.set(testInstance, instance); } catch (SecurityException | IllegalAccessException | IllegalArgumentException e) { throw new IllegalStateException(e); } }); return null; }); }
@Override public void reifyTest(Set< FieldDescriptor> fieldDescriptors) { doPrivileged((PrivilegedAction<Object>) () -> { fieldDescriptors .parallelStream() .forEach(p -> { try { Field field = p.getField(); Type fieldType = p.getGenericType(); Set<? extends Annotation> fieldAnnotations = p.getAnnotations(); Optional<Real> real = p.getAnnotation(Real.class ); Object instance = locator.getService(fieldType, fieldAnnotations); if (instance == null) { return; } if (real.isPresent() && real.get().value()) { instance = mock(p.getType(), delegatesTo(instance)); } field.setAccessible(true); field.set(testInstance, instance); } catch (SecurityException | IllegalAccessException | IllegalArgumentException e) { throw new IllegalStateException(e); } }); return null; }); }
protected void blockDataContainerIteration(final Cache<?, ?> cache, final CyclicBarrier barrier) { InternalDataContainer dataContainer = TestingUtil.extractComponent(cache, InternalDataContainer.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(dataContainer); InternalDataContainer mockContainer = mock(InternalDataContainer.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up barrier.await(10, TimeUnit.SECONDS); // Now wait until main thread lets us through barrier.await(10, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockContainer).removeSegments(any()); TestingUtil.replaceComponent(cache, InternalDataContainer.class, mockContainer, true); } }
private void blockRebalanceConfirmation(final EmbeddedCacheManager manager, final CheckPoint checkPoint, int rebalanceTopologyId) throws Exception { ClusterTopologyManager ctm = TestingUtil.extractGlobalComponent(manager, ClusterTopologyManager.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(ctm); ClusterTopologyManager mockManager = mock(ClusterTopologyManager.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { Object[] arguments = invocation.getArguments(); Address source = (Address) arguments[1]; int topologyId = (Integer) arguments[2]; if (topologyId == rebalanceTopologyId) { checkPoint.trigger("pre_rebalance_confirmation_" + topologyId + "_from_" + source); checkPoint.awaitStrict("resume_rebalance_confirmation_" + topologyId + "_from_" + source, 20, SECONDS); } return forwardedAnswer.answer(invocation); }).when(mockManager).handleRebalancePhaseConfirm(anyString(), any(Address.class), anyInt(), isNull(), anyInt()); TestingUtil.replaceComponent(manager, ClusterTopologyManager.class, mockManager, true); }
protected void waitUntilNotificationRaised(final Cache<?, ?> cache, final CheckPoint checkPoint) { CacheNotifier cn = TestingUtil.extractComponent(cache, CacheNotifier.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(cn); CacheNotifier mockNotifier = mock(CacheNotifier.class, withSettings().extraInterfaces(ClusterCacheNotifier.class) .defaultAnswer(forwardedAnswer)); Answer answer = invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_raise_notification_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_raise_notification_release", 10, TimeUnit.SECONDS); try { return forwardedAnswer.answer(invocation); } finally { // Wait for main thread to sync up checkPoint.trigger("post_raise_notification_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("post_raise_notification_release", 10, TimeUnit.SECONDS); } }; doAnswer(answer).when(mockNotifier).notifyCacheEntryCreated(any(), any(), any(Metadata.class), eq(false), any(InvocationContext.class), any(FlagAffectedCommand.class)); doAnswer(answer).when(mockNotifier).notifyCacheEntryModified(any(), any(), any(Metadata.class), any(), any(Metadata.class), anyBoolean(), any(InvocationContext.class), any(FlagAffectedCommand.class)); doAnswer(answer).when(mockNotifier).notifyCacheEntryRemoved(any(), any(), any(Metadata.class), eq(false), any(InvocationContext.class), any(FlagAffectedCommand.class)); TestingUtil.replaceComponent(cache, CacheNotifier.class, mockNotifier, true); }
protected AsyncInterceptorChain mockEntrySet(final Cache<?, ?> cache, StreamMocking mocking) { AsyncInterceptorChain chain = TestingUtil.extractComponent(cache, AsyncInterceptorChain.class); AsyncInterceptorChain mockChain = spy(chain); doAnswer(i -> { CacheSet cacheSet = (CacheSet) i.callRealMethod(); CacheSet mockSet = mock(CacheSet.class, withSettings().defaultAnswer(delegatesTo(cacheSet))); when(mockSet.stream()).then(j -> { CacheStream stream = cacheSet.stream(); return mockStream(stream, mocking); }); return mockSet; }).when(mockChain).invoke(any(InvocationContext.class), any(EntrySetCommand.class)); TestingUtil.replaceComponent(cache, AsyncInterceptorChain.class, mockChain, true); return chain; } }
protected PersistenceManager waitUntilAboutToProcessStoreTask(final Cache<?, ?> cache, final CheckPoint checkPoint) { PersistenceManager pm = TestingUtil.extractComponent(cache, PersistenceManager.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(pm); PersistenceManager mockManager = mock(PersistenceManager.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_process_on_all_stores_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_process_on_all_stores_released", 10, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockManager).publishEntries(any(), any(), anyBoolean(), anyBoolean(), any()); TestingUtil.replaceComponent(cache, PersistenceManager.class, mockManager, true); return pm; }
protected void waitUntilToplogyInstalled(final Cache<?, ?> cache, final CheckPoint checkPoint) { StateTransferLock sc = TestingUtil.extractComponent(cache, StateTransferLock.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(sc); StateTransferLock mockConsumer = mock(StateTransferLock.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { Object answer = forwardedAnswer.answer(invocation); // Wait for main thread to sync up checkPoint.trigger("post_topology_installed_invoked_" + cache); // Now wait until main thread lets us through checkPoint.awaitStrict("post_topology_installed_released_" + cache, 10, TimeUnit.SECONDS); return answer; }).when(mockConsumer).notifyTopologyInstalled(anyInt()); TestingUtil.replaceComponent(cache, StateTransferLock.class, mockConsumer, true); } }
protected void waitUntilStateBeingTransferred(final Cache<?, ?> cache, final CheckPoint checkPoint) { StateConsumer sc = TestingUtil.extractComponent(cache, StateConsumer.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(sc); StateConsumer mockConsumer = mock(StateConsumer.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_state_apply_invoked_for_" + cache); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_state_apply_release_for_" + cache, 20, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockConsumer).applyState(any(Address.class), anyInt(), anyBoolean(), anyCollection()); TestingUtil.replaceComponent(cache, StateConsumer.class, mockConsumer, true); } }
protected void waitUntilBeforeTopologyInstalled(final Cache<?, ?> cache, final CheckPoint checkPoint) { StateConsumer sc = TestingUtil.extractComponent(cache, StateConsumer.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(sc); StateConsumer mockConsumer = mock(StateConsumer.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_topology_installed_invoked_" + cache); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_topology_installed_released_" + cache, 10, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockConsumer).onTopologyUpdate(any(CacheTopology.class), anyBoolean()); TestingUtil.replaceComponent(cache, StateConsumer.class, mockConsumer, true); }
protected PersistenceManager waitUntilAboutToProcessStoreTask(final Cache<?, ?> cache, final CheckPoint checkPoint) { PersistenceManager pm = TestingUtil.extractComponent(cache, PersistenceManager.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(pm); PersistenceManager mockManager = mock(PersistenceManager.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_process_on_all_stores_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_process_on_all_stores_released", 10, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockManager).publishEntries(any(), anyBoolean(), anyBoolean(), any()); TestingUtil.replaceComponent(cache, PersistenceManager.class, mockManager, true); return pm; }
protected void waitUntilViewChangeOccurs(final CacheContainer cacheContainer, final String uniqueId, final CheckPoint checkPoint) { CacheManagerNotifier cmn = TestingUtil.extractGlobalComponent(cacheContainer, CacheManagerNotifier.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(cmn); CacheManagerNotifier mockNotifier = mock(CacheManagerNotifier.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_view_listener_invoked_" + uniqueId); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_view_listener_release_" + uniqueId, 10, TimeUnit.SECONDS); try { return forwardedAnswer.answer(invocation); } finally { checkPoint.trigger("post_view_listener_invoked_" + uniqueId); } }).when(mockNotifier).notifyViewChange(anyList(), anyList(), any(Address.class), anyInt()); TestingUtil.replaceComponent(cacheContainer, CacheManagerNotifier.class, mockNotifier, true); } }
/** * Replaces StateTransferLock in cache with a proxy one that will block on * {#link StateTransferLock#acquireSharedTopologyLock} until the checkpoint is triggered * @param cache The cache to replace the StateTransferLock on * @param checkPoint The checkpoint to use to trigger blocking * @return The original real StateTransferLock */ protected StateTransferLock waitUntilAboutToAcquireLock(final Cache<?, ?> cache, final CheckPoint checkPoint) { StateTransferLock stl = TestingUtil.extractComponent(cache, StateTransferLock.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(stl); StateTransferLock mockLock = mock(StateTransferLock.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_acquire_shared_topology_lock_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_acquire_shared_topology_lock_released", 10, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockLock).acquireSharedTopologyLock(); TestingUtil.replaceComponent(cache, StateTransferLock.class, mockLock, true); return stl; }
/** * Replaces L1Manager in cache with a proxy one that will block on * {#link L1Manager#registerL1WriteSynchronizer} until the checkpoint is triggered * @param cache The cache to replace the L1Manager on * @param checkPoint The checkpoint to use to trigger blocking * @return The original real L1Manager */ protected L1Manager waitUntilL1Registration(final Cache<?, ?> cache, final CheckPoint checkPoint) { L1Manager l1Manager = TestingUtil.extractComponent(cache, L1Manager.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(l1Manager); L1Manager mockL1 = mock(L1Manager.class, withSettings().defaultAnswer(forwardedAnswer).extraInterfaces(RemoteValueRetrievedListener.class)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_acquire_shared_topology_lock_invoked"); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_acquire_shared_topology_lock_released", 10, TimeUnit.SECONDS); return forwardedAnswer.answer(invocation); }).when(mockL1).registerL1WriteSynchronizer(Mockito.notNull(), Mockito.any(L1WriteSynchronizer.class)); TestingUtil.replaceComponent(cache, L1Manager.class, mockL1, true); return l1Manager; } }
protected void waitUntilRequestingListeners(final Cache<?, ?> cache, final CheckPoint checkPoint) { StateProvider sp = TestingUtil.extractComponent(cache, StateProvider.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(sp); StateProvider mockProvider = mock(StateProvider.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_cluster_listeners_invoked_" + cache); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_cluster_listeners_release_" + cache, 10, TimeUnit.SECONDS); try { return forwardedAnswer.answer(invocation); } finally { // Wait for main thread to sync up checkPoint.trigger("post_cluster_listeners_invoked_" + cache); // Now wait until main thread lets us through checkPoint.awaitStrict("post_cluster_listeners_release_" + cache, 10, TimeUnit.SECONDS); } }).when(mockProvider).getClusterListenersToInstall(); TestingUtil.replaceComponent(cache, StateProvider.class, mockProvider, true); } }
protected void waitUntilListenerInstalled(final Cache<?, ?> cache, final CheckPoint checkPoint) { CacheNotifier cn = TestingUtil.extractComponent(cache, CacheNotifier.class); final Answer<Object> forwardedAnswer = AdditionalAnswers.delegatesTo(cn); ClusterCacheNotifier mockNotifier = mock(ClusterCacheNotifier.class, withSettings().defaultAnswer(forwardedAnswer)); doAnswer(invocation -> { // Wait for main thread to sync up checkPoint.trigger("pre_add_listener_invoked_" + cache); // Now wait until main thread lets us through checkPoint.awaitStrict("pre_add_listener_release_" + cache, 10, TimeUnit.SECONDS); try { return forwardedAnswer.answer(invocation); } finally { // Wait for main thread to sync up checkPoint.trigger("post_add_listener_invoked_" + cache); // Now wait until main thread lets us through checkPoint.awaitStrict("post_add_listener_release_" + cache, 10, TimeUnit.SECONDS); } }).when(mockNotifier).addFilteredListener(notNull(), nullable(CacheEventFilter.class), nullable(CacheEventConverter.class), any(Set.class)); TestingUtil.replaceComponent(cache, CacheNotifier.class, mockNotifier, true); }