-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
453 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
cluster-boost-core/src/test/java/com/technologicgroup/boost/Fixtures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
cluster-boost-core/src/test/java/com/technologicgroup/boost/audit/AuditServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
cluster-boost-core/src/test/java/com/technologicgroup/boost/chain/ChainBeanTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
124 changes: 124 additions & 0 deletions
124
cluster-boost-core/src/test/java/com/technologicgroup/boost/chain/ChainStepTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.