|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestSubjects(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + updatedSince string |
| 16 | + serverResponse *SubjectsResponse |
| 17 | + statusCode int |
| 18 | + expectError bool |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "successful response", |
| 22 | + updatedSince: time.Now().AddDate(0, 0, -3).UTC().Format("2006-01-02T15:04:05.000"), |
| 23 | + serverResponse: &SubjectsResponse{ |
| 24 | + Data: []Subject{ |
| 25 | + { |
| 26 | + ID: "123778d5-ffcc-4911-8d3b-e43cfdb426f7", |
| 27 | + Name: "Test Subject", |
| 28 | + LastPosition: LastPosition{ |
| 29 | + Geometry: Geometry{ |
| 30 | + Coordinates: []float64{-121.6670876888658, 47.44309785582009}, |
| 31 | + Type: "Point", |
| 32 | + }, |
| 33 | + Properties: Properties{ |
| 34 | + DateTime: "2024-12-23T18:34:51+00:00", |
| 35 | + Title: "Test Subject", |
| 36 | + }, |
| 37 | + Type: "Feature", |
| 38 | + }, |
| 39 | + LastPositionDate: "2024-12-23T18:34:51+00:00", |
| 40 | + SubjectType: "person", |
| 41 | + SubjectSubtype: "ranger", |
| 42 | + }, |
| 43 | + }, |
| 44 | + Status: struct { |
| 45 | + Code int `json:"code"` |
| 46 | + Message string `json:"message"` |
| 47 | + }{ |
| 48 | + Code: 200, |
| 49 | + Message: "OK", |
| 50 | + }, |
| 51 | + }, |
| 52 | + statusCode: http.StatusOK, |
| 53 | + expectError: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "server error", |
| 57 | + updatedSince: time.Now().AddDate(0, 0, -3).UTC().Format("2006-01-02T15:04:05.000"), |
| 58 | + serverResponse: nil, |
| 59 | + statusCode: http.StatusInternalServerError, |
| 60 | + expectError: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "empty response", |
| 64 | + updatedSince: time.Now().AddDate(0, 0, -3).UTC().Format("2006-01-02T15:04:05.000"), |
| 65 | + serverResponse: &SubjectsResponse{ |
| 66 | + Data: []Subject{}, |
| 67 | + Status: struct { |
| 68 | + Code int `json:"code"` |
| 69 | + Message string `json:"message"` |
| 70 | + }{ |
| 71 | + Code: 200, |
| 72 | + Message: "OK", |
| 73 | + }, |
| 74 | + }, |
| 75 | + statusCode: http.StatusOK, |
| 76 | + expectError: false, |
| 77 | + }, |
| 78 | + } |
| 79 | + |
| 80 | + for _, tt := range tests { |
| 81 | + t.Run(tt.name, func(t *testing.T) { |
| 82 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 83 | + if r.Method != http.MethodGet { |
| 84 | + t.Errorf("expected GET request, got %s", r.Method) |
| 85 | + } |
| 86 | + if r.Header.Get("Authorization") != "Bearer testtoken" { |
| 87 | + t.Errorf("expected Bearer testtoken, got %s", r.Header.Get("Authorization")) |
| 88 | + } |
| 89 | + |
| 90 | + query := r.URL.Query() |
| 91 | + updatedSince := query.Get("updated_since") |
| 92 | + if updatedSince == "" { |
| 93 | + t.Error("expected updated_since parameter, got none") |
| 94 | + } |
| 95 | + |
| 96 | + if !strings.HasSuffix(r.URL.Path, API_SUBJECTS) { |
| 97 | + t.Errorf("expected path to end with %s, got %s", API_SUBJECTS, r.URL.Path) |
| 98 | + } |
| 99 | + |
| 100 | + w.WriteHeader(tt.statusCode) |
| 101 | + if tt.serverResponse != nil { |
| 102 | + if err := json.NewEncoder(w).Encode(tt.serverResponse); err != nil { |
| 103 | + t.Errorf("failed to encode response: %v", err) |
| 104 | + return |
| 105 | + } |
| 106 | + } |
| 107 | + })) |
| 108 | + defer server.Close() |
| 109 | + |
| 110 | + client := ERClient("test", "testtoken", server.URL) |
| 111 | + resp, err := client.Subjects(tt.updatedSince) |
| 112 | + |
| 113 | + if tt.expectError && err == nil { |
| 114 | + t.Error("expected error, got nil") |
| 115 | + } |
| 116 | + if !tt.expectError && err != nil { |
| 117 | + t.Errorf("unexpected error: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + if !tt.expectError && tt.serverResponse != nil { |
| 121 | + if resp == nil { |
| 122 | + t.Fatal("expected response, got nil") |
| 123 | + } |
| 124 | + |
| 125 | + if len(resp.Data) != len(tt.serverResponse.Data) { |
| 126 | + t.Errorf("expected %d subjects, got %d", |
| 127 | + len(tt.serverResponse.Data), len(resp.Data)) |
| 128 | + } |
| 129 | + |
| 130 | + if len(resp.Data) > 0 { |
| 131 | + expectedSubject := tt.serverResponse.Data[0] |
| 132 | + actualSubject := resp.Data[0] |
| 133 | + |
| 134 | + if actualSubject.ID != expectedSubject.ID { |
| 135 | + t.Errorf("expected subject ID %s, got %s", |
| 136 | + expectedSubject.ID, actualSubject.ID) |
| 137 | + } |
| 138 | + |
| 139 | + if actualSubject.Name != expectedSubject.Name { |
| 140 | + t.Errorf("expected subject name %s, got %s", |
| 141 | + expectedSubject.Name, actualSubject.Name) |
| 142 | + } |
| 143 | + |
| 144 | + if len(actualSubject.LastPosition.Geometry.Coordinates) != 2 { |
| 145 | + t.Error("expected coordinates to have latitude and longitude") |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if resp.Status.Code != tt.serverResponse.Status.Code { |
| 150 | + t.Errorf("expected status code %d, got %d", |
| 151 | + tt.serverResponse.Status.Code, resp.Status.Code) |
| 152 | + } |
| 153 | + } |
| 154 | + }) |
| 155 | + } |
| 156 | +} |
0 commit comments