@SuppressWarnings("deprecation") @Test void testRunScriptMissingParameterValues() { String scriptName = "MyScript"; ScriptParameter scriptParameter = when(mock(ScriptParameter.class).getName()).thenReturn("param0").getMock(); Script script = mock(Script.class); when(script.getParameters()).thenReturn(singletonList(scriptParameter)); @SuppressWarnings("unchecked") Query<Script> query = mock(Query.class, RETURNS_SELF); when(query.eq("name", scriptName).findOne()).thenReturn(script); when(dataService.query("sys_scr_Script", Script.class)).thenReturn(query); assertThrows( GenerateScriptException.class, () -> savedScriptRunner.runScript(scriptName, emptyMap())); }
public ScriptResult runScript(String scriptName, Map<String, Object> parameters) { Script script = dataService.query(SCRIPT, Script.class).eq(ScriptMetadata.NAME, scriptName).findOne(); if (script == null) { throw new UnknownEntityException(SCRIPT, scriptName); } if (script.getParameters() != null) { for (ScriptParameter param : script.getParameters()) { if (!parameters.containsKey(param.getName())) { throw new GenerateScriptException("Missing parameter [" + param + "]"); } } } Map<String, Object> scriptParameters = new HashMap<>(parameters); if (script.isGenerateToken()) { String token = tokenService.generateAndStoreToken( SecurityUtils.getCurrentUsername(), "For script " + script.getName()); scriptParameters.put("molgenisToken", token); } ScriptRunner scriptRunner = scriptRunnerFactory.getScriptRunner(script.getScriptType().getName()); FileMeta fileMeta = null; if (scriptRunner.hasFileOutput(script)) { String name = generateRandomString(); String resultFileExtension = script.getResultFileExtension(); if (resultFileExtension != null) { name += "." + script.getResultFileExtension(); } File file = fileStore.getFileUnchecked(name); scriptParameters.put("outputFile", file.getAbsolutePath()); fileMeta = createFileMeta(name, file); dataService.add(FILE_META, fileMeta); } String output = scriptRunner.runScript(script, scriptParameters); return new ScriptResult(fileMeta, output); }
SavedScriptRunner { public ScriptResult runScript(String scriptName, Map<String, Object> parameters) { Script script = dataService.query(SCRIPT, Script.class).eq(ScriptMetadata.NAME, scriptName).findOne(); if (script == null) { throw new UnknownEntityException(SCRIPT, scriptName); } if (script.getParameters() != null) { for (ScriptParameter param : script.getParameters()) { if (!parameters.containsKey(param.getName())) { throw new GenerateScriptException("Missing parameter [" + param + "]"); } } } Map<String, Object> scriptParameters = new HashMap<>(parameters); if (script.isGenerateToken()) { String token = tokenService.generateAndStoreToken( SecurityUtils.getCurrentUsername(), "For script " + script.getName()); scriptParameters.put("molgenisToken", token); } ScriptRunner scriptRunner = scriptRunnerFactory.getScriptRunner(script.getScriptType().getName()); FileMeta fileMeta = null; if (scriptRunner.hasFileOutput(script)) { String name = generateRandomString(); String resultFileExtension = script.getResultFileExtension(); if (resultFileExtension != null) { name += "." + script.getResultFileExtension(); } File file = fileStore.getFileUnchecked(name); scriptParameters.put("outputFile", file.getAbsolutePath()); fileMeta = createFileMeta(name, file); dataService.add(FILE_META, fileMeta); } String output = scriptRunner.runScript(script, scriptParameters); return new ScriptResult(fileMeta, output); } }
SavedScriptRunner { public ScriptResult runScript(String scriptName, Map<String, Object> parameters) { Script script = dataService.query(SCRIPT, Script.class).eq(ScriptMetadata.NAME, scriptName).findOne(); if (script == null) { throw new UnknownEntityException(SCRIPT, scriptName); } if (script.getParameters() != null) { for (ScriptParameter param : script.getParameters()) { if (!parameters.containsKey(param.getName())) { throw new GenerateScriptException("Missing parameter [" + param + "]"); } } } Map<String, Object> scriptParameters = new HashMap<>(parameters); if (script.isGenerateToken()) { String token = tokenService.generateAndStoreToken( SecurityUtils.getCurrentUsername(), "For script " + script.getName()); scriptParameters.put("molgenisToken", token); } ScriptRunner scriptRunner = scriptRunnerFactory.getScriptRunner(script.getScriptType().getName()); FileMeta fileMeta = null; if (scriptRunner.hasFileOutput(script)) { String name = generateRandomString(); String resultFileExtension = script.getResultFileExtension(); if (resultFileExtension != null) { name += "." + script.getResultFileExtension(); } File file = fileStore.getFileUnchecked(name); scriptParameters.put("outputFile", file.getAbsolutePath()); fileMeta = createFileMeta(name, file); dataService.add(FILE_META, fileMeta); } String output = scriptRunner.runScript(script, scriptParameters); return new ScriptResult(fileMeta, output); } SavedScriptRunner(
      ScriptRunnerFactory scriptRunnerFactory,
      DataService dataService,
      FileStore fileStore,
      TokenService tokenService,
      FileMetaFactory fileMetaFactory); }
