use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class AccountManagerTest method setUserLoggedInFalseTest.
/**
* Tests that a user that has been authenticated is tagged as logged in
*
* Will pass if there exists a child node in the online_users reference equal to the users
* username.
*
* @throws Exception
*/
@Test
public void setUserLoggedInFalseTest() throws Exception {
// create a test user and set them as online
User testUser = TestingHelper.createTestUser();
// set the user as online then set them as offline
AccountManager.setUserLoginState(testUser.getUsername(), false);
// attempt to retrieve a reference to the logged in user
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference usersReference = database.getReference("online_users/" + testUser.getUsername());
// attach a listener for data changes of the users reference. this will occur when
// the reference is populated
usersReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// if the reference exists, then the user is tagged as logged in and we should fail
// the test, otherwise it should succeed
assertFalse("Expecting non-empty result from database, but no data returned...", dataSnapshot.exists());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class AccountManagerTest method toggleOnlineUserTest.
@Test
public void toggleOnlineUserTest() throws Exception {
final User testUser = TestingHelper.createTestUser();
AccountManager.createUser(testUser);
sleep(1000);
AccountManager.authenticate(testUser, TestingHelper.assertTrueBooleanResult());
sleep(1000);
AccountManager.setOnline(false);
testUser.addUserExercise(TestingHelper.testExercise1);
AccountManager.updateUser(testUser);
AccountManager.setOnline(true);
sleep(1000);
// retrieve a reference to the users node
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference usersReference = database.getReference("users/" + testUser.getUsername());
// attach a listener for data changes of the users reference. this will occur when
// the reference is populated
usersReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
assertTrue(dataSnapshot.exists());
if (dataSnapshot.exists()) {
assertEquals(1, dataSnapshot.getValue(User.class).getUserExercises().size());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
sleep(1000);
AccountManager.deleteUser(testUser, TestingHelper.assertTrueBooleanResult());
}
use of com.google.firebase.database.FirebaseDatabase in project OnlineCanteen by josephgunawan97.
the class EditProductActivity method submitData.
// To submit data
private void submitData() {
Log.i(TAG, "TEXT2 MASUK");
if (validateRegisterInfo()) {
Log.i(TAG, "TEXT2 MASUK LAGI");
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference reference = firebaseDatabase.getReference();
Query query = reference.child("products").orderByChild("tokoId").equalTo(merchant.getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot productSnapshot : dataSnapshot.getChildren()) {
// DataSnapshot nodeDataSnapshot = dataSnapshot.getChildren().iterator().next();
Product product = productSnapshot.getValue(Product.class);
if (merchant.getUid().equals(product.getTokoId()) && product.getName().equals(choose.toString())) {
Log.i(TAG, "TEXT2 test " + productSnapshot.getValue().toString());
// Log.i(TAG, "TEXT2 test " + nodeDataSnapshot.getValue().toString());
// String key = nodeDataSnapshot.getKey();
// String path = "/" + dataSnapshot.getKey() + "/" + key;
HashMap<String, Object> result = new HashMap<>();
// result.put("imageUrl", );
result.put("name", productName.getText().toString());
// HashMap<Integer, Object> result2 = new HashMap<>();
result.put("price", Integer.parseInt(productPrice.getText().toString()));
result.put("stock", Integer.parseInt(productQty.getText().toString()));
result.put("imageUrl", profPicUrl);
if (product.getImageUrl() != null) {
StorageReference photoRef = FirebaseStorage.getInstance().getReferenceFromUrl(product.getImageUrl());
photoRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// File deleted successfully
Log.d(TAG, "onSuccess: deleted file");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
Log.d(TAG, "onFailure: did not delete file");
}
});
}
// if(!product.getImageUrl().equals())
reference.child("products").child(productSnapshot.getKey()).updateChildren(result);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Logger.error(TAG, ">>> Error:" + "find onCancelled:" + databaseError);
}
});
backToScreen();
}
}
use of com.google.firebase.database.FirebaseDatabase in project OnlineCanteen by josephgunawan97.
the class EditProductActivity method submitDatawithoutImage.
private void submitDatawithoutImage() {
Log.i(TAG, "TEXT2 MASUK");
if (validateRegisterInfo()) {
Log.i(TAG, "TEXT2 MASUK LAGI");
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference reference = firebaseDatabase.getReference();
Query query = reference.child("products").orderByChild("tokoId").equalTo(merchant.getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot productSnapshot : dataSnapshot.getChildren()) {
// DataSnapshot nodeDataSnapshot = dataSnapshot.getChildren().iterator().next();
Product product = productSnapshot.getValue(Product.class);
if (merchant.getUid().equals(product.getTokoId()) && product.getName().equals(choose.toString())) {
Log.i(TAG, "TEXT2 test " + productSnapshot.getValue().toString());
// String key = nodeDataSnapshot.getKey();
// String path = "/" + dataSnapshot.getKey() + "/" + key;
HashMap<String, Object> result = new HashMap<>();
// result.put("imageUrl", );
result.put("name", productName.getText().toString());
// HashMap<Integer, Object> result2 = new HashMap<>();
result.put("price", Integer.parseInt(productPrice.getText().toString()));
result.put("stock", Integer.parseInt(productQty.getText().toString()));
// if(!product.getImageUrl().equals())
reference.child("products").child(productSnapshot.getKey()).updateChildren(result);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Logger.error(TAG, ">>> Error:" + "find onCancelled:" + databaseError);
}
});
backToScreen();
}
}
use of com.google.firebase.database.FirebaseDatabase in project OnlineCanteen by josephgunawan97.
the class AccountUtil method createRole.
private static Task<Void> createRole(String role) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
DatabaseReference reference = firebaseDatabase.getReference("role");
return reference.child(firebaseUser.getUid()).setValue(role);
}
Aggregations