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

Commit

Permalink
Commenting system logic (#199)
Browse files Browse the repository at this point in the history
* Adapt model and add database method to comment review

* add DB test

* fix codeclimate and add tests

* remove useless comment methods
  • Loading branch information
MariemBaccari authored May 20, 2023
1 parent cc2515d commit c92bdd0
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 1 deletion.
13 changes: 13 additions & 0 deletions .idea/androidTestResultsUserPreferences.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.github.sdp.mediato.data.CollectionsDatabase;
import com.github.sdp.mediato.data.ReviewInteractionDatabase;
import com.github.sdp.mediato.data.UserDatabase;
import com.github.sdp.mediato.model.Comment;
import com.github.sdp.mediato.model.Location;
import com.github.sdp.mediato.model.Review;
import com.github.sdp.mediato.model.User;
Expand All @@ -33,6 +35,7 @@
*/
public class CollectionsTests {
private final static int STANDARD_COLLECTION_TIMEOUT = 10;
private final static String COMMENTER_USERNAME = "commenter";
private final User user1 = new User.UserBuilder("uniqueId1")
.setUsername("user_test_1")
.setEmail("email_test_1")
Expand Down Expand Up @@ -76,6 +79,7 @@ public static void cleanDatabase() {
DataBaseTestUtil.cleanDatabase();
}


@Test
//Tests that the collections are added, retrieved and removed properly
public void addsAndRetrievesCollectionProperly() throws ExecutionException, InterruptedException, TimeoutException {
Expand Down Expand Up @@ -127,4 +131,27 @@ public void addsReviewToCollectionProperly() throws InterruptedException, Execut
&& retrievedCollection.getReviews().containsKey(review2.getMedia().getTitle()));
}

@Test
public void commentsReviewProperly() throws InterruptedException, ExecutionException, TimeoutException {
//Add collection
CollectionsDatabase.addCollection(user1.getUsername(), collection2);
Thread.sleep(1000);

//Add review to the collection
CollectionsDatabase.addReviewToCollection(user1.getUsername(), collection2.getCollectionName(), review1);
Thread.sleep(1000);

//Comment review
Comment comment = new Comment(collection2.getCollectionName(), review1.getMedia().getTitle(), "This is a comment", COMMENTER_USERNAME);
ReviewInteractionDatabase.commentReview(user1.getUsername(), comment);
Thread.sleep(1000);

//Retrieve review
Review retrievedReview = CollectionsDatabase.getCollection(user1.getUsername(), collection2.getCollectionName())
.get(STANDARD_COLLECTION_TIMEOUT, TimeUnit.SECONDS).getReviews().get(review1.getMedia().getTitle());

assertTrue(retrievedReview.getComments().containsKey(COMMENTER_USERNAME));
assertEquals(retrievedReview.getComments().get(COMMENTER_USERNAME), "This is a comment");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class DatabaseUtils {

static final String USERS_PATH = "Users/";
static final String REVIEWS_PATH = "reviews/";
static final String COMMENTS_PATH = "comments/";
static final String USER_COLLECTIONS_PATH = "/collections/";
static final String LOCATION_PATH = "/location/";
static final String FOLLOWING_PATH = "/following/";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.sdp.mediato.data;

import static com.github.sdp.mediato.data.DatabaseUtils.COMMENTS_PATH;
import static com.github.sdp.mediato.data.DatabaseUtils.getReactionReference;
import static com.github.sdp.mediato.data.DatabaseUtils.getReviewReference;

import com.github.sdp.mediato.errorCheck.Preconditions;
import com.github.sdp.mediato.model.Comment;
import com.github.sdp.mediato.model.Review;
import com.github.sdp.mediato.model.post.Reaction;
import com.google.firebase.database.DatabaseReference;
Expand All @@ -17,6 +20,20 @@ public class ReviewInteractionDatabase {

public static FirebaseDatabase database = FirebaseDatabase.getInstance();

/**
* Adds a comment to a review.
* @param tarUsername the username of the user who made the review
* @param comment the comment
*/
public static void commentReview(String tarUsername, Comment comment) {
Preconditions.checkUsername(comment.getRefUsername());
Preconditions.checkUsername(tarUsername);
getReviewReference(tarUsername, comment.getCollectionName(), comment.getReview()).child(COMMENTS_PATH)
.child(comment.getRefUsername())
.setValue(comment.getText())
.addOnCompleteListener(task -> System.out.println("Commented review"));
}

/**
* Determines if the user has liked a given review.
* @param refUsername the username of the user who is checked
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/com/github/sdp/mediato/model/Comment.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.sdp.mediato.model;

/**
* A class that represents a comment on a review
*/
public class Comment {
private String collectionName;
private String review;
private String text;
private String refUsername;

public Comment(String collectionName, String review, String text, String refUsername) {
this.collectionName = collectionName;
this.review = review;
this.text = text;
this.refUsername = refUsername;
}

public String getCollectionName() {
return collectionName;
}

public String getReview() {
return review;
}

public String getText() {
return text;
}

public String getRefUsername() {
return refUsername;
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/com/github/sdp/mediato/model/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Review implements Serializable {
private String comment;
private Map<String, Boolean> likes = new HashMap<>();
private Map<String, Boolean> dislikes = new HashMap<>();
private Map<String, String> comments = new HashMap<>();

private Review() {
}
Expand Down Expand Up @@ -65,6 +66,8 @@ public String getComment() {
return comment;
}

public Map<String, String> getComments() { return comments;}

public void setComment(String comment) {
this.comment = comment;
}
Expand Down Expand Up @@ -101,6 +104,9 @@ public void dislike(String username) {
dislikes.put(username, true);
likes.put(username, false);
}
public void addComment(String username, String comment) {
comments.put(username, comment);
}

@Override
public boolean equals(Object obj) {
Expand All @@ -110,5 +116,4 @@ public boolean equals(Object obj) {
Review other = (Review) obj;
return Objects.equals(this.username, other.username) && Objects.equals(this.media, other.media) && this.grade == other.grade && Objects.equals(this.comment, other.comment);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public String getComment() {
public void unDislike(String username) {this.review.unDislike(username);}
public void like(String username) {this.review.like(username);}
public void dislike(String username) {this.review.dislike(username);}
public void addComment(String username, String comment) {this.review.addComment(username, comment);}

@Override
public boolean equals(@Nullable Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;

import com.github.sdp.mediato.model.media.Collection;
import com.github.sdp.mediato.model.media.CollectionType;
Expand Down Expand Up @@ -62,4 +64,17 @@ public void defaultCollectionConstructorFailsWhenTypeIsCustom(){
() -> new Collection(CollectionType.CUSTOM, reviews)
);
}

@Test
//Tests that comment objects are created properly and added to reviews
public void createsAndAddsCommentsProperly(){
Collection collection = new Collection("MyCustom", reviews);
Review review = reviews.get(SAMPLE_TITLE);
//Comment the review
Comment comment = new Comment(collection.getCollectionName(), review.getMedia().getTitle(), "This is a comment", SAMPLE_USERNAME);
review.addComment(SAMPLE_USERNAME, comment.getText());
//Check that the comment was added
assertTrue(review.getComments().containsKey(SAMPLE_USERNAME));
assertEquals(review.getComments().get(SAMPLE_USERNAME), "This is a comment");
}
}

0 comments on commit c92bdd0

Please sign in to comment.