All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.camunda.process.test.impl.testresult.CamundaProcessTestResultPrinter Maven / Gradle / Ivy

There is a newer version: 8.7.0-alpha3
Show newest version
/*
 * Copyright © 2017 camunda services GmbH ([email protected])
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.camunda.process.test.impl.testresult;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;

public class CamundaProcessTestResultPrinter {

  private static final String NO_ENTRIES = "";
  private static final int MAX_VALUE_LENGTH = 500;

  private final Consumer printStream;

  public CamundaProcessTestResultPrinter(final Consumer printStream) {
    this.printStream = printStream;
  }

  public void print(final ProcessTestResult result) {
    final String formattedResult = formatResult(result);
    printStream.accept(formattedResult);
  }

  private String formatResult(final ProcessTestResult result) {
    return "Process test results:\n"
        + "=====================\n\n"
        + formatProcessInstances(result.getProcessInstanceTestResults())
        + "\n"
        + "=====================\n";
  }

  private static String formatProcessInstances(
      final List processInstanceResults) {
    return processInstanceResults.stream()
        .map(CamundaProcessTestResultPrinter::formatProcessInstance)
        .collect(Collectors.joining("\n---------------------\n\n"));
  }

  private static String formatProcessInstance(final ProcessInstanceResult result) {
    final String formattedProcessInstance =
        String.format(
            "Process instance: %d [process-id: '%s']",
            result.getProcessInstanceKey(), result.getProcessId());

    return formattedProcessInstance
        + "\n\n"
        + "Variables:\n"
        + formatVariables(result.getVariables())
        + "\n\n"
        + "Open incidents:\n"
        + formatIncidents(result.getOpenIncidents());
  }

  private static String formatVariables(final Map variables) {
    if (variables.isEmpty()) {
      return NO_ENTRIES;
    } else {
      return variables.entrySet().stream()
          .map(variable -> formatVariable(variable.getKey(), variable.getValue()))
          .collect(Collectors.joining("\n"));
    }
  }

  private static String formatVariable(final String key, final String value) {
    return String.format("- '%s': %s", key, abbreviate(value));
  }

  private static String abbreviate(final String value) {
    return StringUtils.abbreviate(value, MAX_VALUE_LENGTH);
  }

  private static String formatIncidents(final List incidents) {
    if (incidents.isEmpty()) {
      return NO_ENTRIES;
    } else {
      return incidents.stream()
          .map(CamundaProcessTestResultPrinter::formatIncident)
          .collect(Collectors.joining("\n"));
    }
  }

  private static String formatIncident(final OpenIncident incident) {
    return String.format(
        "- '%s' [type: %s] \"%s\"",
        incident.getFlowNodeId(), incident.getType(), abbreviate(incident.getMessage()));
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy