org.camunda.spin.DataFormats Java Examples

The following examples show how to use org.camunda.spin.DataFormats. 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: DataFormatLoadingTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest(DataFormats.class)
public void testConfigureDataFormatWithConfiguratorList() {
  // given a custom data format provider that is returned by the service loader API
  mockProviders(new CustomDataFormatProvider());
  mockConfigurators();
  DataFormatConfigurator configurator = new ExampleCustomDataFormatConfigurator();

  // when a list of data format configurators is passed to the "load" method
  DataFormats.loadDataFormats(DataFormats.class.getClassLoader(),
                              Collections.singletonList(configurator));

  // then the configuration was applied
  ExampleCustomDataFormat customFormat = (ExampleCustomDataFormat) DataFormats
      .getDataFormat(CustomDataFormatProvider.NAME);
  assertThat(customFormat.getProperty())
      .isEqualTo(ExampleCustomDataFormatConfigurator.UPDATED_PROPERTY);
}
 
Example #2
Source File: JsonSerializationWithValidationOnMultipleEnginesTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUseNegativeValidator() {
  // given
  engineRuleNegative.manageDeployment(engineRuleNegative.getRepositoryService().createDeployment()
      .addModelInstance("foo.bpmn", getOneTaskModel())
      .deploy());
  ProcessInstance instance = engineRuleNegative.getRuntimeService().startProcessInstanceByKey("oneTaskProcess");

  // add serialized value
  JsonSerializable bean = new JsonSerializable("a String", 42, true);
  engineRuleNegative.getRuntimeService().setVariable(instance.getId(), "simpleBean",
      objectValue(bean).serializationDataFormat(DataFormats.JSON_DATAFORMAT_NAME).create());

  // then
  thrown.expect(ProcessEngineException.class);
  thrown.expectMessage("Cannot deserialize");
  thrown.expectCause(isA(SpinJsonException.class));

  // when
  engineRuleNegative.getRuntimeService().getVariable(instance.getId(), "simpleBean");
}
 
Example #3
Source File: JsonSerializationWithValidationOnMultipleEnginesTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldUsePositiveValidator() {
  // given
  engineRulePositive.manageDeployment(engineRulePositive.getRepositoryService().createDeployment()
      .addModelInstance("foo.bpmn", getOneTaskModel())
      .deploy());
  ProcessInstance instance = engineRulePositive.getRuntimeService().startProcessInstanceByKey("oneTaskProcess");

  // add serialized value
  JsonSerializable bean = new JsonSerializable("a String", 42, true);
  engineRulePositive.getRuntimeService().setVariable(instance.getId(), "simpleBean",
      objectValue(bean).serializationDataFormat(DataFormats.JSON_DATAFORMAT_NAME).create());

  // when
  Object value = engineRulePositive.getRuntimeService().getVariable(instance.getId(), "simpleBean");

  // then
  assertEquals(bean, value);
}
 
Example #4
Source File: DataFormatLoadingTest.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@Test
@PrepareForTest(DataFormats.class)
public void testRegisterDataFormatWithConfiguratorList() {
  // given a custom data format provider that is returned by the service loader API
  mockProviders(new CustomDataFormatProvider());
  mockConfigurators();
  DataFormatConfigurator configurator = new ExampleCustomDataFormatConfigurator();

  // when a list of data format configurators is passed to the "load" method
  DataFormats.getInstance().registerDataFormats(DataFormats.class.getClassLoader(),
                                                Collections.singletonList(configurator));

  // then the configuration was applied
  ExampleCustomDataFormat customFormat = (ExampleCustomDataFormat) DataFormats
      .getDataFormat(CustomDataFormatProvider.NAME);
  assertThat(customFormat.getProperty())
      .isEqualTo(ExampleCustomDataFormatConfigurator.UPDATED_PROPERTY);
}
 
Example #5
Source File: SpinFactoryImpl.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Spin<?>> T createSpinFromReader(Reader parameter) {
  ensureNotNull("parameter", parameter);

  RewindableReader rewindableReader = new RewindableReader(parameter, READ_SIZE);

  DataFormat<T> matchingDataFormat = null;
  for (DataFormat<?> format : DataFormats.getAvailableDataFormats()) {
    if (format.getReader().canRead(rewindableReader, rewindableReader.getRewindBufferSize())) {
      matchingDataFormat = (DataFormat<T>) format;
    }

    try {
      rewindableReader.rewind();
    } catch (IOException e) {
      throw LOG.unableToReadFromReader(e);
    }

  }

  if (matchingDataFormat == null) {
    throw LOG.unrecognizableDataFormatException();
  }

  return createSpin(rewindableReader, matchingDataFormat);
}
 
