<commit_before>package pl.touk.esp.engine.standalone.test

import argonaut.PrettyParams
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigValueFactory.fromAnyRef
import org.scalatest.{FlatSpec, Matchers}
import pl.touk.esp.engine.api.Context
import pl.touk.esp.engine.api.deployment.test._
import pl.touk.esp.engine.api.exception.EspExceptionInfo
import pl.touk.esp.engine.build.EspProcessBuilder
import pl.touk.esp.engine.marshall.ProcessMarshaller
import pl.touk.esp.engine.spel
import pl.touk.esp.engine.standalone.management.StandaloneTestMain
import pl.touk.esp.engine.standalone.{ProcessorService, Request1}

class StandaloneTestMainSpec extends FlatSpec with Matchers {

  import spel.Implicits._
  import scala.concurrent.ExecutionContext.Implicits.global

  val ProcessMarshaller = new ProcessMarshaller

  it should "perform test on mocks" in {
    val process = EspProcessBuilder
      .id("proc1")
      .exceptionHandler()
      .source("start", "request1-source")
      .filter("filter1", "#input.field1() == 'a'")
      .enricher("enricher", "var1", "enricherService")
      .processor("processor", "processorService")
      .sink("endNodeIID", "#var1", "response-sink")

    val input = """{ "field1": "a", "field2": "b" }
      |{ "field1": "c", "field2": "d" }""".stripMargin
    val config = ConfigFactory.load()
      .withValue("processConfigCreatorClass", fromAnyRef("pl.touk.esp.engine.standalone.StandaloneProcessConfigCreator"))

    val results = StandaloneTestMain.run(ProcessMarshaller.toJson(process, PrettyParams.spaces2), config, new TestData(input.getBytes), List())

    results.nodeResults("filter1") shouldBe List(
      NodeResult(Context("proc1-0", Map("input" -> Request1("a","b")))),
      NodeResult(Context("proc1-1", Map("input" -> Request1("c","d"))))
    )

    results.invocationResults("filter1") shouldBe List(
      ExpressionInvocationResult(Context("proc1-0", Map("input" -> Request1("a","b"))), "expression", true),
      ExpressionInvocationResult(Context("proc1-1", Map("input" -> Request1("c","d"))), "expression", false)
    )

    results.mockedResults("processor") shouldBe List(MockedResult(Context("proc1-0"), "processorService", "processor service invoked"))
    results.mockedResults("endNodeIID") shouldBe List(MockedResult(Context("proc1-0"), "response-sink", "alamakota"))

    ProcessorService.invocationsCount.get shouldBe 0

  }

  it should "detect errors in nodes" in {
    val process = EspProcessBuilder
      .id("proc1")
      .exceptionHandler()
      .source("start", "request1-source")
      .filter("occasionallyThrowFilter", "#input.field1() == 'a' ? 1/0 == 0 : true")
      .filter("filter1", "#input.field1() == 'a'")
      .enricher("enricher", "var1", "enricherService")
      .processor("processor", "processorService")
      .sink("endNodeIID", "#var1", "response-sink")

    val input = """{ "field1": "a", "field2": "b" }
                  |{ "field1": "c", "field2": "d" }""".stripMargin
    val config = ConfigFactory.load()
      .withValue("processConfigCreatorClass", fromAnyRef("pl.touk.esp.engine.standalone.StandaloneProcessConfigCreator"))

    val results = StandaloneTestMain.run(ProcessMarshaller.toJson(process, PrettyParams.spaces2), config, new TestData(input.getBytes), List())

    results.invocationResults("occasionallyThrowFilter") shouldBe List(ExpressionInvocationResult(Context("proc1-1", Map("input" -> Request1("c","d"))), "expression", true))
    results.exceptions should have size 1
    results.exceptions.head.context shouldBe Context("proc1-0", Map("input" -> Request1("a","b")))
    results.exceptions.head.nodeId shouldBe Some("occasionallyThrowFilter")
    results.exceptions.head.throwable.getMessage shouldBe "/ by zero"
  }


}<commit_msg>non-deterministic test fix<commit_after>package pl.touk.esp.engine.standalone.test

