org.camunda.spin.json.SpinJsonNode Java Examples

The following examples show how to use org.camunda.spin.json.SpinJsonNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JsonTreeSetPropertyTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
public void replaceIntegerProperty() {
  Integer value = 42;
  Integer oldValue = dueUntil.numberValue().intValue();

  assertThat(customers.isArray()).isTrue();
  assertThat(dueUntil.isNumber());

  // set new values
  jsonNode.prop("dueUntil", value);
  jsonNode.prop("customers", value);
  SpinJsonNode newValue1 = jsonNode.prop("dueUntil");
  SpinJsonNode newValue2 = jsonNode.prop("customers");

  assertThat(newValue1.numberValue()).isNotEqualTo(oldValue);
  assertThat(newValue1.numberValue()).isEqualTo(value);

  assertThat(newValue2.isArray()).isFalse();
  assertThat(newValue2.isNumber()).isTrue();
  assertThat(newValue2.numberValue()).isEqualTo(value);
}
 
Example #2
Source File: JsonTreeMapObjectToJsonScriptTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@Script
public void shouldMapPrimitives() {
  SpinJsonNode json = script.getVariable("stringVar");

  assertThat(json.isString()).isTrue();
  assertThat(json.stringValue()).isEqualTo("a String");

  json = script.getVariable("booleanVar");

  assertThat(json.isBoolean()).isTrue();
  assertThat(json.boolValue()).isFalse();

  json = script.getVariable("integerVar");

  assertThat(json.isNumber()).isTrue();
  assertThat(json.numberValue().intValue()).isEqualTo(42);

  SpinJsonNode jsonList = script.getVariable("listVar");

  assertThat(jsonList.isArray()).isTrue();
  SpinList<SpinJsonNode> elements = jsonList.elements();
  assertThat(elements.get(0).stringValue()).isEqualTo("Waldo");
  assertThat(elements.get(1).stringValue()).isEqualTo("Hugo");
  assertThat(elements.get(2).stringValue()).isEqualTo("Kermit");
}
 
Example #3
Source File: SpinVariableSerializers.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public static List<TypedValueSerializer<?>> createSpinValueSerializers(DataFormats dataFormats) {
  List<TypedValueSerializer<?>> serializers = new ArrayList<TypedValueSerializer<?>>();

  if(dataFormats.getDataFormatByName(DataFormats.JSON_DATAFORMAT_NAME) != null) {
    DataFormat<SpinJsonNode> jsonDataFormat =
        (DataFormat<SpinJsonNode>) dataFormats.getDataFormatByName(DataFormats.JSON_DATAFORMAT_NAME);
    serializers.add(new JsonValueSerializer(jsonDataFormat));
  }
  if(dataFormats.getDataFormatByName(DataFormats.XML_DATAFORMAT_NAME) != null){
    DataFormat<SpinXmlElement> xmlDataFormat =
        (DataFormat<SpinXmlElement>) dataFormats.getDataFormatByName(DataFormats.XML_DATAFORMAT_NAME);
    serializers.add(new XmlValueSerializer(xmlDataFormat));
  }

  return serializers;
}
 
Example #4
Source File: JsonTreeEditListPropertyTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
public void insertAtWithNegativeIndex() {
  Integer oldSize = currencies.elements().size();
  Integer oldPosition = currencies.indexOf("dollar");
  SpinJsonNode oldNode = currencies.elements().get(0);

  currencies.insertAt(-2, "test1");

  Integer newSize = currencies.elements().size();
  Integer newPosition = currencies.indexOf("dollar");
  SpinJsonNode newNode = currencies.elements().get(0);

  assertThat(oldSize + 1).isEqualTo(newSize);
  assertThat(newNode.equals(oldNode)).isFalse();
  assertThat(newNode.stringValue()).isEqualTo("test1");
  assertThat(oldPosition + 1).isEqualTo(newPosition);
}
 
Example #5
Source File: JsonTreeReadPropertyScriptTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@Script
@ScriptVariable(name = "input", file = EXAMPLE_JSON_FILE_NAME)
public void shouldReadChildNodeProperty() {
  SpinJsonNode property1 = script.getVariable("property1");
  SpinJsonNode property2 = script.getVariable("property2");

  Number value1 = script.getVariable("value1");
  String value2 = script.getVariable("value2");

  assertThat(property1).isNotNull();
  assertThat(property2).isNotNull();

  // Ruby casts this to long instead int
  assertThat(value1.intValue()).isEqualTo(32000);
  assertThat(value2).isEqualTo("dollar");
}
 
