You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
11 KiB

  1. package export;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import org.json.simple.JSONArray;
  9. import org.json.simple.JSONObject;
  10. import org.junit.Assert;
  11. import org.junit.Test;
  12. public class MainTest
  13. {
  14. //Test data from:
  15. //https://developers.strava.com/playground/#/
  16. //https://developers.strava.com/docs/reference/
  17. @Test
  18. public void testAddStreams() throws NoAccessException
  19. {
  20. String requestDistance = "[ { \"type\" : \"distance\", "
  21. + "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
  22. + "\"series_type\" : \"distance\", \"original_size\" : 12, \"resolution\" : \"high\"}]";
  23. //Time data is the same as distance only with different type and series_type
  24. String requestDistanceAndTime = "[{\"type\" : \"distance\", "
  25. + "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
  26. + "\"series_type\" : \"distance\", \"original_size\" : 12, \"resolution\" : \"high\"},"
  27. + "{\"type\" : \"time\", "
  28. + "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
  29. + "\"series_type\" : \"time\", \"original_size\" : 12, \"resolution\" : \"high\"}]";
  30. Main.setTest(requestDistance);
  31. JSONObject res = Main.addStreams("id", new JSONObject());
  32. Assert.assertNull(res.get("stream_time"));
  33. Assert.assertNotNull(res.get("stream_distance"));
  34. Assert.assertNull(res.get("stream_altitude"));
  35. Main.setTest(requestDistanceAndTime);
  36. res = Main.addStreams("id", new JSONObject());
  37. Assert.assertNotNull(res.get("stream_time"));
  38. Assert.assertNotNull(res.get("stream_distance"));
  39. Assert.assertNull(res.get("stream_altitude"));
  40. }
  41. @Test
  42. public void testGetActivities() throws NoAccessException
  43. {
  44. String request_full = "[{\"resource_state\": 2, "
  45. + "\"athlete\": {\"id\": 134815, \"resource_state\": 1}, "
  46. + "\"name\": \"Happy Friday\", \"distance\": 24931.4, \"moving_time\": 4500, "
  47. + "\"elapsed_time\": 4500, \"total_elevation_gain\": 0, \"type\": \"Ride\", "
  48. + "\"workout_type\": null, \"id\": 154504250376823, "
  49. + "\"external_id\": \"garmin_push_12345678987654321\", "
  50. + "\"upload_id\": 987654321234567900000, \"start_date\": \"2018-05-02T12:15:09Z\", "
  51. + "\"start_date_local\": \"2018-05-02T05:15:09Z\", "
  52. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\", \"utc_offset\": -25200, "
  53. + "\"start_latlng\": null, \"end_latlng\": null, \"location_city\": null, "
  54. + "\"location_state\": null, \"location_country\": \"United States\", "
  55. + "\"achievement_count\": 0, \"kudos_count\": 3, \"comment_count\": 1, "
  56. + "\"athlete_count\": 1, \"photo_count\": 0, "
  57. + "\"map\": {\"id\": \"a12345678987654321\", \"summary_polyline\": null, \"resource_state\": 2 },"
  58. + "\"trainer\": true, \"commute\": false, \"manual\": false, \"private\": false, "
  59. + "\"flagged\": false, \"gear_id\": \"b12345678987654321\", \"from_accepted_tag\": false, "
  60. + "\"average_speed\": 5.54, \"max_speed\": 11, \"average_cadence\": 67.1, "
  61. + "\"average_watts\": 175.3, \"weighted_average_watts\": 210, \"kilojoules\": 788.7, "
  62. + "\"device_watts\": true, \"has_heartrate\": true, \"average_heartrate\": 140.3, "
  63. + "\"max_heartrate\": 178, \"max_watts\": 406, \"pr_count\": 0, \"total_photo_count\": 1,"
  64. + "\"has_kudoed\": false,\"suffer_score\": 82},"
  65. + "{\"resource_state\": 2, \"athlete\": {\"id\": 167560, \"resource_state\": 1},"
  66. + "\"name\": \"Bondcliff\", \"distance\": 23676.5, \"moving_time\": 5400,"
  67. + "\"elapsed_time\": 5400, \"total_elevation_gain\": 0, \"type\": \"Ride\", "
  68. + "\"workout_type\": null, \"id\": 1234567809, "
  69. + "\"external_id\": \"garmin_push_12345678987654321\", \"upload_id\": 1234567819,"
  70. + "\"start_date\": \"2018-04-30T12:35:51Z\", \"start_date_local\": \"2018-04-30T05:35:51Z\","
  71. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\", \"utc_offset\": -25200, "
  72. + "\"start_latlng\": null, \"end_latlng\": null, \"location_city\": null, \"location_state\": null,"
  73. + "\"location_country\": \"United States\", \"achievement_count\": 0, \"kudos_count\": 4, "
  74. + "\"comment_count\": 0, \"athlete_count\": 1, \"photo_count\": 0, "
  75. + "\"map\": {\"id\": \"a12345689\", \"summary_polyline\": null, \"resource_state\": 2},"
  76. + "\"trainer\": true, \"commute\": false, \"manual\": false, \"private\": false, "
  77. + "\"flagged\": false, \"gear_id\": \"b12345678912343\", \"from_accepted_tag\": false,"
  78. + "\"average_speed\": 4.385, \"max_speed\": 8.8, \"average_cadence\": 69.8, "
  79. + "\"average_watts\": 200, \"weighted_average_watts\": 214, \"kilojoules\": 1080, "
  80. + "\"device_watts\": true, \"has_heartrate\": true, \"average_heartrate\": 152.4,"
  81. + "\"max_heartrate\": 183, \"max_watts\": 403, \"pr_count\": 0, \"total_photo_count\": 1, "
  82. + "\"has_kudoed\": false, \"suffer_score\": 162}]";
  83. String request_shortened = "[{\"type\": \"Ride\", \"id\": 154504250376823, "
  84. + "\"start_date_local\": \"2018-05-02T05:15:09Z\", "
  85. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"},"
  86. + "{\"type\": \"Ride\", \"id\": 1234567809, "
  87. + "\"start_date_local\": \"2018-04-30T05:35:51Z\","
  88. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"}]";
  89. String request_shortened_one = "[{\"type\": \"Ride\", \"id\": 1234567809, "
  90. + "\"start_date_local\": \"2018-04-30T05:35:51Z\","
  91. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"}]";
  92. String request_missing_type = "[{\"id\": 1234567809, "
  93. + "\"start_date_local\": \"2018-04-30T05:35:51Z\","
  94. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"}]";
  95. String request_missing_id = "[{\"type\": \"Ride\", "
  96. + "\"start_date_local\": \"2018-04-30T05:35:51Z\","
  97. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"}]";
  98. String request_missing_start = "[{\"type\": \"Ride\", \"id\": 1234567809, "
  99. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"}]";
  100. String request_missing_timezone = "[{\"type\": \"Ride\", \"id\": 1234567809, "
  101. + "\"start_date_local\": \"2018-04-30T05:35:51Z\"}]";
  102. String[] requestsOneActivity = {request_shortened_one, request_missing_start,
  103. request_missing_timezone, request_missing_type};
  104. String[] requestsTwoActivities = {request_full, request_shortened};
  105. for (String request: requestsTwoActivities)
  106. {
  107. Main.setTest(request);
  108. Map<String, JSONObject> res = Main.getActivities();
  109. Assert.assertEquals(res.size(), 2);
  110. }
  111. for (String request: requestsOneActivity)
  112. {
  113. Main.setTest(request);
  114. Map<String, JSONObject> res = Main.getActivities();
  115. Assert.assertEquals(res.size(), 1);
  116. }
  117. Main.setTest(request_missing_id);
  118. Map<String, JSONObject> res = Main.getActivities();
  119. Assert.assertEquals(res.size(), 0);
  120. }
  121. @Test
  122. public void testSaveGeneralInformation() throws NoAccessException
  123. {
  124. String request_full ="{ \"id\" : 1234567890987654321, \"username\" : \"marianne_t\", \"resource_state\" : 3,"
  125. + "\"firstname\" : \"Marianne\", \"lastname\" : \"Teutenberg\", \"city\" : \"San Francisco\", "
  126. + "\"state\" : \"CA\", \"country\" : \"US\", \"sex\" : \"F\", \"premium\" : true, "
  127. + "\"created_at\" : \"2017-11-14T02:30:05Z\", \"updated_at\" : \"2018-02-06T19:32:20Z\", "
  128. + "\"badge_type_id\" : 4, "
  129. + "\"profile_medium\" : \"https://xxxxxx.cloudfront.net/pictures/athletes/123456789/123456789/2/medium.jpg\", "
  130. + "\"profile\" : \"https://xxxxx.cloudfront.net/pictures/athletes/123456789/123456789/2/large.jpg\", "
  131. + "\"friend\" : null, \"follower\" : null, \"follower_count\" : 5, \"friend_count\" : 5, "
  132. + "\"mutual_friend_count\" : 0, \"athlete_type\" : 1, \"date_preference\" : \"%m/%d/%Y\", "
  133. + "\"measurement_preference\" : \"feet\", \"clubs\" : [ ], \"ftp\" : null, \"weight\" : 0, "
  134. + "\"bikes\" : [ { \"id\" : \"b12345678987655\","
  135. + "\"primary\" : true, \"name\" : \"EMC\", \"resource_state\" : 2, "
  136. + "\"distance\" : 0 } ], "
  137. + "\"shoes\" : [ { \"id\" : \"g12345678987655\", \"primary\" : true, \"name\" : \"adidas\", "
  138. + "\"resource_state\" : 2, \"distance\" : 4904 } ]}";
  139. String request_shortened ="{\"country\" : \"US\", \"sex\" : \"F\", \"date_preference\" : \"%m/%d/%Y\", "
  140. + "\"measurement_preference\" : \"feet\", \"weight\" : 0}";
  141. String request_missing_country ="{\"sex\" : \"F\", \"date_preference\" : \"%m/%d/%Y\", "
  142. + "\"measurement_preference\" : \"feet\", \"weight\" : 0}";
  143. String request_missing_gender ="{\"country\" : \"US\", \"date_preference\" : \"%m/%d/%Y\", "
  144. + "\"measurement_preference\" : \"feet\", \"weight\" : 0}";
  145. String request_missing_date_pref ="{\"country\" : \"US\", \"sex\" : \"F\", "
  146. + "\"measurement_preference\" : \"feet\", \"weight\" : 0}";
  147. String request_missing_meas_pref ="{\"country\" : \"US\", \"sex\" : \"F\", "
  148. + " \"date_preference\" : \"%m/%d/%Y\", \"weight\" : 0}";
  149. String request_missing_weight ="{\"country\" : \"US\", \"sex\" : \"F\", "
  150. + " \"date_preference\" : \"%m/%d/%Y\", \"measurement_preference\" : \"feet\"}";
  151. String request_bad_format ="{\"c: \"%m/%d/%Y\", \"measurement_preference\" : \"feet\"}";
  152. String[] allRequests = {request_full, request_shortened, request_missing_country,
  153. request_missing_date_pref, request_missing_gender, request_missing_meas_pref,
  154. request_missing_weight};
  155. for (String request: allRequests)
  156. {
  157. Main.setTest(request);
  158. JSONObject o = Main.saveGeneralInformation();
  159. Assert.assertNotNull(o);
  160. }
  161. Main.setTest(request_bad_format);
  162. JSONObject o = Main.saveGeneralInformation();
  163. Assert.assertNull(o);
  164. }
  165. @SuppressWarnings("unchecked")
  166. @Test
  167. public void testStructure() throws NoAccessException
  168. {
  169. String request_shortened = "[{\"type\": \"Ride\", \"id\": 154504250376823, "
  170. + "\"start_date_local\": \"2018-05-02T05:15:09Z\", "
  171. + "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"}]";
  172. Main.setTest(request_shortened);
  173. Map<String, JSONObject> res = Main.getActivities();
  174. String requestDistanceAndTime = "[{\"type\" : \"distance\", "
  175. + "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
  176. + "\"series_type\" : \"distance\", \"original_size\" : 12, \"resolution\" : \"high\"},"
  177. + "{\"type\" : \"time\", "
  178. + "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
  179. + "\"series_type\" : \"time\", \"original_size\" : 12, \"resolution\" : \"high\"}]";
  180. JSONArray allActivities = new JSONArray();
  181. for(String activity : res.keySet())
  182. {
  183. Main.setTest(requestDistanceAndTime);
  184. JSONObject toSave = Main.addStreams("id", res.get(activity));
  185. allActivities.add(toSave);
  186. }
  187. Assert.assertEquals(allActivities.size(), res.keySet().size());
  188. String request_missing_country ="{\"sex\" : \"F\", \"date_preference\" : \"%m/%d/%Y\", "
  189. + "\"measurement_preference\" : \"feet\", \"weight\" : 0}";
  190. Main.setTest(request_missing_country);
  191. JSONObject a = Main.saveGeneralInformation();
  192. a.put("activities", allActivities);
  193. try
  194. {
  195. File temp = File.createTempFile("Athlete_0" , ".json");
  196. temp.deleteOnExit();
  197. BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
  198. bw.write(a.toString());
  199. bw.close();
  200. Map<String, File> map = new HashMap<>();
  201. map.put("athlete_0.json", temp);
  202. Main.zipFiles(map,0);
  203. }
  204. catch (IOException e)
  205. {
  206. Assert.fail();
  207. }
  208. }
  209. }