Included reidentification on expired access token
This commit is contained in:
+106
-60
@@ -49,6 +49,11 @@ public class Main
|
|||||||
private static int dailyRequestCount = 0;
|
private static int dailyRequestCount = 0;
|
||||||
private static int waitTimeMil = 60000 * 3 * 15 / requestLimit15Minutes;
|
private static int waitTimeMil = 60000 * 3 * 15 / requestLimit15Minutes;
|
||||||
private static long lastRequestTimeInMillis = 0;
|
private static long lastRequestTimeInMillis = 0;
|
||||||
|
private static String accessToken = "";
|
||||||
|
/**
|
||||||
|
* a is client_id, b is client_secret, c is refresh_token
|
||||||
|
*/
|
||||||
|
private static Triplet refreshInfo;
|
||||||
|
|
||||||
private static void writeError(String text)
|
private static void writeError(String text)
|
||||||
{
|
{
|
||||||
@@ -80,32 +85,50 @@ public class Main
|
|||||||
/**
|
/**
|
||||||
* Saves the data of one athlete into a temp file and returns it.
|
* Saves the data of one athlete into a temp file and returns it.
|
||||||
*
|
*
|
||||||
* @param token Identifier / authorization token of the athlete
|
|
||||||
* @return created temp file
|
* @return created temp file
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
static File oneAthlete(String token)
|
static File oneAthlete()
|
||||||
{
|
{
|
||||||
// get Activities
|
// get Activities and general information
|
||||||
Map<String, JSONObject> activities = getActivities(token);
|
Map<String, JSONObject> activities;
|
||||||
|
JSONObject athleteInfo;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
activities = getActivities();
|
||||||
|
athleteInfo = saveGeneralInformation();
|
||||||
|
}
|
||||||
|
catch (NoAccessException e1)
|
||||||
|
{
|
||||||
|
writeError("Athlete " + athleteId + ": Access expired and no new token possible");
|
||||||
|
return null; // no data at all. Stop right away
|
||||||
|
}
|
||||||
|
if (athleteInfo == null) // error getting General Information
|
||||||
|
{
|
||||||
|
athleteInfo = new JSONObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// for each activity: save streams
|
// for each activity: save streams
|
||||||
JSONArray allActivities = new JSONArray();
|
JSONArray allActivities = new JSONArray();
|
||||||
int simpleActivityId = 0;
|
int simpleActivityId = 0;
|
||||||
for (String id : activities.keySet())
|
for (String id : activities.keySet())
|
||||||
{
|
{
|
||||||
JSONObject data = addStreams(id, activities.get(id), token);
|
JSONObject data;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
data = addStreams(id, activities.get(id));
|
||||||
|
}
|
||||||
|
catch (NoAccessException e)
|
||||||
|
{
|
||||||
|
writeError("Athlete " + athleteId + ": Access expired and no new token possible");
|
||||||
|
break; //stop the loop and save what you got up to there
|
||||||
|
}
|
||||||
data.put("activity_id", simpleActivityId);
|
data.put("activity_id", simpleActivityId);
|
||||||
allActivities.add(data);
|
allActivities.add(data);
|
||||||
simpleActivityId++;
|
simpleActivityId++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get general information
|
|
||||||
JSONObject athleteInfo = saveGeneralInformation(token);
|
|
||||||
if(athleteInfo==null) //error getting General Information
|
|
||||||
{
|
|
||||||
athleteInfo = new JSONObject();
|
|
||||||
}
|
|
||||||
athleteInfo.put("activities", allActivities);
|
athleteInfo.put("activities", allActivities);
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -129,17 +152,17 @@ public class Main
|
|||||||
*
|
*
|
||||||
* @param id Strava id of the activity
|
* @param id Strava id of the activity
|
||||||
* @param data general information of the activity
|
* @param data general information of the activity
|
||||||
* @param token Identifier / authorization token of the athlete
|
|
||||||
* @return The data with the added streams
|
* @return The data with the added streams
|
||||||
|
* @throws NoAccessException If the access token expired and no new one could be acquired.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
static JSONObject addStreams(String id, JSONObject data, String token)
|
static JSONObject addStreams(String id, JSONObject data) throws NoAccessException
|
||||||
{
|
{
|
||||||
String requestUrlExtension = "activities/" + id + "/streams?"
|
String requestUrlExtension = "activities/" + id + "/streams?"
|
||||||
+ "keys=[time,distance,latlng,altitude,velocity_smooth,heartrate,"
|
+ "keys=[time,distance,latlng,altitude,velocity_smooth,heartrate,"
|
||||||
+ "cadence,watts,temp,moving,grade_smooth]&key_by_type=true";
|
+ "cadence,watts,temp,moving,grade_smooth]&key_by_type=true";
|
||||||
|
|
||||||
String json = makeGetRequestWithRetry(requestUrlExtension, token);;
|
String json = makeGetRequestWithRetry(requestUrlExtension);
|
||||||
|
|
||||||
if (json == null || json.isEmpty() || json.isBlank() || json.equals(""))
|
if (json == null || json.isEmpty() || json.isBlank() || json.equals(""))
|
||||||
{
|
{
|
||||||
@@ -167,12 +190,12 @@ public class Main
|
|||||||
* Gathers all activities of a user, extracts the general information and the
|
* Gathers all activities of a user, extracts the general information and the
|
||||||
* ids.
|
* ids.
|
||||||
*
|
*
|
||||||
* @param token Identifier / authorization token of the athlete
|
|
||||||
* @return A Map with key = Strava Id of an activity and value = JSONObject with
|
* @return A Map with key = Strava Id of an activity and value = JSONObject with
|
||||||
* the general information of the activity
|
* the general information of the activity
|
||||||
|
* @throws NoAccessException If the access token expired and no new one could be acquired.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
static Map<String, JSONObject> getActivities(String token)
|
static Map<String, JSONObject> getActivities() throws NoAccessException
|
||||||
{
|
{
|
||||||
Map<String, JSONObject> result = new HashMap<>();
|
Map<String, JSONObject> result = new HashMap<>();
|
||||||
/*
|
/*
|
||||||
@@ -191,10 +214,12 @@ public class Main
|
|||||||
int pageIndex = 1;
|
int pageIndex = 1;
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
//TODO: this 'per_page' value could perhaps be the root of all evil
|
|
||||||
String requestExtension = "athlete/activities?per_page=100&page=" + pageIndex;
|
String requestExtension = "athlete/activities?per_page=100&page=" + pageIndex;
|
||||||
String json = makeGetRequestWithRetry(requestExtension, token);
|
String json = makeGetRequestWithRetry(requestExtension);
|
||||||
if (json==null || json.isEmpty() || json.isBlank() || json.equals("") || json.equals("[]")) // don't know where the last page is...
|
if (json == null || json.isEmpty() || json.isBlank() || json.equals("") || json.equals("[]")) // don't know
|
||||||
|
// where the
|
||||||
|
// last page
|
||||||
|
// is...
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -231,7 +256,8 @@ public class Main
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In the case that the daily request limit has been reached, this method waits for the next day
|
* In the case that the daily request limit has been reached, this method waits
|
||||||
|
* for the next day
|
||||||
*/
|
*/
|
||||||
static void checkRequestLimit()
|
static void checkRequestLimit()
|
||||||
{
|
{
|
||||||
@@ -257,13 +283,15 @@ public class Main
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method is used to find the next request window: It tries the same request again after 5 minutes.
|
* Method is used to find the next request window: It tries the same request
|
||||||
* After a set number of times (retryTimes) it stops if it still wasn't successful.
|
* again after 5 minutes. After a set number of times (retryTimes) it stops if
|
||||||
|
* it still wasn't successful.
|
||||||
|
*
|
||||||
* @param urlExtension UrlExtension for the request
|
* @param urlExtension UrlExtension for the request
|
||||||
* @param token Token for the request
|
|
||||||
* @return Data as a String or {@code null} if there is no data
|
* @return Data as a String or {@code null} if there is no data
|
||||||
|
* @throws NoAccessException If the access token expired and no new one could be acquired.
|
||||||
*/
|
*/
|
||||||
static String makeGetRequestWithRetry(String urlExtension, String token)
|
static String makeGetRequestWithRetry(String urlExtension) throws NoAccessException
|
||||||
{
|
{
|
||||||
checkRequestLimit();
|
checkRequestLimit();
|
||||||
String json = null;
|
String json = null;
|
||||||
@@ -272,7 +300,7 @@ public class Main
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
json = makeOneGetRequest(urlExtension, token);
|
json = makeOneGetRequest(urlExtension);
|
||||||
dailyRequestCount++;
|
dailyRequestCount++;
|
||||||
}
|
}
|
||||||
catch (ResponseCodeWrongException e)
|
catch (ResponseCodeWrongException e)
|
||||||
@@ -280,12 +308,19 @@ public class Main
|
|||||||
// tried enough times, so stop now
|
// tried enough times, so stop now
|
||||||
if (count >= retryTimes)
|
if (count >= retryTimes)
|
||||||
{
|
{
|
||||||
writeError("Athlete: "+athleteId+" Retry limit reached. Last error code: "+e.getResponseCode());
|
writeError(
|
||||||
|
"Athlete: " + athleteId + " Retry limit reached. Last error code: " + e.getResponseCode());
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
//request limit is reached, try again later
|
if (e.getResponseCode()==HttpURLConnection.HTTP_UNAUTHORIZED)
|
||||||
if (e.getResponseCode()==httpCodeLimitReached)
|
{ //token might have expired
|
||||||
|
if(!getAccessToken()) //token doesn't work anymore and we can't get a new one
|
||||||
{
|
{
|
||||||
|
throw new NoAccessException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (e.getResponseCode() == httpCodeLimitReached)
|
||||||
|
{// request limit is reached, try again later
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
else // some other error: try only one other time!
|
else // some other error: try only one other time!
|
||||||
@@ -309,11 +344,11 @@ public class Main
|
|||||||
/**
|
/**
|
||||||
* Extracts an athletes general information.
|
* Extracts an athletes general information.
|
||||||
*
|
*
|
||||||
* @param token Identifier / authorization token of the athlete
|
|
||||||
* @return extracted data or null if there was an error
|
* @return extracted data or null if there was an error
|
||||||
|
* @throws NoAccessException If the access token expired and no new one could be acquired.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
static JSONObject saveGeneralInformation(String token)
|
static JSONObject saveGeneralInformation() throws NoAccessException
|
||||||
{
|
{
|
||||||
String sex = "sex"; // Possible values = M, F
|
String sex = "sex"; // Possible values = M, F
|
||||||
String country = "country";
|
String country = "country";
|
||||||
@@ -321,8 +356,7 @@ public class Main
|
|||||||
String meas_pref = "measurement_preference"; // Possible values = feet, meters
|
String meas_pref = "measurement_preference"; // Possible values = feet, meters
|
||||||
String weight = "weight";
|
String weight = "weight";
|
||||||
|
|
||||||
|
String json = makeGetRequestWithRetry("athlete");
|
||||||
String json = makeGetRequestWithRetry("athlete", token);
|
|
||||||
|
|
||||||
JSONObject toSave = new JSONObject();
|
JSONObject toSave = new JSONObject();
|
||||||
try
|
try
|
||||||
@@ -384,12 +418,10 @@ public class Main
|
|||||||
* Handles one GET request to the API
|
* Handles one GET request to the API
|
||||||
*
|
*
|
||||||
* @param requestUrlExtension Extension for the baseUrl (without '/')
|
* @param requestUrlExtension Extension for the baseUrl (without '/')
|
||||||
* @param token Identification / authorization token of the
|
|
||||||
* athlete
|
|
||||||
* @return The response as a String, an empty String in case of error.
|
* @return The response as a String, an empty String in case of error.
|
||||||
* @throws ResponseCodeWrongException If there was an http error
|
* @throws ResponseCodeWrongException If there was an http error
|
||||||
*/
|
*/
|
||||||
static String makeOneGetRequest(String requestUrlExtension, String token) throws ResponseCodeWrongException
|
static String makeOneGetRequest(String requestUrlExtension) throws ResponseCodeWrongException
|
||||||
{
|
{
|
||||||
if (testRequest != null)
|
if (testRequest != null)
|
||||||
{
|
{
|
||||||
@@ -410,13 +442,14 @@ public class Main
|
|||||||
catch (InterruptedException e)
|
catch (InterruptedException e)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
;
|
||||||
lastRequestTimeInMillis = System.currentTimeMillis();
|
lastRequestTimeInMillis = System.currentTimeMillis();
|
||||||
// Create connection
|
// Create connection
|
||||||
URL url = new URL(baseUrl + requestUrlExtension);
|
URL url = new URL(baseUrl + requestUrlExtension);
|
||||||
connection = (HttpsURLConnection) url.openConnection();
|
connection = (HttpsURLConnection) url.openConnection();
|
||||||
connection.setRequestMethod("GET");
|
connection.setRequestMethod("GET");
|
||||||
connection.setRequestProperty("Authorization", "Bearer " + token);
|
connection.setRequestProperty("Authorization", "Bearer " + accessToken);
|
||||||
|
|
||||||
return getResponse(connection);
|
return getResponse(connection);
|
||||||
}
|
}
|
||||||
@@ -433,7 +466,8 @@ public class Main
|
|||||||
* @param connection Connection to the site
|
* @param connection Connection to the site
|
||||||
* @return Response as a String
|
* @return Response as a String
|
||||||
* @throws IOException in case of error with the stream
|
* @throws IOException in case of error with the stream
|
||||||
* @throws ResponseCodeWrongException if no data was read because of http problems
|
* @throws ResponseCodeWrongException if no data was read because of http
|
||||||
|
* problems
|
||||||
*/
|
*/
|
||||||
static String getResponse(HttpsURLConnection connection) throws IOException, ResponseCodeWrongException
|
static String getResponse(HttpsURLConnection connection) throws IOException, ResponseCodeWrongException
|
||||||
{
|
{
|
||||||
@@ -467,12 +501,10 @@ public class Main
|
|||||||
/**
|
/**
|
||||||
* Used for generating the accessToken
|
* Used for generating the accessToken
|
||||||
*
|
*
|
||||||
* @param data Triplet containing: a the client_id, b the client_secret, c the
|
|
||||||
* refresh_token
|
|
||||||
* @return The response as a String, an empty String in case of error.
|
* @return The response as a String, an empty String in case of error.
|
||||||
* @throws ResponseCodeWrongException If there was an http error
|
* @throws ResponseCodeWrongException If there was an http error
|
||||||
*/
|
*/
|
||||||
static String makeOnePostRequest(Triplet data) throws ResponseCodeWrongException
|
static String makeOnePostRequest() throws ResponseCodeWrongException
|
||||||
{
|
{
|
||||||
HttpsURLConnection connection = null;
|
HttpsURLConnection connection = null;
|
||||||
try
|
try
|
||||||
@@ -484,10 +516,10 @@ public class Main
|
|||||||
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
||||||
|
|
||||||
Map<String, String> params = new LinkedHashMap<>();
|
Map<String, String> params = new LinkedHashMap<>();
|
||||||
params.put("client_id", data.getA());
|
params.put("client_id", refreshInfo.getA());
|
||||||
params.put("client_secret", data.getB());
|
params.put("client_secret", refreshInfo.getB());
|
||||||
params.put("grant_type", "refresh_token");
|
params.put("grant_type", "refresh_token");
|
||||||
params.put("refresh_token", data.getC());
|
params.put("refresh_token", refreshInfo.getC());
|
||||||
|
|
||||||
StringBuilder postData = new StringBuilder();
|
StringBuilder postData = new StringBuilder();
|
||||||
for (String key : params.keySet())
|
for (String key : params.keySet())
|
||||||
@@ -516,11 +548,15 @@ public class Main
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends the old refresh token to strava and retrieves a new one and an access token
|
* Sends the old refresh token to strava and retrieves a new one and an access
|
||||||
* @param refreshInfo Refresh data with a the cliend_id, b the client_secret and c the refresh_token
|
* token
|
||||||
* @return A tuple with a the access_token and b the new refresh_token or {@code null} if there was an error
|
*
|
||||||
|
* @param refreshInfo Refresh data with a the cliend_id, b the client_secret and
|
||||||
|
* c the refresh_token
|
||||||
|
* @return {@code true} if everything went right or {@code false} if there was
|
||||||
|
* an error
|
||||||
*/
|
*/
|
||||||
static Tuple getAccessToken(Triplet refreshInfo)
|
static boolean getAccessToken()
|
||||||
{
|
{
|
||||||
checkRequestLimit();
|
checkRequestLimit();
|
||||||
String json = null;
|
String json = null;
|
||||||
@@ -529,7 +565,7 @@ public class Main
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
json = makeOnePostRequest(refreshInfo);
|
json = makeOnePostRequest();
|
||||||
dailyRequestCount++;
|
dailyRequestCount++;
|
||||||
}
|
}
|
||||||
catch (ResponseCodeWrongException e)
|
catch (ResponseCodeWrongException e)
|
||||||
@@ -537,11 +573,19 @@ public class Main
|
|||||||
// tried enough times, so stop now
|
// tried enough times, so stop now
|
||||||
if (count >= retryTimes)
|
if (count >= retryTimes)
|
||||||
{
|
{
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
//request limit is reached, try again later
|
|
||||||
if (e.getResponseCode()==httpCodeLimitReached)
|
if (e.getResponseCode()==HttpURLConnection.HTTP_UNAUTHORIZED)
|
||||||
|
{ //token might have expired
|
||||||
|
if(!getAccessToken()) //token doesn't work anymore and we can't get a new one
|
||||||
{
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (e.getResponseCode() == httpCodeLimitReached)
|
||||||
|
{
|
||||||
|
// request limit is reached, try again later
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
else // some other error: try only one other time!
|
else // some other error: try only one other time!
|
||||||
@@ -563,13 +607,15 @@ public class Main
|
|||||||
{
|
{
|
||||||
Object obj = parser.parse(json);
|
Object obj = parser.parse(json);
|
||||||
JSONObject data = (JSONObject) obj;
|
JSONObject data = (JSONObject) obj;
|
||||||
return new Tuple(data.get("access_token").toString(), data.get("refresh_token").toString());
|
accessToken = data.get("access_token").toString();
|
||||||
|
refreshInfo.setC(data.get("refresh_token").toString());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
catch (ParseException e)
|
catch (ParseException e)
|
||||||
{
|
{
|
||||||
writeError("Athlete " + athleteId + ": Error parsing general information.");
|
writeError("Athlete " + athleteId + ": Error parsing refresh info.");
|
||||||
}
|
}
|
||||||
return null;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
@@ -586,21 +632,21 @@ public class Main
|
|||||||
int zipcount = 1;
|
int zipcount = 1;
|
||||||
for (Triplet oneUser : refreshTokens)
|
for (Triplet oneUser : refreshTokens)
|
||||||
{
|
{
|
||||||
|
refreshInfo = oneUser;
|
||||||
athleteId++;
|
athleteId++;
|
||||||
// a is access_token and b is new refresh_token
|
if (!getAccessToken())
|
||||||
Tuple withAccessToken = getAccessToken(oneUser);
|
|
||||||
if (withAccessToken == null)
|
|
||||||
{
|
{
|
||||||
writeError("Couldn't get new access token for client " + athleteId);
|
writeError("Couldn't get new access token for client " + athleteId);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
newRefreshTokens.add(new Triplet(oneUser.getA(), oneUser.getB(), withAccessToken.getB()));
|
|
||||||
|
|
||||||
File athlete = oneAthlete(withAccessToken.getA());
|
File athlete = oneAthlete();
|
||||||
if (athlete != null)
|
if (athlete != null)
|
||||||
{
|
{
|
||||||
allFiles.put("Athlete_" + athleteId + ".json", athlete);
|
allFiles.put("Athlete_" + athleteId + ".json", athlete);
|
||||||
}
|
}
|
||||||
|
newRefreshTokens.add(refreshInfo);
|
||||||
|
|
||||||
// pack zip-files of 10 athletes
|
// pack zip-files of 10 athletes
|
||||||
if (allFiles.size() >= 10)
|
if (allFiles.size() >= 10)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class MainTest
|
|||||||
//https://developers.strava.com/docs/reference/
|
//https://developers.strava.com/docs/reference/
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAddStreams()
|
public void testAddStreams() throws NoAccessException
|
||||||
{
|
{
|
||||||
String requestDistance = "[ { \"type\" : \"distance\", "
|
String requestDistance = "[ { \"type\" : \"distance\", "
|
||||||
+ "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
|
+ "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
|
||||||
@@ -34,13 +34,13 @@ public class MainTest
|
|||||||
|
|
||||||
|
|
||||||
Main.setTest(requestDistance);
|
Main.setTest(requestDistance);
|
||||||
JSONObject res = Main.addStreams("id", new JSONObject(), "token");
|
JSONObject res = Main.addStreams("id", new JSONObject());
|
||||||
Assert.assertNull(res.get("stream_time"));
|
Assert.assertNull(res.get("stream_time"));
|
||||||
Assert.assertNotNull(res.get("stream_distance"));
|
Assert.assertNotNull(res.get("stream_distance"));
|
||||||
Assert.assertNull(res.get("stream_altitude"));
|
Assert.assertNull(res.get("stream_altitude"));
|
||||||
|
|
||||||
Main.setTest(requestDistanceAndTime);
|
Main.setTest(requestDistanceAndTime);
|
||||||
res = Main.addStreams("id", new JSONObject(), "token");
|
res = Main.addStreams("id", new JSONObject());
|
||||||
Assert.assertNotNull(res.get("stream_time"));
|
Assert.assertNotNull(res.get("stream_time"));
|
||||||
Assert.assertNotNull(res.get("stream_distance"));
|
Assert.assertNotNull(res.get("stream_distance"));
|
||||||
Assert.assertNull(res.get("stream_altitude"));
|
Assert.assertNull(res.get("stream_altitude"));
|
||||||
@@ -49,7 +49,7 @@ public class MainTest
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetActivities()
|
public void testGetActivities() throws NoAccessException
|
||||||
{
|
{
|
||||||
String request_full = "[{\"resource_state\": 2, "
|
String request_full = "[{\"resource_state\": 2, "
|
||||||
+ "\"athlete\": {\"id\": 134815, \"resource_state\": 1}, "
|
+ "\"athlete\": {\"id\": 134815, \"resource_state\": 1}, "
|
||||||
@@ -124,25 +124,25 @@ public class MainTest
|
|||||||
for (String request: requestsTwoActivities)
|
for (String request: requestsTwoActivities)
|
||||||
{
|
{
|
||||||
Main.setTest(request);
|
Main.setTest(request);
|
||||||
Map<String, JSONObject> res = Main.getActivities("token");
|
Map<String, JSONObject> res = Main.getActivities();
|
||||||
Assert.assertEquals(res.size(), 2);
|
Assert.assertEquals(res.size(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (String request: requestsOneActivity)
|
for (String request: requestsOneActivity)
|
||||||
{
|
{
|
||||||
Main.setTest(request);
|
Main.setTest(request);
|
||||||
Map<String, JSONObject> res = Main.getActivities("token");
|
Map<String, JSONObject> res = Main.getActivities();
|
||||||
Assert.assertEquals(res.size(), 1);
|
Assert.assertEquals(res.size(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
Main.setTest(request_missing_id);
|
Main.setTest(request_missing_id);
|
||||||
Map<String, JSONObject> res = Main.getActivities("token");
|
Map<String, JSONObject> res = Main.getActivities();
|
||||||
Assert.assertEquals(res.size(), 0);
|
Assert.assertEquals(res.size(), 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSaveGeneralInformation()
|
public void testSaveGeneralInformation() throws NoAccessException
|
||||||
{
|
{
|
||||||
String request_full ="{ \"id\" : 1234567890987654321, \"username\" : \"marianne_t\", \"resource_state\" : 3,"
|
String request_full ="{ \"id\" : 1234567890987654321, \"username\" : \"marianne_t\", \"resource_state\" : 3,"
|
||||||
+ "\"firstname\" : \"Marianne\", \"lastname\" : \"Teutenberg\", \"city\" : \"San Francisco\", "
|
+ "\"firstname\" : \"Marianne\", \"lastname\" : \"Teutenberg\", \"city\" : \"San Francisco\", "
|
||||||
@@ -187,26 +187,26 @@ public class MainTest
|
|||||||
for (String request: allRequests)
|
for (String request: allRequests)
|
||||||
{
|
{
|
||||||
Main.setTest(request);
|
Main.setTest(request);
|
||||||
JSONObject o = Main.saveGeneralInformation("token");
|
JSONObject o = Main.saveGeneralInformation();
|
||||||
Assert.assertNotNull(o);
|
Assert.assertNotNull(o);
|
||||||
}
|
}
|
||||||
|
|
||||||
Main.setTest(request_bad_format);
|
Main.setTest(request_bad_format);
|
||||||
JSONObject o = Main.saveGeneralInformation("token");
|
JSONObject o = Main.saveGeneralInformation();
|
||||||
Assert.assertNull(o);
|
Assert.assertNull(o);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
public void testStructure()
|
public void testStructure() throws NoAccessException
|
||||||
{
|
{
|
||||||
String request_shortened = "[{\"type\": \"Ride\", \"id\": 154504250376823, "
|
String request_shortened = "[{\"type\": \"Ride\", \"id\": 154504250376823, "
|
||||||
+ "\"start_date_local\": \"2018-05-02T05:15:09Z\", "
|
+ "\"start_date_local\": \"2018-05-02T05:15:09Z\", "
|
||||||
+ "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"}]";
|
+ "\"timezone\": \"(GMT-08:00) America/Los_Angeles\"}]";
|
||||||
|
|
||||||
Main.setTest(request_shortened);
|
Main.setTest(request_shortened);
|
||||||
Map<String, JSONObject> res = Main.getActivities("token");
|
Map<String, JSONObject> res = Main.getActivities();
|
||||||
|
|
||||||
String requestDistanceAndTime = "[{\"type\" : \"distance\", "
|
String requestDistanceAndTime = "[{\"type\" : \"distance\", "
|
||||||
+ "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
|
+ "\"data\" : [ 2.9, 5.8, 8.5, 11.7, 15, 19, 23.2, 28, 32.8, 38.1, 43.8, 49.5 ], "
|
||||||
@@ -219,7 +219,7 @@ public class MainTest
|
|||||||
for(String activity : res.keySet())
|
for(String activity : res.keySet())
|
||||||
{
|
{
|
||||||
Main.setTest(requestDistanceAndTime);
|
Main.setTest(requestDistanceAndTime);
|
||||||
JSONObject toSave = Main.addStreams("id", res.get(activity), "token");
|
JSONObject toSave = Main.addStreams("id", res.get(activity));
|
||||||
allActivities.add(toSave);
|
allActivities.add(toSave);
|
||||||
}
|
}
|
||||||
Assert.assertEquals(allActivities.size(), res.keySet().size());
|
Assert.assertEquals(allActivities.size(), res.keySet().size());
|
||||||
@@ -228,7 +228,7 @@ public class MainTest
|
|||||||
+ "\"measurement_preference\" : \"feet\", \"weight\" : 0}";
|
+ "\"measurement_preference\" : \"feet\", \"weight\" : 0}";
|
||||||
|
|
||||||
Main.setTest(request_missing_country);
|
Main.setTest(request_missing_country);
|
||||||
JSONObject a = Main.saveGeneralInformation("token");
|
JSONObject a = Main.saveGeneralInformation();
|
||||||
a.put("activities", allActivities);
|
a.put("activities", allActivities);
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -241,7 +241,7 @@ public class MainTest
|
|||||||
|
|
||||||
Map<String, File> map = new HashMap<>();
|
Map<String, File> map = new HashMap<>();
|
||||||
map.put("athlete_0.json", temp);
|
map.put("athlete_0.json", temp);
|
||||||
Main.zipFiles(map);
|
Main.zipFiles(map,0);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (IOException e)
|
catch (IOException e)
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package export;
|
||||||
|
|
||||||
|
public class NoAccessException extends Exception
|
||||||
|
{
|
||||||
|
private static final long serialVersionUID = -3852341856319438435L;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -14,4 +14,9 @@ public class Triplet extends Tuple
|
|||||||
{
|
{
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setC(String c)
|
||||||
|
{
|
||||||
|
this.c=c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user