inspection_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package inspection
  2. import (
  3. "testing"
  4. shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
  5. )
  6. type stubEventHandler struct {
  7. events []interface{}
  8. }
  9. func (h *stubEventHandler) CargoWasMisdirected(c *shipping.Cargo) {
  10. h.events = append(h.events, c)
  11. }
  12. func (h *stubEventHandler) CargoHasArrived(c *shipping.Cargo) {
  13. h.events = append(h.events, c)
  14. }
  15. func TestInspectMisdirectedCargo(t *testing.T) {
  16. var cargos mockCargoRepository
  17. events := mockHandlingEventRepository{
  18. events: make(map[shipping.TrackingID][]shipping.HandlingEvent),
  19. }
  20. handler := stubEventHandler{make([]interface{}, 0)}
  21. s := NewService(&cargos, &events, &handler)
  22. id := shipping.TrackingID("ABC123")
  23. c := shipping.NewCargo(id, shipping.RouteSpecification{
  24. Origin: shipping.SESTO,
  25. Destination: shipping.CNHKG,
  26. })
  27. voyage := shipping.VoyageNumber("001A")
  28. c.AssignToRoute(shipping.Itinerary{Legs: []shipping.Leg{
  29. {VoyageNumber: voyage, LoadLocation: shipping.SESTO, UnloadLocation: shipping.AUMEL},
  30. {VoyageNumber: voyage, LoadLocation: shipping.AUMEL, UnloadLocation: shipping.CNHKG},
  31. }})
  32. if err := cargos.Store(c); err != nil {
  33. t.Fatal(err)
  34. }
  35. storeEvent(&events, id, voyage, shipping.Receive, shipping.SESTO)
  36. storeEvent(&events, id, voyage, shipping.Load, shipping.SESTO)
  37. storeEvent(&events, id, voyage, shipping.Unload, shipping.USNYC)
  38. if len(handler.events) != 0 {
  39. t.Errorf("no events should be handled")
  40. }
  41. s.InspectCargo(id)
  42. if len(handler.events) != 1 {
  43. t.Errorf("1 event should be handled")
  44. }
  45. s.InspectCargo("no_such_id")
  46. // no events was published
  47. if len(handler.events) != 1 {
  48. t.Errorf("len(handler.events) = %d; want = %d", len(handler.events), 1)
  49. }
  50. }
  51. func TestInspectUnloadedCargo(t *testing.T) {
  52. var cargos mockCargoRepository
  53. events := mockHandlingEventRepository{
  54. events: make(map[shipping.TrackingID][]shipping.HandlingEvent),
  55. }
  56. handler := stubEventHandler{make([]interface{}, 0)}
  57. s := &service{
  58. cargos: &cargos,
  59. events: &events,
  60. handler: &handler,
  61. }
  62. id := shipping.TrackingID("ABC123")
  63. unloadedCargo := shipping.NewCargo(id, shipping.RouteSpecification{
  64. Origin: shipping.SESTO,
  65. Destination: shipping.CNHKG,
  66. })
  67. var voyage shipping.VoyageNumber = "001A"
  68. unloadedCargo.AssignToRoute(shipping.Itinerary{Legs: []shipping.Leg{
  69. {VoyageNumber: voyage, LoadLocation: shipping.SESTO, UnloadLocation: shipping.AUMEL},
  70. {VoyageNumber: voyage, LoadLocation: shipping.AUMEL, UnloadLocation: shipping.CNHKG},
  71. }})
  72. cargos.Store(unloadedCargo)
  73. storeEvent(&events, id, voyage, shipping.Receive, shipping.SESTO)
  74. storeEvent(&events, id, voyage, shipping.Load, shipping.SESTO)
  75. storeEvent(&events, id, voyage, shipping.Unload, shipping.AUMEL)
  76. storeEvent(&events, id, voyage, shipping.Load, shipping.AUMEL)
  77. storeEvent(&events, id, voyage, shipping.Unload, shipping.CNHKG)
  78. if len(handler.events) != 0 {
  79. t.Errorf("len(handler.events) = %d; want = %d", len(handler.events), 0)
  80. }
  81. s.InspectCargo(id)
  82. if len(handler.events) != 1 {
  83. t.Errorf("len(handler.events) = %d; want = %d", len(handler.events), 1)
  84. }
  85. }
  86. func storeEvent(r shipping.HandlingEventRepository, id shipping.TrackingID, voyageNumber shipping.VoyageNumber, typ shipping.HandlingEventType, loc shipping.UNLocode) {
  87. e := shipping.HandlingEvent{
  88. TrackingID: id,
  89. Activity: shipping.HandlingActivity{
  90. VoyageNumber: voyageNumber,
  91. Type: typ,
  92. Location: loc,
  93. },
  94. }
  95. r.Store(e)
  96. }
  97. type mockCargoRepository struct {
  98. cargo *shipping.Cargo
  99. }
  100. func (r *mockCargoRepository) Store(c *shipping.Cargo) error {
  101. r.cargo = c
  102. return nil
  103. }
  104. func (r *mockCargoRepository) Find(id shipping.TrackingID) (*shipping.Cargo, error) {
  105. if r.cargo != nil {
  106. return r.cargo, nil
  107. }
  108. return nil, shipping.ErrUnknownCargo
  109. }
  110. func (r *mockCargoRepository) FindAll() []*shipping.Cargo {
  111. return []*shipping.Cargo{r.cargo}
  112. }
  113. type mockHandlingEventRepository struct {
  114. events map[shipping.TrackingID][]shipping.HandlingEvent
  115. }
  116. func (r *mockHandlingEventRepository) Store(e shipping.HandlingEvent) {
  117. if _, ok := r.events[e.TrackingID]; !ok {
  118. r.events[e.TrackingID] = make([]shipping.HandlingEvent, 0)
  119. }
  120. r.events[e.TrackingID] = append(r.events[e.TrackingID], e)
  121. }
  122. func (r *mockHandlingEventRepository) QueryHandlingHistory(id shipping.TrackingID) shipping.HandlingHistory {
  123. return shipping.HandlingHistory{HandlingEvents: r.events[id]}
  124. }