Example #6
Source File: JsonTreeEditListPropertyTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
public void insertAtWithIndex() {
  Integer oldSize = currencies.elements().size();
  Integer oldPosition = currencies.indexOf("dollar");
  SpinJsonNode oldNode = currencies.elements().get(1);

  currencies.insertAt(1, "test1");

  Integer newSize = currencies.elements().size();
  Integer newPosition = currencies.indexOf("dollar");
  SpinJsonNode newNode = currencies.elements().get(1);

  assertThat(oldSize + 1).isEqualTo(newSize);
  assertThat(newNode.equals(oldNode)).isFalse();
  assertThat(newNode.stringValue()).isEqualTo("test1");
  assertThat(oldPosition + 1).isEqualTo(newPosition);
}
 
Example #7
Source File: JacksonJsonNode.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
public SpinJsonNode prop(String name, Number newProperty) {
  ObjectNode node = (ObjectNode) jsonNode;

  // Numbers magic because Jackson has no native .put(Number value)
  if (newProperty == null) {
    node.putNull(name);
  } else if(newProperty instanceof Long) {
    node.put(name, newProperty.longValue());
  } else if(newProperty instanceof Integer) {
    node.put(name, newProperty.intValue());
  } else if(newProperty instanceof Float) {
      node.put(name, newProperty.floatValue());
  } else {
    // convert any other sub class of Number into Float
    node.put(name, newProperty.floatValue());
  }

  return this;
}
 
Example #8
Source File: JsonTreeEditListPropertyTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
public void insertBeforeSearchObject() {
  SpinJsonNode oldNode = currencies.elements().get(1);
  Integer size = currencies.elements().size();

  currencies.insertBefore("dollar", "Test");

  SpinJsonNode newNode = currencies.elements().get(1);
  SpinJsonNode oldNodeNewPosition = currencies.elements().get(2);
  Integer newSize = currencies.elements().size();

  assertThat(oldNode).isNotEqualTo(newNode);
  assertThat(newNode.stringValue()).isEqualTo("Test");
  assertThat(oldNode.stringValue()).isEqualTo(oldNodeNewPosition.stringValue());
  assertThat(size).isNotEqualTo(newSize);
}
 
Example #9
Source File: JsonTreeSetPropertyTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
public void replaceBooleanProperty() {
  SpinJsonNode active = jsonNode.prop("active");

  Boolean oldValue = active.boolValue();
  Boolean value = !oldValue;

  assertThat(customers.isArray()).isTrue();
  assertThat(active.isBoolean());

  // set new values
  jsonNode.prop("active", value);
  jsonNode.prop("customers", value);
  SpinJsonNode newValue1 = jsonNode.prop("active");
  SpinJsonNode newValue2 = jsonNode.prop("customers");

  assertThat(newValue1.boolValue()).isNotEqualTo(oldValue);
  assertThat(newValue1.boolValue()).isEqualTo(value);

  assertThat(newValue2.isArray()).isFalse();
  assertThat(newValue2.isBoolean()).isTrue();
  assertThat(newValue2.boolValue()).isEqualTo(value);
}
 
Example #10
Source File: JsonTreeReadPropertyScriptTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@Script
@ScriptVariable(name = "input", file = EXAMPLE_JSON_FILE_NAME)
public void shouldBeSameAsJavaValue() {

  SpinJsonNode node = Spin.JSON(EXAMPLE_JSON);
  SpinJsonNode childNode = node.prop("orderDetails");

  SpinJsonNode property1 = node.prop("order");
  SpinJsonNode property2 = childNode.prop("price");
  SpinJsonNode property3 = node.prop("active");

  String javaVariable1 = property1.stringValue();
  Number javaVariable2 = property2.numberValue();
  Boolean javaVariable3 = property3.boolValue();

  String scriptVariable1 = script.getVariable("stringValue");
  Number scriptVariable2 = script.getVariable("numberValue");
  Boolean scriptVariable3 = script.getVariable("boolValue");

  assertThat(javaVariable1).isEqualTo(scriptVariable1);
  assertThat(javaVariable2).isEqualTo(scriptVariable2);
  assertThat(javaVariable3).isEqualTo(scriptVariable3);
}
 
