<commit_before>package pl.touk.esp.ui.process.marshall

import org.scalatest.{FlatSpec, Matchers}
import pl.touk.esp.engine.api.MetaData
import pl.touk.esp.engine.canonicalgraph.CanonicalProcess
import pl.touk.esp.engine.compile.ProcessValidator
import pl.touk.esp.engine.definition.DefinitionExtractor.ObjectDefinition
import pl.touk.esp.engine.definition.ProcessDefinitionExtractor.ProcessDefinition
import pl.touk.esp.engine.graph.exceptionhandler.ExceptionHandlerRef
import pl.touk.esp.engine.graph.expression.Expression
import pl.touk.esp.engine.graph.node._
import pl.touk.esp.engine.graph.service.ServiceRef
import pl.touk.esp.engine.graph.source.SourceRef
import pl.touk.esp.ui.api.ProcessValidation
import pl.touk.esp.ui.process.displayedgraph.displayablenode.Edge
import pl.touk.esp.ui.process.displayedgraph.{DisplayableProcess, ProcessProperties}

class ProcessConverterSpec extends FlatSpec with Matchers {

  val converter = {
    val processDefinition = ProcessDefinition[ObjectDefinition](Map("ref" -> ObjectDefinition.noParam),
      Map("sourceRef" -> ObjectDefinition.noParam), Map(), Map(), ObjectDefinition.noParam, Map(), List())
    val validator = ProcessValidator.default(processDefinition)
    val processValidation = new ProcessValidation(validator)
    new ProcessConverter(processValidation)
  }

  def canonicalDisplayable = (converter.toDisplayable _).andThen(converter.fromDisplayable)

  def displayableCanonical = (converter.fromDisplayable _).andThen(converter.toDisplayable)

  it should "be able to convert empty process" in {
    val emptyProcess = CanonicalProcess(MetaData(id = "t1"), ExceptionHandlerRef(List()), List())

    canonicalDisplayable(emptyProcess) shouldBe emptyProcess
  }

  it should "be able to handle different node order" in {
    val process = DisplayableProcess("t1", ProcessProperties(Some(2), ExceptionHandlerRef(List()), None),
      List(
        Processor("e", ServiceRef("ref", List())),
        Source("s", SourceRef("sourceRef", List()))
      ), List(Edge("s", "e", None)), ProcessValidation.ValidationResult.success)

    displayableCanonical(process).nodes.toSet shouldBe process.nodes.toSet
    displayableCanonical(process).edges.toSet shouldBe process.edges.toSet

  }

  it should "be able to convert process ending not properly" in {

    val unexpectedEnds = List(
      Filter("e", Expression("spel", "0")),
      Switch("e", Expression("spel", "0"), "a"),
      Processor("e", ServiceRef("ref", List())),
      Enricher("e", ServiceRef("ref", List()), "out"),
      Split("e")
    )

    unexpectedEnds.foreach { unexpectedEnd =>
      val process = DisplayableProcess("t1", ProcessProperties(Some(2), ExceptionHandlerRef(List()), None),
        List(Source("s", SourceRef("sourceRef", List())), unexpectedEnd),
        List(Edge("s", "e", None)), ProcessValidation.ValidationResult.success
      )
      displayableCanonical(process) shouldBe process
    }
  }


}<commit_msg>process with invalid end serialization test fix<commit_after>package pl.touk.esp.ui.process.marshall

import org.scalatest.prop.TableDrivenPropertyChecks
import org.scalatest.{FlatSpec, Matchers}
import pl.touk.esp.engine.api.MetaData
import pl.touk.esp.engine.canonicalgraph.CanonicalProcess
import pl.touk.esp.engine.compile.ProcessValidator
import pl.touk.esp.engine.definition.DefinitionExtractor.ObjectDefinition
import pl.touk.esp.engine.definition.ProcessDefinitionExtractor.ProcessDefinition
import pl.touk.esp.engine.graph.exceptionhandler.ExceptionHandlerRef
import pl.touk.esp.engine.graph.expression.Expression
import pl.touk.esp.engine.graph.node._
import pl.touk.esp.engine.graph.service.ServiceRef
import pl.touk.esp.engine.graph.source.SourceRef
import pl.touk.esp.ui.api.ProcessValidation
import pl.touk.esp.ui.api.ProcessValidation.{NodeValidationError, ValidationResult}
import pl.touk.esp.ui.process.displayedgraph.displayablenode.Edge
import pl.touk.esp.ui.process.displayedgraph.{DisplayableProcess, ProcessProperties}

class ProcessConverterSpec extends FlatSpec with Matchers with TableDrivenPropertyChecks {

  val converter = {
    val processDefinition = ProcessDefinition[ObjectDefinition](Map("ref" -> ObjectDefinition.noParam),
      Map("sourceRef" -> ObjectDefinition.noParam), Map(), Map(), ObjectDefinition.noParam, Map(), List())
    val validator = ProcessValidator.default(processDefinition)
    val processValidation = new ProcessValidation(validator)
    new ProcessConverter(processValidation)
  }

  def canonicalDisplayable = (converter.toDisplayable _).andThen(converter.fromDisplayable)

  def displayableCanonical = (converter.fromDisplayable _).andThen(converter.toDisplayable)

  it should "be able to convert empty process" in {
    val emptyProcess = CanonicalProcess(MetaData(id = "t1"), ExceptionHandlerRef(List()), List())

    canonicalDisplayable(emptyProcess) shouldBe emptyProcess
  }

  it should "be able to handle different node order" in {
    val process = DisplayableProcess("t1", ProcessProperties(Some(2), ExceptionHandlerRef(List()), None),
      List(
        Processor("e", ServiceRef("ref", List())),
        Source("s", SourceRef("sourceRef", List()))
      ), List(Edge("s", "e", None)), ProcessValidation.ValidationResult.success)

    displayableCanonical(process).nodes.toSet shouldBe process.nodes.toSet
    displayableCanonical(process).edges.toSet shouldBe process.edges.toSet

  }

  it should "be able to convert process ending not properly" in {

    forAll(Table(
      "unexpectedEnd",
      Filter("e", Expression("spel", "0")),
      Switch("e", Expression("spel", "0"), "a"),
      Enricher("e", ServiceRef("ref", List()), "out"),
      Split("e")
    )) { (unexpectedEnd) =>
      val process = DisplayableProcess("t1", ProcessProperties(Some(2), ExceptionHandlerRef(List()), None),
        List(Source("s", SourceRef("sourceRef", List())), unexpectedEnd),
        List(Edge("s", "e", None)), ValidationResult(Map(unexpectedEnd.id -> List(NodeValidationError("InvalidTailOfBranch","InvalidTailOfBranch",None))))
      )
      displayableCanonical(process) shouldBe process
    }
  }


}
{'fraction_non_alphanumeric': 0.12481644640234948, 'fraction_numerical': 0.002284222548539729, 'mean_word_length': 5.210739614994934, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3144013', 'n_tokens_mistral': 1789, 'n_tokens_neox': 1733, 'n_words': 343}