NIFI-10387: Fixed negative logic bug in scripted components (#6325)

This commit is contained in:
Matt Burgess 2022-08-23 13:53:12 -04:00 committed by GitHub
parent 90aa778a6c
commit 6f0ca87304
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 10 deletions

View File

@ -241,9 +241,9 @@ public class BaseScriptedLookupService extends AbstractScriptedControllerService
public void setup() {
if (scriptNeedsReload.get() || lookupService.get() == null) {
if (ScriptingComponentHelper.isFile(scriptingComponentHelper.getScriptPath())) {
scriptNeedsReload.set(reloadScriptFile(scriptingComponentHelper.getScriptPath()));
scriptNeedsReload.set(!reloadScriptFile(scriptingComponentHelper.getScriptPath()));
} else {
scriptNeedsReload.set(reloadScriptBody(scriptingComponentHelper.getScriptBody()));
scriptNeedsReload.set(!reloadScriptBody(scriptingComponentHelper.getScriptBody()));
}
}
}

View File

@ -225,9 +225,9 @@ public class InvokeScriptedProcessor extends AbstractSessionFactoryProcessor {
public void setup() {
if (scriptNeedsReload.get() || processor.get() == null) {
if (ScriptingComponentHelper.isFile(scriptingComponentHelper.getScriptPath())) {
scriptNeedsReload.set(reloadScriptFile(scriptingComponentHelper.getScriptPath()));
scriptNeedsReload.set(!reloadScriptFile(scriptingComponentHelper.getScriptPath()));
} else {
scriptNeedsReload.set(reloadScriptBody(scriptingComponentHelper.getScriptBody()));
scriptNeedsReload.set(!reloadScriptBody(scriptingComponentHelper.getScriptBody()));
}
}
}

View File

@ -107,9 +107,9 @@ public class ScriptedRecordSink extends AbstractScriptedControllerService implem
public void setup() {
if (scriptNeedsReload.get() || recordSink.get() == null) {
if (ScriptingComponentHelper.isFile(scriptingComponentHelper.getScriptPath())) {
scriptNeedsReload.set(reloadScriptFile(scriptingComponentHelper.getScriptPath()));
scriptNeedsReload.set(!reloadScriptFile(scriptingComponentHelper.getScriptPath()));
} else {
scriptNeedsReload.set(reloadScriptBody(scriptingComponentHelper.getScriptBody()));
scriptNeedsReload.set(!reloadScriptBody(scriptingComponentHelper.getScriptBody()));
}
}
}

View File

@ -78,9 +78,9 @@ public class ScriptedRulesEngine extends AbstractScriptedControllerService imple
public void setup() {
if (scriptNeedsReload.get() || rulesEngine.get() == null) {
if (ScriptingComponentHelper.isFile(scriptingComponentHelper.getScriptPath())) {
scriptNeedsReload.set(reloadScriptFile(scriptingComponentHelper.getScriptPath()));
scriptNeedsReload.set(!reloadScriptFile(scriptingComponentHelper.getScriptPath()));
} else {
scriptNeedsReload.set(reloadScriptBody(scriptingComponentHelper.getScriptBody()));
scriptNeedsReload.set(!reloadScriptBody(scriptingComponentHelper.getScriptBody()));
}
}
}

View File

@ -80,9 +80,9 @@ public class ScriptedActionHandler extends AbstractScriptedControllerService imp
public void setup() {
if (scriptNeedsReload.get() || actionHandler.get() == null) {
if (ScriptingComponentHelper.isFile(scriptingComponentHelper.getScriptPath())) {
scriptNeedsReload.set(reloadScriptFile(scriptingComponentHelper.getScriptPath()));
scriptNeedsReload.set(!reloadScriptFile(scriptingComponentHelper.getScriptPath()));
} else {
scriptNeedsReload.set(reloadScriptBody(scriptingComponentHelper.getScriptBody()));
scriptNeedsReload.set(!reloadScriptBody(scriptingComponentHelper.getScriptBody()));
}
}
}

View File

@ -46,8 +46,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue
*/
class TestScriptedLookupService {
private static final String GROOVY_SCRIPT = "test_lookup_inline.groovy"
private static final String ALTERNATE_GROOVY_SCRIPT = "test_simple_lookup_inline.groovy"
private static final Path SOURCE_PATH = Paths.get("src/test/resources/groovy", GROOVY_SCRIPT)
private static final Path ALTERNATE_SOURCE_PATH = Paths.get("src/test/resources/groovy", ALTERNATE_GROOVY_SCRIPT)
private static final Path TARGET_PATH = Paths.get("target", GROOVY_SCRIPT)
private static final Path ALTERNATE_TARGET_PATH = Paths.get("target", ALTERNATE_GROOVY_SCRIPT)
private static final Logger logger = LoggerFactory.getLogger(TestScriptedLookupService)
ScriptedLookupService scriptedLookupService
def scriptingComponent
@ -59,7 +62,9 @@ class TestScriptedLookupService {
logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}")
}
Files.copy(SOURCE_PATH, TARGET_PATH, StandardCopyOption.REPLACE_EXISTING)
Files.copy(ALTERNATE_SOURCE_PATH, ALTERNATE_TARGET_PATH, StandardCopyOption.REPLACE_EXISTING)
TARGET_PATH.toFile().deleteOnExit()
ALTERNATE_TARGET_PATH.toFile().deleteOnExit()
}
@BeforeEach
@ -96,6 +101,40 @@ class TestScriptedLookupService {
assertFalse(opt.present)
}
@Test
void testLookupServiceScriptReload() {
final TestRunner runner = TestRunners.newTestRunner(new AbstractProcessor() {
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
}
});
runner.addControllerService("lookupService", scriptedLookupService)
runner.setProperty(scriptedLookupService, "Script Engine", "Groovy")
runner.setProperty(scriptedLookupService, ScriptingComponentUtils.SCRIPT_BODY, (String) null)
runner.setProperty(scriptedLookupService, ScriptingComponentUtils.SCRIPT_FILE, ALTERNATE_TARGET_PATH.toString())
runner.setProperty(scriptedLookupService, ScriptingComponentUtils.MODULES, (String) null)
// This call to setup should fail loading the script but should mark that the script should be reloaded
scriptedLookupService.setup()
// This prevents the (lookupService == null) check from passing, in order to force the reload from the
// scriptNeedsReload variable specifically
scriptedLookupService.lookupService.set(new MockScriptedLookupService())
runner.enableControllerService(scriptedLookupService)
MockFlowFile mockFlowFile = new MockFlowFile(1L)
InputStream inStream = new ByteArrayInputStream('Flow file content not used'.bytes)
Optional opt = scriptedLookupService.lookup(['key':'Hello'])
assertTrue(opt.present)
assertEquals('Hi', opt.get())
opt = scriptedLookupService.lookup(['key':'World'])
assertTrue(opt.present)
assertEquals('there', opt.get())
opt = scriptedLookupService.lookup(['key':'Not There'])
assertFalse(opt.present)
}
class MockScriptedLookupService extends ScriptedLookupService implements AccessibleScriptingComponentHelper {
@Override