-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
1,674 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
src/androidTest/java/de/dennisguse/opentracks/data/FirestoreCRUDUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
package de.dennisguse.opentracks.data; | ||
|
||
import org.junit.Test; | ||
import com.google.gson.JsonObject; | ||
import de.dennisguse.opentracks.data.interfaces.ActionCallback; | ||
import de.dennisguse.opentracks.data.interfaces.ReadCallback; | ||
import de.dennisguse.opentracks.data.models.CRUDConstants; | ||
import static org.mockito.Mockito.*; | ||
|
||
|
||
/** | ||
* Tests for FirestoreCRUDUtil class | ||
*/ | ||
public class FirestoreCRUDUtilTest { | ||
|
||
final String trackId = "track_01"; | ||
|
||
/** | ||
* Expect SUCCESSFUL createEntry as the proper id and valid jsonData are passed | ||
*/ | ||
@Test | ||
public void testCreateEntry() { | ||
FirestoreCRUDUtil firestoreCRUDUtil = mock(FirestoreCRUDUtil.class); | ||
ActionCallback callback = mock(ActionCallback.class); | ||
|
||
JsonObject jsonData = generateMockData(); | ||
doAnswer(invocation -> { | ||
// get the 4th param in the `createEntry` method | ||
((ActionCallback) invocation.getArgument(3)).onSuccess(); | ||
return null; | ||
}).when(firestoreCRUDUtil).createEntry(anyString(), anyString(), any(JsonObject.class), any(ActionCallback.class)); | ||
firestoreCRUDUtil.createEntry(CRUDConstants.RUNS_TABLE, trackId, jsonData, callback); | ||
|
||
verify(callback).onSuccess(); | ||
} | ||
|
||
|
||
/** | ||
* Expect SUCCESSFUL createEntry as the proper id and valid jsonData are passed and make sure | ||
* getEntry retreives what we just created | ||
*/ | ||
@Test | ||
public void testCreateAndRetrieveEntry() { | ||
FirestoreCRUDUtil firestoreCRUDUtil = mock(FirestoreCRUDUtil.class); | ||
ReadCallback readCallback = mock(ReadCallback.class); | ||
ActionCallback createCallback = mock(ActionCallback.class); | ||
JsonObject jsonData = generateMockData(); | ||
|
||
doAnswer(invocation -> { | ||
// get the 4th param in the `createEntry` method | ||
((ActionCallback) invocation.getArgument(3)).onSuccess(); | ||
return null; | ||
}).when(firestoreCRUDUtil).createEntry(anyString(), anyString(), any(JsonObject.class), any(ActionCallback.class)); | ||
|
||
doAnswer(invocation -> { | ||
// get the 3rd param in the `getEntry` method | ||
((ReadCallback) invocation.getArgument(2)).onSuccess(jsonData); | ||
return null; | ||
}).when(firestoreCRUDUtil).getEntry(anyString(), anyString(), any(ReadCallback.class)); | ||
|
||
firestoreCRUDUtil.createEntry(CRUDConstants.RUNS_TABLE, trackId, jsonData, createCallback); | ||
firestoreCRUDUtil.getEntry(CRUDConstants.RUNS_TABLE, trackId, readCallback); | ||
|
||
verify(createCallback).onSuccess(); | ||
verify(readCallback).onSuccess(jsonData); | ||
} | ||
|
||
|
||
/** | ||
* Expect SUCCESSFUL updateEntry as the proper id and valid jsonData are passed | ||
*/ | ||
@Test | ||
public void testUpdateEntry() { | ||
FirestoreCRUDUtil firestoreCRUDUtil = mock(FirestoreCRUDUtil.class); | ||
ActionCallback callback = mock(ActionCallback.class); | ||
|
||
JsonObject jsonData = generateMockData(); | ||
doAnswer(invocation -> { | ||
// get the 4th param in the `updateEntry` method | ||
((ActionCallback) invocation.getArgument(3)).onSuccess(); | ||
return null; | ||
}).when(firestoreCRUDUtil).updateEntry(anyString(), anyString(), any(JsonObject.class), any(ActionCallback.class)); | ||
firestoreCRUDUtil.updateEntry(CRUDConstants.RUNS_TABLE, trackId, jsonData, callback); | ||
|
||
verify(callback).onSuccess(); | ||
} | ||
|
||
/** | ||
* Expect SUCCESSFUL deleteEntry as the proper id is passed | ||
*/ | ||
@Test | ||
public void testDeleteEntry() { | ||
FirestoreCRUDUtil firestoreCRUDUtil = mock(FirestoreCRUDUtil.class); | ||
ActionCallback callback = mock(ActionCallback.class); | ||
|
||
JsonObject jsonData = generateMockData(); | ||
doAnswer(invocation -> { | ||
// get the 3rd param in the `deleteEntry` method | ||
((ActionCallback) invocation.getArgument(2)).onSuccess(); | ||
return null; | ||
}).when(firestoreCRUDUtil).deleteEntry(anyString(), anyString(), any(ActionCallback.class)); | ||
firestoreCRUDUtil.deleteEntry(CRUDConstants.RUNS_TABLE, trackId, callback); | ||
|
||
verify(callback).onSuccess(); | ||
} | ||
|
||
/** | ||
* Expect SUCCESSFUL getEntry as the proper id is passed | ||
*/ | ||
@Test | ||
public void testGetEntry() { | ||
FirestoreCRUDUtil firestoreCRUDUtil = mock(FirestoreCRUDUtil.class); | ||
ReadCallback callback = mock(ReadCallback.class); | ||
|
||
JsonObject jsonData = generateMockData(); | ||
doAnswer(invocation -> { | ||
// get the 3rd param in the `getEntry` method | ||
((ReadCallback) invocation.getArgument(2)).onSuccess(jsonData); | ||
return null; | ||
}).when(firestoreCRUDUtil).getEntry(anyString(), anyString(), any(ReadCallback.class)); | ||
firestoreCRUDUtil.getEntry(CRUDConstants.RUNS_TABLE, trackId, callback); | ||
|
||
System.out.println(jsonData.toString()); | ||
verify(callback).onSuccess(jsonData); | ||
} | ||
|
||
/** | ||
* Generates mock data for testing purposes | ||
* @return JsonObject with mock data | ||
*/ | ||
public static JsonObject generateMockData() { | ||
JsonObject mockData = new JsonObject(); | ||
|
||
JsonObject idObject = new JsonObject(); | ||
idObject.addProperty("id", 42); | ||
idObject.addProperty("name", "2024-04-04T12:12:12"); | ||
|
||
JsonObject altitudeExtremitiesObject = new JsonObject(); | ||
altitudeExtremitiesObject.addProperty("max", "-Infinity"); | ||
altitudeExtremitiesObject.addProperty("min", "Infinity"); | ||
|
||
JsonObject maxSpeedObject = new JsonObject(); | ||
maxSpeedObject.addProperty("speed_mps", 0); | ||
|
||
JsonObject movingTimeObject = new JsonObject(); | ||
movingTimeObject.addProperty("skiing", false); | ||
movingTimeObject.addProperty("slopePercentageSeason", 0); | ||
|
||
JsonObject totalDistanceObject = new JsonObject(); | ||
totalDistanceObject.addProperty("distance_m", 0); | ||
totalDistanceObject.addProperty("totalRunsSeason", 0); | ||
totalDistanceObject.addProperty("totalSkiDaysSeason", 0); | ||
|
||
JsonObject totalTimeObject = new JsonObject(); | ||
totalTimeObject.addProperty("waiting", false); | ||
|
||
JsonObject trackStatisticsObject = new JsonObject(); | ||
trackStatisticsObject.add("altitudeExtremities", altitudeExtremitiesObject); | ||
trackStatisticsObject.addProperty("chairlift", false); | ||
trackStatisticsObject.addProperty("isIdle", false); | ||
trackStatisticsObject.add("maxSpeed", maxSpeedObject); | ||
trackStatisticsObject.add("movingTime", movingTimeObject); | ||
trackStatisticsObject.add("startTime", new JsonObject()); | ||
trackStatisticsObject.add("stopTime", new JsonObject()); | ||
trackStatisticsObject.add("totalDistance", totalDistanceObject); | ||
trackStatisticsObject.add("totalTime", totalTimeObject); | ||
trackStatisticsObject.addProperty("uuid", "test"); | ||
|
||
mockData.addProperty("activityType", "SKI"); | ||
mockData.addProperty("activityTypeLocalized", "unknown"); | ||
mockData.addProperty("description", "Updated track"); | ||
mockData.addProperty("externalId", "track_01"); | ||
mockData.add("id", idObject); | ||
mockData.add("trackStatistics", trackStatisticsObject); | ||
|
||
return mockData; | ||
} | ||
} | ||
|
||
|
||
|
88 changes: 88 additions & 0 deletions
88
src/androidTest/java/de/dennisguse/opentracks/data/interfaces/JsonSerializerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package de.dennisguse.opentracks.data.interfaces; | ||
import org.junit.Test; | ||
import com.google.gson.JsonObject; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
|
||
/** | ||
* Tests for JsonSerializer Interface | ||
*/ | ||
public class JsonSerializerTest { | ||
final String description = "Completed track"; | ||
final String externalId = "track_01"; | ||
|
||
@Test | ||
/** | ||
* Testing the toJSON method, making sure the expect key value pairs appear | ||
* The unexpected keys shouldn't resolve to true | ||
*/ | ||
public void testIfValidToJson() { | ||
TestTrack testObject = new TestTrack(description, externalId); | ||
|
||
JsonObject json = testObject.toJSON(); | ||
|
||
assertNotNull(json); | ||
assertTrue(json.has("description")); | ||
assertTrue(json.has("externalId")); | ||
assertFalse(json.has("not there")); | ||
|
||
assertEquals(description, json.get("description").getAsString()); | ||
assertEquals(externalId, json.get("externalId").getAsString()); | ||
} | ||
|
||
// TODO: add tests for FromJSON | ||
|
||
@Test | ||
public void testFromJson() { | ||
// Create a JsonObject representing the JSON structure you expect to deserialize | ||
JsonObject jsonObject = new JsonObject(); | ||
jsonObject.addProperty("description", description); | ||
jsonObject.addProperty("externalId", externalId); | ||
|
||
// Use the fromJSON method to deserialize the JsonObject into a TestTrack object | ||
TestTrack result = JSONSerializable.fromJSON(jsonObject, TestTrack.class); | ||
|
||
assertNotNull(result); | ||
assertEquals(description, result.getDescription()); | ||
assertEquals(externalId, result.getExternalId()); | ||
} | ||
|
||
|
||
/** | ||
* Sample class of Track that extends the JSONSerializable | ||
* This class is a simplified version of the Track class that has only a few fields | ||
*/ | ||
private static class TestTrack implements JSONSerializable<TestTrack> { | ||
private String description; | ||
private String externalId; | ||
|
||
/** | ||
* The constructor will set static value for the purpose of the test | ||
*/ | ||
public TestTrack(String description, String externalId) { | ||
this.description = description; | ||
this.externalId = externalId; | ||
} | ||
// Getter for description | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
// Setter for description | ||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
// Getter for externalId | ||
public String getExternalId() { | ||
return externalId; | ||
} | ||
|
||
// Setter for externalId | ||
public void setExternalId(String externalId) { | ||
this.externalId = externalId; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.