import argonaut.PrettyParams
import com.typesafe.config.ConfigFactory
import com.typesafe.config.ConfigValueFactory.fromAnyRef
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, FlatSpec, Matchers}
import pl.touk.esp.engine.api.Context
import pl.touk.esp.engine.api.deployment.test._
import pl.touk.esp.engine.api.exception.EspExceptionInfo
import pl.touk.esp.engine.build.EspProcessBuilder
import pl.touk.esp.engine.marshall.ProcessMarshaller
import pl.touk.esp.engine.spel
import pl.touk.esp.engine.standalone.management.StandaloneTestMain
import pl.touk.esp.engine.standalone.{ProcessorService, Request1}

class StandaloneTestMainSpec extends FlatSpec with Matchers with BeforeAndAfterEach {

  import spel.Implicits._
  import scala.concurrent.ExecutionContext.Implicits.global

  val ProcessMarshaller = new ProcessMarshaller

  it should "perform test on mocks" in {
    val process = EspProcessBuilder
      .id("proc1")
      .exceptionHandler()
      .source("start", "request1-source")
      .filter("filter1", "#input.field1() == 'a'")
      .enricher("enricher", "var1", "enricherService")
      .processor("processor", "processorService")
      .sink("endNodeIID", "#var1", "response-sink")

    val input = """{ "field1": "a", "field2": "b" }
      |{ "field1": "c", "field2": "d" }""".stripMargin
    val config = ConfigFactory.load()
      .withValue("processConfigCreatorClass", fromAnyRef("pl.touk.esp.engine.standalone.StandaloneProcessConfigCreator"))

    val results = StandaloneTestMain.run(ProcessMarshaller.toJson(process, PrettyParams.spaces2), config, new TestData(input.getBytes), List())

    results.nodeResults("filter1") shouldBe List(
      NodeResult(Context("proc1-0", Map("input" -> Request1("a","b")))),
      NodeResult(Context("proc1-1", Map("input" -> Request1("c","d"))))
    )

    results.invocationResults("filter1") shouldBe List(
      ExpressionInvocationResult(Context("proc1-0", Map("input" -> Request1("a","b"))), "expression", true),
      ExpressionInvocationResult(Context("proc1-1", Map("input" -> Request1("c","d"))), "expression", false)
    )

    results.mockedResults("processor") shouldBe List(MockedResult(Context("proc1-0"), "processorService", "processor service invoked"))
    results.mockedResults("endNodeIID") shouldBe List(MockedResult(Context("proc1-0"), "response-sink", "alamakota"))

    ProcessorService.invocationsCount.get shouldBe 0

  }

  it should "detect errors in nodes" in {
    val process = EspProcessBuilder
      .id("proc1")
      .exceptionHandler()
      .source("start", "request1-source")
      .filter("occasionallyThrowFilter", "#input.field1() == 'a' ? 1/0 == 0 : true")
      .filter("filter1", "#input.field1() == 'a'")
      .enricher("enricher", "var1", "enricherService")
      .processor("processor", "processorService")
      .sink("endNodeIID", "#var1", "response-sink")

    val input = """{ "field1": "a", "field2": "b" }
                  |{ "field1": "c", "field2": "d" }""".stripMargin
    val config = ConfigFactory.load()
      .withValue("processConfigCreatorClass", fromAnyRef("pl.touk.esp.engine.standalone.StandaloneProcessConfigCreator"))

    val results = StandaloneTestMain.run(ProcessMarshaller.toJson(process, PrettyParams.spaces2), config, new TestData(input.getBytes), List())

    results.invocationResults("occasionallyThrowFilter") shouldBe List(ExpressionInvocationResult(Context("proc1-1", Map("input" -> Request1("c","d"))), "expression", true))
    results.exceptions should have size 1
    results.exceptions.head.context shouldBe Context("proc1-0", Map("input" -> Request1("a","b")))
    results.exceptions.head.nodeId shouldBe Some("occasionallyThrowFilter")
    results.exceptions.head.throwable.getMessage shouldBe "/ by zero"
  }

  override protected def beforeEach(): Unit = {
    ProcessorService.clear()
  }
}
{'fraction_non_alphanumeric': 0.15661403054620762, 'fraction_numerical': 0.01371990680818017, 'mean_word_length': 4.840513983371126, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 15, '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': '4337807', 'n_tokens_mistral': 2452, 'n_tokens_neox': 2389, 'n_words': 426}