From 1a3b3162462c373910b6d4dbcad9ff12f734419d Mon Sep 17 00:00:00 2001 From: oricat8 Date: Fri, 5 Jun 2020 11:32:17 +0200 Subject: [PATCH 1/3] Check done --- .../webservice/dao/medalmanager/UserMedalDaoImpl.java | 11 +++++++++++ .../service/medalmanager/UserMedalServiceImpl.java | 1 - 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/pesmypetcare/webservice/dao/medalmanager/UserMedalDaoImpl.java b/src/main/java/org/pesmypetcare/webservice/dao/medalmanager/UserMedalDaoImpl.java index ee02bd2c..077c1acb 100644 --- a/src/main/java/org/pesmypetcare/webservice/dao/medalmanager/UserMedalDaoImpl.java +++ b/src/main/java/org/pesmypetcare/webservice/dao/medalmanager/UserMedalDaoImpl.java @@ -64,6 +64,7 @@ public List getAllUserMedalsData(String owner) throws DatabaseA @Override public void updateField(String owner, String name, String field, Object value) throws DatabaseAccessException, DocumentException { + checkField(field); initializeWithDocumentPath(owner, name); dbDoc.updateDocumentFields(batch, path, field, value); batch.commit(); @@ -128,4 +129,14 @@ private String getUserId(String username) throws DatabaseAccessException, Docume return dbDoc.getStringFromDocument(usernamePath, "user"); } + /** + * This method check that the field are allowed to update. + * @param field The value of the field. + */ + private void checkField(String field) { + if (!("progress".equals(field) || "currentLevel".equals(field) || "completedLevelsDate".equals(field))) { + throw new IllegalArgumentException("Field not allowed to update."); + } + } + } diff --git a/src/main/java/org/pesmypetcare/webservice/service/medalmanager/UserMedalServiceImpl.java b/src/main/java/org/pesmypetcare/webservice/service/medalmanager/UserMedalServiceImpl.java index 9db4d5b2..0985cabe 100644 --- a/src/main/java/org/pesmypetcare/webservice/service/medalmanager/UserMedalServiceImpl.java +++ b/src/main/java/org/pesmypetcare/webservice/service/medalmanager/UserMedalServiceImpl.java @@ -8,7 +8,6 @@ import org.springframework.stereotype.Service; import java.util.List; -import java.util.Map; /** * @author Oriol Catalán From 99078789d19d8b889675b43524bb367cc35ab393 Mon Sep 17 00:00:00 2001 From: oricat8 Date: Fri, 5 Jun 2020 11:57:02 +0200 Subject: [PATCH 2/3] Check done --- .../webservice/entity/medalmanager/UserMedalEntity.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/pesmypetcare/webservice/entity/medalmanager/UserMedalEntity.java b/src/main/java/org/pesmypetcare/webservice/entity/medalmanager/UserMedalEntity.java index 4dfb8334..0c9b2cd6 100644 --- a/src/main/java/org/pesmypetcare/webservice/entity/medalmanager/UserMedalEntity.java +++ b/src/main/java/org/pesmypetcare/webservice/entity/medalmanager/UserMedalEntity.java @@ -65,8 +65,8 @@ public static void checkFieldAndValues(String field, Object newValue) { && !(newValue instanceof String)) { throw new IllegalArgumentException("New value must be a String"); } else if ((field.equals(PROGRESS) || field.equals(CURRENT_LEVEL) || field.equals(LEVELS)) - && !(newValue instanceof Double)) { - throw new IllegalArgumentException("New value must be a Double"); + && !(newValue instanceof Double || newValue instanceof Integer)) { + throw new IllegalArgumentException("New value must be a Double or Integer"); } } From e77cc41d63fc2483690467185872f2098eb0a331 Mon Sep 17 00:00:00 2001 From: santidrj Date: Fri, 5 Jun 2020 12:34:22 +0200 Subject: [PATCH 3/3] Fix ForumDaoImpl --- .../dao/communitymanager/ForumDaoImpl.java | 12 ++++++------ .../webservice/dao/usermanager/UserDaoImpl.java | 10 ++++++++-- .../dao/communitymanager/ForumDaoTest.java | 16 ++++++++-------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/pesmypetcare/webservice/dao/communitymanager/ForumDaoImpl.java b/src/main/java/org/pesmypetcare/webservice/dao/communitymanager/ForumDaoImpl.java index d6ba1fbc..71eb83bc 100644 --- a/src/main/java/org/pesmypetcare/webservice/dao/communitymanager/ForumDaoImpl.java +++ b/src/main/java/org/pesmypetcare/webservice/dao/communitymanager/ForumDaoImpl.java @@ -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)); } /** @@ -392,8 +392,8 @@ private void deleteForumFromTag(String tag, String forum, WriteBatch batch) { */ private void addNewTags(String groupId, String forumId, String forumName, List 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); } @@ -410,8 +410,8 @@ private void addNewTags(String groupId, String forumId, String forumName, List 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); } diff --git a/src/main/java/org/pesmypetcare/webservice/dao/usermanager/UserDaoImpl.java b/src/main/java/org/pesmypetcare/webservice/dao/usermanager/UserDaoImpl.java index befcb93d..488f4c05 100644 --- a/src/main/java/org/pesmypetcare/webservice/dao/usermanager/UserDaoImpl.java +++ b/src/main/java/org/pesmypetcare/webservice/dao/usermanager/UserDaoImpl.java @@ -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; @@ -493,9 +494,14 @@ private void updateTokenInSubscribedGroups(String token, String currentToken, Wr ApiFuture subscribedGroups = collectionAdapter .getDocumentsWhereArrayContains(Path.ofCollection(Collections.groups), "notification-tokens", currentToken); try { + List tokens; for (QueryDocumentSnapshot group : subscribedGroups.get().getDocuments()) { - batch.update(group.getReference(), FCM, FieldValue.arrayRemove(currentToken), FCM, - FieldValue.arrayUnion(token)); + tokens = (List) 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()); diff --git a/src/test/java/org/pesmypetcare/webservice/dao/communitymanager/ForumDaoTest.java b/src/test/java/org/pesmypetcare/webservice/dao/communitymanager/ForumDaoTest.java index 8a2420af..59930abe 100644 --- a/src/test/java/org/pesmypetcare/webservice/dao/communitymanager/ForumDaoTest.java +++ b/src/test/java/org/pesmypetcare/webservice/dao/communitymanager/ForumDaoTest.java @@ -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); } @@ -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)))