diff --git a/app/src/androidTest/java/com/github/sdp/mediato/DatabaseTests/UserTests.java b/app/src/androidTest/java/com/github/sdp/mediato/DatabaseTests/UserTests.java index 9ef9c80d..dd532619 100644 --- a/app/src/androidTest/java/com/github/sdp/mediato/DatabaseTests/UserTests.java +++ b/app/src/androidTest/java/com/github/sdp/mediato/DatabaseTests/UserTests.java @@ -36,6 +36,7 @@ */ 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; @@ -79,35 +80,14 @@ public void followUserAddsUsernameInFollowingAndFollowers() throws ExecutionExce 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 following = (Map) 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 followers = (Map) snapshot.getValue(); - assertTrue(followers.get(user2.getUsername())); - } - - @Override - public void onCancelled(@NonNull DatabaseError error) { - throw error.toException(); - } - }); + Thread.sleep(STANDARD_SLEEP_DELAY); + + List followers = Database.getUser(user3.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowers(); + List following = Database.getUser(user2.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowing(); + + assertTrue(followers.contains(user2.getUsername())); + assertTrue(following.contains(user3.getUsername())); + } @Test @@ -115,37 +95,16 @@ public void onCancelled(@NonNull DatabaseError error) { 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 following = (Map) 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 followers = (Map) snapshot.getValue(); - assertFalse(followers.get(user2.getUsername())); - } - - @Override - public void onCancelled(@NonNull DatabaseError error) { - throw error.toException(); - } - }); + Thread.sleep(STANDARD_SLEEP_DELAY); + + List followers = Database.getUser(user3.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowers(); + List following = Database.getUser(user2.getUsername()).get(STANDARD_USER_TIMEOUT, TimeUnit.SECONDS).getFollowing(); + + assertFalse(followers.contains(user2.getUsername())); + assertFalse(following.contains(user3.getUsername())); } @Test diff --git a/app/src/main/java/com/github/sdp/mediato/data/Database.java b/app/src/main/java/com/github/sdp/mediato/data/Database.java index 0101b2d1..fe0e11a4 100644 --- a/app/src/main/java/com/github/sdp/mediato/data/Database.java +++ b/app/src/main/java/com/github/sdp/mediato/data/Database.java @@ -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/"; diff --git a/app/src/main/java/com/github/sdp/mediato/data/GenericDatabase.java b/app/src/main/java/com/github/sdp/mediato/data/GenericDatabase.java deleted file mode 100644 index 50031c24..00000000 --- a/app/src/main/java/com/github/sdp/mediato/data/GenericDatabase.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.github.sdp.mediato.data; - -import android.net.Uri; - -import com.github.sdp.mediato.model.User; - -import java.util.concurrent.CompletableFuture; - -public interface GenericDatabase { - - //Adds a user to the database - public static void addUser(User user) { - } - - //Deletes a user from the database - public static void deleteUser(User user) { - } - - //Retrieves a completable future with the user from the database - public static CompletableFuture getUser(String username) { - return null; - } - //Retrieves a completable future with the Boolean determining whether the username is unique - public static CompletableFuture isUsernameUnique(String username) { - return null; - } - - //Retrieves a completable future with the byte array representing the profile pic - public static CompletableFuture getProfilePic(String username) { - return null; - } - - //Uploads the given profile pic for a given username - public static void setProfilePic(String username, Uri profilePicUri) { - } - -} diff --git a/app/src/test/java/com/github/sdp/mediato/ExampleUnitTest.java b/app/src/test/java/com/github/sdp/mediato/ExampleUnitTest.java deleted file mode 100644 index e48877c9..00000000 --- a/app/src/test/java/com/github/sdp/mediato/ExampleUnitTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.github.sdp.mediato; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Example local unit test, which will execute on the development machine (host). - * - * @see Testing documentation - */ -public class ExampleUnitTest { - @Test - public void addition_isCorrect() { - assertEquals(4, 2 + 2); - } -} \ No newline at end of file diff --git a/app/src/test/java/com/github/sdp/mediato/formats/DatesTests.java b/app/src/test/java/com/github/sdp/mediato/formats/DatesTests.java new file mode 100644 index 00000000..0da0b3ad --- /dev/null +++ b/app/src/test/java/com/github/sdp/mediato/formats/DatesTests.java @@ -0,0 +1,25 @@ +package com.github.sdp.mediato.formats; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import java.time.LocalDate; +import java.util.Date; + +public class DatesTests { + + @Test + //Tests that the Dates class returns today's date with the right format + public void getsTodaysDateProperly(){ + LocalDate today = LocalDate.now(); + String expected = pad(today.getDayOfMonth()) + "/" + pad(today.getMonthValue()) + "/" + today.getYear(); + assertEquals(expected, Dates.getToday()); + } + + //Adds leading zero to number + public String pad(int number){ + if(number < 10) return "0" + number; + else return String.valueOf(number); + } +} diff --git a/app/src/test/java/com/github/sdp/mediato/model/CollectionTests.java b/app/src/test/java/com/github/sdp/mediato/model/CollectionTests.java new file mode 100644 index 00000000..d889e84a --- /dev/null +++ b/app/src/test/java/com/github/sdp/mediato/model/CollectionTests.java @@ -0,0 +1,65 @@ +package com.github.sdp.mediato.model; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertThrows; + +import com.github.sdp.mediato.model.media.Collection; +import com.github.sdp.mediato.model.media.CollectionType; +import com.github.sdp.mediato.model.media.Movie; + +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +public class CollectionTests { + private static Map reviews = new HashMap<>(); + private static String SAMPLE_TITLE = "Harry Potter"; + private static String SAMPLE_DESCRIPTION = "Description"; + private static String SAMPLE_URL = "Url"; + private static int SAMPLE_ID = 1; + + private static String SAMPLE_USERNAME = "testUser"; + private static final Movie SAMPLE_MOVIE = new Movie(SAMPLE_TITLE , SAMPLE_DESCRIPTION, SAMPLE_URL, SAMPLE_ID); + + @BeforeClass + public static void setUp(){ + reviews.put(SAMPLE_TITLE, new Review(SAMPLE_USERNAME, SAMPLE_MOVIE)); + } + + @Test + //Tests that a custom collection is created properly + public void createsCustomCollectionProperly(){ + Collection collection = new Collection("MyCustom", reviews); + assertThat(collection.getCollectionType(), is(CollectionType.CUSTOM)); + assertThat(collection.getCollectionName(), is("MyCustom")); + assertThat(CollectionType.CUSTOM.toString(), is("Custom")); + } + + @Test + //Tests that a favourites collection is created properly + public void createsFavouriteCollectionProperly(){ + Collection collection = new Collection(CollectionType.FAVOURITES, reviews); + assertThat(collection.getCollectionType(), is(CollectionType.FAVOURITES)); + assertThat(collection.getCollectionName(), is(CollectionType.FAVOURITES.toString())); + } + + @Test + //Tests that a recently watched collection is created properly + public void createsRecentlyWatchedCollectionProperly(){ + Collection collection = new Collection(CollectionType.RECENTLY_WATCHED, reviews); + assertThat(collection.getCollectionType(), is(CollectionType.RECENTLY_WATCHED)); + assertThat(collection.getCollectionName(), is(CollectionType.RECENTLY_WATCHED.toString())); + } + + @Test + //Tests that custom collection creation fails when the wrong constructor is used + public void defaultCollectionConstructorFailsWhenTypeIsCustom(){ + assertThrows( + IllegalArgumentException.class, + () -> new Collection(CollectionType.CUSTOM, reviews) + ); + } +}