MongoDbInterpreter extends Interpreter { @Override public InterpreterResult interpret(String script, InterpreterContext context) { LOGGER.debug("Run MongoDB script: {}", script); if (StringUtils.isEmpty(script)) { return new InterpreterResult(Code.SUCCESS); } String paragraphId = context.getParagraphId(); final File scriptFile = new File(getScriptFileName(paragraphId)); try { FileUtils.write(scriptFile, shellExtension + script); } catch (IOException e) { LOGGER.error("Can not write script in temp file", e); return new InterpreterResult(Code.ERROR, e.getMessage()); } InterpreterResult result = new InterpreterResult(InterpreterResult.Code.SUCCESS); final DefaultExecutor executor = new DefaultExecutor(); final ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(context.out, errorStream)); executor.setWatchdog(new ExecuteWatchdog(commandTimeout)); final CommandLine cmdLine = CommandLine.parse(getProperty("mongo.shell.path")); cmdLine.addArgument("--quiet", false); cmdLine.addArgument(dbAddress, false); cmdLine.addArgument(scriptFile.getAbsolutePath(), false); try { executor.execute(cmdLine); runningProcesses.put(paragraphId, executor); } catch (ExecuteException e) { LOGGER.error("Can not run script in paragraph {}", paragraphId, e); final int exitValue = e.getExitValue(); Code code = Code.ERROR; String msg = errorStream.toString(); if (exitValue == SIGTERM_CODE) { code = Code.INCOMPLETE; msg = msg + "Paragraph received a SIGTERM.\n"; LOGGER.info("The paragraph {} stopped executing: {}", paragraphId, msg); } msg += "ExitValue: " + exitValue; result = new InterpreterResult(code, msg); } catch (IOException e) { LOGGER.error("Can not run script in paragraph {}", paragraphId, e); result = new InterpreterResult(Code.ERROR, e.getMessage()); } finally { FileUtils.deleteQuietly(scriptFile); stopProcess(paragraphId); } return result; } MongoDbInterpreter(Properties property); @Override void open(); @Override void close(); @Override FormType getFormType(); @Override InterpreterResult interpret(String script, InterpreterContext context); @Override int getProgress(InterpreterContext context); @Override void cancel(InterpreterContext context); @Override Scheduler getScheduler();  }
@Test public void testSuccess() { final String userScript = "print('hello');"; final InterpreterResult res = interpreter.interpret(userScript, context); assertSame("Check SUCCESS: " + res.message(), Code.SUCCESS, res.code()); try { out.flush(); } catch (IOException ex) { System.out.println(ex.getMessage()); } final String resultScript = new String(getBufferBytes()); final String expectedScript = SHELL_EXTENSION.replace( "TABLE_LIMIT_PLACEHOLDER", interpreter.getProperty("mongo.shell.command.table.limit")) .replace("TARGET_DB_PLACEHOLDER", interpreter.getProperty("mongo.server.database")) .replace("USER_NAME_PLACEHOLDER", interpreter.getProperty("mongo.server.username")) .replace("PASSWORD_PLACEHOLDER", interpreter.getProperty("mongo.server.password")) .replace("AUTH_DB_PLACEHOLDER", interpreter. getProperty("mongo.server.authenticationDatabase"))+ userScript; assertEquals("Check SCRIPT", expectedScript, resultScript); }
@Test public void testBadConf() { props.setProperty("mongo.shell.path", "/bad/path/to/mongo"); final InterpreterResult res = interpreter.interpret("print('hello')", context); assertSame(Code.ERROR, res.code()); }