Example #6
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 #7
Source File: SpinVariableSerializers.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public static List<TypedValueSerializer<?>> createObjectValueSerializers(DataFormats dataFormats) {
  List<TypedValueSerializer<?>> serializers = new ArrayList<TypedValueSerializer<?>>();

  Set<DataFormat<?>> availableDataFormats = dataFormats.getAllAvailableDataFormats();
  for (DataFormat<?> dataFormat : availableDataFormats) {
    serializers.add(new SpinObjectValueSerializer("spin://"+dataFormat.getName(), dataFormat));
  }

  return serializers;
}
 
Example #8
Source File: SpringBootSpinProcessEnginePlugin.java    From camunda-bpm-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
protected void loadSpringBootDataFormats(ClassLoader classloader) {
  List<DataFormatConfigurator> configurators = new ArrayList<>();

  // add the auto-config Jackson Java 8 module configurators
  dataFormatConfiguratorJsr310.ifPresent(configurator -> configurators.add(configurator));
  dataFormatConfiguratorParameterNames.ifPresent(configurator -> configurators.add(configurator));
  dataFormatConfiguratorJdk8.ifPresent(configurator -> configurators.add(configurator));

  // next, add any configurators defined in the spring.factories file
  configurators.addAll(SpringFactoriesLoader.loadFactories(DataFormatConfigurator.class, classloader));

  DataFormats.loadDataFormats(classloader, configurators);
}
 
Example #9
Source File: SpinApplication.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Bean
public JavaDelegate spinDeserializerDelegate() {
  return delegateExecution -> {
    SpinJsonNode jsonNode = S("{\"dateTime\": \"2019-12-12T22:22:22\"}");
    Object key = jsonNode.mapTo(SpinDto.class);
    delegateExecution.setVariables(Variables.createVariables().putValueTyped("dateTime",
        Variables
         .objectValue(key)
         .serializationDataFormat(DataFormats.JSON_DATAFORMAT_NAME)
         .create()));
  };
}
 
Example #10
Source File: SpinValueImpl.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public DataFormat<? extends Spin<?>> getDataFormat() {
  if(isDeserialized) {
    return DataFormats.getDataFormat(dataFormatName);
  }
  else {
    throw new IllegalStateException("Spin value is not deserialized.");
  }
}
 
Example #11
Source File: SpinBpmPlatformPlugin.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected List<TypedValueSerializer<?>> lookupSpinSerializers(ClassLoader classLoader) {

    DataFormats paDataFormats = new DataFormats();
    paDataFormats.registerDataFormats(classLoader);

    // does not create PA-local serializers for native Spin values;
    // this is still an open feature CAM-5246
    return SpinVariableSerializers.createObjectValueSerializers(paDataFormats);
  }
 
Example #12
Source File: SpinProcessEnginePlugin.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected List<TypedValueSerializer<?>> lookupSpinSerializers() {
  DataFormats globalFormats = DataFormats.getInstance();
  List<TypedValueSerializer<?>> serializers =
      SpinVariableSerializers.createObjectValueSerializers(globalFormats);
  serializers.addAll(SpinVariableSerializers.createSpinValueSerializers(globalFormats));

  return serializers;
}
 
Example #13
Source File: SpringBootSpinProcessEnginePlugin.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
protected void loadSpringBootDataFormats(ClassLoader classloader) {
  List<DataFormatConfigurator> configurators = new ArrayList<>();

  // add the auto-config Jackson Java 8 module configurators
  dataFormatConfiguratorJsr310.ifPresent(configurator -> configurators.add(configurator));
  dataFormatConfiguratorParameterNames.ifPresent(configurator -> configurators.add(configurator));
  dataFormatConfiguratorJdk8.ifPresent(configurator -> configurators.add(configurator));

  // next, add any configurators defined in the spring.factories file
  configurators.addAll(SpringFactoriesLoader.loadFactories(DataFormatConfigurator.class, classloader));

  DataFormats.loadDataFormats(classloader, configurators);
}
 
Example #14
Source File: SpinProcessEnginePluginTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testPluginDoesNotRegisterXmlSerializerIfNotPresentInClasspath() throws IOException {
  ClassLoader mockClassloader = Mockito.mock(ClassLoader.class);
  Mockito.when(mockClassloader.getResources(Mockito.anyString())).thenReturn(Collections.enumeration(Collections.<URL>emptyList()));
  DataFormats.loadDataFormats(mockClassloader);
  ProcessEngineConfigurationImpl mockConfig = Mockito.mock(ProcessEngineConfigurationImpl.class);
  DefaultVariableSerializers serializers = new DefaultVariableSerializers();
  Mockito.when(mockConfig.getVariableSerializers()).thenReturn(serializers);
  new SpinProcessEnginePlugin().registerSerializers(mockConfig);

  assertTrue(serializers.getSerializerByName(XmlValueType.TYPE_NAME) == null);
}
 
