Skip to content

Commit

Permalink
Fix ForumDaoImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
santidrj committed Jun 5, 2020
1 parent 9907878 commit e77cc41
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,8 @@ private void changeNameInTags(String currentName, String newName, WriteBatch bat
*/
private void deleteForumFromTag(String tag, String forum, WriteBatch batch) {
documentAdapter
.updateDocumentFields(Path.ofDocument(Collections.tags, tag), FORUMS_FIELD, FieldValue.arrayRemove(forum),
batch);
.updateDocumentFields(batch, Path.ofDocument(Collections.tags, tag), FORUMS_FIELD,
FieldValue.arrayRemove(forum));
}

/**
Expand All @@ -392,8 +392,8 @@ private void deleteForumFromTag(String tag, String forum, WriteBatch batch) {
*/
private void addNewTags(String groupId, String forumId, String forumName, List<String> newTags, WriteBatch batch)
throws DatabaseAccessException {
documentAdapter.updateDocumentFields(Path.ofDocument(Collections.forums, groupId, forumId), TAGS_FIELD,
FieldValue.arrayUnion(newTags.toArray()), batch);
documentAdapter.updateDocumentFields(batch, Path.ofDocument(Collections.forums, groupId, forumId), TAGS_FIELD,
FieldValue.arrayUnion(newTags.toArray()));
for (String tag : newTags) {
addForumToTag(tag, forumName, batch);
}
Expand All @@ -410,8 +410,8 @@ private void addNewTags(String groupId, String forumId, String forumName, List<S
*/
private void removeDeletedTags(String groupId, String forumId, String forumName, List<String> deletedTags,
WriteBatch batch) {
documentAdapter.updateDocumentFields(Path.ofDocument(Collections.forums, groupId, forumId), TAGS_FIELD,
FieldValue.arrayRemove(deletedTags.toArray()), batch);
documentAdapter.updateDocumentFields(batch, Path.ofDocument(Collections.forums, groupId, forumId), TAGS_FIELD,
FieldValue.arrayRemove(deletedTags.toArray()));
for (String tag : deletedTags) {
deleteForumFromTag(tag, forumName, batch);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class UserDaoImpl implements UserDao {
private static final String UPDATE_FAILED_CODE = "update-failed";
private static final String FIELD_LIKED_BY = "likedBy";
private static final String FCM = "FCM";
private static final String NOTIFICATIONS_FIELD = "notification-tokens";
private static final String WRITE_FAILED_CODE = "write-failed";
private FirebaseAuth myAuth;
private CollectionReference users;
Expand Down Expand Up @@ -493,9 +494,14 @@ private void updateTokenInSubscribedGroups(String token, String currentToken, Wr
ApiFuture<QuerySnapshot> subscribedGroups = collectionAdapter
.getDocumentsWhereArrayContains(Path.ofCollection(Collections.groups), "notification-tokens", currentToken);
try {
List<String> tokens;
for (QueryDocumentSnapshot group : subscribedGroups.get().getDocuments()) {
batch.update(group.getReference(), FCM, FieldValue.arrayRemove(currentToken), FCM,
FieldValue.arrayUnion(token));
tokens = (List<String>) group.get(NOTIFICATIONS_FIELD);
if (tokens != null) {
tokens.remove(currentToken);
tokens.add(token);
batch.update(group.getReference(), NOTIFICATIONS_FIELD, tokens);
}
}
} catch (InterruptedException e) {
throw new DatabaseAccessException(WRITE_FAILED_CODE, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,19 @@ public void createForum() throws DatabaseAccessException, DocumentException {
public void updateTags() throws DatabaseAccessException, DocumentException {
mockGetGroupAndForumIds();
willDoNothing().given(documentAdapter)
.updateDocumentFields(anyString(), anyString(), any(FieldValue.class), any(WriteBatch.class));
.updateDocumentFields(any(WriteBatch.class), anyString(), anyString(), any(FieldValue.class));
mockAddForumToTag();

dao.updateTags(groupName, forumName, tags, tags);
verify(documentAdapter)
.updateDocumentFields(eq(forumPath), eq("tags"), eq(FieldValue.arrayRemove(tags.toArray())),
same(batch));
.updateDocumentFields(same(batch), eq(forumPath), eq("tags"),
eq(FieldValue.arrayRemove(tags.toArray())));
verify(documentAdapter)
.updateDocumentFields(eq(tagPath), eq("forums"), eq(FieldValue.arrayRemove(forumName)),
same(batch));
.updateDocumentFields(same(batch), eq(tagPath), eq("forums"),
eq(FieldValue.arrayRemove(forumName)));
verify(documentAdapter)
.updateDocumentFields(eq(forumPath), eq("tags"), eq(FieldValue.arrayUnion(tags.toArray())),
same(batch));
.updateDocumentFields(same(batch), eq(forumPath), eq("tags"),
eq(FieldValue.arrayUnion(tags.toArray())));
verifyAddForumToTag(forumName);
}

Expand Down Expand Up @@ -371,7 +371,7 @@ public void updateName() throws DatabaseAccessException, DocumentException {
.updateDocumentFields(anyString(), anyString(), anyString(), any(WriteBatch.class));
willDoNothing().given(documentAdapter).deleteDocument(anyString(), any(WriteBatch.class));
willDoNothing().given(documentAdapter)
.updateDocumentFields(anyString(), anyString(), any(FieldValue.class), any(WriteBatch.class));
.updateDocumentFields(any(WriteBatch.class), anyString(), anyString(), any(FieldValue.class));
mockAddForumToTag();
given(
documentAdapter.createDocumentWithId(anyString(), anyString(), anyMap(), any(WriteBatch.class)))
Expand Down

0 comments on commit e77cc41

Please sign in to comment.