@Test void testExecuteScript() { String entityId = "entity"; String attributeName = "myAttribute"; String attributeValue = "value"; String jsScript = "MyJsScript"; Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(STRING); when(attributeFactory.create()).thenReturn(attribute); EntityType entityType = mock(EntityType.class); when(entityType.getAttribute(attributeName)).thenReturn(attribute); when(entityTypeFactory.create(entityId)).thenReturn(entityType); Object object = mock(Object.class); ArgumentCaptor<Entity> entityCaptor = ArgumentCaptor.forClass(Entity.class); when(jsMagmaScriptEvaluator.eval(eq(jsScript), entityCaptor.capture())).thenReturn(object); Map<String, Object> parameters = Collections.singletonMap(attributeName, attributeValue); assertEquals(object, jsMagmaScriptExecutor.executeScript(jsScript, parameters)); verify(attribute).setName(attributeName); assertEquals(attributeValue, entityCaptor.getValue().get(attributeName)); }
Object executeScript(String jsScript, Map<String, Object> parameters) { EntityType entityType = entityTypeFactory.create("entity"); Set<String> attributeNames = parameters.keySet(); attributeNames.forEach(key -> entityType.addAttribute(attributeFactory.create().setName(key))); if (attributeNames.iterator().hasNext()) { entityType.getAttribute(attributeNames.iterator().next()).setIdAttribute(true); } Entity entity = new DynamicEntity(entityType); parameters.forEach(entity::set); return jsMagmaScriptEvaluator.eval(jsScript, entity); }
JsMagmaScriptExecutor { Object executeScript(String jsScript, Map<String, Object> parameters) { EntityType entityType = entityTypeFactory.create("entity"); Set<String> attributeNames = parameters.keySet(); attributeNames.forEach(key -> entityType.addAttribute(attributeFactory.create().setName(key))); if (attributeNames.iterator().hasNext()) { entityType.getAttribute(attributeNames.iterator().next()).setIdAttribute(true); } Entity entity = new DynamicEntity(entityType); parameters.forEach(entity::set); return jsMagmaScriptEvaluator.eval(jsScript, entity); } }
JsMagmaScriptExecutor { Object executeScript(String jsScript, Map<String, Object> parameters) { EntityType entityType = entityTypeFactory.create("entity"); Set<String> attributeNames = parameters.keySet(); attributeNames.forEach(key -> entityType.addAttribute(attributeFactory.create().setName(key))); if (attributeNames.iterator().hasNext()) { entityType.getAttribute(attributeNames.iterator().next()).setIdAttribute(true); } Entity entity = new DynamicEntity(entityType); parameters.forEach(entity::set); return jsMagmaScriptEvaluator.eval(jsScript, entity); } JsMagmaScriptExecutor(
      JsMagmaScriptEvaluator jsMagmaScriptEvaluator,
      EntityTypeFactory entityTypeFactory,
      AttributeFactory attributeFactory); }
JsMagmaScriptExecutor { Object executeScript(String jsScript, Map<String, Object> parameters) { EntityType entityType = entityTypeFactory.create("entity"); Set<String> attributeNames = parameters.keySet(); attributeNames.forEach(key -> entityType.addAttribute(attributeFactory.create().setName(key))); if (attributeNames.iterator().hasNext()) { entityType.getAttribute(attributeNames.iterator().next()).setIdAttribute(true); } Entity entity = new DynamicEntity(entityType); parameters.forEach(entity::set); return jsMagmaScriptEvaluator.eval(jsScript, entity); } JsMagmaScriptExecutor(
      JsMagmaScriptEvaluator jsMagmaScriptEvaluator,
      EntityTypeFactory entityTypeFactory,
      AttributeFactory attributeFactory);  }
JsMagmaScriptExecutor { Object executeScript(String jsScript, Map<String, Object> parameters) { EntityType entityType = entityTypeFactory.create("entity"); Set<String> attributeNames = parameters.keySet(); attributeNames.forEach(key -> entityType.addAttribute(attributeFactory.create().setName(key))); if (attributeNames.iterator().hasNext()) { entityType.getAttribute(attributeNames.iterator().next()).setIdAttribute(true); } Entity entity = new DynamicEntity(entityType); parameters.forEach(entity::set); return jsMagmaScriptEvaluator.eval(jsScript, entity); } JsMagmaScriptExecutor(
      JsMagmaScriptEvaluator jsMagmaScriptEvaluator,
      EntityTypeFactory entityTypeFactory,
      AttributeFactory attributeFactory);   }