Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Picard4 authored Apr 28, 2024
2 parents c66475f + 83b8bf3 commit 4f9233d
Show file tree
Hide file tree
Showing 31 changed files with 1,674 additions and 280 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ android {
}

dependencies {

implementation 'com.google.firebase:firebase-firestore:24.10.3'
implementation 'junit:junit:4.12'
implementation 'com.google.firebase:firebase-database:20.3.1'
androidTestImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'

coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'
Expand Down
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;
}
}



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;
}
}
}
3 changes: 3 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@
<activity
android:name=".ui.friends.FriendsActivity"
android:exported="false" />
<activity
android:name=".ui.friends.FriendClActivity"
android:exported="false" />
<activity
android:name=".ui.profile.ProfileActivity"
android:exported="false" />
Expand Down
52 changes: 29 additions & 23 deletions src/main/java/de/dennisguse/opentracks/AboutActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.gson.JsonObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.dennisguse.opentracks.data.FirestoreCRUDUtil;
import de.dennisguse.opentracks.data.interfaces.ReadCallback;
import de.dennisguse.opentracks.data.models.CRUDConstants;
import de.dennisguse.opentracks.databinding.AboutBinding;
import de.dennisguse.opentracks.ui.util.ViewUtils;
import de.dennisguse.opentracks.util.SystemUtils;
Expand All @@ -28,7 +33,29 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setTitle(getString(R.string.about_preference_title));
//checkDBConnection("FirstName","LastName");
ReadCallback callback = new ReadCallback() {
@Override
public void onSuccess(JsonObject data) {


}

@Override
public void onSuccess(ArrayList<JsonObject> data) {
Log.d("Test", "Data" + " => " + data.toString());


}

@Override
public void onFailure() {

}
};
FirestoreCRUDUtil dbUtil = FirestoreCRUDUtil.getInstance();

dbUtil.getUserRuns("defaultUser",callback);


viewBinding.aboutTextDescription.setText(getString(R.string.about_description));
viewBinding.aboutTextVersionName.setText(getString(R.string.about_version_name, SystemUtils.getAppVersionName(this)));
Expand All @@ -50,26 +77,5 @@ protected void onDestroy() {
super.onDestroy();
viewBinding = null;
}
void checkDBConnection(String firstName, String lastName)

{
FirebaseFirestore db = FirebaseFirestore.getInstance();
Map<String, Object> user = new HashMap<>(); //Tester code to check DB connection from Firestore Doc s.
user.put("firstName", firstName);
user.put("lastName", lastName);
db.collection("users")
.add(user)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.d("ADDED", "DocumentSnapshot added with ID: " + documentReference.getId());
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("ERROR", "Error adding user", e);
}
});
}

}
Loading

0 comments on commit 4f9233d

Please sign in to comment.