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

de.ikor.sip.foundation.testkit.util.HttpInvokerHelper Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version
package de.ikor.sip.foundation.testkit.util;

import de.ikor.sip.foundation.core.proxies.ProcessorProxy;
import de.ikor.sip.foundation.testkit.workflow.whenphase.routeinvoker.RouteInvoker;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.builder.ExchangeBuilder;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;

/** Helper class with common methods for rest and soap invokers */
public class HttpInvokerHelper {

  private HttpInvokerHelper() {}

  /**
   * Creates headers for test kit execution
   *
   * @param exchange {@link Exchange} from which data is extracted
   * @return Map with test kit headers
   */
  public static MultiValueMap prepareHeaders(Exchange exchange) {
    MultiValueMap headers = new HttpHeaders();
    Map existingHeaders = exchange.getMessage().getHeaders();

    existingHeaders.forEach(
        (key, value) -> {
          if (value instanceof List) {
            ((List) value).forEach(val -> headers.add(key, val.toString()));
          } else {
            headers.add(key, value.toString());
          }
        });
    addHeaderIfAbsent(
        headers,
        RouteInvoker.TEST_NAME_HEADER,
        exchange.getMessage().getHeader(RouteInvoker.TEST_NAME_HEADER, String.class));
    addHeaderIfAbsent(
        headers,
        ProcessorProxy.TEST_MODE_HEADER,
        exchange.getMessage().getHeader(ProcessorProxy.TEST_MODE_HEADER, String.class));
    return headers;
  }

  private static void addHeaderIfAbsent(
      MultiValueMap headers, String headerName, String headerValue) {
    if (!headers.containsKey(headerName) && headerValue != null) {
      headers.add(headerName, headerValue);
    }
  }

  /**
   * Create {@link Exchange} with a response recovered from test execution
   *
   * @param response {@link ResponseEntity}asd98 of test execution
   * @param camelContext {@link CamelContext}
   * @return {@link Exchange} with response
   */
  public static Exchange createExchangeResponse(
      ResponseEntity response, CamelContext camelContext) {
    ExchangeBuilder exchangeBuilder =
        ExchangeBuilder.anExchange(camelContext).withBody(formatToOneLine(response.getBody()));
    response.getHeaders().forEach(exchangeBuilder::withHeader);
    return exchangeBuilder.build();
  }

  private static String formatToOneLine(String multilineString) {
    if (multilineString != null) {
      return multilineString.lines().map(String::strip).collect(Collectors.joining(""));
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy