@Test 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), 3); AlgorithmEvaluation eval = result.iterator().next(); assertEquals("algorithm is not defined", eval.getErrorMessage()); }
@Override public Iterable<AlgorithmEvaluation> applyAlgorithm( Attribute targetAttribute, String algorithm, Iterable<Entity> sourceEntities, int depth) { return stream(sourceEntities) .map( entity -> { AlgorithmEvaluation algorithmResult = new AlgorithmEvaluation(entity); Object derivedValue; try { Object result = jsMagmaScriptEvaluator.eval(algorithm, entity, depth); if (result instanceof ScriptException) { return algorithmResult.errorMessage(((ScriptException) result).getMessage()); } derivedValue = convert(result, targetAttribute); } catch (RuntimeException e) { if (e.getMessage() == null) { return algorithmResult.errorMessage( "Applying an algorithm on a null source value caused an exception. Is the target attribute required?"); } return algorithmResult.errorMessage(e.getLocalizedMessage()); } return algorithmResult.value(derivedValue); }) .collect(toList()); }
AlgorithmServiceImpl implements AlgorithmService { @Override public Iterable<AlgorithmEvaluation> applyAlgorithm( Attribute targetAttribute, String algorithm, Iterable<Entity> sourceEntities, int depth) { return stream(sourceEntities) .map( entity -> { AlgorithmEvaluation algorithmResult = new AlgorithmEvaluation(entity); Object derivedValue; try { Object result = jsMagmaScriptEvaluator.eval(algorithm, entity, depth); if (result instanceof ScriptException) { return algorithmResult.errorMessage(((ScriptException) result).getMessage()); } derivedValue = convert(result, targetAttribute); } catch (RuntimeException e) { if (e.getMessage() == null) { return algorithmResult.errorMessage( "Applying an algorithm on a null source value caused an exception. Is the target attribute required?"); } return algorithmResult.errorMessage(e.getLocalizedMessage()); } return algorithmResult.value(derivedValue); }) .collect(toList()); } }
AlgorithmServiceImpl implements AlgorithmService { @Override public Iterable<AlgorithmEvaluation> applyAlgorithm( Attribute targetAttribute, String algorithm, Iterable<Entity> sourceEntities, int depth) { return stream(sourceEntities) .map( entity -> { AlgorithmEvaluation algorithmResult = new AlgorithmEvaluation(entity); Object derivedValue; try { Object result = jsMagmaScriptEvaluator.eval(algorithm, entity, depth); if (result instanceof ScriptException) { return algorithmResult.errorMessage(((ScriptException) result).getMessage()); } derivedValue = convert(result, targetAttribute); } catch (RuntimeException e) { if (e.getMessage() == null) { return algorithmResult.errorMessage( "Applying an algorithm on a null source value caused an exception. Is the target attribute required?"); } return algorithmResult.errorMessage(e.getLocalizedMessage()); } return algorithmResult.value(derivedValue); }) .collect(toList()); } AlgorithmServiceImpl(
      SemanticSearchService semanticSearchService,
      AlgorithmGeneratorService algorithmGeneratorService,
      EntityManager entityManager,
      JsMagmaScriptEvaluator jsMagmaScriptEvaluator); }
AlgorithmServiceImpl implements AlgorithmService { @Override public Iterable<AlgorithmEvaluation> applyAlgorithm( Attribute targetAttribute, String algorithm, Iterable<Entity> sourceEntities, int depth) { return stream(sourceEntities) .map( entity -> { AlgorithmEvaluation algorithmResult = new AlgorithmEvaluation(entity); Object derivedValue; try { Object result = jsMagmaScriptEvaluator.eval(algorithm, entity, depth); if (result instanceof ScriptException) { return algorithmResult.errorMessage(((ScriptException) result).getMessage()); } derivedValue = convert(result, targetAttribute); } catch (RuntimeException e) { if (e.getMessage() == null) { return algorithmResult.errorMessage( "Applying an algorithm on a null source value caused an exception. Is the target attribute required?"); } return algorithmResult.errorMessage(e.getLocalizedMessage()); } return algorithmResult.value(derivedValue); }) .collect(toList()); } 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 Iterable<AlgorithmEvaluation> applyAlgorithm( Attribute targetAttribute, String algorithm, Iterable<Entity> sourceEntities, int depth) { return stream(sourceEntities) .map( entity -> { AlgorithmEvaluation algorithmResult = new AlgorithmEvaluation(entity); Object derivedValue; try { Object result = jsMagmaScriptEvaluator.eval(algorithm, entity, depth); if (result instanceof ScriptException) { return algorithmResult.errorMessage(((ScriptException) result).getMessage()); } derivedValue = convert(result, targetAttribute); } catch (RuntimeException e) { if (e.getMessage() == null) { return algorithmResult.errorMessage( "Applying an algorithm on a null source value caused an exception. Is the target attribute required?"); } return algorithmResult.errorMessage(e.getLocalizedMessage()); } return algorithmResult.value(derivedValue); }) .collect(toList()); } 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);  }