Example #11
Source File: JsonTreeSetPropertyTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
public void replaceLongProperty() {
  Long value = 4200000000L;

  Long oldValue = id.numberValue().longValue();

  assertThat(customers.isArray()).isTrue();
  assertThat(id.isNumber());

  // set new values
  jsonNode.prop("id", value);
  jsonNode.prop("customers", value);
  SpinJsonNode newValue1 = jsonNode.prop("id");
  SpinJsonNode newValue2 = jsonNode.prop("customers");

  assertThat(newValue1.numberValue()).isNotEqualTo(oldValue);
  assertThat(newValue1.numberValue()).isEqualTo(value);

  assertThat(newValue2.isArray()).isFalse();
  assertThat(newValue2.isNumber()).isTrue();
  assertThat(newValue2.numberValue()).isEqualTo(value);
}
 
Example #12
Source File: JsonValueTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Deployment(resources = ONE_TASK_PROCESS)
public void testGetUntypedJsonValue() throws JSONException {
  // given
  JsonValue jsonValue = jsonValue(jsonString).create();
  VariableMap variables = Variables.createVariables().putValueTyped(variableName, jsonValue);

  String processInstanceId = runtimeService.startProcessInstanceByKey(ONE_TASK_PROCESS_KEY, variables).getId();

  // when
  SpinJsonNode value = (SpinJsonNode) runtimeService.getVariable(processInstanceId, variableName);

  // then
  JSONAssert.assertEquals(jsonString, value.toString(), true);
  assertEquals(json().getName(), value.getDataFormatName());
}
 
Example #13
Source File: JsonTreeJsonPathScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@Script
@ScriptVariable(name = "input", file = EXAMPLE_JSON_FILE_NAME)
public void shouldGetSingleArrayEntry() {
  SpinJsonNode node = script.getVariable("node");

  assertThat(node.isObject());
  assertThat(node.prop("name").isString());
  assertThat(node.prop("name").stringValue()).isEqualTo("Kermit");
}
 
Example #14
Source File: JsonTreeJsonPathScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@Script
@ScriptVariable(name = "input", file = EXAMPLE_JSON_FILE_NAME)
public void shouldGetMultipleArrayEntries() {
  SpinList<SpinJsonNode> nodeList = script.getVariable("nodeList");

  assertThat(nodeList).hasSize(2);
  assertThat(nodeList.get(0).prop("name").stringValue()).isEqualTo("Kermit");
  assertThat(nodeList.get(1).prop("name").stringValue()).isEqualTo("Waldo");
}
 
Example #15
Source File: JsonTreeEditListPropertyTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void removeAtWithNegativeIndex() {
  Integer oldSize = currencies.elements().size();

  currencies.removeAt(-2);

  Integer newSize = currencies.elements().size();
  SpinJsonNode node = currencies.elements().get(newSize - 1);

  assertThat(newSize).isEqualTo(1);
  assertThat(newSize + 1).isEqualTo(oldSize);
  assertThat(node.stringValue()).isEqualTo("dollar");
}
 
Example #16
Source File: JsonTreeSetPropertyScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@Script
@ScriptVariable(name = "input", file = EXAMPLE_JSON_FILE_NAME)
public void shouldSetIntegerProperty() {
  SpinJsonNode propertyNode = script.getVariable("propertyNode");
  Number value = script.getVariable("value");

  assertThat(propertyNode).isNotNull();
  // Ruby casts Number to long
  assertThat(value.intValue()).isEqualTo(42);
}
 
Example #17
Source File: JsonTreeSetPropertyScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@Script
@ScriptVariable(name = "input", file = EXAMPLE_JSON_FILE_NAME)
public void shouldSetFloatProperty() {
  SpinJsonNode propertyNode = script.getVariable("propertyNode");
  Number value = script.getVariable("value");

  assertThat(propertyNode).isNotNull();

  // python returns Double, needs to cast to Float
  assertThat(value.floatValue()).isEqualTo(42.00F);
}
 
Example #18
Source File: JsonTreeSetPropertyScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
/**
 * Please note: in jython there is a known issue that Boolean
 * and boolean values are casted to long if a matching method
 * is found first. See script source code for workaround.
 */
@Test
@Script
@ScriptVariable(name = "input", file = EXAMPLE_JSON_FILE_NAME)
public void shouldSetBooleanProperty() {
  SpinJsonNode propertyNode = script.getVariable("propertyNode");
  Boolean value = script.getVariable("value");

  assertThat(propertyNode).isNotNull();
  assertThat(value).isFalse();
}
 
