Search in sources :

Example 26 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class MagicUndefineGlobalTest method testWithNoVariable.

@Test
public void testWithNoVariable() {
    // define a global
    executor.getGlobalConfig().put("global", 22);
    assertEquals(1, executor.getGlobalConfig().size());
    // no arg specifying the var to undefine
    StellarResult result = magic.execute("%undefine", executor);
    // validate the result
    assertTrue(result.isError());
    assertTrue(result.getException().isPresent());
    // ensure that the global variables were not changed
    assertEquals(1, executor.getGlobalConfig().size());
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Test(org.junit.Test)

Example 27 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class MagicUndefineGlobalTest method testUndefine.

@Test
public void testUndefine() {
    // define a global
    executor.getGlobalConfig().put("global", 22);
    assertEquals(1, executor.getGlobalConfig().size());
    // use the magic to undefine the global variable
    StellarResult result = magic.execute("%undefine global", executor);
    // validate the result
    assertTrue(result.isSuccess());
    assertTrue(result.getValue().isPresent());
    // ensure that the global variable does not exist
    assertEquals(0, executor.getGlobalConfig().size());
}
Also used : StellarResult(org.apache.metron.stellar.common.shell.StellarResult) Test(org.junit.Test)

Example 28 with StellarResult

use of org.apache.metron.stellar.common.shell.StellarResult in project metron by apache.

the class StellarInterpreter method interpret.

@Override
public InterpreterResult interpret(final String input, InterpreterContext context) {
    InterpreterResult result;
    try {
        // execute the input
        StellarResult stellarResult = executor.execute(input);
        if (stellarResult.isSuccess()) {
            // on success - if no result, use a blank value
            Object value = stellarResult.getValue().orElse("");
            String text = value.toString();
            result = new InterpreterResult(SUCCESS, TEXT, text);
        } else if (stellarResult.isError()) {
            // an error occurred
            Optional<Throwable> e = stellarResult.getException();
            String message = getErrorMessage(e, input);
            result = new InterpreterResult(ERROR, TEXT, message);
        } else {
            // should never happen
            throw new IllegalStateException("Unexpected error. result=" + stellarResult);
        }
    } catch (Throwable t) {
        // unexpected exception
        String message = getErrorMessage(Optional.of(t), input);
        result = new InterpreterResult(ERROR, TEXT, message);
    }
    return result;
}
Also used : Optional(java.util.Optional) StellarResult(org.apache.metron.stellar.common.shell.StellarResult) InterpreterResult(org.apache.zeppelin.interpreter.InterpreterResult)

Aggregations

StellarResult (org.apache.metron.stellar.common.shell.StellarResult)28 Test (org.junit.Test)23 Optional (java.util.Optional)2 StellarAssignment (org.apache.metron.stellar.common.StellarAssignment)2 Spliterator (java.util.Spliterator)1 Function (java.util.function.Function)1 StreamSupport (java.util.stream.StreamSupport)1 StringUtils (org.apache.commons.lang3.StringUtils)1 StellarResult.error (org.apache.metron.stellar.common.shell.StellarResult.error)1 StellarResult.success (org.apache.metron.stellar.common.shell.StellarResult.success)1 StellarShellExecutor (org.apache.metron.stellar.common.shell.StellarShellExecutor)1 StellarFunctionInfo (org.apache.metron.stellar.dsl.StellarFunctionInfo)1 InterpreterResult (org.apache.zeppelin.interpreter.InterpreterResult)1