Skip to content

Commit

Permalink
Unit tests for ignite module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurkambay committed Jul 8, 2020
1 parent 4a1e150 commit 70f9303
Show file tree
Hide file tree
Showing 21 changed files with 945 additions and 10 deletions.
10 changes: 7 additions & 3 deletions cluster-boost-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,35 @@
<parent>
<artifactId>cluster-boost</artifactId>
<groupId>com.technologicgroup.cluster</groupId>
<version>0.1</version>
<version>1.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>cluster-boost-core</artifactId>
<version>${core.version}</version>

<dependencies>
<!-- Testing dependencies -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit4-engine</artifactId>
<version>5.0.0-ALPHA</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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());
}

}
2 changes: 1 addition & 1 deletion cluster-boost-ignite-audit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>cluster-boost</artifactId>
<groupId>com.technologicgroup.cluster</groupId>
<version>0.1</version>
<version>1.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
25 changes: 24 additions & 1 deletion cluster-boost-ignite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>cluster-boost</artifactId>
<groupId>com.technologicgroup.cluster</groupId>
<version>0.1</version>
<version>1.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down Expand Up @@ -36,6 +36,29 @@
<artifactId>ignite-indexing</artifactId>
<version>${ignite.version}</version>
</dependency>

<!-- Testing dependencies -->
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit4-engine</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

</dependencies>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public Map<K, V> 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.
Expand All @@ -84,7 +84,7 @@ public void putAll(Map<K, V> map) {
*/
@Override
public Map<K, V> find(Predicate<V> predicate) {
ScanQuery<K,V> query = new ScanQuery<>((k, v) -> predicate.test(v));
ScanQuery<K, V> query = new ScanQuery<>((k, v) -> predicate.test(v));

try (QueryCursor<Cache.Entry<K, V>> cursor = repository.cache().query(query)) {
return Stream.of(cursor.getAll())
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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<Integer> chainFilter;

@Test
public void testRun_filtered_OK() {
String nodeId = UUID.randomUUID().toString();
Integer nodeResult = 100;

Collection<ChainResult<Integer>> 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<ChainResult<Integer>> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> repository;

@Mock
private IgniteCache<Integer, Integer> cache;

@Mock
private QueryCursor<Cache.Entry<Integer, Integer>> cursor;

public static class DataAccessor extends CommonDataAccessor<Integer, Integer> {
public DataAccessor(CommonRepository<Integer, Integer> 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<Integer> 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<Integer, Integer> 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));
}

}
Loading

0 comments on commit 70f9303

Please sign in to comment.