aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/xyz/driver/pdsuidomain/formats/json/DocumentHistoryFormatSuite.scala
blob: d85a53bd3a0db04e36f8310f49d806cc5ce39110 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package xyz.driver.pdsuidomain.formats.json

import java.time.LocalDateTime

import org.scalatest.{FreeSpecLike, Matchers}
import spray.json._
import xyz.driver.pdsuicommon.domain.LongId
import xyz.driver.pdsuidomain.entities.DocumentHistory

class DocumentHistoryFormatSuite extends FreeSpecLike with Matchers {
  import xyz.driver.pdsuidomain.formats.json.documenthistory._

  "Can read and write DocumentHistory states" - {
    val states = DocumentHistory.State.All
    states.foreach { state =>s"$state" in test(state)}
  }

  "Can read and write DocumentHistory actions" - {
    val actions = DocumentHistory.Action.All
    actions.foreach { action =>s"$action" in test(action)}
  }

  private def test(state: DocumentHistory.State) = {
    documentStateFormat.read(documentStateFormat.write(state)) shouldBe state
  }

  private def test(action: DocumentHistory.Action) = {
    documentActionFormat.read(documentActionFormat.write(action)) shouldBe action
  }

  "Json format for DocumentHistory should read and write correct JSON" - {
    val documentHistory = DocumentHistory(
      id = LongId(10),
      documentId = LongId(1),
      executor = xyz.driver.core.Id("userId-001"),
      state = DocumentHistory.State.Extract,
      action = DocumentHistory.Action.Start,
      created = LocalDateTime.parse("2017-08-10T18:00:00")
    )
    val writtenJson = documentHistoryFormat.write(documentHistory)

    writtenJson should be("""{"id":10,"executor":"userId-001","documentId":1,"state":"Extract",
        "action":"Start","created":"2017-08-10T18:00Z"}""".parseJson)

    val parsedDocumentHistory = documentHistoryFormat.read(writtenJson)
    parsedDocumentHistory should be(documentHistory)
  }

}