From fd9120c28bd84a8f88a49798e1290d32c16a024d Mon Sep 17 00:00:00 2001 From: Andy LoPresto Date: Tue, 10 May 2016 16:08:02 -0700 Subject: [PATCH] NIFI-1822 Added unit test skeleton for pooled script processor execution. --- .../script/ExecuteScriptGroovyTest.groovy | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/test/java/org/apache/nifi/processors/script/ExecuteScriptGroovyTest.groovy diff --git a/nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/test/java/org/apache/nifi/processors/script/ExecuteScriptGroovyTest.groovy b/nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/test/java/org/apache/nifi/processors/script/ExecuteScriptGroovyTest.groovy new file mode 100644 index 0000000000..479ab6ff6f --- /dev/null +++ b/nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/test/java/org/apache/nifi/processors/script/ExecuteScriptGroovyTest.groovy @@ -0,0 +1,71 @@ +package org.apache.nifi.processors.script + +import org.apache.nifi.security.util.EncryptionMethod +import org.junit.After +import org.junit.Before +import org.junit.BeforeClass +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.JUnit4 +import org.python.bouncycastle.util.encoders.Hex +import org.slf4j.Logger +import org.slf4j.LoggerFactory + +import javax.crypto.Cipher + +@RunWith(JUnit4.class) +class ExecuteScriptGroovyTest extends GroovyTestCase { + private static final Logger logger = LoggerFactory.getLogger(ExecuteScriptGroovyTest.class) + + @BeforeClass + public static void setUpOnce() throws Exception { + logger.metaClass.methodMissing = { String name, args -> + logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") + } + } + + @Before + public void setUp() throws Exception { + + } + + @After + public void tearDown() throws Exception { + + } + + @Test + public void testShouldExecuteScript() throws Exception { + // Arrange + final String PASSWORD = "shortPassword"; + final byte[] SALT = cipherProvider.generateSalt() + + final String plaintext = "This is a plaintext message."; + + // Act + for (EncryptionMethod em : strongKDFEncryptionMethods) { + logger.info("Using algorithm: ${em.getAlgorithm()}"); + + // Initialize a cipher for encryption + Cipher cipher = cipherProvider.getCipher(em, PASSWORD, SALT, DEFAULT_KEY_LENGTH, true); + byte[] iv = cipher.getIV(); + logger.info("IV: ${Hex.encodeHexString(iv)}") + + byte[] cipherBytes = cipher.doFinal(plaintext.getBytes("UTF-8")); + logger.info("Cipher text: ${Hex.encodeHexString(cipherBytes)} ${cipherBytes.length}"); + + cipher = cipherProvider.getCipher(em, PASSWORD, SALT, iv, DEFAULT_KEY_LENGTH, false); + byte[] recoveredBytes = cipher.doFinal(cipherBytes); + String recovered = new String(recoveredBytes, "UTF-8"); + logger.info("Recovered: ${recovered}") + + // Assert + assert plaintext.equals(recovered); + } + } + + //testShouldExecuteScriptWithPool + //testShouldHandleFailingScript + //testShouldHandleNoAvailableEngine + //testPooledExecutionShouldBeFaster +}