|
| 1 | +package tripsgo |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "io/ioutil" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + tripSvc "github.com/Azure-Samples/openhack-devops-team/apis/trips/tripsgo" |
| 12 | +) |
| 13 | + |
| 14 | +var tripID string |
| 15 | + |
| 16 | +var apiTestList = []tripSvc.APITestCase{ |
| 17 | + { |
| 18 | + Tag: "t1 - Get all trips", |
| 19 | + Method: "GET", |
| 20 | + URL: "/api/trips", |
| 21 | + Status: 200, |
| 22 | + }, |
| 23 | + { |
| 24 | + Tag: "t2 - Get a nonexistent trip", |
| 25 | + Method: "GET", |
| 26 | + URL: "/api/trips/99999", |
| 27 | + Status: 404, |
| 28 | + }, |
| 29 | + { |
| 30 | + Tag: "t3 - Create a Trip", |
| 31 | + Method: "POST", |
| 32 | + URL: "/api/trips", |
| 33 | + Body: `{ |
| 34 | + "Name":"Trip CREATE TEST", |
| 35 | + "UserId":"GO_TEST", |
| 36 | + "RecordedTimeStamp": "2018-04-19T19:08:16.03Z", |
| 37 | + "EndTimeStamp": "2018-04-19T19:42:49.573Z", |
| 38 | + "Rating":95, |
| 39 | + "IsComplete":false, |
| 40 | + "HasSimulatedOBDData":true, |
| 41 | + "AverageSpeed":100, |
| 42 | + "FuelUsed":10.27193484, |
| 43 | + "HardStops":2, |
| 44 | + "HardAccelerations":4, |
| 45 | + "Distance":30.0275486, |
| 46 | + "CreatedAt":"2018-01-01T12:00:00Z", |
| 47 | + "UpdatedAt":"2001-01-01T12:00:00Z" |
| 48 | + }`, |
| 49 | + Status: 200, |
| 50 | + }, |
| 51 | + { |
| 52 | + Tag: "t4 - Update a trip", |
| 53 | + Method: "PATCH", |
| 54 | + URL: "/api/trips/{tripID}", |
| 55 | + Body: `{ |
| 56 | + "Name":"Trip UPDATE TEST", |
| 57 | + "UserId":"GO_TEST", |
| 58 | + "RecordedTimeStamp": "2018-04-19T19:08:16.03Z", |
| 59 | + "EndTimeStamp": "2018-04-19T19:42:49.573Z", |
| 60 | + "Rating":91005, |
| 61 | + "IsComplete":true, |
| 62 | + "HasSimulatedOBDData":true, |
| 63 | + "AverageSpeed":100, |
| 64 | + "FuelUsed":10.27193484, |
| 65 | + "HardStops":2, |
| 66 | + "HardAccelerations":4, |
| 67 | + "Distance":30.0275486, |
| 68 | + "CreatedAt":"2018-01-01T12:00:00Z", |
| 69 | + "UpdatedAt":"2001-01-01T12:00:00Z" |
| 70 | + }`, |
| 71 | + Status: 200, |
| 72 | + }, |
| 73 | + { |
| 74 | + Tag: "t5 - Create Trip Point", |
| 75 | + Method: "POST", |
| 76 | + URL: "/api/trips/{tripID}/trippoints", |
| 77 | + Body: `{ |
| 78 | + "TripId": "{tripID}", |
| 79 | + "Latitude": 47.67598, |
| 80 | + "Longitude": -122.10612, |
| 81 | + "Speed": -255, |
| 82 | + "RecordedTimeStamp": "2018-05-24T10:00:15.003Z", |
| 83 | + "Sequence": 2, |
| 84 | + "RPM": -255, |
| 85 | + "ShortTermFuelBank": -255, |
| 86 | + "LongTermFuelBank": -255, |
| 87 | + "ThrottlePosition": -255, |
| 88 | + "RelativeThrottlePosition": -255, |
| 89 | + "Runtime": -255, |
| 90 | + "DistanceWithMalfunctionLight": -255, |
| 91 | + "EngineLoad": -255, |
| 92 | + "EngineFuelRate": -255, |
| 93 | + "CreatedAt": "0001-01-01T00:00:00Z", |
| 94 | + "UpdatedAt": "0001-01-01T00:00:00Z" |
| 95 | + }`, |
| 96 | + Status: 200, |
| 97 | + }, |
| 98 | + { |
| 99 | + Tag: "t6 - Update Trip Point", |
| 100 | + Method: "PATCH", |
| 101 | + URL: "/api/trips/{tripID}/trippoints/{tripPointID}", |
| 102 | + Body: `{ |
| 103 | + "Id": "{tripPointID}", |
| 104 | + "TripId": "{tripID}", |
| 105 | + "Latitude": 47.67598, |
| 106 | + "Longitude": -122.10612, |
| 107 | + "Speed": -255, |
| 108 | + "RecordedTimeStamp": "2018-05-24T10:00:15.003Z", |
| 109 | + "Sequence": 2, |
| 110 | + "RPM": -255, |
| 111 | + "ShortTermFuelBank": -255, |
| 112 | + "LongTermFuelBank": -255, |
| 113 | + "ThrottlePosition": -255, |
| 114 | + "RelativeThrottlePosition": -255, |
| 115 | + "Runtime": -255, |
| 116 | + "DistanceWithMalfunctionLight": -255, |
| 117 | + "EngineLoad": -255, |
| 118 | + "EngineFuelRate": -255, |
| 119 | + "Created": "0001-01-01T00:00:00Z", |
| 120 | + "UpdatedAt": "0001-01-01T00:00:00Z" |
| 121 | + }`, |
| 122 | + ExpectedResponse: "", |
| 123 | + Status: 200, |
| 124 | + }, |
| 125 | + { |
| 126 | + Tag: "t7 - Read Trip Points for Trip", |
| 127 | + Method: "GET", |
| 128 | + URL: "/api/trips/{tripID}/trippoints", |
| 129 | + Status: 200, |
| 130 | + }, |
| 131 | + { |
| 132 | + Tag: "t8 - Read Trip Points By Trip Point ID", |
| 133 | + Method: "GET", |
| 134 | + URL: "/api/trips/{tripID}/trippoints/{tripPointID}", |
| 135 | + Status: 200, |
| 136 | + }, |
| 137 | + { |
| 138 | + Tag: "t9 - Delete Trip Point", |
| 139 | + Method: "DELETE", |
| 140 | + URL: "/api/trips/{tripID}/trippoints/{tripPointID}", |
| 141 | + Status: 200, |
| 142 | + }, |
| 143 | + { |
| 144 | + Tag: "t10 - Delete a Trip", |
| 145 | + Method: "DELETE", |
| 146 | + URL: "/api/trips/{tripID}", |
| 147 | + Status: 200, |
| 148 | + }, |
| 149 | +} |
| 150 | + |
| 151 | +func TestTrip(t *testing.T) { |
| 152 | + router := tripSvc.NewRouter() |
| 153 | + var debug, present = os.LookupEnv("DEBUG_LOGGING") |
| 154 | + |
| 155 | + if present && debug == "true" { |
| 156 | + tripSvc.InitLogging(os.Stdout, os.Stdout, os.Stdout) |
| 157 | + } else { |
| 158 | + // if debug env is not present or false, do not log debug output to console |
| 159 | + tripSvc.InitLogging(os.Stdout, ioutil.Discard, os.Stdout) |
| 160 | + } |
| 161 | + tripSvc.RunAPITests(t, router, apiTestList[0:4]) |
| 162 | + |
| 163 | + // setup update trip test (URL, Body, expected Response) |
| 164 | + apiTestList[4].URL = strings.Replace(apiTestList[4].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1) |
| 165 | + apiTestList[4].Body = GetUpdateTrip(apiTestList[3].ActualResponse, apiTestList[4].Body) |
| 166 | + apiTestList[4].ExpectedResponse = apiTestList[4].Body |
| 167 | + |
| 168 | + // setup create trip point test |
| 169 | + apiTestList[5].URL = strings.Replace(apiTestList[5].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1) |
| 170 | + apiTestList[5].Body = strings.Replace(apiTestList[5].Body, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1) |
| 171 | + |
| 172 | + // run update trip and create trip point tests |
| 173 | + tripSvc.RunAPITests(t, router, apiTestList[4:6]) |
| 174 | + |
| 175 | + // setup update trip point test |
| 176 | + apiTestList[6].URL = strings.Replace(apiTestList[6].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1) |
| 177 | + apiTestList[6].URL = strings.Replace(apiTestList[6].URL, "{tripPointID}", TripPointFromStr(apiTestList[5].ActualResponse).ID, 1) |
| 178 | + apiTestList[6].Body = GetUpdateTripPoint(apiTestList[5].ActualResponse, apiTestList[6].Body) |
| 179 | + //apiTestList[6].ExpectedResponse = apiTestList[6].Body |
| 180 | + |
| 181 | + // setup read trip points for trip test |
| 182 | + apiTestList[7].URL = strings.Replace(apiTestList[7].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1) |
| 183 | + |
| 184 | + // setup ready trip points by trip point id test |
| 185 | + apiTestList[8].URL = strings.Replace(apiTestList[8].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1) |
| 186 | + apiTestList[8].URL = strings.Replace(apiTestList[8].URL, "{tripPointID}", TripPointFromStr(apiTestList[5].ActualResponse).ID, 1) |
| 187 | + |
| 188 | + //setup delete trip point test |
| 189 | + apiTestList[9].URL = strings.Replace(apiTestList[9].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1) |
| 190 | + apiTestList[9].URL = strings.Replace(apiTestList[9].URL, "{tripPointID}", TripPointFromStr(apiTestList[5].ActualResponse).ID, 1) |
| 191 | + |
| 192 | + // setup delete test (URL) |
| 193 | + apiTestList[10].URL = strings.Replace(apiTestList[10].URL, "{tripID}", TripFromStr(apiTestList[3].ActualResponse).ID, 1) |
| 194 | + // run update test |
| 195 | + tripSvc.RunAPITests(t, router, apiTestList[6:10]) |
| 196 | +} |
| 197 | + |
| 198 | +func GetUpdateTrip(tripCreate string, tripUpdate string) string { |
| 199 | + tripC := TripFromStr(tripCreate) |
| 200 | + tripU := TripFromStr(tripUpdate) |
| 201 | + |
| 202 | + tripU.ID = tripC.ID |
| 203 | + |
| 204 | + serializedTripUpdate, _ := json.Marshal(tripU) |
| 205 | + |
| 206 | + return string(serializedTripUpdate) |
| 207 | +} |
| 208 | + |
| 209 | +func GetUpdateTripPoint(tripPointCreate string, tripPointUpdate string) string { |
| 210 | + tripPointC := TripPointFromStr(tripPointCreate) |
| 211 | + tripPointU := TripPointFromStr(tripPointUpdate) |
| 212 | + |
| 213 | + tripPointU.ID = tripPointC.ID |
| 214 | + tripPointU.TripID = tripPointC.TripID |
| 215 | + |
| 216 | + serializedTripUpdate, _ := json.Marshal(tripPointU) |
| 217 | + |
| 218 | + return string(serializedTripUpdate) |
| 219 | +} |
| 220 | + |
| 221 | +func TripFromStr(tripStr string) tripSvc.Trip { |
| 222 | + trip := tripSvc.Trip{} |
| 223 | + |
| 224 | + tripSvc.Debug.Println(tripStr) |
| 225 | + |
| 226 | + errCreate := json.Unmarshal([]byte(tripStr), &trip) |
| 227 | + if errCreate != nil { |
| 228 | + log.Println("TripFromStr - Invalid trip string") |
| 229 | + log.Fatal(errCreate) |
| 230 | + } |
| 231 | + |
| 232 | + return trip |
| 233 | +} |
| 234 | + |
| 235 | +func TripPointFromStr(tripPointStr string) tripSvc.TripPoint { |
| 236 | + tripPoint := tripSvc.TripPoint{} |
| 237 | + |
| 238 | + errCreate := json.Unmarshal([]byte(tripPointStr), &tripPoint) |
| 239 | + if errCreate != nil { |
| 240 | + log.Println("TripPointFromStr - Invalid trip point string") |
| 241 | + log.Fatal(errCreate) |
| 242 | + } |
| 243 | + |
| 244 | + tripSvc.Debug.Println(tripPointStr) |
| 245 | + |
| 246 | + return tripPoint |
| 247 | +} |
0 commit comments