Example #15
Source File: SpinProcessEnginePluginTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testPluginDoesNotRegisterJsonSerializerIfNotPresentInClasspath() throws IOException {
  ClassLoader mockClassloader = Mockito.mock(ClassLoader.class);
  Mockito.when(mockClassloader.getResources(Mockito.anyString())).thenReturn(Collections.enumeration(Collections.<URL>emptyList()));
  DataFormats.loadDataFormats(mockClassloader);
  ProcessEngineConfigurationImpl mockConfig = Mockito.mock(ProcessEngineConfigurationImpl.class);
  DefaultVariableSerializers serializers = new DefaultVariableSerializers();
  Mockito.when(mockConfig.getVariableSerializers()).thenReturn(serializers);
  new SpinProcessEnginePlugin().registerSerializers(mockConfig);

  assertTrue(serializers.getSerializerByName(JsonValueType.TYPE_NAME) == null);
}
 
Example #16
Source File: SpinProcessEnginePluginTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testPluginRegistersXmlSerializerIfPresentInClasspath(){
  DataFormats.loadDataFormats(null);
  ProcessEngineConfigurationImpl mockConfig = Mockito.mock(ProcessEngineConfigurationImpl.class);
  Mockito.when(mockConfig.getVariableSerializers()).thenReturn(processEngineConfiguration.getVariableSerializers());
  new SpinProcessEnginePlugin().registerSerializers(mockConfig);

  assertTrue(processEngineConfiguration.getVariableSerializers().getSerializerByName(XmlValueType.TYPE_NAME) instanceof XmlValueSerializer);
}
 
Example #17
Source File: SpinProcessEnginePluginTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
public void testPluginRegistersJsonSerializerIfPresentInClasspath(){
  DataFormats.loadDataFormats(null);
  ProcessEngineConfigurationImpl mockConfig = Mockito.mock(ProcessEngineConfigurationImpl.class);
  Mockito.when(mockConfig.getVariableSerializers()).thenReturn(processEngineConfiguration.getVariableSerializers());
  new SpinProcessEnginePlugin().registerSerializers(mockConfig);

  assertTrue(processEngineConfiguration.getVariableSerializers().getSerializerByName(JsonValueType.TYPE_NAME) instanceof JsonValueSerializer);
}
 
Example #18
Source File: PaContextSwitchCustomSerializerTest.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Tests following scenario:
 * 1. Process application 1 declares custom de-/serializer for object variable. Process is started with object variable within process application 1.
 * 2. Process is modified within process application 2, so that variable deserialization is required -> correct deserializer is used.
 */
@Test
@OperateOnDeployment("pa3")
public void test() throws Exception {

  final ProcessInstance processInstance = withProcessApplicationContext(new Callable<ProcessInstance>() {

    @Override
    public ProcessInstance call() throws Exception {
      final XmlSerializable var = new XmlSerializable();
      var.setProperty("jonny");
      return runtimeService.startProcessInstanceByKey("processWithTimer", Variables.createVariables()
        .putValueTyped("testObject", Variables.objectValue(var).serializationDataFormat(DataFormats.JSON_DATAFORMAT_NAME).create()));
    }

  }, "pa3");

  withProcessApplicationContext(new Callable<Void>() {

    @Override
    public Void call() throws Exception {
      runtimeService.createProcessInstanceModification(processInstance.getProcessInstanceId()).startTransition("flow2")
        .execute();
      return null;
    }

  }, "pa4");

  assertEquals(1, historyService.createHistoricActivityInstanceQuery().activityId("exclusiveGateway").finished().count());

}
 
Example #19
Source File: XmlDomCreateTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldBeIdempotent() {
  SpinXmlElement xml = XML(EXAMPLE_XML);
  assertThat(xml).isEqualTo(XML(xml));
  assertThat(xml).isEqualTo(S(xml, xml()));
  assertThat(xml).isEqualTo(S(xml, DataFormats.XML_DATAFORMAT_NAME));
  assertThat(xml).isEqualTo(S(xml));
}
 
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 spinDeserializerDelegate() {
  return delegateExecution -> {
    SpinJsonNode jsonNode = S("{\"dateTime\": \"2019-12-12T22:22:22\"}");
    Object key = jsonNode.mapTo(SpinDto.class);
    delegateExecution.setVariables(Variables.createVariables().putValueTyped("dateTime",
        Variables
         .objectValue(key)
         .serializationDataFormat(DataFormats.JSON_DATAFORMAT_NAME)
         .create()));
  };
}
 
