ProcessVariablesPayloadValidator { private void checkPayloadVariables(Map<String, Object> variablePayloadMap, String processDefinitionId) { if (variablePayloadMap == null ) { return; } final String errorVariableName = "Variable has not a valid name: {0}"; final String errorVariableType = "Variables fail type validation: {0}"; final String errorVariableExpressionValue = "Expressions in variable values are only allowed as default value when modeling the process: {0}"; final Optional<Map<String, VariableDefinition>> variableDefinitionMap = getVariableDefinitionMap(processDefinitionId); List<ActivitiException> activitiExceptions = new ArrayList<>(); Set<String> mismatchedVars = new HashSet<>(); for (Map.Entry<String, Object> payloadVar : variablePayloadMap.entrySet()) { String name = payloadVar.getKey(); if (!variableNameValidator.validate(name)) { activitiExceptions.add(new ActivitiException(MessageFormat.format(errorVariableName, (name != null ? name : "null" )))); } else if (expressionResolver.containsExpression(payloadVar.getValue())) { activitiExceptions.add(new ActivitiException(MessageFormat.format(errorVariableExpressionValue, (name != null ? name : "null" )))); } else { boolean found = validateVariablesAgainstDefinitions(variableDefinitionMap, payloadVar, mismatchedVars); if (!found) { Object value = payloadVar.getValue(); if (value != null && (value instanceof String)) { try { payloadVar.setValue(dateFormatterProvider.toDate(value)); } catch (Exception e) { } } } } } if (!mismatchedVars.isEmpty()) { activitiExceptions.add(new ActivitiException(MessageFormat.format(errorVariableType, String.join(", ", mismatchedVars)))); } if (!activitiExceptions.isEmpty()) { throw new IllegalStateException(activitiExceptions.stream() .map(ex -> ex.getMessage()) .collect(Collectors.joining(","))); } } ProcessVariablesPayloadValidator(DateFormatterProvider dateFormatterProvider,
                                            ProcessExtensionService processExtensionService,
                                            VariableValidationService variableValidationService,
                                            VariableNameValidator variableNameValidator,
                                            ExpressionResolver expressionResolver); void checkPayloadVariables(SetProcessVariablesPayload setProcessVariablesPayload,
                                      String processDefinitionId); void checkStartProcessPayloadVariables(StartProcessPayload startProcessPayload,
                                                  String processDefinitionId); void checkStartMessagePayloadVariables(StartMessagePayload startMessagePayload,
                                                  String processDefinitionId); void checkReceiveMessagePayloadVariables(ReceiveMessagePayload receiveMessagePayload,
                                                    String processDefinitionId); void checkSignalPayloadVariables(SignalPayload signalPayload,
                                            String processDefinitionId);  }
@Test public void should_returnErrorList_when_setVariablesWithWrongType() { Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder .setVariables() .withVariables(map( "name", "Alice", "age", "24", "subscribe", "false" )) .build(), "10")); assertThat(throwable) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("subscribe, age"); }
@Test public void should_returnErrorList_when_setVariablesWithNameWrongType() { String expectedTypeErrorMessage = "age"; Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder .setVariables() .withVariables(map( "name", "Alice", "gender", "female", "age", "24", "subs", true, "subscribe", true, "mydate", "2019-08-26T10:20:30.000Z" )) .build(), "10")); assertThat(throwable) .isInstanceOf(IllegalStateException.class) .hasMessageContaining(expectedTypeErrorMessage); }
@Test public void should_returnError_when_setVariablesWithWrongDateFormat() { Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder .setVariables() .withVariables(singletonMap( "mydate", "2019-08-26TT10:20:30.000Z" )) .build(), "10")); assertThat(throwable).isInstanceOf(IllegalStateException.class); }
@Test public void should_returnError_when_setVariablesWithWrongDatetimeFormat() { Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder .setVariables() .withVariables(singletonMap( "mydatetime", "2019-08-26TT10:20:30.000Z" )) .build(), "10")); assertThat(throwable).isInstanceOf(IllegalStateException.class); }
@Test public void should_returnErrorList_when_setVariableWithWrongCharactersInName() { String expectedTypeErrorMessage = "gen-der"; Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder .setVariables() .withVariables(map( "name", "Alice", "gen-der", "female" )) .build(), "10")); assertThat(throwable) .isInstanceOf(IllegalStateException.class) .hasMessageContaining(expectedTypeErrorMessage); }
@Test public void should_throwIllegalStateException_when_payloadVariableWithExpressionInStringVariable() { Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder.setVariables().withVariables(map( "expression_string", "${variable}", "variable", "no-expression" )).build(), "10")); assertThat(throwable) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("expression"); }
@Test public void should_throwIllegalStateException_when_payloadVariableWithExpressionInObjectVariable() throws IOException { Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder.setVariables().withVariables(map( "expression_object", objectMapper.createObjectNode() .put("attr1", "value1") .put("attr2", "${variable}"), "variable", "no-expression" )).build(), "10")); assertThat(throwable) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("expression"); }
@Test public void should_throwIllegalStateException_when_payloadVariableWithExpressionInList() throws IOException { Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder.setVariables().withVariables(map( "expression_list", objectMapper.createObjectNode() .put("attr1", "value1") .set("attr2", objectMapper.createArrayNode() .add("1") .add("${variable}") .add("2") ) , "variable", "no-expression" )).build(), "10")); assertThat(throwable) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("expression"); }
@Test public void should_throwIllegalStateException_when_payloadVariableWithExpressionInMap() { Throwable throwable = catchThrowable(() -> processVariablesValidator.checkPayloadVariables( ProcessPayloadBuilder.setVariables().withVariables( singletonMap("expression_map", singletonMap("expression_string", "${variable}"))) .build(), "10")); assertThat(throwable) .isInstanceOf(IllegalStateException.class) .hasMessageContaining("expression"); }