<commit_before>package hmda.persistence.processing

import akka.actor.ActorRef
import akka.testkit.TestProbe
import hmda.model.fi.{ Submission, SubmissionId }
import hmda.persistence.messages.CommonMessages.GetState
import hmda.persistence.model.ActorSpec
import hmda.persistence.processing.ProcessingMessages._
import hmda.persistence.processing.SubmissionFSM._

class SubmissionFSMSpec extends ActorSpec {

  val probe = TestProbe()

  val submissionId = SubmissionId("12345", "2016", 1)

  "Submission Finite State Machine" must {
    "transition through states" in {
      val fsm = actorRef()

      probe.send(fsm, Create)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Created)))

      probe.send(fsm, StartUpload)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Uploading)))

      probe.send(fsm, CompleteUpload)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Uploaded)))

      probe.send(fsm, StartParsing)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Parsing)))

      probe.send(fsm, CompleteParsing)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Parsed)))

      probe.send(fsm, BeginValidation(probe.testActor))
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Validating)))

      probe.send(fsm, CompleteValidation(probe.testActor))
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Validated)))

      probe.send(fsm, Sign)
      probe.expectMsg(Some(hmda.model.fi.Signed))
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Signed)))

      system stop fsm

    }
    "respond with None for invalid state transition" in {
      val subId = SubmissionId("instId", "period", 4)
      val fsm = createSubmissionFSM(system, subId)

      probe.send(fsm, Create)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(subId, hmda.model.fi.Created)))

      // Cannot sign a submission that isn't in state Validated or ValidatedWithErrors
      probe.send(fsm, Sign)
      probe.expectMsg(None)

      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(subId, hmda.model.fi.Created)))

      system stop fsm
    }
    "recover persisted state" in {
      val fsm = actorRef()

      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Signed)))
    }

    /*
    "fail a submission" in {
      val fsm = actorRef()

      probe.send(fsm, None)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Failed(SubmissionFSM.failedMsg))))
    }
    */
  }

  private def actorRef(): ActorRef = createSubmissionFSM(system, submissionId)

}<commit_msg>Uncomment test, since FSM now uses Failed state again<commit_after>package hmda.persistence.processing

import akka.actor.ActorRef
import akka.testkit.TestProbe
import hmda.model.fi.{ Submission, SubmissionId }
import hmda.persistence.messages.CommonMessages.GetState
import hmda.persistence.model.ActorSpec
import hmda.persistence.processing.ProcessingMessages._
import hmda.persistence.processing.SubmissionFSM._

class SubmissionFSMSpec extends ActorSpec {

  val probe = TestProbe()

  val submissionId = SubmissionId("12345", "2016", 1)

  "Submission Finite State Machine" must {
    "transition through states" in {
      val fsm = actorRef()

      probe.send(fsm, Create)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Created)))

      probe.send(fsm, StartUpload)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Uploading)))

      probe.send(fsm, CompleteUpload)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Uploaded)))

      probe.send(fsm, StartParsing)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Parsing)))

      probe.send(fsm, CompleteParsing)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Parsed)))

      probe.send(fsm, BeginValidation(probe.testActor))
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Validating)))

      probe.send(fsm, CompleteValidation(probe.testActor))
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Validated)))

      probe.send(fsm, Sign)
      probe.expectMsg(Some(hmda.model.fi.Signed))
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Signed)))

      system stop fsm

    }
    "respond with None for invalid state transition" in {
      val subId = SubmissionId("instId", "period", 4)
      val fsm = createSubmissionFSM(system, subId)

      probe.send(fsm, Create)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(subId, hmda.model.fi.Created)))

      // Cannot sign a submission that isn't in state Validated or ValidatedWithErrors
      probe.send(fsm, Sign)
      probe.expectMsg(None)

      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(subId, hmda.model.fi.Created)))

      system stop fsm
    }
    "recover persisted state" in {
      val fsm = actorRef()

      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Signed)))
    }

    "fail a submission" in {
      val fsm = actorRef()

      probe.send(fsm, None)
      probe.send(fsm, GetState)
      probe.expectMsg(NonEmptySubmissionData(Submission(submissionId, hmda.model.fi.Failed(SubmissionFSM.failedMsg))))
    }
  }

  private def actorRef(): ActorRef = createSubmissionFSM(system, submissionId)

}
{'fraction_non_alphanumeric': 0.10703125, 'fraction_numerical': 0.0034375, 'mean_word_length': 3.9968774395003903, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4047740', 'n_tokens_mistral': 2125, 'n_tokens_neox': 2062, 'n_words': 346}