tracking_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package endpoint
  2. import (
  3. "encoding/json"
  4. "io/ioutil"
  5. "net/http"
  6. "net/http/httptest"
  7. "reflect"
  8. "testing"
  9. "time"
  10. "github.com/go-kit/kit/log"
  11. "github.com/longjoy/micro-go-course/section19/cargo/mock"
  12. shipping "github.com/longjoy/micro-go-course/section19/cargo/model"
  13. "github.com/longjoy/micro-go-course/section19/cargo/service/tracking"
  14. )
  15. func TestTrackCargo(t *testing.T) {
  16. var cargos mockCargoRepository
  17. var events mock.HandlingEventRepository
  18. events.QueryHandlingHistoryFn = func(shipping.TrackingID) shipping.HandlingHistory {
  19. return shipping.HandlingHistory{}
  20. }
  21. s := tracking.NewService(&cargos, &events)
  22. c := shipping.NewCargo("TEST", shipping.RouteSpecification{
  23. Origin: "SESTO",
  24. Destination: "FIHEL",
  25. ArrivalDeadline: time.Date(2005, 12, 4, 0, 0, 0, 0, time.UTC),
  26. })
  27. cargos.Store(c)
  28. logger := log.NewLogfmtLogger(ioutil.Discard)
  29. h := New(nil, s, nil, logger)
  30. req, _ := http.NewRequest("GET", "http://example.com/tracking/v1/cargos/TEST", nil)
  31. rec := httptest.NewRecorder()
  32. h.ServeHTTP(rec, req)
  33. if rec.Code != http.StatusOK {
  34. t.Errorf("rec.Code = %d; want = %d", rec.Code, http.StatusOK)
  35. }
  36. if content := rec.Header().Get("Content-Type"); content != "application/json; charset=utf-8" {
  37. t.Errorf("Content-Type = %q; want = %q", content, "application/json; charset=utf-8")
  38. }
  39. var response struct {
  40. Cargo *tracking.Cargo `json:"cargo"`
  41. Err error `json:"err"`
  42. }
  43. if err := json.NewDecoder(rec.Body).Decode(&response); err != nil {
  44. t.Error(err)
  45. }
  46. if response.Err != nil {
  47. t.Errorf("response.Err = %q", response.Err)
  48. }
  49. var eta time.Time
  50. want := tracking.Cargo{
  51. TrackingID: "TEST",
  52. Origin: "SESTO",
  53. Destination: "FIHEL",
  54. ArrivalDeadline: time.Date(2005, 12, 4, 0, 0, 0, 0, time.UTC),
  55. ETA: eta.In(time.UTC),
  56. StatusText: "Not received",
  57. NextExpectedActivity: "There are currently no expected activities for this shipping.",
  58. Events: nil,
  59. }
  60. if !reflect.DeepEqual(want, *response.Cargo) {
  61. t.Errorf("response.Cargo = %#v; want = %#v", response.Cargo, want)
  62. }
  63. }
  64. func TestTrackUnknownCargo(t *testing.T) {
  65. var cargos mockCargoRepository
  66. var events mock.HandlingEventRepository
  67. events.QueryHandlingHistoryFn = func(shipping.TrackingID) shipping.HandlingHistory {
  68. return shipping.HandlingHistory{}
  69. }
  70. s := tracking.NewService(&cargos, &events)
  71. logger := log.NewLogfmtLogger(ioutil.Discard)
  72. h := New(nil, s, nil, logger)
  73. req, _ := http.NewRequest("GET", "http://example.com/tracking/v1/cargos/not_found", nil)
  74. rec := httptest.NewRecorder()
  75. h.ServeHTTP(rec, req)
  76. if rec.Code != http.StatusNotFound {
  77. t.Errorf("rec.Code = %d; want = %d", rec.Code, http.StatusNotFound)
  78. }
  79. wantContent := "application/json; charset=utf-8"
  80. if got := rec.Header().Get("Content-Type"); got != wantContent {
  81. t.Errorf("Content-Type = %q; want = %q", got, wantContent)
  82. }
  83. var response map[string]interface{}
  84. if err := json.NewDecoder(rec.Body).Decode(&response); err != nil {
  85. t.Error(err)
  86. }
  87. err, ok := response["error"]
  88. if !ok {
  89. t.Error("missing error")
  90. }
  91. if err != "unknown cargo" {
  92. t.Errorf(`"error": %q; want = %q`, err, "unknown cargo")
  93. }
  94. }
  95. type mockCargoRepository struct {
  96. cargo *shipping.Cargo
  97. }
  98. func (r *mockCargoRepository) Store(c *shipping.Cargo) error {
  99. r.cargo = c
  100. return nil
  101. }
  102. func (r *mockCargoRepository) Find(id shipping.TrackingID) (*shipping.Cargo, error) {
  103. if r.cargo != nil {
  104. return r.cargo, nil
  105. }
  106. return nil, shipping.ErrUnknownCargo
  107. }
  108. func (r *mockCargoRepository) FindAll() []*shipping.Cargo {
  109. return []*shipping.Cargo{r.cargo}
  110. }