Skip to content

Commit

Permalink
script runner tests and ability to capture implicit value of last sta…
Browse files Browse the repository at this point in the history
…tement
  • Loading branch information
JuicyDragon committed Aug 10, 2023
1 parent 6eef654 commit d40d862
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import org.jetbrains.annotations.NotNull;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;
import org.jruby.embed.internal.BiVariableMap;

import javax.swing.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/***
Expand All @@ -40,9 +42,7 @@ public void write(@NotNull char[] cbuf, int off, int len) throws IOException {
char[] subchars = new char[len];
System.arraycopy(cbuf, off, subchars, 0, len);
String value = new String(subchars);
SwingUtilities.invokeLater(() -> {
consumer.accept(value);
});
consumer.accept(value);
}
}
}
Expand All @@ -61,10 +61,20 @@ public void close() throws IOException {
protected Thread scriptThread;
protected Consumer<String> standardOutput;
protected Consumer<String> errorOutput;
protected Consumer<Object> completedCallback;

public RubyScriptRunner() {
}

/***
* Allows you to provide a callback to be invoked when script finishes.
* @param completedCallback A {@link BiConsumer} that accepts an Object (the final returned value) and
* a Map containing all the variables in the scripting container upon completion.
*/
public void whenScriptCompletes(Consumer<Object> completedCallback) {
this.completedCallback = completedCallback;
}

/***
* Interrupts running script thread if there is one running. See also {@link #isAlive()}.
*/
Expand Down Expand Up @@ -138,7 +148,8 @@ public void runScriptAsync(String script, String nuixVersion, Map<String, Object
initialize(nuixVersion, variables);

scriptThread = new Thread(() -> {
scriptingContainer.runScriptlet(script);
Object returnedValue = scriptingContainer.runScriptlet(script);
fireCompletedCallback(returnedValue);
});

scriptThread.start();
Expand All @@ -156,7 +167,8 @@ public void runFileAsync(File scriptFile, String nuixVersion, Map<String, Object

scriptThread = new Thread(() -> {
try (InputStream scriptFileInputStream = FileUtils.openInputStream(scriptFile)) {
scriptingContainer.runScriptlet(scriptFileInputStream, scriptFile.getAbsolutePath());
Object returnedValue = scriptingContainer.runScriptlet(scriptFileInputStream, scriptFile.getAbsolutePath());
fireCompletedCallback(returnedValue);
} catch (Exception exc) {
errorOutput.accept(ExceptionUtils.getMessage(exc) + "\n" + ExceptionUtils.getStackTrace(exc));
}
Expand Down Expand Up @@ -194,4 +206,10 @@ private void initialize(String nuixVersion, Map<String, Object> variablesToSet)
scriptingContainer.put(variableToSet.getKey(), variableToSet.getValue());
}
}

private void fireCompletedCallback(Object returnedValue) {
if(completedCallback != null) {
completedCallback.accept(returnedValue);
}
}
}
48 changes: 48 additions & 0 deletions Nx/src/test/java/RubyScriptRunnerTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import com.nuix.innovation.enginewrapper.RubyScriptRunner;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RubyScriptRunnerTests {
@Test
public void RunScript() throws Exception {
// Validate that script runs and output is captured
String script = "puts 'hello'";
List<String> output = new ArrayList<>();
RubyScriptRunner rubyScriptRunner = new RubyScriptRunner();
rubyScriptRunner.setStandardOutputConsumer(output::add);
rubyScriptRunner.setErrorOutputConsumer(output::add);
rubyScriptRunner.runScriptAsync(script, "0.0.0.0", Map.of());
rubyScriptRunner.join();
assertTrue(output.size() > 0);
}

@Test
public void ReturnedValue() throws Exception {
// Test that returned value from last statement is captured
String script = "response['sum'] = 10 + 15 + 15 + 2";
List<String> output = new ArrayList<>();
RubyScriptRunner rubyScriptRunner = new RubyScriptRunner();

rubyScriptRunner.whenScriptCompletes((ret) -> {
assertEquals(42L, ret);
});

Map<String, Object> varContainer = new HashMap<>();
Map<String, Object> response = new HashMap<>();
varContainer.put("response", response);

rubyScriptRunner.setStandardOutputConsumer(output::add);
rubyScriptRunner.setErrorOutputConsumer(output::add);
rubyScriptRunner.runScriptAsync(script, "0.0.0.0", varContainer);
rubyScriptRunner.join();

assertEquals(42L, response.get("sum"));
}
}

0 comments on commit d40d862

Please sign in to comment.