Skip to content
This repository has been archived by the owner on Feb 1, 2025. It is now read-only.

Commit

Permalink
edit follow test
Browse files Browse the repository at this point in the history
  • Loading branch information
MariemBaccari committed Mar 28, 2023
1 parent 73c66bb commit 4adee47
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@

/**
* This class contains all the tests for database collections interactions
*/
/*/
public class CollectionsTests {
/*
private final static int STANDARD_COLLECTION_TIMEOUT = 10;
private User user1 = new User.UserBuilder("uniqueId1")
.setUsername("user_test_1")
Expand All @@ -50,6 +51,7 @@ public class CollectionsTests {
private Collection collection1;
private Collection collection2;
private Review review1 = new Review(user1.getUsername(), new Media(MediaType.MOVIE, "Harry Potter 1", "the chosen one", "url", 1));
private Review review2 = new Review(user1.getUsername(), new Media(MediaType.MOVIE, "Harry Potter 2", "the chosen two", "url", 2), 9);
private Review review3 = new Review(user1.getUsername(), new Media(MediaType.MOVIE, "Harry Potter 3", "the chosen three", "url", 3), 2, "meh");
Expand Down Expand Up @@ -125,5 +127,5 @@ public void addsReviewToCollectionProperly() throws InterruptedException, Execut
assertTrue(retrievedCollection.getReviews().containsKey(review1.getMedia().getTitle())
&& retrievedCollection.getReviews().containsKey(review2.getMedia().getTitle()));
}

*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ValueEventListener;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -36,17 +37,20 @@
*/
public class UserTests {
private final static int STANDARD_USER_TIMEOUT = 10;
private final static int STANDARD_SLEEP_DELAY = 1000;
User user1;
User user2;
User user3;

@Before
public void setUp() {
public void setUp() throws InterruptedException {
try {
Database.database.useEmulator("10.0.2.2", 9000);
} catch (Exception ignored) {
} catch (Exception e) {
throw e;
}
//Create new sample users
/**
user1 = new User.UserBuilder("uniqueId1")
.setUsername("user_test_1")
.setEmail("email_test_1")
Expand All @@ -64,88 +68,53 @@ public void setUp() {
.setEmail("email_test_3")
.setRegisterDate("19/03/2023")
.setLocation(new Location(3.14, 3.14))
.build();
.build();*/
}

@AfterClass
public static void cleanDatabase() {
Database.database.getReference().setValue(null);
@Test
public void test() throws InterruptedException {
Database.database.getReference().child("Test").setValue("testval");
Thread.sleep(5000);
}

//@AfterClass
//public static void cleanDatabase() {
//Database.database.getReference().setValue(null);
//}

/**
@Test
//Tests that following a user adds the right username in the following list and the followers list
public void followUserAddsUsernameInFollowingAndFollowers() throws ExecutionException, InterruptedException, TimeoutException {
Database.addUser(user2).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS);
Database.addUser(user3).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS);
Database.followUser(user2.getUsername(), user3.getUsername());
DatabaseReference user1FollowingRef = Database.database.getReference().child(Database.USERS_PATH + user2.getUsername() + Database.FOLLOWING_PATH);
user1FollowingRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

Map<String, Boolean> following = (Map<String, Boolean>) snapshot.getValue();
assertTrue(following.get(user3.getUsername()));
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});

DatabaseReference user2FollowersRef = Database.database.getReference().child(Database.USERS_PATH + user3.getUsername() + Database.FOLLOWERS_PATH);
user2FollowersRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

Map<String, Boolean> followers = (Map<String, Boolean>) snapshot.getValue();
assertTrue(followers.get(user2.getUsername()));
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});
Thread.sleep(STANDARD_SLEEP_DELAY);
List<String> followers = Database.getUser(user3.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowers();
List<String> following = Database.getUser(user2.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowing();
assertTrue(followers.contains(user2.getUsername()));
assertTrue(following.contains(user3.getUsername()));
}
@Test
//Tests that unfollowing a user removes the right username from the following list and the followers list
public void unfollowUserRemovesUsernameFromFollowingAndFollowers() throws ExecutionException, InterruptedException, TimeoutException {
Database.addUser(user2).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS);
Database.addUser(user3).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS);
Database.followUser(user2.getUsername(), user3.getUsername());
Database.unfollowUser(user2.getUsername(), user3.getUsername());
DatabaseReference user1FollowingRef = Database.database.getReference().child(Database.USERS_PATH + user2.getUsername() + Database.FOLLOWING_PATH);
user1FollowingRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

Map<String, Boolean> following = (Map<String, Boolean>) snapshot.getValue();
assertFalse(following.get(user3.getUsername()));
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});

DatabaseReference user2FollowersRef = Database.database.getReference().child(Database.USERS_PATH + user3.getUsername() + Database.FOLLOWERS_PATH);
user2FollowersRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {

Map<String, Boolean> followers = (Map<String, Boolean>) snapshot.getValue();
assertFalse(followers.get(user2.getUsername()));
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
throw error.toException();
}
});
Thread.sleep(STANDARD_SLEEP_DELAY);
List<String> followers = Database.getUser(user3.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowers();
List<String> following = Database.getUser(user2.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowing();
assertFalse(followers.contains(user2.getUsername()));
assertFalse(following.contains(user3.getUsername()));
}
@Test
Expand Down Expand Up @@ -200,6 +169,6 @@ public void isUsernameUniqueReturnsTrueForUniqueUsername() throws ExecutionExcep
public void isUsernameUniqueReturnsFalseForAlreadyExistingUsername() throws ExecutionException, InterruptedException, TimeoutException {
Database.addUser(user1).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS);
assertFalse(Database.isUsernameUnique("user_test_1").get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS));
}
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;

public class Database implements GenericDatabase {
public class Database {

public static final String USERS_PATH = "Users/";

Expand Down
17 changes: 0 additions & 17 deletions app/src/test/java/com/github/sdp/mediato/ExampleUnitTest.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.sdp.mediato.model;

public class LocationTests {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.sdp.mediato.model;

public class ReviewTests {
}
2 changes: 2 additions & 0 deletions app/src/test/java/com/github/sdp/mediato/model/UserTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ public void user_builder_fails_with_missing_mandatory_attributes(){
.build();
});
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.github.sdp.mediato.model.mediaTests;

public class CollectionTests {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.sdp.mediato.model;
package com.github.sdp.mediato.model.mediaTests;


import static org.hamcrest.CoreMatchers.is;
Expand Down
Loading

0 comments on commit 4adee47

Please sign in to comment.