NIFI-8776: Using mocks in Test*IgniteCache
Signed-off-by: Matthew Burgess <mattyb149@apache.org>
This closes #5211
diff --git a/nifi-nar-bundles/nifi-ignite-bundle/nifi-ignite-processors/src/test/java/org/apache/nifi/processors/ignite/cache/TestGetIgniteCache.java b/nifi-nar-bundles/nifi-ignite-bundle/nifi-ignite-processors/src/test/java/org/apache/nifi/processors/ignite/cache/TestGetIgniteCache.java
index a48d9e5..1c2dc32 100644
--- a/nifi-nar-bundles/nifi-ignite-bundle/nifi-ignite-processors/src/test/java/org/apache/nifi/processors/ignite/cache/TestGetIgniteCache.java
+++ b/nifi-nar-bundles/nifi-ignite-bundle/nifi-ignite-processors/src/test/java/org/apache/nifi/processors/ignite/cache/TestGetIgniteCache.java
@@ -16,26 +16,30 @@
*/
package org.apache.nifi.processors.ignite.cache;
-import static org.junit.Assert.assertEquals;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.ArgumentMatchers;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import org.apache.ignite.Ignite;
-import org.apache.ignite.IgniteCache;
-import org.apache.ignite.Ignition;
-import org.apache.nifi.util.MockFlowFile;
-import org.apache.nifi.util.TestRunner;
-import org.apache.nifi.util.TestRunners;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Assume;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.AdditionalMatchers.or;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class TestGetIgniteCache {
@@ -44,63 +48,45 @@
private GetIgniteCache getIgniteCache;
private Map<String,String> properties1;
private Map<String,String> properties2;
- private static Ignite ignite;
-
- // Check if the JDK running these tests is pre-Java 11, so that tests can be ignored if JDK is Java 11+
- // TODO Once a version of Ignite that supports Java 11 is released, this check can be removed.
- private static boolean preJava11;
- static {
- String javaVersion = System.getProperty("java.version");
- preJava11 = Integer.parseInt(javaVersion.substring(0, javaVersion.indexOf('.'))) < 11;
- }
-
- @BeforeClass
- public static void setUpClass() {
- if (preJava11) {
- ignite = Ignition.start("test-ignite.xml");
- }
- }
-
- @AfterClass
- public static void tearDownClass() {
- if (preJava11) {
- ignite.close();
- Ignition.stop(true);
- }
- }
+ private Ignite ignite;
@Before
- public void setUp() throws IOException {
- if (preJava11) {
- getIgniteCache = new GetIgniteCache() {
- @Override
- protected Ignite getIgnite() {
- return TestGetIgniteCache.ignite;
- }
+ public void setUp() {
+ ignite = mock(Ignite.class);
+ final IgniteCache igniteCache = mockIgniteCache();
+ when(ignite.getOrCreateCache(or(ArgumentMatchers.eq(CACHE_NAME), isNull()))).thenReturn(igniteCache);
+ getIgniteCache = new GetIgniteCache() {
+ @Override
+ protected Ignite getIgnite() {
+ return ignite;
+ }
- };
+ };
- properties1 = new HashMap<String,String>();
- properties1.put("igniteKey", "key1");
- properties2 = new HashMap<String,String>();
- properties2.put("igniteKey", "key2");
- }
+ properties1 = new HashMap<String,String>();
+ properties1.put("igniteKey", "key1");
+ properties2 = new HashMap<String,String>();
+ properties2.put("igniteKey", "key2");
}
@After
public void teardown() {
- if (preJava11) {
- Assume.assumeTrue(preJava11);
- getRunner = null;
- ignite.destroyCache(CACHE_NAME);
- }
+ getRunner = null;
+ }
+
+ static IgniteCache<Object, Object> mockIgniteCache() {
+ final IgniteCache<Object, Object> igniteCache = mock(IgniteCache.class);
+ final Map<Object, Object> map = new HashMap<>();
+ doAnswer(args -> map.put(args.getArgument(0), args.getArgument(1))).when(igniteCache).put(any(), any());
+ when(igniteCache.get(any())).thenAnswer(args -> map.get(args.getArgument(0)));
+ when(igniteCache.containsKey(anyString())).thenAnswer(args -> map.containsKey(args.getArgument(0)));
+
+ return igniteCache;
}
@Test
- public void testGetIgniteCacheDefaultConfOneFlowFileWithPlainKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testGetIgniteCacheDefaultConfOneFlowFileWithPlainKey() throws IOException {
getRunner = TestRunners.newTestRunner(getIgniteCache);
getRunner.setProperty(GetIgniteCache.IGNITE_CACHE_ENTRY_KEY, "mykey");
@@ -127,12 +113,11 @@
@Test
public void testGetIgniteCacheNullGetCacheThrowsException() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
getIgniteCache = new GetIgniteCache() {
@Override
protected Ignite getIgnite() {
- return TestGetIgniteCache.ignite;
+ return ignite;
}
@Override
@@ -166,8 +151,6 @@
@Test
public void testGetIgniteCacheDefaultConfOneFlowFileWithKeyExpression() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
getRunner = TestRunners.newTestRunner(getIgniteCache);
getRunner.setProperty(GetIgniteCache.CACHE_NAME, CACHE_NAME);
getRunner.setProperty(GetIgniteCache.IGNITE_CACHE_ENTRY_KEY, "${igniteKey}");
@@ -195,8 +178,6 @@
@Test
public void testGetIgniteCacheDefaultConfTwoFlowFilesWithExpressionKeys() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
getRunner = TestRunners.newTestRunner(getIgniteCache);
getRunner.setProperty(GetIgniteCache.CACHE_NAME, CACHE_NAME);
getRunner.setProperty(GetIgniteCache.IGNITE_CACHE_ENTRY_KEY, "${igniteKey}");
@@ -235,8 +216,6 @@
@Test
public void testGetIgniteCacheDefaultConfOneFlowFileNoKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
getRunner = TestRunners.newTestRunner(getIgniteCache);
getRunner.setProperty(GetIgniteCache.IGNITE_CACHE_ENTRY_KEY, "${igniteKey}");
@@ -264,8 +243,6 @@
@Test
public void testGetIgniteCacheDefaultConfTwoFlowFilesNoKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
getRunner = TestRunners.newTestRunner(getIgniteCache);
getRunner.setProperty(GetIgniteCache.IGNITE_CACHE_ENTRY_KEY, "${igniteKey}");
@@ -296,8 +273,6 @@
@Test
public void testGetIgniteCacheDefaultConfTwoFlowFileFirstNoKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
getRunner = TestRunners.newTestRunner(getIgniteCache);
getRunner.setProperty(GetIgniteCache.CACHE_NAME, CACHE_NAME);
getRunner.setProperty(GetIgniteCache.IGNITE_CACHE_ENTRY_KEY, "${igniteKey}");
@@ -329,9 +304,7 @@
}
@Test
- public void testGetIgniteCacheDefaultConfTwoFlowFileSecondNoKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testGetIgniteCacheDefaultConfTwoFlowFileSecondNoKey() throws IOException {
getRunner = TestRunners.newTestRunner(getIgniteCache);
getRunner.setProperty(GetIgniteCache.CACHE_NAME, CACHE_NAME);
getRunner.setProperty(GetIgniteCache.IGNITE_CACHE_ENTRY_KEY, "${igniteKey}");
@@ -366,8 +339,6 @@
@Test
public void testGetIgniteCacheDefaultConfThreeFlowFilesOneOkSecondOkThirdNoExpressionKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
getRunner = TestRunners.newTestRunner(getIgniteCache);
getRunner.setProperty(GetIgniteCache.CACHE_NAME, CACHE_NAME);
getRunner.setProperty(GetIgniteCache.IGNITE_CACHE_ENTRY_KEY, "${igniteKey}");
diff --git a/nifi-nar-bundles/nifi-ignite-bundle/nifi-ignite-processors/src/test/java/org/apache/nifi/processors/ignite/cache/TestPutIgniteCache.java b/nifi-nar-bundles/nifi-ignite-bundle/nifi-ignite-processors/src/test/java/org/apache/nifi/processors/ignite/cache/TestPutIgniteCache.java
index 21b31c4..dab7b32 100644
--- a/nifi-nar-bundles/nifi-ignite-bundle/nifi-ignite-processors/src/test/java/org/apache/nifi/processors/ignite/cache/TestPutIgniteCache.java
+++ b/nifi-nar-bundles/nifi-ignite-bundle/nifi-ignite-processors/src/test/java/org/apache/nifi/processors/ignite/cache/TestPutIgniteCache.java
@@ -16,26 +16,36 @@
*/
package org.apache.nifi.processors.ignite.cache;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
+import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.ignite.Ignite;
-import org.apache.ignite.Ignition;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteDataStreamer;
+import org.apache.ignite.lang.IgniteFuture;
import org.apache.nifi.util.MockFlowFile;
import org.apache.nifi.util.TestRunner;
import org.apache.nifi.util.TestRunners;
import org.junit.After;
-import org.junit.AfterClass;
import org.junit.Assert;
-import org.junit.Assume;
import org.junit.Before;
-import org.junit.BeforeClass;
import org.junit.Test;
+import org.mockito.ArgumentMatchers;
+
+import java.io.IOException;
+import java.util.AbstractMap;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.mockito.AdditionalMatchers.or;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyBoolean;
+import static org.mockito.ArgumentMatchers.isNull;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class TestPutIgniteCache {
@@ -44,66 +54,54 @@
private PutIgniteCache putIgniteCache;
private Map<String,String> properties1;
private Map<String,String> properties2;
- private static Ignite ignite;
-
- // Check if the JDK running these tests is pre-Java 11, so that tests can be ignored if JDK is Java 11+
- // TODO Once a version of Ignite that supports Java 11 is released, this check can be removed.
- private static boolean preJava11;
- static {
- String javaVersion = System.getProperty("java.version");
- preJava11 = Integer.parseInt(javaVersion.substring(0, javaVersion.indexOf('.'))) < 11;
- }
-
- @BeforeClass
- public static void setUpClass() {
- if (preJava11) {
- List<Ignite> grids = Ignition.allGrids();
- if ( grids.size() == 1 )
- ignite = grids.get(0);
- else
- ignite = Ignition.start("test-ignite.xml");
- }
-
- }
-
- @AfterClass
- public static void tearDownClass() {
- if (preJava11) {
- if ( ignite != null )
- ignite.close();
- Ignition.stop(true);
- }
- }
+ private Ignite ignite;
@Before
- public void setUp() throws IOException {
- if (preJava11) {
- putIgniteCache = new PutIgniteCache() {
- @Override
- protected Ignite getIgnite() {
- return TestPutIgniteCache.ignite;
- }
+ public void setUp() {
+ ignite = mock(Ignite.class);
+ final IgniteCache igniteCache = TestGetIgniteCache.mockIgniteCache();
+ when(igniteCache.getName()).thenReturn(CACHE_NAME);
+ when(ignite.getOrCreateCache(or(ArgumentMatchers.eq(CACHE_NAME), isNull()))).thenReturn(igniteCache);
+ final IgniteDataStreamer dataStreamer = mock(IgniteDataStreamer.class);
+ final IgniteFuture igniteFuture = mock(IgniteFuture.class);
+ when(igniteFuture.get()).thenReturn(new Object());
- };
- properties1 = new HashMap<String,String>();
- properties1.put("igniteKey", "key1");
- properties2 = new HashMap<String,String>();
- properties2.put("igniteKey", "key2");
- }
+ final MutableBoolean allowOverride = new MutableBoolean(false);
+ doAnswer(args -> {
+ allowOverride.setValue((boolean) args.getArgument(0));
+ return null;
+ }).when(dataStreamer).allowOverwrite(anyBoolean());
+
+ when(dataStreamer.addData(any(Collection.class))).thenAnswer(args -> {
+ ((Collection)args.getArgument(0)).forEach(entry -> {
+ final AbstractMap.SimpleEntry simpleEntry = (AbstractMap.SimpleEntry) entry;
+ if (allowOverride.booleanValue() || !igniteCache.containsKey(simpleEntry.getKey())) {
+ igniteCache.put(simpleEntry.getKey(), simpleEntry.getValue());
+ }
+ });
+ return igniteFuture;
+ });
+ when(ignite.dataStreamer(CACHE_NAME)).thenReturn(dataStreamer);
+ putIgniteCache = new PutIgniteCache() {
+ @Override
+ protected Ignite getIgnite() {
+ return ignite;
+ }
+
+ };
+ properties1 = new HashMap<String,String>();
+ properties1.put("igniteKey", "key1");
+ properties2 = new HashMap<String,String>();
+ properties2.put("igniteKey", "key2");
}
@After
public void teardown() {
- if (preJava11) {
- runner = null;
- ignite.destroyCache(CACHE_NAME);
- }
+ runner = null;
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationOneFlowFileWithPlainKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationOneFlowFileWithPlainKey() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -133,9 +131,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationOneFlowFile() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationOneFlowFile() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -165,9 +161,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesAllowOverrideDefaultFalse() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesAllowOverrideDefaultFalse() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -210,9 +204,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesAllowOverrideTrue() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesAllowOverrideTrue() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -257,9 +249,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationOneFlowFileNoKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationOneFlowFileNoKey() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -291,9 +281,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationOneFlowFileNoBytes() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationOneFlowFileNoBytes() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -324,9 +312,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFiles() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFiles() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -369,9 +355,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesNoKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesNoKey() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -417,9 +401,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFileFirstNoKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFileFirstNoKey() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -462,9 +444,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFileSecondNoKey() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFileSecondNoKey() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -509,9 +489,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesOneNoKeyOneNoBytes() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesOneNoKeyOneNoBytes() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);
@@ -557,9 +535,7 @@
}
@Test
- public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesOneNoKeySecondOkThirdNoBytes() throws IOException, InterruptedException {
- Assume.assumeTrue(preJava11);
-
+ public void testPutIgniteCacheOnTriggerDefaultConfigurationTwoFlowFilesOneNoKeySecondOkThirdNoBytes() throws IOException {
runner = TestRunners.newTestRunner(putIgniteCache);
runner.setProperty(PutIgniteCache.BATCH_SIZE, "5");
runner.setProperty(PutIgniteCache.CACHE_NAME, CACHE_NAME);