Example #19
Source File: JsonTreeSetPropertyScriptTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@Script
@ScriptVariable(name = "input", file = EXAMPLE_JSON_FILE_NAME)
public void shouldSetArrayProperty() {
  SpinJsonNode propertyNode = script.getVariable("propertyNode");
  String value = script.getVariable("value");

  assertThat(propertyNode).isNotNull();
  assertThat(propertyNode.isArray()).isTrue();
  assertThat(value).isEqualTo("test2");
}
 
Example #20
Source File: SpinApplication.java    From camunda-bpm-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaDelegate spinJava8DeserializerDelegate() {
  return delegateExecution -> {
    SpinJsonNode jsonNode = S("{\"dateTime\": \"2019-12-12T22:22:22\"}");
    Object         key      = jsonNode.mapTo(SpinJava8Dto.class);
    delegateExecution.setVariable("dateTime", key);
  };
}
 
Example #21
Source File: JsonValueImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public JsonValueImpl(
    SpinJsonNode value,
    String serializedValue,
    String dataFormatName,
    boolean isDeserialized,
    boolean isTransient) {
  super(value, serializedValue, dataFormatName, isDeserialized, SpinValueType.JSON, isTransient);
}
 
Example #22
Source File: JsonTreeReadPropertyTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReadProperty() {
  assertThat(jsonNode).isNotNull();

  SpinJsonNode property = jsonNode.prop("order");
  assertThat(property).isNotNull();
}
 
Example #23
Source File: JsonValueImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public JsonValueImpl(
    SpinJsonNode value,
    String serializedValue,
    String dataFormatName,
    boolean isDeserialized) {
  this(value, serializedValue, dataFormatName, isDeserialized, false);
}
 
Example #24
Source File: JsonTreeSetPropertyTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void setLongProperty() {
  Long longValue = 4200000000L;
  jsonNode.prop("comment", longValue);
  SpinJsonNode comment = jsonNode.prop("comment");

  assertThat(comment.isNumber()).isTrue();
  assertThat(comment.numberValue().longValue()).isEqualTo(longValue);
}
 
Example #25
Source File: JsonTreeCreateTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeIdempotent() {
  SpinJsonNode json = JSON(EXAMPLE_JSON);
  assertThat(json).isEqualTo(JSON(json));
  assertThat(json).isEqualTo(S(json, json()));
  assertThat(json).isEqualTo(S(json, DataFormats.JSON_DATAFORMAT_NAME));
  assertThat(json).isEqualTo(S(json));
}
 
Example #26
Source File: JsonTreeEditListPropertyTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void removeObject() {
  Integer oldSize = currencies.elements().size();

  currencies.remove("euro");

  Integer newSize = currencies.elements().size();
  SpinJsonNode node = currencies.elements().get(0);

  assertThat(oldSize).isNotEqualTo(newSize);
  assertThat(oldSize - 1).isEqualTo(newSize);
  assertThat(node.stringValue()).isEqualTo("dollar");
}
 
Example #27
Source File: JsonTreeCreateTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateForString() {
  SpinJsonNode json = JSON(EXAMPLE_JSON);
  assertThat(json).isNotNull();

  json = S(EXAMPLE_JSON, json());
  assertThat(json).isNotNull();

  json = S(EXAMPLE_JSON, DataFormats.JSON_DATAFORMAT_NAME);
  assertThat(json).isNotNull();

  json = S(EXAMPLE_JSON);
  assertThat(json).isNotNull();
}
 
Example #28
Source File: JsonTreeSetPropertyTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void setNullSpinJsonNodeProperty() {
  jsonNode.prop("newNullValue", (SpinJsonNode) null);

  assertThat(jsonNode.prop("newNullValue").isNull()).isTrue();
  assertThat(jsonNode.prop("newNullValue").value()).isNull();
}
 
Example #29
Source File: JsonTreeSetPropertyTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void setPropertyWithJSON() {
  String json = "{\"agent\":\"Smith\"}";

  jsonNode.prop("name", JSON(json));
  SpinJsonNode name = jsonNode.prop("name");
  assertThat(name.isObject()).isTrue();
  assertThat(name.prop("agent").stringValue()).isEqualTo("Smith");
}
 
Example #30
Source File: JsonTreeSetPropertyTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void setObjectProperty() {
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("id1", "object1");
  map.put("id2", 1337);

  jsonNode.prop("comment", map);
  SpinJsonNode comment = jsonNode.prop("comment");

  assertThat(comment.isObject()).isTrue();
  assertThat(comment.hasProp("id1")).isTrue();
  assertThat(comment.prop("id2").isNumber()).isTrue();
  assertThat(comment.prop("id2").numberValue()).isEqualTo(1337);
}