From 2d76e09e9fa5256e418a1e03cff1ecc6a6fab445 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Wed, 21 Nov 2018 13:59:29 +0200 Subject: [PATCH 1/3] NY-4778: Changes in managing of contact info's label - changed managing of contact info not to use the given label as a factor of uniqueness; - fixed unit test; Signed-off-by: Stanimir Penkov --- .../AccountRepositoryAdditionalImpl.java | 84 ++++++++++++++++--- .../account/services/AccountServiceTests.java | 2 +- 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 265d52f..966c1e6 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -569,14 +569,24 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio logger.error("Existing account with the provided id {} was not found.", accountId); return false; } + if (contactInfo == null) { + logger.error("Error adding contact info to account {}. contactInfo is not set.", accountId); + return false; + } Set contactsInfoSet = accountToUpdate.getContactsInfo(); if (contactsInfoSet == null) { contactsInfoSet = new HashSet(); } - if (contactInfo != null) { + + if (foundExistingContactInfo(contactInfo, contactsInfoSet)) { + logger.error("Error adding contact info to account {}. {}:{} already exists.", accountId, + contactInfo.getType(), contactInfo.getValue()); + return false; + } else { + setEmptyLabelExceptPhone(contactInfo); if (!contactsInfoSet.add(contactInfo)) { - logger.error("Error adding contact info to account {}. {} already exists.", accountId, - contactInfo.toString()); + logger.error("Error adding contact info {}:{} to account {}.", contactInfo.getType(), + contactInfo.getValue(), accountId); return false; } accountToUpdate.setContactsInfo(contactsInfoSet); @@ -590,6 +600,36 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return false; } + private boolean deletedExistingContactInfo(ContactInfo contactInfo, Set contactsInfoSet) { + if (contactInfo == null || contactsInfoSet == null || contactsInfoSet.size() == 0) { + return false; + } + for (ContactInfo c : contactsInfoSet) { + if (c.getType().equals(contactInfo.getType()) && c.getValue().equals(contactInfo.getValue())) { + return contactsInfoSet.remove(c); + } + } + return false; + } + + private void setEmptyLabelExceptPhone(ContactInfo contactInfo) { + if (!contactInfo.getType().equals(ContactType.PHONE_CONTACT.toString())) { + contactInfo.setLabel(""); + } + } + + private boolean foundExistingContactInfo(ContactInfo contactInfo, Set contactsInfoSet) { + if (contactsInfoSet == null || contactsInfoSet.size() == 0) { + return false; + } + for (ContactInfo c : contactsInfoSet) { + if (c.getType().equals(contactInfo.getType()) && c.getValue().equals(contactInfo.getValue())) { + return true; + } + } + return false; + } + @Override public boolean deleteContactInfo(UUID accountId, ContactInfo contactInfo) { Account accountToUpdate = accountRepository.findByAccountId(accountId); @@ -597,14 +637,19 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio logger.error("Existing account with the provided id {} was not found.", accountId); return false; } + if (contactInfo == null) { + logger.error("Error removing contact info from account {}. contactInfo is not set.", accountId); + return false; + } Set contactsInfoSet = accountToUpdate.getContactsInfo(); if (contactsInfoSet == null) { logger.error("No existing contact info found for account {}.", accountId); return false; } - if (!contactsInfoSet.remove(contactInfo)) { - logger.error("Error removing contact info from account {}. Existing contact info: {} was not found.", - accountId, contactInfo.toString()); + + if (!deletedExistingContactInfo(contactInfo, contactsInfoSet)) { + logger.error("Error removing contact info from account {}. Existing contact info: {}:{} was not found.", + accountId, contactInfo.getType(), contactInfo.getValue()); return false; } accountToUpdate.setContactsInfo(contactsInfoSet); @@ -661,21 +706,36 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio logger.error("Existing account with the provided id {} was not found.", accountId); return false; } + if (oldContactInfo == null || editedContactInfo == null) { + logger.error("Error editing contact info for account {}. oldContactInfo/editedContactInfo is not set.", + accountId); + return false; + } Set contactsInfoSet = accountToUpdate.getContactsInfo(); if (contactsInfoSet == null) { logger.error("No existing contact info found for account {}.", accountId); return false; } - if (!contactsInfoSet.remove(oldContactInfo)) { - logger.error("Error removing contact info from account {}. Existing contact info: {} was not found.", - accountId, oldContactInfo.toString()); + + if (!deletedExistingContactInfo(oldContactInfo, contactsInfoSet)) { + logger.error("Error removing contact info from account {}. Existing contact info: {}:{} was not found.", + accountId, oldContactInfo.getType(), oldContactInfo.getValue()); return false; } - if (!contactsInfoSet.add(editedContactInfo)) { - logger.error("Error adding contact info to account {}. {} already exists.", accountId, - editedContactInfo.toString()); + + if (foundExistingContactInfo(editedContactInfo, contactsInfoSet)) { + logger.error("Error adding contact info to account {}. {}:{} already exists.", accountId, + editedContactInfo.getType(), editedContactInfo.getValue()); return false; + } else { + setEmptyLabelExceptPhone(editedContactInfo); + if (!contactsInfoSet.add(editedContactInfo)) { + logger.error("Error adding contact info {}:{} to account {}.", editedContactInfo.getType(), + editedContactInfo.getValue(), accountId); + return false; + } } + accountToUpdate.setContactsInfo(contactsInfoSet); Long timeUpdated = Instant.now().toEpochMilli(); accountToUpdate.setLastUpdateTimestamp(timeUpdated); diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 8b36171..654c85f 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -1255,7 +1255,7 @@ public class AccountServiceTests extends GrpcServerTestBase { .setContactInfo( ContactDetails.newBuilder().setType(ContactType.EMAIL_CONTACT).setValue(Util.EMAIL).build()) .build(); - given(accountRepositoryAdditional.addContactInfo(Mockito.any(UUID.class), Mockito.any(ContactInfo.class))) + given(accountRepositoryAdditional.deleteContactInfo(Mockito.any(UUID.class), Mockito.any(ContactInfo.class))) .willReturn(false); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc -- GitLab From cb96c9b905f5b6b4173d322e10c66d801ed25aed Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Wed, 21 Nov 2018 14:25:21 +0200 Subject: [PATCH 2/3] NY-4778: Add needed import Signed-off-by: Stanimir Penkov --- .../account/repositories/AccountRepositoryAdditionalImpl.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 966c1e6..4fb125f 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -32,6 +32,7 @@ import biz.nynja.account.components.StatementsPool; import biz.nynja.account.components.Validator; import biz.nynja.account.grpc.AccessStatus; import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.ContactType; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.models.Account; import biz.nynja.account.models.AccountByAuthenticationProvider; -- GitLab From 84d05d2a9c100908b497b977d233de430eb13124 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Wed, 21 Nov 2018 17:04:35 +0200 Subject: [PATCH 3/3] NY-4778: Code Review Feedback Signed-off-by: Stanimir Penkov --- .../AccountRepositoryAdditionalImpl.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 4fb125f..5820442 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -565,15 +565,15 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio @Override public boolean addContactInfo(UUID accountId, ContactInfo contactInfo) { + if (contactInfo == null) { + logger.error("Error adding contact info to account {}. contactInfo is not set.", accountId); + return false; + } Account accountToUpdate = accountRepository.findByAccountId(accountId); if (accountToUpdate == null) { logger.error("Existing account with the provided id {} was not found.", accountId); return false; } - if (contactInfo == null) { - logger.error("Error adding contact info to account {}. contactInfo is not set.", accountId); - return false; - } Set contactsInfoSet = accountToUpdate.getContactsInfo(); if (contactsInfoSet == null) { contactsInfoSet = new HashSet(); @@ -633,15 +633,15 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio @Override public boolean deleteContactInfo(UUID accountId, ContactInfo contactInfo) { + if (contactInfo == null) { + logger.error("Error removing contact info from account {}. contactInfo is not set.", accountId); + return false; + } Account accountToUpdate = accountRepository.findByAccountId(accountId); if (accountToUpdate == null) { logger.error("Existing account with the provided id {} was not found.", accountId); return false; } - if (contactInfo == null) { - logger.error("Error removing contact info from account {}. contactInfo is not set.", accountId); - return false; - } Set contactsInfoSet = accountToUpdate.getContactsInfo(); if (contactsInfoSet == null) { logger.error("No existing contact info found for account {}.", accountId); @@ -702,16 +702,16 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio @Override public boolean editContactInfo(UUID accountId, ContactInfo oldContactInfo, ContactInfo editedContactInfo) { - Account accountToUpdate = accountRepository.findByAccountId(accountId); - if (accountToUpdate == null) { - logger.error("Existing account with the provided id {} was not found.", accountId); - return false; - } if (oldContactInfo == null || editedContactInfo == null) { logger.error("Error editing contact info for account {}. oldContactInfo/editedContactInfo is not set.", accountId); return false; } + Account accountToUpdate = accountRepository.findByAccountId(accountId); + if (accountToUpdate == null) { + logger.error("Existing account with the provided id {} was not found.", accountId); + return false; + } Set contactsInfoSet = accountToUpdate.getContactsInfo(); if (contactsInfoSet == null) { logger.error("No existing contact info found for account {}.", accountId); -- GitLab