@Test 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")); Exception exception = assertThrows( AlgorithmException.class, () -> algorithmServiceImpl.apply(attributeMapping, sourceEntity, 3)); assertThat(exception.getMessage()) .containsPattern("org.molgenis.script.core.ScriptException: algorithm is not defined"); }
@Override public Object apply(AttributeMapping attributeMapping, Entity sourceEntity, int depth) { String algorithm = attributeMapping.getAlgorithm(); if (isEmpty(algorithm)) { return null; } Object result = jsMagmaScriptEvaluator.eval(algorithm, sourceEntity, depth); if (result instanceof Throwable) { throw new AlgorithmException((Throwable) result); } return convert(result, attributeMapping.getTargetAttribute()); }
AlgorithmServiceImpl implements AlgorithmService { @Override public Object apply(AttributeMapping attributeMapping, Entity sourceEntity, int depth) { String algorithm = attributeMapping.getAlgorithm(); if (isEmpty(algorithm)) { return null; } Object result = jsMagmaScriptEvaluator.eval(algorithm, sourceEntity, depth); if (result instanceof Throwable) { throw new AlgorithmException((Throwable) result); } return convert(result, attributeMapping.getTargetAttribute()); } }
AlgorithmServiceImpl implements AlgorithmService { @Override public Object apply(AttributeMapping attributeMapping, Entity sourceEntity, int depth) { String algorithm = attributeMapping.getAlgorithm(); if (isEmpty(algorithm)) { return null; } Object result = jsMagmaScriptEvaluator.eval(algorithm, sourceEntity, depth); if (result instanceof Throwable) { throw new AlgorithmException((Throwable) result); } return convert(result, attributeMapping.getTargetAttribute()); } AlgorithmServiceImpl(
      SemanticSearchService semanticSearchService,
      AlgorithmGeneratorService algorithmGeneratorService,
      EntityManager entityManager,
      JsMagmaScriptEvaluator jsMagmaScriptEvaluator); }
AlgorithmServiceImpl implements AlgorithmService { @Override public Object apply(AttributeMapping attributeMapping, Entity sourceEntity, int depth) { String algorithm = attributeMapping.getAlgorithm(); if (isEmpty(algorithm)) { return null; } Object result = jsMagmaScriptEvaluator.eval(algorithm, sourceEntity, depth); if (result instanceof Throwable) { throw new AlgorithmException((Throwable) result); } return convert(result, attributeMapping.getTargetAttribute()); } AlgorithmServiceImpl(
      SemanticSearchService semanticSearchService,
      AlgorithmGeneratorService algorithmGeneratorService,
      EntityManager entityManager,
      JsMagmaScriptEvaluator jsMagmaScriptEvaluator); @Override String generateAlgorithm(
      Attribute targetAttribute,
      EntityType targetEntityType,
      List<Attribute> sourceAttributes,
      EntityType sourceEntityType); @Override void copyAlgorithms(EntityMapping sourceEntityMapping, EntityMapping targetEntityMapping); @Override @RunAsSystem void autoGenerateAlgorithm(
      EntityType sourceEntityType, EntityType targetEntityType, EntityMapping mapping); @Override Iterable<AlgorithmEvaluation> applyAlgorithm(
      Attribute targetAttribute, String algorithm, Iterable<Entity> sourceEntities, int depth); @Override Object apply(AttributeMapping attributeMapping, Entity sourceEntity, int depth); @Override Collection<String> getSourceAttributeNames(String algorithmScript); }
AlgorithmServiceImpl implements AlgorithmService { @Override public Object apply(AttributeMapping attributeMapping, Entity sourceEntity, int depth) { String algorithm = attributeMapping.getAlgorithm(); if (isEmpty(algorithm)) { return null; } Object result = jsMagmaScriptEvaluator.eval(algorithm, sourceEntity, depth); if (result instanceof Throwable) { throw new AlgorithmException((Throwable) result); } return convert(result, attributeMapping.getTargetAttribute()); } AlgorithmServiceImpl(
      SemanticSearchService semanticSearchService,
      AlgorithmGeneratorService algorithmGeneratorService,
      EntityManager entityManager,
      JsMagmaScriptEvaluator jsMagmaScriptEvaluator); @Override String generateAlgorithm(
      Attribute targetAttribute,
      EntityType targetEntityType,
      List<Attribute> sourceAttributes,
      EntityType sourceEntityType); @Override void copyAlgorithms(EntityMapping sourceEntityMapping, EntityMapping targetEntityMapping); @Override @RunAsSystem void autoGenerateAlgorithm(
      EntityType sourceEntityType, EntityType targetEntityType, EntityMapping mapping); @Override Iterable<AlgorithmEvaluation> applyAlgorithm(
      Attribute targetAttribute, String algorithm, Iterable<Entity> sourceEntities, int depth); @Override Object apply(AttributeMapping attributeMapping, Entity sourceEntity, int depth); @Override Collection<String> getSourceAttributeNames(String algorithmScript);  }