Example #21
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 #22
Source File: JsonTreeCreateTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateForReader() {
  SpinJsonNode json = JSON(stringAsReader(EXAMPLE_JSON));
  assertThat(json).isNotNull();

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

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

  json = S(stringAsReader(EXAMPLE_JSON));
  assertThat(json).isNotNull();
}
 
Example #23
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 #24
Source File: XmlDomCreateTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateForString() {
  SpinXmlElement xml = XML(EXAMPLE_XML);
  assertThat(xml).isNotNull();

  xml = S(EXAMPLE_XML, xml());
  assertThat(xml).isNotNull();

  xml = S(EXAMPLE_XML, DataFormats.XML_DATAFORMAT_NAME);
  assertThat(xml).isNotNull();

  xml = S(EXAMPLE_XML);
  assertThat(xml).isNotNull();
}
 
Example #25
Source File: XmlDomCreateTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldCreateForReader() {
  SpinXmlElement xml = XML(stringAsReader(EXAMPLE_XML));
  assertThat(xml).isNotNull();

  xml = S(stringAsReader(EXAMPLE_XML), xml());
  assertThat(xml).isNotNull();

  xml = S(stringAsReader(EXAMPLE_XML), DataFormats.XML_DATAFORMAT_NAME);
  assertThat(xml).isNotNull();

  xml = S(stringAsReader(EXAMPLE_XML));
  assertThat(xml).isNotNull();
}
 
Example #26
Source File: DataFormatLoadingTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( { DataFormats.class })
public void testConfigureDataFormat() {
  // given a custom data format provider that is returned by the service loader API
  mockProviders(new CustomDataFormatProvider());
  mockConfigurators(new ExampleCustomDataFormatConfigurator());

  DataFormat<?> format = DataFormats.getDataFormat(CustomDataFormatProvider.NAME);
  assertThat(format).isSameAs(CustomDataFormatProvider.DATA_FORMAT);

  // then the configuration was applied
  ExampleCustomDataFormat customFormat = (ExampleCustomDataFormat) format;
  assertThat(customFormat.getProperty()).isEqualTo(ExampleCustomDataFormatConfigurator.UPDATED_PROPERTY);
}
 
Example #27
Source File: JaxBContextProviderTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
/**
 * This test uses a dataformat with a JAXBContext that cannot resolve any classes.
 * Thus, it is expected that mapping an object to XML using this context fails.
 */
@Test
public void testCustomJaxBProvider() {

  Object objectToConvert = new Customer();

  // using the default jaxb context provider for conversion should work
  SpinXmlElement spinWrapper = Spin.XML(objectToConvert);
  spinWrapper.writeToWriter(new StringWriter());

  // using the custom jaxb context provider should fail with a JAXBException
  ((DomXmlDataFormat) DataFormats.xml()).setJaxBContextProvider(new EmptyContextProvider());
  try {
    spinWrapper = Spin.XML(objectToConvert);
    spinWrapper.writeToWriter(new StringWriter());
  } catch (SpinXmlDataFormatException e) {

    // assert that there is a jaxb exception somewhere in the exception hierarchy
    Set<Throwable> processedExceptions = new HashSet<Throwable>();
    while (!processedExceptions.contains(e.getCause()) && e.getCause() != null) {
      if (e.getCause() instanceof JAXBException) {
        // happy path
        return;
      }

      processedExceptions.add(e.getCause());
    }

    fail("expected a JAXBException in the cause hierarchy of the spin exception");
  }

}
 
Example #28
Source File: SpinFactoryImpl.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Spin<?>> T createSpin(Object parameter, String dataFormatName) {
  ensureNotNull("dataFormatName", dataFormatName);

  DataFormat<T> dataFormat = (DataFormat<T>) DataFormats.getDataFormat(dataFormatName);

  return createSpin(parameter, dataFormat);
}
 
Example #29
Source File: DataFormatLoadingTest.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
@Test
@PrepareForTest( { DataFormats.class })
public void testCustomDataFormatProvider() {
  // given a custom data format provider that is returned by the service loader API
  mockProviders(new CustomDataFormatProvider());
  mockConfigurators();

  // when the custom data format is requested
  DataFormat<?> customDataFormat = DataFormats.getDataFormat(CustomDataFormatProvider.NAME);

  // then it should be properly returned
  assertThat(customDataFormat).isNotNull();
  assertThat(customDataFormat).isSameAs(CustomDataFormatProvider.DATA_FORMAT);
}
 
Example #30
Source File: JsonValueSerializer.java    From camunda-bpm-platform with Apache License 2.0 4 votes vote down vote up
public JsonValueSerializer() {
  this(DataFormats.json());
}