From 70f9303166ea2fe8bf30e211039f459fbb5ca861 Mon Sep 17 00:00:00 2001 From: Leonid Baranov Date: Wed, 8 Jul 2020 15:07:32 +0200 Subject: [PATCH] Unit tests for ignite module --- cluster-boost-core/pom.xml | 10 +- .../boost/chain/ChainTest.java | 32 +++ cluster-boost-ignite-audit/pom.xml | 2 +- cluster-boost-ignite/pom.xml | 25 ++- .../boost/common/CommonDataAccessor.java | 6 +- .../boost/chain/ChainBeanCommonTest.java | 44 ++++ .../boost/chain/ChainFilterBeanImplTest.java | 56 +++++ .../boost/common/CommonDataAccessorTest.java | 94 ++++++++ .../boost/common/CommonRepositoryTest.java | 73 +++++++ .../boost/common/ContextHolderTest.java | 73 +++++++ .../boost/common/IgniteClusterTest.java | 205 ++++++++++++++++++ .../common/PublishReadyEventJobTest.java | 30 +++ .../boost/common/ReadyDetectorJobTest.java | 59 +++++ .../BeanProviderCommonFactoryTest.java | 41 ++++ .../common/providers/BeanProviderJobTest.java | 58 +++++ .../providers/BeanProviderRunnableTest.java | 40 ++++ .../providers/BeanProviderTaskTest.java | 56 +++++ .../boost/mock/TestClusterJob.java | 20 ++ .../boost/mock/TestClusterTask.java | 16 ++ .../boost/mock/TestRunnableBean.java | 8 + pom.xml | 7 +- 21 files changed, 945 insertions(+), 10 deletions(-) create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/chain/ChainBeanCommonTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/chain/ChainFilterBeanImplTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/CommonDataAccessorTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/CommonRepositoryTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/ContextHolderTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/IgniteClusterTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/PublishReadyEventJobTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/ReadyDetectorJobTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderCommonFactoryTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderJobTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderRunnableTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderTaskTest.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestClusterJob.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestClusterTask.java create mode 100644 cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestRunnableBean.java diff --git a/cluster-boost-core/pom.xml b/cluster-boost-core/pom.xml index 29a7e36..377bec4 100644 --- a/cluster-boost-core/pom.xml +++ b/cluster-boost-core/pom.xml @@ -5,7 +5,7 @@ cluster-boost com.technologicgroup.cluster - 0.1 + 1.1 4.0.0 @@ -13,23 +13,27 @@ ${core.version} + org.junit junit4-engine - 5.0.0-ALPHA + ${junit.version} test + org.assertj assertj-core + ${assertj.version} test org.mockito mockito-core - 2.23.4 + ${mockito.version} test + \ No newline at end of file diff --git a/cluster-boost-core/src/test/java/com/technologicgroup/boost/chain/ChainTest.java b/cluster-boost-core/src/test/java/com/technologicgroup/boost/chain/ChainTest.java index 4137b50..3acbd1e 100644 --- a/cluster-boost-core/src/test/java/com/technologicgroup/boost/chain/ChainTest.java +++ b/cluster-boost-core/src/test/java/com/technologicgroup/boost/chain/ChainTest.java @@ -9,9 +9,15 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import java.util.Collection; import java.util.Collections; import java.util.UUID; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + @RunWith(MockitoJUnitRunner.class) public class ChainTest { @@ -62,9 +68,35 @@ public void testMapArg_OK() { Assert.assertEquals(TestClusterTask.class, chain.getSteps().get(0).getBean()); } + @Test + public void testRunClusterGroup_OK() { + ClusterGroup clusterGroup = new ClusterGroup(Collections.singleton(UUID.randomUUID().toString())); + + Chain chain = Chain.of(cluster); + chain.on(clusterGroup); + chain.run(); + + verify(cluster, times(1)).runBean(eq(clusterGroup), eq(ChainBean.class), any()); + } + + @Test + public void testRunEmptyClusterGroup_OK() { + ClusterGroup clusterGroup = new ClusterGroup(Collections.emptySet()); + + Chain chain = Chain.of(cluster); + chain.on(clusterGroup); + Collection result = chain.run(); + + verify(cluster, times(0)).runBean(eq(clusterGroup), eq(ChainBean.class), any()); + Assert.assertEquals(0, result.size()); + } + @Test public void testRun_OK() { + Chain chain = Chain.of(cluster); + chain.run(); + verify(cluster, times(1)).runBean(eq(ChainBean.class), any()); } } diff --git a/cluster-boost-ignite-audit/pom.xml b/cluster-boost-ignite-audit/pom.xml index abb0094..0f41ea5 100644 --- a/cluster-boost-ignite-audit/pom.xml +++ b/cluster-boost-ignite-audit/pom.xml @@ -5,7 +5,7 @@ cluster-boost com.technologicgroup.cluster - 0.1 + 1.1 4.0.0 diff --git a/cluster-boost-ignite/pom.xml b/cluster-boost-ignite/pom.xml index 51bed61..6a7553d 100644 --- a/cluster-boost-ignite/pom.xml +++ b/cluster-boost-ignite/pom.xml @@ -5,7 +5,7 @@ cluster-boost com.technologicgroup.cluster - 0.1 + 1.1 4.0.0 @@ -36,6 +36,29 @@ ignite-indexing ${ignite.version} + + + + org.junit + junit4-engine + ${junit.version} + test + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + diff --git a/cluster-boost-ignite/src/main/java/com/technologicgroup/boost/common/CommonDataAccessor.java b/cluster-boost-ignite/src/main/java/com/technologicgroup/boost/common/CommonDataAccessor.java index 92325d9..0b11888 100644 --- a/cluster-boost-ignite/src/main/java/com/technologicgroup/boost/common/CommonDataAccessor.java +++ b/cluster-boost-ignite/src/main/java/com/technologicgroup/boost/common/CommonDataAccessor.java @@ -63,8 +63,8 @@ public Map networkGetAll() { */ @Override public void put(K key, V value) { - repository.put(key, value); - } + repository.put(key, value); + } /** * Puts a key/value pair map to the repository associated with a service. @@ -84,7 +84,7 @@ public void putAll(Map map) { */ @Override public Map find(Predicate predicate) { - ScanQuery query = new ScanQuery<>((k, v) -> predicate.test(v)); + ScanQuery query = new ScanQuery<>((k, v) -> predicate.test(v)); try (QueryCursor> cursor = repository.cache().query(query)) { return Stream.of(cursor.getAll()) diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/chain/ChainBeanCommonTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/chain/ChainBeanCommonTest.java new file mode 100644 index 0000000..cd720f6 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/chain/ChainBeanCommonTest.java @@ -0,0 +1,44 @@ +package com.technologicgroup.boost.chain; + +import com.technologicgroup.boost.core.Cluster; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ApplicationContext; + +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public class ChainBeanCommonTest { + + @Mock + private Cluster cluster; + + @Mock + private ApplicationContext context; + + @InjectMocks + private ChainBeanCommon chainBean; + + @Test + public void testGetBean_OK() { + chainBean.getBean(Object.class); + verify(context, times(1)).getBean(Object.class); + } + + @Test + public void testOnFinishBean_OK() { + chainBean.onFinishBean("", null, null, null, null, null); + verify(cluster, times(1)).getLocalNode(); + } + + @Test + public void testOnStartBean_OK() { + chainBean.onStartBean("", null, null); + verify(cluster, times(1)).getLocalNode(); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/chain/ChainFilterBeanImplTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/chain/ChainFilterBeanImplTest.java new file mode 100644 index 0000000..22fb692 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/chain/ChainFilterBeanImplTest.java @@ -0,0 +1,56 @@ +package com.technologicgroup.boost.chain; + +import com.technologicgroup.boost.core.Cluster; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.UUID; + +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class ChainFilterBeanImplTest { + + @Mock + private Cluster cluster; + + @InjectMocks + private ChainFilterBeanImpl chainFilter; + + @Test + public void testRun_filtered_OK() { + String nodeId = UUID.randomUUID().toString(); + Integer nodeResult = 100; + + Collection> chainResults = new ArrayList<>(); + chainResults.add(new ChainResult<>(nodeId, nodeResult)); + + when(cluster.getLocalNode()).thenReturn(nodeId); + Integer result = chainFilter.run(chainResults); + + verify(cluster, times(1)).getLocalNode(); + Assert.assertEquals(nodeResult, result); + } + + @Test + public void testRun_filtered_Null() { + String nodeId = UUID.randomUUID().toString(); + Integer nodeResult = 100; + + Collection> chainResults = new ArrayList<>(); + chainResults.add(new ChainResult<>(nodeId, nodeResult)); + + when(cluster.getLocalNode()).thenReturn(UUID.randomUUID().toString()); + Integer result = chainFilter.run(chainResults); + + verify(cluster, times(1)).getLocalNode(); + Assert.assertNull(result); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/CommonDataAccessorTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/CommonDataAccessorTest.java new file mode 100644 index 0000000..d73685b --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/CommonDataAccessorTest.java @@ -0,0 +1,94 @@ +package com.technologicgroup.boost.common; + +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.query.QueryCursor; +import org.apache.ignite.cache.query.ScanQuery; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +import javax.cache.Cache; +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class CommonDataAccessorTest { + + @Mock + private CommonRepository repository; + + @Mock + private IgniteCache cache; + + @Mock + private QueryCursor> cursor; + + public static class DataAccessor extends CommonDataAccessor { + public DataAccessor(CommonRepository repository) { + super(repository); + } + } + + @Test + public void testNetworkGet_one_OK() { + DataAccessor dataAccessor = new DataAccessor(repository); + when(repository.cache()).thenReturn(cache); + + dataAccessor.networkGet(1); + verify(cache, times(1)).get(1); + } + + @Test + public void testNetworkGet_keys_OK() { + DataAccessor dataAccessor = new DataAccessor(repository); + when(repository.cache()).thenReturn(cache); + + Set keys = Collections.singleton(1); + dataAccessor.networkGet(keys); + verify(cache, times(1)).getAll(keys); + } + + @Test + @SuppressWarnings("unchecked") + public void testNetworkGet_all_OK() { + DataAccessor dataAccessor = new DataAccessor(repository); + when(repository.cache()).thenReturn(cache); + when(cache.query(any(ScanQuery.class))).thenReturn(cursor); + + dataAccessor.networkGetAll(); + verify(cache, times(1)).query(any(ScanQuery.class)); + } + + @Test + public void testPut_one_OK() { + DataAccessor dataAccessor = new DataAccessor(repository); + + dataAccessor.put(1, 0); + verify(repository, times(1)).put(1, 0); + } + + @Test + public void testPut_all_OK() { + DataAccessor dataAccessor = new DataAccessor(repository); + + Map map = Collections.singletonMap(1, 0); + dataAccessor.putAll(map); + verify(repository, times(1)).putAll(map); + } + + @Test + @SuppressWarnings("unchecked") + public void testFind_OK() { + DataAccessor dataAccessor = new DataAccessor(repository); + when(repository.cache()).thenReturn(cache); + when(cache.query(any(ScanQuery.class))).thenReturn(cursor); + + dataAccessor.find((i) -> true); + verify(cache, times(1)).query(any(ScanQuery.class)); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/CommonRepositoryTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/CommonRepositoryTest.java new file mode 100644 index 0000000..9fb55ec --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/CommonRepositoryTest.java @@ -0,0 +1,73 @@ +package com.technologicgroup.boost.common; + +import lombok.extern.slf4j.Slf4j; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.configuration.CacheConfiguration; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ApplicationContext; + +import static org.mockito.Mockito.*; + +@Slf4j +@RunWith(MockitoJUnitRunner.class) +public class CommonRepositoryTest { + + @InjectMocks + private ContextHolder contextHolder; + + @Mock + private CacheConfiguration configuration; + + @Mock + private IgniteCache cache; + + @Mock + private Ignite ignite; + + @Mock + private org.apache.ignite.IgniteCluster igniteCluster; + + @Mock + private ApplicationContext context; + + @Mock + private org.apache.ignite.cluster.ClusterGroup igniteClusterGroup; + + private Repository repository; + + public static class Repository extends CommonRepository { } + + @Before + @SuppressWarnings("unchecked") + public void setUp() { + repository = new Repository(); + when(context.getBean(Ignite.class)).thenReturn(ignite); + when(ignite.cluster()).thenReturn(igniteCluster); + when(ignite.cache(any())).thenReturn((IgniteCache) cache); + + log.info("{}", contextHolder.toString()); + log.info("{}", configuration.toString()); + } + + @Test + public void testGetName_OK() { + String name = repository.getName(); + Assert.assertEquals("IntegerStringCache", name); + } + + @Test + public void testGetClusterGroup_OK() { + when(cache.getName()).thenReturn("IntegerStringCache"); + when(igniteCluster.forCacheNodes("IntegerStringCache")).thenReturn(igniteClusterGroup); + repository.getClusterGroup(); + verify(igniteCluster, times(1)).forCacheNodes("IntegerStringCache"); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/ContextHolderTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/ContextHolderTest.java new file mode 100644 index 0000000..7cb317f --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/ContextHolderTest.java @@ -0,0 +1,73 @@ +package com.technologicgroup.boost.common; + +import lombok.extern.slf4j.Slf4j; +import org.apache.ignite.Ignite; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ApplicationContext; + +import static org.mockito.Mockito.when; + +@Slf4j +@RunWith(MockitoJUnitRunner.class) +public class ContextHolderTest { + + @InjectMocks + private ContextHolder contextHolder; + + @Mock + private Ignite ignite; + + @Mock + private ApplicationContext context; + + @Mock + private org.apache.ignite.IgniteCluster igniteCluster; + + private IgniteCluster cluster; + + @Before + public void setUp() { + cluster = new IgniteCluster(ignite, null, 0, null); + + log.info("{}", contextHolder.toString()); + log.info("{}", context.toString()); + } + + @Test + public void testIsReady_true() { + ContextHolder.cluster = cluster; + + when(ignite.cluster()).thenReturn(igniteCluster); + when(igniteCluster.active()).thenReturn(true); + Assert.assertTrue(ContextHolder.isReady()); + } + + @Test + public void testIsReady_false() { + ContextHolder.cluster = cluster; + + when(ignite.cluster()).thenReturn(igniteCluster); + when(igniteCluster.active()).thenReturn(false); + Assert.assertFalse(ContextHolder.isReady()); + } + + @Test + public void testIsReady_context_null() { + ContextHolder.cluster = cluster; + ContextHolder.context = null; + Assert.assertFalse(ContextHolder.isReady()); + } + + @Test + public void testIsReady_cluster_null() { + ContextHolder.cluster = null; + Assert.assertFalse(ContextHolder.isReady()); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/IgniteClusterTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/IgniteClusterTest.java new file mode 100644 index 0000000..5bd25a5 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/IgniteClusterTest.java @@ -0,0 +1,205 @@ +package com.technologicgroup.boost.common; + +import com.technologicgroup.boost.common.providers.BeanProviderFactory; +import com.technologicgroup.boost.core.ClusterGroup; +import com.technologicgroup.boost.mock.TestClusterJob; +import com.technologicgroup.boost.mock.TestClusterTask; +import com.technologicgroup.boost.mock.TestRunnableBean; +import lombok.extern.slf4j.Slf4j; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCompute; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.lang.IgniteCallable; +import org.apache.ignite.lang.IgniteRunnable; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; + +import java.util.*; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +@Slf4j +@RunWith(MockitoJUnitRunner.class) +public class IgniteClusterTest { + + private IgniteCluster cluster; + + @Mock + private Ignite ignite; + + @Mock + private BeanProviderFactory beanFactory; + + @Mock + private org.apache.ignite.IgniteCluster igniteCluster; + + @Mock + private IgniteCompute igniteCompute; + + private String[] hosts; + + @Before + public void setUp() { + when(ignite.cluster()).thenReturn(igniteCluster); + + hosts = new String[] { UUID.randomUUID().toString() }; + cluster = new IgniteCluster(ignite, hosts, 10000, beanFactory); + } + + @Test + public void testExecute_group_OK() { + when(ignite.compute(any())).thenReturn(igniteCompute); + + ClusterGroup clusterGroup = new ClusterGroup(Collections.singleton(UUID.randomUUID().toString())); + cluster.execute(clusterGroup, new TestRunnableBean()); + + verify(igniteCompute, times(1)).broadcast(any(IgniteRunnable.class)); + } + + @Test + public void testExecute_OK() { + when(ignite.compute()).thenReturn(igniteCompute); + cluster.execute(new TestRunnableBean()); + verify(igniteCompute, times(1)).broadcast(any(IgniteRunnable.class)); + } + + @Test + public void testExecuteAsync_group_OK() { + when(ignite.compute(any())).thenReturn(igniteCompute); + + ClusterGroup clusterGroup = new ClusterGroup(Collections.singleton(UUID.randomUUID().toString())); + cluster.executeAsync(clusterGroup, new TestRunnableBean()); + + verify(igniteCompute, times(1)).broadcastAsync(any(IgniteRunnable.class)); + } + + @Test + public void testExecuteAsync_OK() { + when(ignite.compute()).thenReturn(igniteCompute); + cluster.executeAsync(new TestRunnableBean()); + verify(igniteCompute, times(1)).broadcastAsync(any(IgniteRunnable.class)); + } + + @Test + public void testExecuteBeanAsync_OK() { + when(ignite.compute()).thenReturn(igniteCompute); + when(beanFactory.getRunnable(TestRunnableBean.class)).thenReturn((IgniteRunnable) () -> { }); + + cluster.executeBeanAsync(TestRunnableBean.class); + verify(igniteCompute, times(1)).broadcastAsync(any(IgniteRunnable.class)); + verify(beanFactory, times(1)).getRunnable(TestRunnableBean.class); + } + + @Test + public void testExecuteBean_group_OK() { + ClusterGroup clusterGroup = new ClusterGroup(Collections.singleton(UUID.randomUUID().toString())); + + when(ignite.compute(any())).thenReturn(igniteCompute); + when(beanFactory.getRunnable(TestRunnableBean.class)).thenReturn((IgniteRunnable) () -> { }); + + cluster.executeBean(clusterGroup, TestRunnableBean.class); + verify(igniteCompute, times(1)).broadcast(any(IgniteRunnable.class)); + verify(beanFactory, times(1)).getRunnable(TestRunnableBean.class); + } + + @Test + public void testExecuteBean_OK() { + when(ignite.compute()).thenReturn(igniteCompute); + when(beanFactory.getRunnable(TestRunnableBean.class)).thenReturn((IgniteRunnable) () -> { }); + + cluster.executeBean(TestRunnableBean.class); + verify(igniteCompute, times(1)).broadcast(any(IgniteRunnable.class)); + verify(beanFactory, times(1)).getRunnable(TestRunnableBean.class); + } + + @Test + @SuppressWarnings({"unchecked", "rawusage"}) + public void testRunBean_OK() { + when(ignite.compute()).thenReturn(igniteCompute); + when(beanFactory.getJob(TestClusterJob.class)).thenReturn((IgniteCallable) () -> null); + + Collection results = cluster.runBean(TestClusterJob.class); + Assert.assertNotNull(results); + verify(igniteCompute, times(1)).broadcast(any(IgniteCallable.class)); + verify(beanFactory, times(1)).getJob(TestClusterJob.class); + } + + @Test + @SuppressWarnings("unchecked") + public void testRunBean_task_OK() { + when(ignite.compute()).thenReturn(igniteCompute); + when(beanFactory.getTask(TestClusterTask.class, "")).thenReturn((IgniteCallable) () -> null); + + Collection results = cluster.runBean(TestClusterTask.class, ""); + Assert.assertNotNull(results); + verify(igniteCompute, times(1)).broadcast(any(IgniteCallable.class)); + verify(beanFactory, times(1)).getTask(TestClusterTask.class, ""); + } + + @Test + @SuppressWarnings("unchecked") + public void testRunBean_group_task_OK() { + ClusterGroup clusterGroup = new ClusterGroup(Collections.singleton(UUID.randomUUID().toString())); + + when(ignite.compute(any())).thenReturn(igniteCompute); + when(beanFactory.getTask(TestClusterTask.class, "")).thenReturn((IgniteCallable) () -> null); + + Collection results = cluster.runBean(clusterGroup, TestClusterTask.class, ""); + Assert.assertNotNull(results); + verify(igniteCompute, times(1)).broadcast(any(IgniteCallable.class)); + verify(beanFactory, times(1)).getTask(TestClusterTask.class, ""); + } + + @Test + public void testGetNodeOrder() { + ClusterNode node = Mockito.mock(ClusterNode.class); + when(igniteCluster.localNode()).thenReturn(node); + when(node.order()).thenReturn(10L); + Assert.assertEquals(10L, cluster.getNodeOrder()); + } + + @Test + public void testIsFirstNode() { + ClusterNode node = Mockito.mock(ClusterNode.class); + when(igniteCluster.localNode()).thenReturn(node); + when(node.order()).thenReturn(1L); + Assert.assertTrue(cluster.isFirstNode()); + } + + @Test + @SuppressWarnings("unchecked") + public void testActivate_skipped() { + ClusterNode node = Mockito.mock(ClusterNode.class); + when(igniteCluster.localNode()).thenReturn(node); + when(node.order()).thenReturn(2L); + + cluster.activate(); + verify(igniteCompute, times(0)).broadcast(any(IgniteCallable.class)); + } + + @Test + @SuppressWarnings("unchecked") + public void testActivate_OK() { + ClusterNode node = Mockito.mock(ClusterNode.class); + when(igniteCluster.localNode()).thenReturn(node); + when(node.order()).thenReturn(1L); + when(ignite.compute()).thenReturn(igniteCompute); + + List results = new ArrayList<>(); + for (int i = 0; i < hosts.length; i++) { + results.add(true); + } + when(beanFactory.getJob(ReadyDetectorJob.class)).thenReturn((IgniteCallable) () -> true); + when(igniteCompute.broadcast(any(IgniteCallable.class))).thenReturn(results); + + cluster.activate(); + verify(igniteCompute, times(1)).broadcast(any(IgniteCallable.class)); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/PublishReadyEventJobTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/PublishReadyEventJobTest.java new file mode 100644 index 0000000..6c44e2f --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/PublishReadyEventJobTest.java @@ -0,0 +1,30 @@ +package com.technologicgroup.boost.common; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ApplicationEventPublisher; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public class PublishReadyEventJobTest { + + @Mock + private ApplicationEventPublisher publisher; + + @InjectMocks + private PublishReadyEventJob job; + + @Test + public void testRun_OK() throws InterruptedException { + Assert.assertTrue(job.run()); + Thread.sleep(10); + verify(publisher, times(1)).publishEvent(any(ClusterReadyEvent.class)); + } +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/ReadyDetectorJobTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/ReadyDetectorJobTest.java new file mode 100644 index 0000000..4d7c136 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/ReadyDetectorJobTest.java @@ -0,0 +1,59 @@ +package com.technologicgroup.boost.common; + +import com.technologicgroup.boost.common.providers.BeanProviderFactory; +import org.apache.ignite.Ignite; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ApplicationContext; + +import java.util.UUID; + +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class ReadyDetectorJobTest { + + @Mock + private ApplicationContext context; + + @Mock + private Ignite ignite; + + @Mock + private BeanProviderFactory beanFactory; + + @Mock + private org.apache.ignite.IgniteCluster igniteCluster; + + private ReadyDetectorJob job; + + @Before + public void setUp() { + String[] hosts = new String[] { UUID.randomUUID().toString() }; + IgniteCluster cluster = new IgniteCluster(ignite, hosts, 10000, beanFactory); + + when(ignite.cluster()).thenReturn(igniteCluster); + + ContextHolder.cluster = cluster; + ContextHolder.context = context; + + job = new ReadyDetectorJob(); + } + + @Test + public void testRun_active() { + when(igniteCluster.active()).thenReturn(true); + Assert.assertTrue(job.run()); + } + + @Test + public void testRun_not_active() { + when(igniteCluster.active()).thenReturn(false); + Assert.assertFalse(job.run()); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderCommonFactoryTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderCommonFactoryTest.java new file mode 100644 index 0000000..067b589 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderCommonFactoryTest.java @@ -0,0 +1,41 @@ +package com.technologicgroup.boost.common.providers; + +import com.technologicgroup.boost.mock.TestClusterJob; +import com.technologicgroup.boost.mock.TestClusterTask; +import com.technologicgroup.boost.mock.TestRunnableBean; +import org.apache.ignite.lang.IgniteCallable; +import org.apache.ignite.lang.IgniteRunnable; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.util.UUID; + +public class BeanProviderCommonFactoryTest { + + private BeanProviderCommonFactory factory; + + @Before + public void setUp() { + factory = new BeanProviderCommonFactory(); + } + + @Test + public void testGetRunnable() { + IgniteRunnable runnable = factory.getRunnable(TestRunnableBean.class); + Assert.assertNotNull(runnable); + } + + @Test + public void testGetJob() { + IgniteCallable callable = factory.getJob(TestClusterJob.class); + Assert.assertNotNull(callable); + } + + @Test + public void testGetTask() { + IgniteCallable callable = factory.getTask(TestClusterTask.class, UUID.randomUUID().toString()); + Assert.assertNotNull(callable); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderJobTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderJobTest.java new file mode 100644 index 0000000..a57e19e --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderJobTest.java @@ -0,0 +1,58 @@ +package com.technologicgroup.boost.common.providers; + +import com.technologicgroup.boost.common.ContextHolder; +import com.technologicgroup.boost.mock.TestClusterJob; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ApplicationContext; + +import static com.technologicgroup.boost.mock.TestClusterJob.EXCEPTION_CODE; +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class BeanProviderJobTest { + + @InjectMocks + private ContextHolder contextHolder; + + @Mock + private ApplicationContext context; + + private BeanProviderJob provider; + + @Before + public void setUp() { + provider = new BeanProviderJob<>(TestClusterJob.class); + } + + @Test + public void testCall_run_OK() { + when(context.getBean(TestClusterJob.class)).thenReturn(new TestClusterJob(5)); + Integer result = provider.call(); + + verify(context, times(1)).getBean(TestClusterJob.class); + Assert.assertEquals(5, result.longValue()); + } + + @Test + public void testCall_run_bean_notFound() { + when(context.getBean(TestClusterJob.class)).thenReturn(null); + Integer result = provider.call(); + + verify(context, times(1)).getBean(TestClusterJob.class); + Assert.assertNull(result); + Assert.assertNotEquals(0, contextHolder.hashCode()); + } + + @Test(expected = RuntimeException.class) + public void testCall_run_exception() { + when(context.getBean(TestClusterJob.class)).thenReturn(new TestClusterJob(EXCEPTION_CODE)); + provider.call(); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderRunnableTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderRunnableTest.java new file mode 100644 index 0000000..73114e7 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderRunnableTest.java @@ -0,0 +1,40 @@ +package com.technologicgroup.boost.common.providers; + +import com.technologicgroup.boost.common.ContextHolder; +import com.technologicgroup.boost.mock.TestRunnableBean; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ApplicationContext; + +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class BeanProviderRunnableTest { + + @InjectMocks + private ContextHolder contextHolder; + + @Mock + private ApplicationContext context; + + private BeanProviderRunnable provider; + + @Before + public void setUp() { + provider = new BeanProviderRunnable<>(TestRunnableBean.class); + } + + @Test + public void testCall_run_OK() { + when(context.getBean(TestRunnableBean.class)).thenReturn(new TestRunnableBean()); + provider.run(); + verify(context, times(1)).getBean(TestRunnableBean.class); + Assert.assertNotEquals(0, contextHolder.hashCode()); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderTaskTest.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderTaskTest.java new file mode 100644 index 0000000..f223679 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/common/providers/BeanProviderTaskTest.java @@ -0,0 +1,56 @@ +package com.technologicgroup.boost.common.providers; + +import com.technologicgroup.boost.common.ContextHolder; +import com.technologicgroup.boost.mock.TestClusterTask; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.context.ApplicationContext; + +import static org.mockito.Mockito.*; + +@RunWith(MockitoJUnitRunner.class) +public class BeanProviderTaskTest { + @InjectMocks + private ContextHolder contextHolder; + + @Mock + private ApplicationContext context; + + private BeanProviderTask provider; + + @Test + public void testCall_run_OK() { + provider = new BeanProviderTask<>(TestClusterTask.class, "10"); + + when(context.getBean(TestClusterTask.class)).thenReturn(new TestClusterTask()); + Integer result = provider.call(); + + verify(context, times(1)).getBean(TestClusterTask.class); + Assert.assertEquals(10, result.longValue()); + } + + @Test + public void testCall_run_bean_notFound() { + provider = new BeanProviderTask<>(TestClusterTask.class, "10"); + + when(context.getBean(TestClusterTask.class)).thenReturn(null); + Integer result = provider.call(); + + verify(context, times(1)).getBean(TestClusterTask.class); + Assert.assertNull(result); + Assert.assertNotEquals(0, contextHolder.hashCode()); + } + + @Test(expected = RuntimeException.class) + public void testCall_run_exception() { + provider = new BeanProviderTask<>(TestClusterTask.class, null); + + when(context.getBean(TestClusterTask.class)).thenReturn(new TestClusterTask()); + provider.call(); + } + +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestClusterJob.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestClusterJob.java new file mode 100644 index 0000000..ed2044d --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestClusterJob.java @@ -0,0 +1,20 @@ +package com.technologicgroup.boost.mock; + +import com.technologicgroup.boost.core.ClusterJob; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class TestClusterJob implements ClusterJob { + + public static final int EXCEPTION_CODE = 10101; + + private final Integer result; + + @Override + public Integer run() { + if (result == EXCEPTION_CODE) { + throw new RuntimeException(); + } + return result; + } +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestClusterTask.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestClusterTask.java new file mode 100644 index 0000000..f4ff994 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestClusterTask.java @@ -0,0 +1,16 @@ +package com.technologicgroup.boost.mock; + +import com.technologicgroup.boost.core.ClusterTask; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +public class TestClusterTask implements ClusterTask { + + @Override + public Integer run(String arg) { + if (arg == null) { + throw new RuntimeException(); + } + return Integer.valueOf(arg); + } +} diff --git a/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestRunnableBean.java b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestRunnableBean.java new file mode 100644 index 0000000..090d878 --- /dev/null +++ b/cluster-boost-ignite/src/test/java/com/technologicgroup/boost/mock/TestRunnableBean.java @@ -0,0 +1,8 @@ +package com.technologicgroup.boost.mock; + +public class TestRunnableBean implements Runnable { + @Override + public void run() { + + } +} diff --git a/pom.xml b/pom.xml index 2bc439c..4ca298a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.technologicgroup.cluster cluster-boost pom - 0.1 + 1.1 @@ -36,7 +36,10 @@ 1.8 1.8 1.8 - 1.0 + 1.1 + 5.0.0-ALPHA + 2.23.4 + 3.16.1 8.7.10