Skip to content

Commit

Permalink
Add unit test for core module
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurkambay committed Jul 7, 2020
1 parent 7055d76 commit 4a1e150
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 12 deletions.
20 changes: 20 additions & 0 deletions cluster-boost-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,24 @@
<artifactId>cluster-boost-core</artifactId>
<version>${core.version}</version>

<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit4-engine</artifactId>
<version>5.0.0-ALPHA</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.technologicgroup.boost.core.ClusterGroup;
import com.technologicgroup.boost.core.ClusterTask;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -22,11 +24,20 @@
public class Chain {

private final Cluster cluster;

@Getter
private String trackingId;
ClusterGroup clusterGroup;
Object arg;

List<ChainStep<?, ?>> steps = new ArrayList<>();
@Setter(AccessLevel.PACKAGE)
@Getter(AccessLevel.PACKAGE)
private ClusterGroup clusterGroup;

@Setter(AccessLevel.PACKAGE)
@Getter(AccessLevel.PACKAGE)
private Object arg;

@Getter(AccessLevel.PACKAGE)
private final List<ChainStep<?, ?>> steps = new ArrayList<>();

/**
* Static constructor of Chain object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,10 @@ public <Res> Res collect(Function<Collection<R>, Res> function) {
* @return the next chain step
*/
public <Res, NR> ChainStep<Res, NR> reduce(Function<Collection<R>, Res> function) {
chain.arg = collect(function);
chain.setArg(collect(function));
ChainStep<Res, NR> step = new ChainStep<>(null, chain);
chain.steps.add(step);
chain.getSteps().clear();
chain.getSteps().add(step);
return step;
}

Expand All @@ -81,9 +82,10 @@ public <Res, NR> ChainStep<Res, NR> reduce(Function<Collection<R>, Res> function
*/
public <Any, Res, T extends ClusterTask<Any, Res>> ChainStep<Any, Res> reduce(Class<T> bean) {
chain.run();
chain.arg = null;
chain.setArg(null);
ChainStep<Any, Res> step = new ChainStep<>(bean, chain);
chain.steps.add(step);
chain.getSteps().clear();
chain.getSteps().add(step);
return step;
}

Expand All @@ -101,9 +103,10 @@ public <Any, Res, T extends ClusterTask<Any, Res>> ChainStep<Any, Res> reduce(Cl
*/
public <Res, NR, T extends ClusterTask<Res, NR>> ChainStep<Res, NR> reduce(Function<Collection<R>, Res> function,
Class<T> bean) {
chain.arg = collect(function);
chain.setArg(collect(function));
ChainStep<Res, NR> step = new ChainStep<>(bean, chain);
chain.steps.add(step);
chain.getSteps().clear();
chain.getSteps().add(step);
return step;
}

Expand All @@ -116,7 +119,7 @@ public <Res, NR, T extends ClusterTask<Res, NR>> ChainStep<Res, NR> reduce(Funct
*/
public <Res, T extends ClusterTask<R, Res>> ChainStep<R, Res> map(Class<T> bean) {
ChainStep<R, Res> step = new ChainStep<>(bean, chain);
chain.steps.add(step);
chain.getSteps().add(step);
return step;
}

Expand All @@ -137,8 +140,8 @@ public ChainStep<R, R> filter(Predicate<ChainResult<R>> predicate) {
.map(ChainResult::getNodeId)
.collect(Collectors.toSet());

chain.clusterGroup = new ClusterGroup(nodeList);
chain.arg = results;
chain.setClusterGroup(new ClusterGroup(nodeList));
chain.setArg(results);

return map(ChainFilterBean.class);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.technologicgroup.boost;

import com.technologicgroup.boost.audit.AuditItem;
import com.technologicgroup.boost.audit.AuditTaskInfo;

import java.sql.Timestamp;
import java.util.UUID;

public class Fixtures {
public static AuditItem auditItem(String trackingId, Timestamp start) {
AuditTaskInfo taskInfo = new AuditTaskInfo(start, new Timestamp(start.getTime() + 100L), null, null, Object.class);
return new AuditItem(UUID.randomUUID().toString(), trackingId, taskInfo, UUID.randomUUID().toString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.technologicgroup.boost.audit;

import com.technologicgroup.boost.Fixtures;
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.sql.Timestamp;
import java.util.*;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class AuditServiceTest {
@InjectMocks
private AuditService auditService;

@Mock
private AuditItemAccessor itemAccessor;

@Test
public void testSorting_OK() {
String trackingId = UUID.randomUUID().toString();

Timestamp start1 = new Timestamp(System.currentTimeMillis());
Timestamp start2 = new Timestamp(System.currentTimeMillis() + 100);
Timestamp start3 = new Timestamp(System.currentTimeMillis() + 200);

Map<String, AuditItem> auditMap = new HashMap<>();
putAuditItem(auditMap, Fixtures.auditItem(trackingId, start1));
putAuditItem(auditMap, Fixtures.auditItem(trackingId, start2));
putAuditItem(auditMap, Fixtures.auditItem(trackingId, start3));

when(itemAccessor.find(any())).thenReturn(auditMap);
List<AuditItem> items = auditService.getItems(trackingId);

verify(itemAccessor, times(1)).find(any());
Assert.assertEquals(3, items.size());
Assert.assertEquals(start1, items.get(0).getTaskInfo().getStart());
}

private void putAuditItem(Map<String, AuditItem> auditMap, AuditItem auditItem) {
auditMap.put(auditItem.getId(), auditItem);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.technologicgroup.boost.chain;

import com.technologicgroup.boost.chain.mock.TestChainBean;
import com.technologicgroup.boost.chain.mock.TestClusterTask;
import com.technologicgroup.boost.chain.mock.TestFailClusterTask;
import com.technologicgroup.boost.core.Cluster;
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 java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;

import static org.mockito.Mockito.*;


@RunWith(MockitoJUnitRunner.class)
public class ChainBeanTest {

@Mock
private Cluster cluster;

@Mock
private Chain chain;

private CountDownLatch startLatch;

private CountDownLatch finishLatch;

@Before
public void setUp() {
startLatch = new CountDownLatch(1);
finishLatch = new CountDownLatch(1);
}

@Test
public void testRun_OK() {
String trackingId = UUID.randomUUID().toString();
String localNode = UUID.randomUUID().toString();
String arg = UUID.randomUUID().toString();

List<ChainStep<?, ?>> items = new ArrayList<>();
items.add(new ChainStep<>(TestClusterTask.class, chain));

when(cluster.getLocalNode()).thenReturn(localNode);

TestChainBean chainBean = new TestChainBean(cluster, startLatch, finishLatch);
ChainResult<Integer> result = chainBean.run(new ChainArgument<>(arg, items, trackingId));

Assert.assertEquals(localNode, result.getNodeId());
Assert.assertEquals(arg.length(), result.getResult().longValue());
Assert.assertEquals(0, startLatch.getCount());
Assert.assertEquals(0, finishLatch.getCount());
}

@Test
public void testRun_Failure() {
String trackingId = UUID.randomUUID().toString();
String arg = UUID.randomUUID().toString();

List<ChainStep<?, ?>> items = new ArrayList<>();
items.add(new ChainStep<>(TestFailClusterTask.class, chain));

TestChainBean chainBean = new TestChainBean(cluster, startLatch, finishLatch);
try {
chainBean.run(new ChainArgument<>(arg, items, trackingId));
Assert.fail();
} catch (Exception e) {
Assert.assertEquals("Failure", e.getMessage());
}

verify(cluster, times(0)).getLocalNode();
Assert.assertEquals(0, startLatch.getCount());
Assert.assertEquals(0, finishLatch.getCount());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.technologicgroup.boost.chain;

import com.technologicgroup.boost.chain.mock.TestClusterTask;
import com.technologicgroup.boost.chain.mock.TestClusterTaskRev;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class ChainStepTest {

@Mock
private Chain chain;

@Test
public void testRun_OK() {
ChainStep<String, Integer> chainStep = new ChainStep<>(TestClusterTask.class, chain);
chainStep.run();

verify(chain, times(1)).run();
}

@Test
public void testCollect_OK() {
when(chain.run()).thenReturn(resultCollection());

ChainStep<String, Integer> chainStep = new ChainStep<>(TestClusterTask.class, chain);
int result = chainStep.collect((i) -> i.stream().reduce(0, Integer::sum));

verify(chain, times(1)).run();
Assert.assertEquals(6, result);
}

@Test
public void testReduceFunc_OK() {
when(chain.getSteps()).thenReturn(dummySteps());
when(chain.run()).thenReturn(resultCollection());

ChainStep<String, Integer> chainStep = new ChainStep<>(TestClusterTask.class, chain);
ChainStep<Integer, String> nextStep = chainStep.reduce((i) -> i.stream().reduce(0, Integer::sum));

verify(chain, times(1)).run();
verify(chain, times(1)).setArg(6);
Assert.assertEquals(1, chain.getSteps().size());
Assert.assertEquals(nextStep, chain.getSteps().get(0));
}

@Test
public void testReduceBean_OK() {
when(chain.getSteps()).thenReturn(dummySteps());
when(chain.run()).thenReturn(resultCollection());

ChainStep<String, Integer> chainStep = new ChainStep<>(TestClusterTask.class, chain);
ChainStep<Integer, String> nextStep = chainStep.reduce(TestClusterTaskRev.class);

verify(chain, times(1)).run();
Assert.assertNull(chain.getArg());
Assert.assertEquals(1, chain.getSteps().size());
Assert.assertEquals(nextStep, chain.getSteps().get(0));
Assert.assertEquals(TestClusterTaskRev.class, chain.getSteps().get(0).getBean());
}

@Test
public void testReduceCollectBean_OK() {
when(chain.getSteps()).thenReturn(dummySteps());
when(chain.run()).thenReturn(resultCollection());

ChainStep<String, Integer> chainStep = new ChainStep<>(TestClusterTask.class, chain);
ChainStep<Integer, String> nextStep = chainStep.reduce((i) -> i.stream().reduce(0, Integer::sum), TestClusterTaskRev.class);

verify(chain, times(1)).setArg(6);
verify(chain, times(1)).run();
Assert.assertEquals(1, chain.getSteps().size());
Assert.assertEquals(nextStep, chain.getSteps().get(0));
Assert.assertEquals(TestClusterTaskRev.class, chain.getSteps().get(0).getBean());
}

@Test
public void testMap_OK() {
when(chain.getSteps()).thenReturn(new ArrayList<>());

ChainStep<String, Integer> chainStep = new ChainStep<>(TestClusterTask.class, chain);
ChainStep<Integer, String> nextStep = chainStep.map(TestClusterTaskRev.class);

verify(chain, times(0)).run();
Assert.assertEquals(1, chain.getSteps().size());
Assert.assertEquals(nextStep, chain.getSteps().get(0));
}

@Test
public void testFilter_OK() {
when(chain.getSteps()).thenReturn(new ArrayList<>());
when(chain.run()).thenReturn(resultCollection());

ChainStep<String, Integer> chainStep = new ChainStep<>(TestClusterTask.class, chain);
ChainStep<?, ?> nextStep = chainStep.filter((res) -> res.getResult() > 0);

verify(chain, times(1)).run();
Assert.assertEquals(1, chain.getSteps().size());
Assert.assertEquals(nextStep, chain.getSteps().get(0));
Assert.assertEquals(ChainFilterBean.class, chain.getSteps().get(0).getBean());
}

private List<ChainResult<Object>> resultCollection() {
return Arrays.asList(
new ChainResult<>("", 1),
new ChainResult<>("", 5));
}

private List<ChainStep<?, ?>> dummySteps() {
List<ChainStep<?, ?>> steps = new ArrayList<>();
steps.add(null);
steps.add(null);
return steps;
}
}
Loading

0 comments on commit 4a1e150

Please sign in to comment.