Search in sources :

Example 1 with ScriptException

use of org.molgenis.script.core.ScriptException in project molgenis by molgenis.

the class AlgorithmServiceImplTest method testApplyWithInvalidScript.

@Test(expectedExceptions = ScriptException.class, expectedExceptionsMessageRegExp = "algorithm is not defined")
public void testApplyWithInvalidScript() {
    AttributeMapping attributeMapping = mock(AttributeMapping.class);
    String algorithm = "algorithm";
    when(attributeMapping.getAlgorithm()).thenReturn(algorithm);
    Entity sourceEntity = mock(Entity.class);
    when(jsMagmaScriptEvaluator.eval(algorithm, sourceEntity, 3)).thenReturn(new ScriptException("algorithm is not defined"));
    algorithmServiceImpl.apply(attributeMapping, sourceEntity, null);
}
Also used : Entity(org.molgenis.data.Entity) ScriptException(org.molgenis.script.core.ScriptException) AttributeMapping(org.molgenis.semanticmapper.mapping.model.AttributeMapping) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Example 2 with ScriptException

use of org.molgenis.script.core.ScriptException in project molgenis by molgenis.

the class JsMagmaScriptEvaluator method eval.

/**
 * Evaluate a expression for the given entity.
 *
 * @param expression JavaScript expression
 * @param entity     entity
 * @return evaluated expression result, return type depends on the expression.
 */
public Object eval(String expression, Entity entity, int depth) {
    Stopwatch stopwatch = null;
    if (LOG.isTraceEnabled()) {
        stopwatch = Stopwatch.createStarted();
    }
    Object scriptEngineValueMap = toScriptEngineValueMap(entity, depth);
    Object value;
    try {
        value = jsScriptEngine.invokeFunction("evalScript", expression, scriptEngineValueMap);
    } catch (Throwable t) {
        return new ScriptException(t);
    }
    if (stopwatch != null) {
        stopwatch.stop();
        LOG.trace("Script evaluation took {} µs", stopwatch.elapsed(MICROSECONDS));
    }
    return value;
}
Also used : ScriptException(org.molgenis.script.core.ScriptException) Stopwatch(com.google.common.base.Stopwatch)

Example 3 with ScriptException

use of org.molgenis.script.core.ScriptException in project molgenis by molgenis.

the class RScriptExecutor method executeScriptExecuteRequest.

/**
 * Execute R script using OpenCPU
 *
 * @param rScript R script
 * @return OpenCPU session key
 * @throws IOException if error occured during script execution request
 */
private String executeScriptExecuteRequest(String rScript) throws IOException {
    URI uri = getScriptExecutionUri();
    HttpPost httpPost = new HttpPost(uri);
    NameValuePair nameValuePair = new BasicNameValuePair("x", rScript);
    httpPost.setEntity(new UrlEncodedFormEntity(singletonList(nameValuePair)));
    String openCpuSessionKey;
    try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode >= 200 && statusCode < 300) {
            Header openCpuSessionKeyHeader = response.getFirstHeader("X-ocpu-session");
            if (openCpuSessionKeyHeader == null) {
                throw new IOException("Missing 'X-ocpu-session' header");
            }
            openCpuSessionKey = openCpuSessionKeyHeader.getValue();
            EntityUtils.consume(response.getEntity());
        } else if (statusCode == 400) {
            HttpEntity entity = response.getEntity();
            String rErrorMessage = EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            throw new ScriptException(rErrorMessage);
        } else {
            throw new ClientProtocolException(format("Unexpected response status: %d", statusCode));
        }
    }
    return openCpuSessionKey;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) ScriptException(org.molgenis.script.core.ScriptException) Header(org.apache.http.Header) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 4 with ScriptException

use of org.molgenis.script.core.ScriptException in project molgenis by molgenis.

the class AlgorithmServiceImplTest method testApplyAlgorithmWitInvalidScript.

@Test
public void testApplyAlgorithmWitInvalidScript() {
    Attribute attribute = mock(Attribute.class);
    String algorithm = "algorithm";
    Entity entity = mock(Entity.class);
    when(jsMagmaScriptEvaluator.eval(algorithm, entity, 3)).thenReturn(new ScriptException("algorithm is not defined"));
    Iterable<AlgorithmEvaluation> result = algorithmServiceImpl.applyAlgorithm(attribute, algorithm, Lists.newArrayList(entity));
    AlgorithmEvaluation eval = result.iterator().next();
    assertEquals(eval.getErrorMessage(), "algorithm is not defined");
}
Also used : Entity(org.molgenis.data.Entity) ScriptException(org.molgenis.script.core.ScriptException) Attribute(org.molgenis.data.meta.model.Attribute) Test(org.testng.annotations.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest)

Aggregations

ScriptException (org.molgenis.script.core.ScriptException)4 Entity (org.molgenis.data.Entity)2 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)2 Test (org.testng.annotations.Test)2 Stopwatch (com.google.common.base.Stopwatch)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 URI (java.net.URI)1 Header (org.apache.http.Header)1 HttpEntity (org.apache.http.HttpEntity)1 NameValuePair (org.apache.http.NameValuePair)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 Attribute (org.molgenis.data.meta.model.Attribute)1 AttributeMapping (org.molgenis.semanticmapper.mapping.model.AttributeMapping)1