SavedScriptRunner { public ScriptResult runScript(String scriptName, Map<String, Object> parameters) { Script script = dataService.query(SCRIPT, Script.class).eq(ScriptMetadata.NAME, scriptName).findOne(); if (script == null) { throw new UnknownEntityException(SCRIPT, scriptName); } if (script.getParameters() != null) { for (ScriptParameter param : script.getParameters()) { if (!parameters.containsKey(param.getName())) { throw new GenerateScriptException("Missing parameter [" + param + "]"); } } } Map<String, Object> scriptParameters = new HashMap<>(parameters); if (script.isGenerateToken()) { String token = tokenService.generateAndStoreToken( SecurityUtils.getCurrentUsername(), "For script " + script.getName()); scriptParameters.put("molgenisToken", token); } ScriptRunner scriptRunner = scriptRunnerFactory.getScriptRunner(script.getScriptType().getName()); FileMeta fileMeta = null; if (scriptRunner.hasFileOutput(script)) { String name = generateRandomString(); String resultFileExtension = script.getResultFileExtension(); if (resultFileExtension != null) { name += "." + script.getResultFileExtension(); } File file = fileStore.getFileUnchecked(name); scriptParameters.put("outputFile", file.getAbsolutePath()); fileMeta = createFileMeta(name, file); dataService.add(FILE_META, fileMeta); } String output = scriptRunner.runScript(script, scriptParameters); return new ScriptResult(fileMeta, output); } SavedScriptRunner(
      ScriptRunnerFactory scriptRunnerFactory,
      DataService dataService,
      FileStore fileStore,
      TokenService tokenService,
      FileMetaFactory fileMetaFactory); ScriptResult runScript(String scriptName, Map<String, Object> parameters); }
SavedScriptRunner { public ScriptResult runScript(String scriptName, Map<String, Object> parameters) { Script script = dataService.query(SCRIPT, Script.class).eq(ScriptMetadata.NAME, scriptName).findOne(); if (script == null) { throw new UnknownEntityException(SCRIPT, scriptName); } if (script.getParameters() != null) { for (ScriptParameter param : script.getParameters()) { if (!parameters.containsKey(param.getName())) { throw new GenerateScriptException("Missing parameter [" + param + "]"); } } } Map<String, Object> scriptParameters = new HashMap<>(parameters); if (script.isGenerateToken()) { String token = tokenService.generateAndStoreToken( SecurityUtils.getCurrentUsername(), "For script " + script.getName()); scriptParameters.put("molgenisToken", token); } ScriptRunner scriptRunner = scriptRunnerFactory.getScriptRunner(script.getScriptType().getName()); FileMeta fileMeta = null; if (scriptRunner.hasFileOutput(script)) { String name = generateRandomString(); String resultFileExtension = script.getResultFileExtension(); if (resultFileExtension != null) { name += "." + script.getResultFileExtension(); } File file = fileStore.getFileUnchecked(name); scriptParameters.put("outputFile", file.getAbsolutePath()); fileMeta = createFileMeta(name, file); dataService.add(FILE_META, fileMeta); } String output = scriptRunner.runScript(script, scriptParameters); return new ScriptResult(fileMeta, output); } SavedScriptRunner(
      ScriptRunnerFactory scriptRunnerFactory,
      DataService dataService,
      FileStore fileStore,
      TokenService tokenService,
      FileMetaFactory fileMetaFactory); ScriptResult runScript(String scriptName, Map<String, Object> parameters);  }