From 4167193307053b4e5394ce574e984cea8ab1b531 Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Mon, 30 Jul 2018 17:35:53 +0300 Subject: [PATCH 01/37] NY-2215: Implement account models hierarchy Created models: - Profile; - UserAccount; - SociacIdentity; - CommunicationProvoder. Signed-off-by: Ralitsa Todorova --- .../grpc/models/CommunicationProvider.java | 93 ++++++++ .../nynja/account/grpc/models/Profile.java | 109 +++++++++ .../account/grpc/models/SocialIdentity.java | 93 ++++++++ .../biz/nynja/account/grpc/models/Status.java | 8 + .../account/grpc/models/UserAccount.java | 207 ++++++++++++++++++ 5 files changed, 510 insertions(+) create mode 100644 src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/Profile.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/Status.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/UserAccount.java diff --git a/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java new file mode 100644 index 0000000..5c444b9 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java @@ -0,0 +1,93 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.util.UUID; + +public class CommunicationProvider { + + private UUID providerId; + private Long creationTimestamp; + private Long lastUpdateTimestamp; + private Status status; + + public UUID getProviderId() { + return providerId; + } + + public void setProviderId(UUID providerId) { + this.providerId = providerId; + } + + public Long getCreationTimestamp() { + return creationTimestamp; + } + + public void setCreationTimestamp(Long creationTimestamp) { + this.creationTimestamp = creationTimestamp; + } + + public Long getLastUpdateTimestamp() { + return lastUpdateTimestamp; + } + + public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { + this.lastUpdateTimestamp = lastUpdateTimestamp; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); + result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); + result = prime * result + ((providerId == null) ? 0 : providerId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CommunicationProvider other = (CommunicationProvider) obj; + if (creationTimestamp == null) { + if (other.creationTimestamp != null) + return false; + } else if (!creationTimestamp.equals(other.creationTimestamp)) + return false; + if (lastUpdateTimestamp == null) { + if (other.lastUpdateTimestamp != null) + return false; + } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) + return false; + if (providerId == null) { + if (other.providerId != null) + return false; + } else if (!providerId.equals(other.providerId)) + return false; + if (status != other.status) + return false; + return true; + } + + @Override + public String toString() { + return "CommunicationProvider [providerId=" + providerId + ", creationTimestamp=" + creationTimestamp + + ", lastUpdateTimestamp=" + lastUpdateTimestamp + ", status=" + status + "]"; + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/Profile.java b/src/main/java/biz/nynja/account/grpc/models/Profile.java new file mode 100644 index 0000000..70dccd7 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/Profile.java @@ -0,0 +1,109 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.util.List; +import java.util.UUID; + +public class Profile { + + private UUID profileId; + private Long creationTimestamp; + private Long lastUpdateTimestamp; + private Status status; + List userAccounts; + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + public Long getCreationTimestamp() { + return creationTimestamp; + } + + public void setCreationTimestamp(Long creationTimestamp) { + this.creationTimestamp = creationTimestamp; + } + + public Long getLastUpdateTimestamp() { + return lastUpdateTimestamp; + } + + public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { + this.lastUpdateTimestamp = lastUpdateTimestamp; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public List getUserAccounts() { + return userAccounts; + } + + public void setUserAccounts(List userAccounts) { + this.userAccounts = userAccounts; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); + result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + result = prime * result + ((userAccounts == null) ? 0 : userAccounts.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Profile other = (Profile) obj; + if (creationTimestamp == null) { + if (other.creationTimestamp != null) + return false; + } else if (!creationTimestamp.equals(other.creationTimestamp)) + return false; + if (lastUpdateTimestamp == null) { + if (other.lastUpdateTimestamp != null) + return false; + } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + if (status != other.status) + return false; + if (userAccounts == null) { + if (other.userAccounts != null) + return false; + } else if (!userAccounts.equals(other.userAccounts)) + return false; + return true; + } + + @Override + public String toString() { + return "Profile [profileId=" + profileId + ", creationTimestamp=" + creationTimestamp + ", lastUpdateTimestamp=" + + lastUpdateTimestamp + ", status=" + status + ", userAccounts=" + userAccounts + "]"; + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java new file mode 100644 index 0000000..00c6dae --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java @@ -0,0 +1,93 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.util.UUID; + +public class SocialIdentity { + + private UUID socialIdentityId; + private Long creationTimestamp; + private Long lastUpdateTimestamp; + private Status status; + + public UUID getSocialIdentityId() { + return socialIdentityId; + } + + public void setSocialIdentityId(UUID socialIdentityId) { + this.socialIdentityId = socialIdentityId; + } + + public Long getCreationTimestamp() { + return creationTimestamp; + } + + public void setCreationTimestamp(Long creationTimestamp) { + this.creationTimestamp = creationTimestamp; + } + + public Long getLastUpdateTimestamp() { + return lastUpdateTimestamp; + } + + public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { + this.lastUpdateTimestamp = lastUpdateTimestamp; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); + result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); + result = prime * result + ((socialIdentityId == null) ? 0 : socialIdentityId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SocialIdentity other = (SocialIdentity) obj; + if (creationTimestamp == null) { + if (other.creationTimestamp != null) + return false; + } else if (!creationTimestamp.equals(other.creationTimestamp)) + return false; + if (lastUpdateTimestamp == null) { + if (other.lastUpdateTimestamp != null) + return false; + } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) + return false; + if (socialIdentityId == null) { + if (other.socialIdentityId != null) + return false; + } else if (!socialIdentityId.equals(other.socialIdentityId)) + return false; + if (status != other.status) + return false; + return true; + } + + @Override + public String toString() { + return "SocialIdentity [socialIdentityId=" + socialIdentityId + ", creationTimestamp=" + creationTimestamp + + ", lastUpdateTimestamp=" + lastUpdateTimestamp + ", status=" + status + "]"; + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/Status.java b/src/main/java/biz/nynja/account/grpc/models/Status.java new file mode 100644 index 0000000..ff6d639 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/Status.java @@ -0,0 +1,8 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +public enum Status { + ACTIVE, SUSPENDED, TERMINATED +} diff --git a/src/main/java/biz/nynja/account/grpc/models/UserAccount.java b/src/main/java/biz/nynja/account/grpc/models/UserAccount.java new file mode 100644 index 0000000..8a0e9b5 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/UserAccount.java @@ -0,0 +1,207 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.util.List; +import java.util.UUID; + +import javax.validation.constraints.Size; + +public class UserAccount { + + private UUID accountId; + @Size(max = 256) + private String firstName; + @Size(max = 256) + private String lastName; + @Size(max = 256) + private String middleName; + @Size(max = 256) + private String username; + private String password; + private Long creationTimestamp; + private Long lastUpdateTimestamp; + private Status status; + private List socialIdentities; + private List communicationProviders; + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getMiddleName() { + return middleName; + } + + public void setMiddleName(String middleName) { + this.middleName = middleName; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public Long getCreationTimestamp() { + return creationTimestamp; + } + + public void setCreationTimestamp(Long creationTimestamp) { + this.creationTimestamp = creationTimestamp; + } + + public Long getLastUpdateTimestamp() { + return lastUpdateTimestamp; + } + + public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { + this.lastUpdateTimestamp = lastUpdateTimestamp; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public List getSocialIdentities() { + return socialIdentities; + } + + public void setSocialIdentities(List socialIdentities) { + this.socialIdentities = socialIdentities; + } + + public List getCommunicationProviders() { + return communicationProviders; + } + + public void setCommunicationProviders(List communicationProviders) { + this.communicationProviders = communicationProviders; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((communicationProviders == null) ? 0 : communicationProviders.hashCode()); + result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); + result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); + result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); + result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); + result = prime * result + ((middleName == null) ? 0 : middleName.hashCode()); + result = prime * result + ((password == null) ? 0 : password.hashCode()); + result = prime * result + ((socialIdentities == null) ? 0 : socialIdentities.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + UserAccount other = (UserAccount) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (communicationProviders == null) { + if (other.communicationProviders != null) + return false; + } else if (!communicationProviders.equals(other.communicationProviders)) + return false; + if (creationTimestamp == null) { + if (other.creationTimestamp != null) + return false; + } else if (!creationTimestamp.equals(other.creationTimestamp)) + return false; + if (firstName == null) { + if (other.firstName != null) + return false; + } else if (!firstName.equals(other.firstName)) + return false; + if (lastName == null) { + if (other.lastName != null) + return false; + } else if (!lastName.equals(other.lastName)) + return false; + if (lastUpdateTimestamp == null) { + if (other.lastUpdateTimestamp != null) + return false; + } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) + return false; + if (middleName == null) { + if (other.middleName != null) + return false; + } else if (!middleName.equals(other.middleName)) + return false; + if (password == null) { + if (other.password != null) + return false; + } else if (!password.equals(other.password)) + return false; + if (socialIdentities == null) { + if (other.socialIdentities != null) + return false; + } else if (!socialIdentities.equals(other.socialIdentities)) + return false; + if (status != other.status) + return false; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + + @Override + public String toString() { + return "UserAccount [accountId=" + accountId + ", firstName=" + firstName + ", lastName=" + lastName + + ", middleName=" + middleName + ", username=" + username + ", password=[]" + ", creationTimestamp=" + + creationTimestamp + ", lastUpdateTimestamp=" + lastUpdateTimestamp + ", status=" + status + + ", socialIdentities=" + socialIdentities + ", communicationProviders=" + communicationProviders + "]"; + } + +} -- GitLab From 9fe01b8c24dbe554a21f3d3482f06d4746e60d5a Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Tue, 31 Jul 2018 15:10:02 +0300 Subject: [PATCH 02/37] NY-2215: Use StringBuilder in models' toString() methods Signed-off-by: Ralitsa Todorova --- .../account/grpc/models/CommunicationProvider.java | 5 +++-- .../java/biz/nynja/account/grpc/models/Profile.java | 6 ++++-- .../biz/nynja/account/grpc/models/SocialIdentity.java | 5 +++-- .../biz/nynja/account/grpc/models/UserAccount.java | 10 ++++++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java index 5c444b9..ad76dbe 100644 --- a/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java +++ b/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java @@ -86,8 +86,9 @@ public class CommunicationProvider { @Override public String toString() { - return "CommunicationProvider [providerId=" + providerId + ", creationTimestamp=" + creationTimestamp - + ", lastUpdateTimestamp=" + lastUpdateTimestamp + ", status=" + status + "]"; + return new StringBuilder("CommunicationProvider [providerId=").append(providerId).append(", creationTimestamp=") + .append(creationTimestamp).append(", lastUpdateTimestamp=").append(lastUpdateTimestamp) + .append(", status=").append(status).append("]").toString(); } } diff --git a/src/main/java/biz/nynja/account/grpc/models/Profile.java b/src/main/java/biz/nynja/account/grpc/models/Profile.java index 70dccd7..c07c871 100644 --- a/src/main/java/biz/nynja/account/grpc/models/Profile.java +++ b/src/main/java/biz/nynja/account/grpc/models/Profile.java @@ -102,8 +102,10 @@ public class Profile { @Override public String toString() { - return "Profile [profileId=" + profileId + ", creationTimestamp=" + creationTimestamp + ", lastUpdateTimestamp=" - + lastUpdateTimestamp + ", status=" + status + ", userAccounts=" + userAccounts + "]"; + return new StringBuilder("Profile [profileId=").append(profileId).append(", creationTimestamp=") + .append(creationTimestamp).append(", lastUpdateTimestamp=").append(lastUpdateTimestamp) + .append(", status=").append(status).append(", userAccounts=").append(userAccounts).append("]") + .toString(); } } diff --git a/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java index 00c6dae..702969b 100644 --- a/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java +++ b/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java @@ -86,8 +86,9 @@ public class SocialIdentity { @Override public String toString() { - return "SocialIdentity [socialIdentityId=" + socialIdentityId + ", creationTimestamp=" + creationTimestamp - + ", lastUpdateTimestamp=" + lastUpdateTimestamp + ", status=" + status + "]"; + return new StringBuilder("SocialIdentity [socialIdentityId=").append(socialIdentityId) + .append(", creationTimestamp=").append(creationTimestamp).append(", lastUpdateTimestamp=") + .append(lastUpdateTimestamp).append(", status=").append(status).append("]").toString(); } } diff --git a/src/main/java/biz/nynja/account/grpc/models/UserAccount.java b/src/main/java/biz/nynja/account/grpc/models/UserAccount.java index 8a0e9b5..8917c07 100644 --- a/src/main/java/biz/nynja/account/grpc/models/UserAccount.java +++ b/src/main/java/biz/nynja/account/grpc/models/UserAccount.java @@ -198,10 +198,12 @@ public class UserAccount { @Override public String toString() { - return "UserAccount [accountId=" + accountId + ", firstName=" + firstName + ", lastName=" + lastName - + ", middleName=" + middleName + ", username=" + username + ", password=[]" + ", creationTimestamp=" - + creationTimestamp + ", lastUpdateTimestamp=" + lastUpdateTimestamp + ", status=" + status - + ", socialIdentities=" + socialIdentities + ", communicationProviders=" + communicationProviders + "]"; + return new StringBuilder("UserAccount [accountId=").append(accountId).append(", firstName=").append(firstName) + .append(", lastName=").append(lastName).append(", middleName=").append(middleName).append(", username=") + .append(username).append(", password=[]").append(", creationTimestamp=").append(creationTimestamp) + .append(", lastUpdateTimestamp=").append(lastUpdateTimestamp).append(", status=").append(status) + .append(", socialIdentities=").append(socialIdentities).append(", communicationProviders=") + .append(communicationProviders).append("]").toString(); } } -- GitLab From 6e5a17730aaa52ad19ce428b0ecd479d7d519103 Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Tue, 31 Jul 2018 15:13:31 +0300 Subject: [PATCH 03/37] NY-2215: Implement account models hierarchy (part 2) Created models: - EmailSocialIdentity; - FacebookSocialIdentity; - PhoneSocialIdentity; - FacebookCommunicationProvider; - PhoneCommunicationProvider; - UsernameCommunicationProvider. Signed-off-by: Ralitsa Todorova --- .../grpc/models/EmailSocialIdentity.java | 51 +++++++++++ .../models/FacebookCommunicationProvider.java | 82 ++++++++++++++++++ .../grpc/models/FacebookSocialIdentity.java | 85 +++++++++++++++++++ .../models/PhoneCommunicationProvider.java | 67 +++++++++++++++ .../grpc/models/PhoneSocialIdentity.java | 67 +++++++++++++++ .../models/UsernameCommunicationProvider.java | 50 +++++++++++ 6 files changed, 402 insertions(+) create mode 100644 src/main/java/biz/nynja/account/grpc/models/EmailSocialIdentity.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/FacebookCommunicationProvider.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/FacebookSocialIdentity.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/PhoneCommunicationProvider.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/PhoneSocialIdentity.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/UsernameCommunicationProvider.java diff --git a/src/main/java/biz/nynja/account/grpc/models/EmailSocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/EmailSocialIdentity.java new file mode 100644 index 0000000..c00f524 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/EmailSocialIdentity.java @@ -0,0 +1,51 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +public class EmailSocialIdentity extends SocialIdentity { + + private String email; + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((email == null) ? 0 : email.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + EmailSocialIdentity other = (EmailSocialIdentity) obj; + if (email == null) { + if (other.email != null) + return false; + } else if (!email.equals(other.email)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("EmailSocialIdentity [email=").append(email).append(", socialIdentityId=") + .append(this.getSocialIdentityId()).append(", creationTimestamp=").append(this.getCreationTimestamp()) + .append(", lastUpdateTimestamp=").append(this.getLastUpdateTimestamp()).append(", status=") + .append(this.getStatus()).append("]").toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/FacebookCommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/FacebookCommunicationProvider.java new file mode 100644 index 0000000..2260123 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/FacebookCommunicationProvider.java @@ -0,0 +1,82 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +public class FacebookCommunicationProvider extends CommunicationProvider { + + private String facebookId; + private String token; + private Long tokenExpirationTimestamp; + + public String getFacebookId() { + return facebookId; + } + + public void setFacebookId(String facebookId) { + this.facebookId = facebookId; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public Long getTokenExpirationTimestamp() { + return tokenExpirationTimestamp; + } + + public void setTokenExpirationTimestamp(Long tokenExpirationTimestamp) { + this.tokenExpirationTimestamp = tokenExpirationTimestamp; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((facebookId == null) ? 0 : facebookId.hashCode()); + result = prime * result + ((token == null) ? 0 : token.hashCode()); + result = prime * result + ((tokenExpirationTimestamp == null) ? 0 : tokenExpirationTimestamp.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + FacebookCommunicationProvider other = (FacebookCommunicationProvider) obj; + if (facebookId == null) { + if (other.facebookId != null) + return false; + } else if (!facebookId.equals(other.facebookId)) + return false; + if (token == null) { + if (other.token != null) + return false; + } else if (!token.equals(other.token)) + return false; + if (tokenExpirationTimestamp == null) { + if (other.tokenExpirationTimestamp != null) + return false; + } else if (!tokenExpirationTimestamp.equals(other.tokenExpirationTimestamp)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("FacebookCommunicationProvider [facebookId=").append(facebookId).append(", token=") + .append(token).append(", tokenExpirationTimestamp=").append(tokenExpirationTimestamp) + .append(", providerId=").append(this.getProviderId()).append(", creationTimestamp=") + .append(this.getCreationTimestamp()).append(", lastUpdateTimestamp=") + .append(this.getLastUpdateTimestamp()).append(", status=").append(this.getStatus()).append("]") + .toString(); + } +} diff --git a/src/main/java/biz/nynja/account/grpc/models/FacebookSocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/FacebookSocialIdentity.java new file mode 100644 index 0000000..73f6098 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/FacebookSocialIdentity.java @@ -0,0 +1,85 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +public class FacebookSocialIdentity extends SocialIdentity { + + private String facebookId; + private String token; + private Long tokenExpirationTimestamp; + + public String getFacebookId() { + return facebookId; + } + + public void setFacebookId(String facebookId) { + this.facebookId = facebookId; + } + + public String getToken() { + return token; + } + + public void setToken(String token) { + this.token = token; + } + + public Long getTokenExpirationTimestamp() { + return tokenExpirationTimestamp; + } + + public void setTokenExpirationTimestamp(Long tokenExpirationTimestamp) { + this.tokenExpirationTimestamp = tokenExpirationTimestamp; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((facebookId == null) ? 0 : facebookId.hashCode()); + result = prime * result + ((token == null) ? 0 : token.hashCode()); + result = prime * result + ((tokenExpirationTimestamp == null) ? 0 : tokenExpirationTimestamp.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + FacebookSocialIdentity other = (FacebookSocialIdentity) obj; + if (facebookId == null) { + if (other.facebookId != null) + return false; + } else if (!facebookId.equals(other.facebookId)) + return false; + if (token == null) { + if (other.token != null) + return false; + } else if (!token.equals(other.token)) + return false; + if (tokenExpirationTimestamp == null) { + if (other.tokenExpirationTimestamp != null) + return false; + } else if (!tokenExpirationTimestamp.equals(other.tokenExpirationTimestamp)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("FacebookSocialIdentity [facebookId=").append(facebookId).append(", token=") + .append(token).append(", tokenExpirationTimestamp=").append(tokenExpirationTimestamp) + .append(", socialIdentityId=").append(this.getSocialIdentityId()).append(", creationTimestamp=") + .append(this.getCreationTimestamp()).append(", lastUpdateTimestamp=") + .append(this.getLastUpdateTimestamp()).append(", status=").append(this.getStatus()).append("]") + .toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/PhoneCommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/PhoneCommunicationProvider.java new file mode 100644 index 0000000..33c0a26 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/PhoneCommunicationProvider.java @@ -0,0 +1,67 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +public class PhoneCommunicationProvider extends CommunicationProvider { + + private String phoneNumber; + private String country; + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((country == null) ? 0 : country.hashCode()); + result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + PhoneCommunicationProvider other = (PhoneCommunicationProvider) obj; + if (country == null) { + if (other.country != null) + return false; + } else if (!country.equals(other.country)) + return false; + if (phoneNumber == null) { + if (other.phoneNumber != null) + return false; + } else if (!phoneNumber.equals(other.phoneNumber)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("PhoneCommunicationProvider [phoneNumber=").append(phoneNumber).append(", country=") + .append(country).append(", providerId=").append(this.getProviderId()).append(", creationTimestamp=") + .append(this.getCreationTimestamp()).append(", lastUpdateTimestamp=") + .append(this.getLastUpdateTimestamp()).append(", status=").append(this.getStatus()).append("]") + .toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/PhoneSocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/PhoneSocialIdentity.java new file mode 100644 index 0000000..7986f8b --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/PhoneSocialIdentity.java @@ -0,0 +1,67 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +public class PhoneSocialIdentity extends SocialIdentity { + + private String phoneNumber; + private String country; + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((country == null) ? 0 : country.hashCode()); + result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + PhoneSocialIdentity other = (PhoneSocialIdentity) obj; + if (country == null) { + if (other.country != null) + return false; + } else if (!country.equals(other.country)) + return false; + if (phoneNumber == null) { + if (other.phoneNumber != null) + return false; + } else if (!phoneNumber.equals(other.phoneNumber)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("PhoneSocialIdentity [phoneNumber=").append(phoneNumber).append(", country=") + .append(country).append(", socialIdentityId=").append(this.getSocialIdentityId()) + .append(", creationTimestamp=").append(this.getCreationTimestamp()).append(", lastUpdateTimestamp=") + .append(this.getLastUpdateTimestamp()).append(", status=").append(this.getStatus()).append("]") + .toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/UsernameCommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/UsernameCommunicationProvider.java new file mode 100644 index 0000000..0a27664 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/UsernameCommunicationProvider.java @@ -0,0 +1,50 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +public class UsernameCommunicationProvider extends CommunicationProvider { + + private String username; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + UsernameCommunicationProvider other = (UsernameCommunicationProvider) obj; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("UsernameCommunicationProvider [username=").append(username).append(", providerId=") + .append(this.getProviderId()).append(", creationTimestamp=").append(this.getCreationTimestamp()) + .append(", lastUpdateTimestamp=").append(this.getLastUpdateTimestamp()).append(", status=") + .append(this.getStatus()).append("]").toString(); + } +} -- GitLab From 6782f9e33243ec59ad44a4c3896a9058d4bd535d Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Wed, 1 Aug 2018 17:34:10 +0300 Subject: [PATCH 04/37] NY-2209 [BE ]Input data validation - phone number - add countries.txt file with all information about countries; - implement phone validator; - add model for CountryInfo; Signed-off-by: abotev-intracol --- .../account/grpc/components/Validator.java | 99 ++++++++ .../account/grpc/models/CountryInfo.java | 88 +++++++ src/main/resources/countries.txt | 236 ++++++++++++++++++ 3 files changed, 423 insertions(+) create mode 100644 src/main/java/biz/nynja/account/grpc/components/Validator.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/CountryInfo.java create mode 100644 src/main/resources/countries.txt diff --git a/src/main/java/biz/nynja/account/grpc/components/Validator.java b/src/main/java/biz/nynja/account/grpc/components/Validator.java new file mode 100644 index 0000000..ba104af --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/components/Validator.java @@ -0,0 +1,99 @@ +package biz.nynja.account.grpc.components; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.annotation.PostConstruct; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Component; + +import biz.nynja.account.grpc.models.CountryInfo; + +@Component +public class Validator { + + private static final Logger logger = LoggerFactory.getLogger(Validator.class); + + private HashMap countryInfoMap; + + @PostConstruct + public void loadPhonesBook() { + + CountryInfo countryInfo = null; + BufferedReader reader = null; + countryInfoMap = new HashMap<>(); + + logger.info("Loading phones information from file."); + try { + Resource resource = new ClassPathResource("countries.txt"); + InputStream resourceInputStream = resource.getInputStream(); + logger.info("Phones information loaded."); + reader = new BufferedReader(new InputStreamReader(resourceInputStream)); + String line; + while ((line = reader.readLine()) != null) { + String[] args = line.split(";"); + countryInfo = new CountryInfo(); + countryInfo.setCountryCode(args[1]); + countryInfo.setCountryPhoneCode(args[0]); + countryInfo.setCountryName(args[2]); + if (args.length > 3) { + countryInfo.setPhoneFormat(args[3]); + } + countryInfoMap.put(args[1], countryInfo); + } + } catch (IOException e) { + logger.error("Error during load phones information: {}", e.getMessage()); + } finally { + try { + reader.close(); + } catch (IOException e) { + logger.error("Close reader error: {}", e.getMessage()); + } + } + + } + + public boolean isPhoneNumberValid(String phoneNumber, String countryCode) { + + logger.info("Checking phoneNumber: {} for country: {}", phoneNumber, countryCode); + CountryInfo countryInfo = countryInfoMap.get(countryCode); + if (countryInfo == null) { + logger.info("Country: {} not found!", countryCode); + } + + char[] digits = countryInfo.getCountryPhoneCode().toCharArray(); + StringBuilder builder = new StringBuilder(); + for (char digit : digits) { + builder.append("[" + digit + "]"); + } + String codePattern = builder.toString(); + + String phoneLength = null; + if (countryInfo.getPhoneFormat() != null) { + phoneLength = "{" + countryInfo.getPhoneFormat().replaceAll("\\s", "").length() + "}"; + } else { + phoneLength = "+"; + } + + final String PHONE_PATTERN = "((?:\\+?([0][0])?" + codePattern + ")?||([0][0]" + codePattern + ")?)(\\d" + + phoneLength + ")$"; + logger.info("Generated phone pattern for country: {}, {}", countryCode, PHONE_PATTERN); + + Pattern pattern = Pattern.compile(PHONE_PATTERN); + Matcher matcher = pattern.matcher(phoneNumber); + + boolean isValid = matcher.matches(); + logger.info("PhoneNumber: {} for country: {} is valid: {}", phoneNumber, countryCode, isValid); + + return isValid; + } +} diff --git a/src/main/java/biz/nynja/account/grpc/models/CountryInfo.java b/src/main/java/biz/nynja/account/grpc/models/CountryInfo.java new file mode 100644 index 0000000..0aaa9e3 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/CountryInfo.java @@ -0,0 +1,88 @@ +package biz.nynja.account.grpc.models; + +public class CountryInfo { + + private String countryPhoneCode; + private String countryCode; + private String countryName; + private String phoneFormat; + + public CountryInfo() { + } + + public String getCountryPhoneCode() { + return countryPhoneCode; + } + + public void setCountryPhoneCode(String countryPhoneCode) { + this.countryPhoneCode = countryPhoneCode; + } + + public String getCountryCode() { + return countryCode; + } + + public void setCountryCode(String countryCode) { + this.countryCode = countryCode; + } + + public String getCountryName() { + return countryName; + } + + public void setCountryName(String countryName) { + this.countryName = countryName; + } + + public String getPhoneFormat() { + return phoneFormat; + } + + public void setPhoneFormat(String phoneFormat) { + this.phoneFormat = phoneFormat; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((countryCode == null) ? 0 : countryCode.hashCode()); + result = prime * result + ((countryName == null) ? 0 : countryName.hashCode()); + result = prime * result + ((countryPhoneCode == null) ? 0 : countryPhoneCode.hashCode()); + result = prime * result + ((phoneFormat == null) ? 0 : phoneFormat.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CountryInfo other = (CountryInfo) obj; + if (countryCode == null) { + if (other.countryCode != null) + return false; + } else if (!countryCode.equals(other.countryCode)) + return false; + if (countryName == null) { + if (other.countryName != null) + return false; + } else if (!countryName.equals(other.countryName)) + return false; + if (countryPhoneCode == null) { + if (other.countryPhoneCode != null) + return false; + } else if (!countryPhoneCode.equals(other.countryPhoneCode)) + return false; + if (phoneFormat == null) { + if (other.phoneFormat != null) + return false; + } else if (!phoneFormat.equals(other.phoneFormat)) + return false; + return true; + } + +} diff --git a/src/main/resources/countries.txt b/src/main/resources/countries.txt new file mode 100644 index 0000000..60d177a --- /dev/null +++ b/src/main/resources/countries.txt @@ -0,0 +1,236 @@ +1876;JM;Jamaica;XXX XXXX +1869;KN;Saint Kitts & Nevis;XXX XXXX +1868;TT;Trinidad & Tobago;XXX XXXX +1784;VC;Saint Vincent & the Grenadines;XXX XXXX +1767;DM;Dominica;XXX XXXX +1758;LC;Saint Lucia;XXX XXXX +1721;SX;Sint Maarten;XXX XXXX +1684;AS;American Samoa;XXX XXXX +1671;GU;Guam;XXX XXXX +1670;MP;Northern Mariana Islands;XXX XXXX +1664;MS;Montserrat;XXX XXXX +1649;TC;Turks & Caicos Islands;XXX XXXX +1473;GD;Grenada;XXX XXXX +1441;BM;Bermuda;XXX XXXX +1345;KY;Cayman Islands;XXX XXXX +1340;VI;US Virgin Islands;XXX XXXX +1284;VG;British Virgin Islands;XXX XXXX +1268;AG;Antigua & Barbuda;XXX XXXX +1264;AI;Anguilla;XXX XXXX +1246;BB;Barbados;XXX XXXX +1242;BS;Bahamas;XXX XXXX +998;UZ;Uzbekistan;XX XXXXXXX +996;KG;Kyrgyzstan;XXX XXXXXX +995;GE;Georgia;XXX XXX XXX +994;AZ;Azerbaijan;XX XXX XXXX +993;TM;Turkmenistan;XX XXXXXX +992;TJ;Tajikistan;XX XXX XXXX +977;NP;Nepal;XX XXXX XXXX +976;MN;Mongolia;XX XX XXXX +975;BT;Bhutan;XX XXX XXX +974;QA;Qatar;XX XXX XXX +973;BH;Bahrain;XXXX XXXX +972;IL;Israel;XX XXX XXXX +971;AE;United Arab Emirates;XX XXX XXXX +970;PS;Palestine;XXX XX XXXX +968;OM;Oman;XXXX XXXX +967;YE;Yemen;XXX XXX XXX +966;SA;Saudi Arabia;XX XXX XXXX +965;KW;Kuwait;XXXX XXXX +964;IQ;Iraq;XXX XXX XXXX +963;SY;Syria;XXX XXX XXX +962;JO;Jordan;X XXXX XXXX +961;LB;Lebanon +960;MV;Maldives;XXX XXXX +886;TW;Taiwan;XXX XXX XXX +883;GO;International Networks +882;GO;International Networks +881;GO;Global Mobile Satellite +880;BD;Bangladesh +856;LA;Laos;XX XX XXX XXX +855;KH;Cambodia +853;MO;Macau;XXXX XXXX +852;HK;Hong Kong;X XXX XXXX +850;KP;North Korea +692;MH;Marshall Islands +691;FM;Micronesia +690;TK;Tokelau +689;PF;French Polynesia +688;TV;Tuvalu +687;NC;New Caledonia +686;KI;Kiribati +685;WS;Samoa +683;NU;Niue +682;CK;Cook Islands +681;WF;Wallis & Futuna +680;PW;Palau +679;FJ;Fiji +678;VU;Vanuatu +677;SB;Solomon Islands +676;TO;Tonga +675;PG;Papua New Guinea +674;NR;Nauru +673;BN;Brunei Darussalam;XXX XXXX +672;NF;Norfolk Island +670;TL;Timor-Leste +599;BQ;Bonaire, Sint Eustatius & Saba +599;CW;Curaçao +598;UY;Uruguay;X XXX XXXX +597;SR;Suriname;XXX XXXX +596;MQ;Martinique +595;PY;Paraguay;XXX XXX XXX +594;GF;French Guiana +593;EC;Ecuador;XX XXX XXXX +592;GY;Guyana +591;BO;Bolivia;X XXX XXXX +590;GP;Guadeloupe;XXX XX XX XX +509;HT;Haiti +508;PM;Saint Pierre & Miquelon +507;PA;Panama;XXXX XXXX +506;CR;Costa Rica;XXXX XXXX +505;NI;Nicaragua;XXXX XXXX +504;HN;Honduras;XXXX XXXX +503;SV;El Salvador;XXXX XXXX +502;GT;Guatemala;X XXX XXXX +501;BZ;Belize +500;FK;Falkland Islands +423;LI;Liechtenstein +421;SK;Slovakia;XXX XXX XXX +420;CZ;Czech Republic;XXX XXX XXX +389;MK;Macedonia;XX XXX XXX +387;BA;Bosnia & Herzegovina;XX XXX XXX +386;SI;Slovenia;XX XXX XXX +385;HR;Croatia +383;XK;Kosovo;XXXX XXXX +382;ME;Montenegro +381;RS;Serbia;XX XXX XXXX +380;UA;Ukraine;XX XXX XX XX +378;SM;San Marino;XXX XXX XXXX +377;MC;Monaco;XXXX XXXX +376;AD;Andorra;XX XX XX +375;BY;Belarus;XX XXX XXXX +374;AM;Armenia;XX XXX XXX +373;MD;Moldova;XX XXX XXX +372;EE;Estonia +371;LV;Latvia;XXX XXXXX +370;LT;Lithuania;XXX XXXXX +359;BG;Bulgaria +358;FI;Finland +357;CY;Cyprus;XXXX XXXX +356;MT;Malta;XX XX XX XX +355;AL;Albania;XX XXX XXXX +354;IS;Iceland;XXX XXXX +353;IE;Ireland;XX XXX XXXX +352;LU;Luxembourg +351;PT;Portugal;X XXXX XXXX +350;GI;Gibraltar;XXXX XXXX +299;GL;Greenland;XXX XXX +298;FO;Faroe Islands;XXX XXX +297;AW;Aruba;XXX XXXX +291;ER;Eritrea;X XXX XXX +290;SH;Saint Helena;XX XXX +269;KM;Comoros;XXX XXXX +268;SZ;Swaziland;XXXX XXXX +267;BW;Botswana;XX XXX XXX +266;LS;Lesotho;XX XXX XXX +265;MW;Malawi;77 XXX XXXX +264;NA;Namibia;XX XXX XXXX +263;ZW;Zimbabwe;XX XXX XXXX +262;RE;Réunion;XXX XXX XXX +261;MG;Madagascar;XX XX XXX XX +260;ZM;Zambia;XX XXX XXXX +258;MZ;Mozambique;XX XXX XXXX +257;BI;Burundi;XX XX XXXX +256;UG;Uganda;XX XXX XXXX +255;TZ;Tanzania;XX XXX XXXX +254;KE;Kenya;XXX XXX XXX +253;DJ;Djibouti;XX XX XX XX +252;SO;Somalia;XX XXX XXX +251;ET;Ethiopia;XX XXX XXXX +250;RW;Rwanda;XXX XXX XXX +249;SD;Sudan;XX XXX XXXX +248;SC;Seychelles;X XX XX XX +247;SH;Saint Helena;XXXX +246;IO;Diego Garcia;XXX XXXX +245;GW;Guinea-Bissau;XXX XXXX +244;AO;Angola;XXX XXX XXX +243;CD;Congo (Dem. Rep.);XX XXX XXXX +242;CG;Congo (Rep.);XX XXX XXXX +241;GA;Gabon;X XX XX XX +240;GQ;Equatorial Guinea;XXX XXX XXX +239;ST;São Tomé & Príncipe;XX XXXXX +238;CV;Cape Verde;XXX XXXX +237;CM;Cameroon;XXXX XXXX +236;CF;Central African Rep.;XX XX XX XX +235;TD;Chad;XX XX XX XX +234;NG;Nigeria +233;GH;Ghana +232;SL;Sierra Leone;XX XXX XXX +231;LR;Liberia +230;MU;Mauritius +229;BJ;Benin;XX XXX XXX +228;TG;Togo;XX XXX XXX +227;NE;Niger;XX XX XX XX +226;BF;Burkina Faso;XX XX XX XX +225;CI;Côte d`Ivoire;XX XXX XXX +224;GN;Guinea;XXX XXX XXX +223;ML;Mali;XXXX XXXX +222;MR;Mauritania;XXXX XXXX +221;SN;Senegal;XX XXX XXXX +220;GM;Gambia;XXX XXXX +218;LY;Libya;XX XXX XXXX +216;TN;Tunisia;XX XXX XXX +213;DZ;Algeria;XXX XX XX XX +212;MA;Morocco;XX XXX XXXX +211;SS;South Sudan;XX XXX XXXX +98;IR;Iran;XXX XXX XXXX +95;MM;Myanmar +94;LK;Sri Lanka;XX XXX XXXX +93;AF;Afghanistan;XXX XXX XXX +92;PK;Pakistan;XXX XXX XXXX +91;IN;India;XXXXX XXXXX +90;TR;Turkey;XXX XXX XXXX +86;CN;China;XXX XXXX XXXX +84;VN;Vietnam +82;KR;South Korea +81;JP;Japan;XX XXXX XXXX +66;TH;Thailand;X XXXX XXXX +65;SG;Singapore;XXXX XXXX +64;NZ;New Zealand +63;PH;Philippines;XXX XXX XXXX +62;ID;Indonesia +61;AU;Australia;XXX XXX XXX +60;MY;Malaysia +58;VE;Venezuela;XXX XXX XXXX +57;CO;Colombia;XXX XXX XXXX +56;CL;Chile;X XXXX XXXX +55;BR;Brazil;XX XXXXX XXXX +54;AR;Argentina +53;CU;Cuba;XXXX XXXX +52;MX;Mexico +51;PE;Peru;XXX XXX XXX +49;DE;Germany +48;PL;Poland;XX XXX XXXX +47;NO;Norway;XXXX XXXX +46;SE;Sweden;XX XXX XXXX +45;DK;Denmark;XXXX XXXX +44;GB;United Kingdom;XXXX XXXXXX +43;AT;Austria +42;YL;Y-land +41;CH;Switzerland;XX XXX XXXX +40;RO;Romania;XXX XXX XXX +39;IT;Italy +36;HU;Hungary;XXX XXX XXX +34;ES;Spain;XXX XXX XXX +33;FR;France;X XX XX XX XX +32;BE;Belgium;XXX XX XX XX +31;NL;Netherlands;X XX XX XX XX +30;GR;Greece;XXX XXX XXXX +27;ZA;South Africa;XX XXX XXXX +20;EG;Egypt;XX XXXX XXXX +7;KZ;Kazakhstan;XXX XXX XX XX +7;RU;Russian Federation;XXX XXX XXXX +1;PR;Puerto Rico;XXX XXX XXXX +1;DO;Dominican Rep.;XXX XXX XXXX +1;CA;Canada;XXX XXX XXXX +1;US;USA;XXX XXX XXXX \ No newline at end of file -- GitLab From c44fda3cb59bffccd4aa6e44e836462e139edb57 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Wed, 1 Aug 2018 17:56:01 +0300 Subject: [PATCH 05/37] NY-2306 Unit tests - add valid unit test; - add invalid unit test; - fix package structure; Signed-off-by: abotev-intracol --- .../nynja/account/grpc/ApplicationTests.java | 78 +--------------- .../grpc/components/ValidatorTests.java | 41 +++++++++ .../grpc/services/AccountServiceTests.java | 90 +++++++++++++++++++ .../grpc/{ => utils}/GrpcServerTestBase.java | 2 +- .../nynja/account/grpc/{ => utils}/Util.java | 2 +- 5 files changed, 136 insertions(+), 77 deletions(-) create mode 100644 src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java create mode 100644 src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java rename src/test/java/biz/nynja/account/grpc/{ => utils}/GrpcServerTestBase.java (98%) rename src/test/java/biz/nynja/account/grpc/{ => utils}/Util.java (95%) diff --git a/src/test/java/biz/nynja/account/grpc/ApplicationTests.java b/src/test/java/biz/nynja/account/grpc/ApplicationTests.java index c0ea2a8..054fc0a 100644 --- a/src/test/java/biz/nynja/account/grpc/ApplicationTests.java +++ b/src/test/java/biz/nynja/account/grpc/ApplicationTests.java @@ -1,88 +1,16 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ package biz.nynja.account.grpc; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; - -import java.util.Optional; -import java.util.concurrent.ExecutionException; - import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; -import biz.nynja.account.grpc.models.AccountInfo; -import biz.nynja.account.grpc.repositories.AccountInfoRepository; -import biz.nynja.blueprint.grpc.AccountServiceGrpc; -import biz.nynja.blueprint.grpc.RegisterError; -import biz.nynja.blueprint.grpc.RegisterRequest; -import biz.nynja.blueprint.grpc.RegisterResponse; - @RunWith(SpringRunner.class) -@SpringBootTest(classes = Util.class, - properties = { - "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration", - "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration" }) -@ActiveProfiles("dev") -public class ApplicationTests extends GrpcServerTestBase { - - @MockBean - private AccountInfoRepository accountInfoRepository; - - @Autowired - @Qualifier("newAccount") - private AccountInfo accountInfo; - - @Autowired - @Qualifier("savedAccount") - private AccountInfo savedAccount; +@SpringBootTest +public class ApplicationTests { @Test - public void testRegister() throws ExecutionException, InterruptedException { - - String testEmail = "email@test.com"; - final RegisterRequest request = RegisterRequest.newBuilder().setId(1).setEmail(testEmail).setPassword("abc") - .build(); - - given(accountInfoRepository.findByEmail(testEmail)).willReturn(null); - given(accountInfoRepository.save(any(AccountInfo.class))).willReturn(savedAccount); - - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc - .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - - final RegisterResponse reply = accountServiceBlockingStub.register(request); - - assertNotNull("Reply should not be null", reply); - assertTrue(String.format("Reply should contain email '%s'", testEmail), - reply.getAccount().getEmail().equals(testEmail)); - } - - @Test - public void testRegisterExistEmail() throws ExecutionException, InterruptedException { - - String testEmail = "email@test.com"; - final RegisterRequest request = RegisterRequest.newBuilder().setId(1).setEmail(testEmail).setPassword("abc") - .build(); - - given(accountInfoRepository.findByEmail(testEmail)).willReturn(accountInfo); - - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc - .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - - final RegisterResponse reply = accountServiceBlockingStub.register(request); - - assertNotNull("Reply should not be null", reply); - assertTrue(String.format("Reply should contain cause '%s'", testEmail), - reply.getError().getCause().equals(RegisterError.Cause.EMAIL_ALREADY_USED)); + public void testContext() { } } diff --git a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java new file mode 100644 index 0000000..33043f6 --- /dev/null +++ b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java @@ -0,0 +1,41 @@ +package biz.nynja.account.grpc.components; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class ValidatorTests { + + @Autowired + private Validator validator; + + @Test + public void validPhoneNumberTest() { + + String phoneNumber = "+359887434646"; + String countryCode = "BG"; + boolean isValid = validator.isPhoneNumberValid(phoneNumber, countryCode); + assertTrue(String.format("Phone number: '%s' should be valid for country '%s'", phoneNumber, countryCode), + isValid == true); + + } + + @Test + public void invalidPhoneNumberTest() { + + String phoneNumber = "+357887434646"; + String countryCode = "BG"; + boolean isValid = validator.isPhoneNumberValid(phoneNumber, countryCode); + assertFalse(String.format("Phone number: '%s' should be invalid for country '%s'", phoneNumber, countryCode), + isValid == true); + + } + +} diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java new file mode 100644 index 0000000..3d90ec9 --- /dev/null +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -0,0 +1,90 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.services; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; + +import java.util.Optional; +import java.util.concurrent.ExecutionException; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import biz.nynja.account.grpc.models.AccountInfo; +import biz.nynja.account.grpc.repositories.AccountInfoRepository; +import biz.nynja.account.grpc.utils.GrpcServerTestBase; +import biz.nynja.account.grpc.utils.Util; +import biz.nynja.blueprint.grpc.AccountServiceGrpc; +import biz.nynja.blueprint.grpc.RegisterError; +import biz.nynja.blueprint.grpc.RegisterRequest; +import biz.nynja.blueprint.grpc.RegisterResponse; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Util.class, + properties = { + "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration", + "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration" }) +@ActiveProfiles("dev") +public class AccountServiceTests extends GrpcServerTestBase { + + @MockBean + private AccountInfoRepository accountInfoRepository; + + @Autowired + @Qualifier("newAccount") + private AccountInfo accountInfo; + + @Autowired + @Qualifier("savedAccount") + private AccountInfo savedAccount; + + @Test + public void testRegister() throws ExecutionException, InterruptedException { + + String testEmail = "email@test.com"; + final RegisterRequest request = RegisterRequest.newBuilder().setId(1).setEmail(testEmail).setPassword("abc") + .build(); + + given(accountInfoRepository.findByEmail(testEmail)).willReturn(null); + given(accountInfoRepository.save(any(AccountInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final RegisterResponse reply = accountServiceBlockingStub.register(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain email '%s'", testEmail), + reply.getAccount().getEmail().equals(testEmail)); + } + + @Test + public void testRegisterExistEmail() throws ExecutionException, InterruptedException { + + String testEmail = "email@test.com"; + final RegisterRequest request = RegisterRequest.newBuilder().setId(1).setEmail(testEmail).setPassword("abc") + .build(); + + given(accountInfoRepository.findByEmail(testEmail)).willReturn(accountInfo); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final RegisterResponse reply = accountServiceBlockingStub.register(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", testEmail), + reply.getError().getCause().equals(RegisterError.Cause.EMAIL_ALREADY_USED)); + } + +} diff --git a/src/test/java/biz/nynja/account/grpc/GrpcServerTestBase.java b/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java similarity index 98% rename from src/test/java/biz/nynja/account/grpc/GrpcServerTestBase.java rename to src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java index d4b3507..ba400d1 100644 --- a/src/test/java/biz/nynja/account/grpc/GrpcServerTestBase.java +++ b/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java @@ -1,4 +1,4 @@ -package biz.nynja.account.grpc; +package biz.nynja.account.grpc.utils; import java.util.Optional; diff --git a/src/test/java/biz/nynja/account/grpc/Util.java b/src/test/java/biz/nynja/account/grpc/utils/Util.java similarity index 95% rename from src/test/java/biz/nynja/account/grpc/Util.java rename to src/test/java/biz/nynja/account/grpc/utils/Util.java index 8a9b1d9..7cc04e7 100644 --- a/src/test/java/biz/nynja/account/grpc/Util.java +++ b/src/test/java/biz/nynja/account/grpc/utils/Util.java @@ -1,4 +1,4 @@ -package biz.nynja.account.grpc; +package biz.nynja.account.grpc.utils; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; -- GitLab From ade948204491d1897c41d0953ce0f422bd391e2e Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Wed, 1 Aug 2018 17:56:57 +0300 Subject: [PATCH 06/37] NY-2305 [BE] Configure logger - add groovy logger to export log in file; Signed-off-by: abotev-intracol --- .gitignore | 1 + pom.xml | 9 +++++-- .../grpc/services/AccountServiceImpl.java | 13 +++++----- src/main/resources/logback-spring.groovy | 24 +++++++++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 src/main/resources/logback-spring.groovy diff --git a/.gitignore b/.gitignore index 82eca33..2a0148e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ /target/ !.mvn/wrapper/maven-wrapper.jar +*.log ### STS ### .apt_generated diff --git a/pom.xml b/pom.xml index 2f5577d..52f7604 100644 --- a/pom.xml +++ b/pom.xml @@ -114,13 +114,18 @@ com.fasterxml.jackson.core jackson-databind - 2.9.5 + 2.9.5 com.fasterxml.jackson.core jackson-core - 2.9.5 + 2.9.5 + + + + org.codehaus.groovy + groovy-all diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index 27414ef..9439f96 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -7,6 +7,7 @@ import org.lognet.springboot.grpc.GRpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.validation.annotation.Validated; import biz.nynja.account.grpc.models.AccountInfo; import biz.nynja.account.grpc.repositories.AccountInfoRepository; @@ -37,12 +38,12 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas AccountInfo accountInfo = AccountInfo.fromProto(request); - if (accountInfoRepository.findByEmail(accountInfo.getEmail()) != null) { - responseObserver.onNext(RegisterResponse.newBuilder() - .setError(RegisterError.newBuilder().setCause(Cause.EMAIL_ALREADY_USED)).build()); - responseObserver.onCompleted(); - return; - } + if (accountInfoRepository.findByEmail(accountInfo.getEmail()) != null) { + responseObserver.onNext(RegisterResponse.newBuilder() + .setError(RegisterError.newBuilder().setCause(Cause.EMAIL_ALREADY_USED)).build()); + responseObserver.onCompleted(); + return; + } AccountInfo savedAccount = accountInfoRepository.save(accountInfo); LOGGER.info("Account \"{}\" saved into the DB", savedAccount.toString()); diff --git a/src/main/resources/logback-spring.groovy b/src/main/resources/logback-spring.groovy new file mode 100644 index 0000000..ffc1800 --- /dev/null +++ b/src/main/resources/logback-spring.groovy @@ -0,0 +1,24 @@ +import ch.qos.logback.classic.encoder.PatternLayoutEncoder +import ch.qos.logback.core.rolling.RollingFileAppender +import ch.qos.logback.core.rolling.TimeBasedRollingPolicy +import ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP +import ch.qos.logback.core.status.OnConsoleStatusListener +import ch.qos.logback.classic.Level + +statusListener(OnConsoleStatusListener) + +def file = "${System.getProperty('log.dir', '')}account-service-%d.%i.log" + +appender("FILE", RollingFileAppender) { + // add a status message regarding the file property + addInfo("Starting logging to $file") + append = true + encoder(PatternLayoutEncoder) { pattern = "%d{HH:mm:ss.SSS} %level %logger - %msg%n" } + rollingPolicy(TimeBasedRollingPolicy) { + fileNamePattern = "$file" + timeBasedFileNamingAndTriggeringPolicy(SizeAndTimeBasedFNATP) { maxFileSize = "10mb" }} +} + +logger("org.springframework.cloud.config.client.ConfigServicePropertySourceLocator", Level.WARN) + +root(Level.toLevel("${System.getProperty('log.level', 'INFO')}"), ["FILE"]) \ No newline at end of file -- GitLab From 5e10670e47995041218daf831cd1a082c92fd150 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Wed, 1 Aug 2018 19:33:08 +0300 Subject: [PATCH 07/37] NY-2209 [BE ]Input data validation - phone number - change log level from info to debug; Signed-off-by: abotev-intracol --- .../account/grpc/components/Validator.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/components/Validator.java b/src/main/java/biz/nynja/account/grpc/components/Validator.java index ba104af..b35db76 100644 --- a/src/main/java/biz/nynja/account/grpc/components/Validator.java +++ b/src/main/java/biz/nynja/account/grpc/components/Validator.java @@ -1,3 +1,6 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ package biz.nynja.account.grpc.components; import java.io.BufferedReader; @@ -18,6 +21,10 @@ import org.springframework.stereotype.Component; import biz.nynja.account.grpc.models.CountryInfo; +/** + * Component which contains all validation methods. + */ + @Component public class Validator { @@ -32,11 +39,11 @@ public class Validator { BufferedReader reader = null; countryInfoMap = new HashMap<>(); - logger.info("Loading phones information from file."); + logger.debug("Loading phones information from file."); try { Resource resource = new ClassPathResource("countries.txt"); InputStream resourceInputStream = resource.getInputStream(); - logger.info("Phones information loaded."); + logger.debug("Phones information loaded."); reader = new BufferedReader(new InputStreamReader(resourceInputStream)); String line; while ((line = reader.readLine()) != null) { @@ -64,10 +71,10 @@ public class Validator { public boolean isPhoneNumberValid(String phoneNumber, String countryCode) { - logger.info("Checking phoneNumber: {} for country: {}", phoneNumber, countryCode); + logger.debug("Checking phoneNumber: {} for country: {}", phoneNumber, countryCode); CountryInfo countryInfo = countryInfoMap.get(countryCode); if (countryInfo == null) { - logger.info("Country: {} not found!", countryCode); + logger.debug("Country: {} not found!", countryCode); } char[] digits = countryInfo.getCountryPhoneCode().toCharArray(); @@ -86,13 +93,13 @@ public class Validator { final String PHONE_PATTERN = "((?:\\+?([0][0])?" + codePattern + ")?||([0][0]" + codePattern + ")?)(\\d" + phoneLength + ")$"; - logger.info("Generated phone pattern for country: {}, {}", countryCode, PHONE_PATTERN); + logger.debug("Generated phone pattern for country: {}, {}", countryCode, PHONE_PATTERN); Pattern pattern = Pattern.compile(PHONE_PATTERN); Matcher matcher = pattern.matcher(phoneNumber); boolean isValid = matcher.matches(); - logger.info("PhoneNumber: {} for country: {} is valid: {}", phoneNumber, countryCode, isValid); + logger.debug("PhoneNumber: {} for country: {} is valid: {}", phoneNumber, countryCode, isValid); return isValid; } -- GitLab From 9c3f9549e07f209d68e8d052a0fed1bb840e8150 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Wed, 1 Aug 2018 19:34:20 +0300 Subject: [PATCH 08/37] [Sub-task] NY-2306 Unit tests - add copyright to all files in tests; Signed-off-by: abotev-intracol --- src/main/resources/logback-spring.groovy | 4 ++++ .../biz/nynja/account/grpc/ApplicationTests.java | 12 ++++++++++-- .../account/grpc/components/ValidatorTests.java | 12 ++++++++++-- .../account/grpc/services/AccountServiceTests.java | 6 ++++++ .../nynja/account/grpc/utils/GrpcServerTestBase.java | 8 ++++++++ src/test/java/biz/nynja/account/grpc/utils/Util.java | 8 ++++++++ 6 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/main/resources/logback-spring.groovy b/src/main/resources/logback-spring.groovy index ffc1800..84642ac 100644 --- a/src/main/resources/logback-spring.groovy +++ b/src/main/resources/logback-spring.groovy @@ -1,3 +1,7 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ + import ch.qos.logback.classic.encoder.PatternLayoutEncoder import ch.qos.logback.core.rolling.RollingFileAppender import ch.qos.logback.core.rolling.TimeBasedRollingPolicy diff --git a/src/test/java/biz/nynja/account/grpc/ApplicationTests.java b/src/test/java/biz/nynja/account/grpc/ApplicationTests.java index 054fc0a..0c31adf 100644 --- a/src/test/java/biz/nynja/account/grpc/ApplicationTests.java +++ b/src/test/java/biz/nynja/account/grpc/ApplicationTests.java @@ -1,12 +1,20 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ + package biz.nynja.account.grpc; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +/** + * Main unit test. + */ + @RunWith(SpringRunner.class) -@SpringBootTest +@ContextConfiguration(classes = { Application.class }) public class ApplicationTests { @Test diff --git a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java index 33043f6..bd58079 100644 --- a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java +++ b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java @@ -1,3 +1,7 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ + package biz.nynja.account.grpc.components; import static org.junit.Assert.assertFalse; @@ -6,11 +10,15 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +/** + * Components unit tests. + */ + @RunWith(SpringRunner.class) -@SpringBootTest +@ContextConfiguration(classes = { Validator.class }) public class ValidatorTests { @Autowired diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index 3d90ec9..43af81b 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -16,6 +16,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; @@ -29,8 +30,13 @@ import biz.nynja.blueprint.grpc.RegisterError; import biz.nynja.blueprint.grpc.RegisterRequest; import biz.nynja.blueprint.grpc.RegisterResponse; +/** + * AccountService unit tests. + */ + @RunWith(SpringRunner.class) @SpringBootTest(classes = Util.class, + webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration", "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration" }) diff --git a/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java b/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java index ba400d1..0a04113 100644 --- a/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java +++ b/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java @@ -1,3 +1,7 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ + package biz.nynja.account.grpc.utils; import java.util.Optional; @@ -15,6 +19,10 @@ import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.inprocess.InProcessChannelBuilder; +/** + * gRPC test configurations. + */ + public abstract class GrpcServerTestBase { @Autowired(required = false) diff --git a/src/test/java/biz/nynja/account/grpc/utils/Util.java b/src/test/java/biz/nynja/account/grpc/utils/Util.java index 7cc04e7..101aa24 100644 --- a/src/test/java/biz/nynja/account/grpc/utils/Util.java +++ b/src/test/java/biz/nynja/account/grpc/utils/Util.java @@ -1,3 +1,7 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ + package biz.nynja.account.grpc.utils; import org.springframework.boot.test.context.TestConfiguration; @@ -5,6 +9,10 @@ import org.springframework.context.annotation.Bean; import biz.nynja.account.grpc.models.AccountInfo;; +/** + * Unit tests variables, beans and help methods. + */ + @TestConfiguration public class Util { -- GitLab From e6de72ea84a7fd5e3a0b61c690f257e0712ba0c3 Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Thu, 2 Aug 2018 11:41:39 +0300 Subject: [PATCH 09/37] NY-2216: Create Cassandra keyspase and tables if missing Signed-off-by: Ralitsa Todorova --- .../grpc/configuration/CassandraConfig.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java diff --git a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java new file mode 100644 index 0000000..c7671ad --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java @@ -0,0 +1,43 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.configuration; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.cassandra.config.AbstractCassandraConfiguration; +import org.springframework.data.cassandra.config.SchemaAction; +import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification; + +@Configuration +public class CassandraConfig extends AbstractCassandraConfiguration { + + @Value("${spring.data.cassandra.keyspace-name}") + private String keyspace; + + @Override + protected String getKeyspaceName() { + return keyspace; + } + + @Override + public SchemaAction getSchemaAction() { + return SchemaAction.CREATE_IF_NOT_EXISTS; + } + + @Override + public String[] getEntityBasePackages() { + return new String[] { "biz.nynja.account.grpc.models" }; + } + + @Override + protected List getKeyspaceCreations() { + CreateKeyspaceSpecification specification = CreateKeyspaceSpecification.createKeyspace(getKeyspaceName()) + .ifNotExists().withSimpleReplication(); + return Arrays.asList(specification); + } + +} -- GitLab From 85933688ce2b8031a86b01de92c84db7df9fdae4 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Thu, 2 Aug 2018 11:46:40 +0300 Subject: [PATCH 10/37] NY-2211 [BE] Input data validation - username - implement username validation; Signed-off-by: abotev-intracol --- .../nynja/account/grpc/components/Validator.java | 16 ++++++++++++++++ .../grpc/services/AccountServiceImpl.java | 1 - 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/biz/nynja/account/grpc/components/Validator.java b/src/main/java/biz/nynja/account/grpc/components/Validator.java index b35db76..d88d859 100644 --- a/src/main/java/biz/nynja/account/grpc/components/Validator.java +++ b/src/main/java/biz/nynja/account/grpc/components/Validator.java @@ -103,4 +103,20 @@ public class Validator { return isValid; } + + public boolean isUsernameValid(String username) { + + logger.debug("Checking username: {}", username); + + final String USERNAME_PATTERN = "^[A-Za-z0-9_]{2,32}$"; + + Pattern pattern = Pattern.compile(USERNAME_PATTERN); + Matcher matcher = pattern.matcher(username); + + boolean isValid = matcher.matches(); + logger.debug("Username: {} is valid: {}", username, isValid); + + return isValid; + } + } diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index 9439f96..d139529 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -7,7 +7,6 @@ import org.lognet.springboot.grpc.GRpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.validation.annotation.Validated; import biz.nynja.account.grpc.models.AccountInfo; import biz.nynja.account.grpc.repositories.AccountInfoRepository; -- GitLab From d99bc4942393d7525036e06f3603130135f211af Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Thu, 2 Aug 2018 11:48:52 +0300 Subject: [PATCH 11/37] NY-2316 Unit tests - add valid and invalid unit tests; Signed-off-by: abotev-intracol --- .../grpc/components/ValidatorTests.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java index bd58079..d9ecfd5 100644 --- a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java +++ b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java @@ -46,4 +46,23 @@ public class ValidatorTests { } + @Test + public void validUsernameTest() { + + String username = "VALID_username1"; + boolean isValid = validator.isUsernameValid(username); + assertTrue(String.format("Username: '%s' should be valid.", username), + isValid == true); + + } + + @Test + public void invalidUsernameTest() { + + String username = "INVALID-username1"; + boolean isValid = validator.isUsernameValid(username); + assertFalse(String.format("Username: '%s' should be invalid.", username), + isValid == true); + + } } -- GitLab From 111f2b63d2dd53cc52b2252c8d06c0bfac30df75 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Fri, 3 Aug 2018 15:39:37 +0300 Subject: [PATCH 12/37] NY-2210 [BE] Input data validation - email address - add email regex as requirements; Signed-off-by: abotev-intracol --- .../nynja/account/grpc/components/Validator.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/biz/nynja/account/grpc/components/Validator.java b/src/main/java/biz/nynja/account/grpc/components/Validator.java index d88d859..f4dacb9 100644 --- a/src/main/java/biz/nynja/account/grpc/components/Validator.java +++ b/src/main/java/biz/nynja/account/grpc/components/Validator.java @@ -119,4 +119,19 @@ public class Validator { return isValid; } + public boolean isEmailValid(String email) { + + logger.debug("Checking email: {}", email); + + final String EMAIL_PATTERN = "^[\\w!#$%&’*+/=?`{|}~^-]?[\\w!#$%&’*+/=?`{|}~^.-]{0,62}[\\w!#$%&’*+/=?`{|}~^-]{1}@[a-zA-Z0-9]?[a-zA-Z0-9-]{0,228}[a-zA-Z0-9]{1}.[a-zA-Z0-9]{2,25}$"; + + Pattern pattern = Pattern.compile(EMAIL_PATTERN); + Matcher matcher = pattern.matcher(email); + + boolean isValid = matcher.matches(); + logger.debug("Email: {} is valid: {}", email, isValid); + + return isValid; + } + } -- GitLab From decad050c4b8d6ea6a10a1cfca2b3e8a09f29c6b Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Fri, 3 Aug 2018 15:40:46 +0300 Subject: [PATCH 13/37] NY-2356 Unit tests - add valid and invalid unit tests; Signed-off-by: abotev-intracol --- .../grpc/components/ValidatorTests.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java index d9ecfd5..c86391f 100644 --- a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java +++ b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java @@ -65,4 +65,24 @@ public class ValidatorTests { isValid == true); } + + @Test + public void validEmailTest() { + + String email = "valid.E-mail1@domain-test.com"; + boolean isValid = validator.isEmailValid(email); + assertTrue(String.format("Email: '%s' should be valid.", email), + isValid == true); + + } + + @Test + public void invalidEmailTest() { + + String email = "invalid.E-mail1.@domain_test.com1"; + boolean isValid = validator.isEmailValid(email); + assertFalse(String.format("Email: '%s' should be invalid.", email), + isValid == true); + + } } -- GitLab From 1644e0ae42f6ac100313a1524136647b807228af Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Mon, 6 Aug 2018 16:48:13 +0300 Subject: [PATCH 14/37] NY-2210 [BE] Input data validation - email address - fix email validation; Signed-off-by: abotev-intracol --- src/main/java/biz/nynja/account/grpc/components/Validator.java | 2 +- .../java/biz/nynja/account/grpc/components/ValidatorTests.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/components/Validator.java b/src/main/java/biz/nynja/account/grpc/components/Validator.java index f4dacb9..0565c49 100644 --- a/src/main/java/biz/nynja/account/grpc/components/Validator.java +++ b/src/main/java/biz/nynja/account/grpc/components/Validator.java @@ -123,7 +123,7 @@ public class Validator { logger.debug("Checking email: {}", email); - final String EMAIL_PATTERN = "^[\\w!#$%&’*+/=?`{|}~^-]?[\\w!#$%&’*+/=?`{|}~^.-]{0,62}[\\w!#$%&’*+/=?`{|}~^-]{1}@[a-zA-Z0-9]?[a-zA-Z0-9-]{0,228}[a-zA-Z0-9]{1}.[a-zA-Z0-9]{2,25}$"; + final String EMAIL_PATTERN = "^(?!\\.)[\\w!#$%&’*+/=?`{|}~^.-]{0,63}[\\w!#$%&’*+/=?`{|}~^-]{1}@(?!\\.)[a-zA-Z0-9-.]{0,229}[a-zA-Z0-9]{1}.[a-zA-Z0-9]{2,25}$"; Pattern pattern = Pattern.compile(EMAIL_PATTERN); Matcher matcher = pattern.matcher(email); diff --git a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java index c86391f..6ef7367 100644 --- a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java +++ b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java @@ -69,7 +69,7 @@ public class ValidatorTests { @Test public void validEmailTest() { - String email = "valid.E-mail1@domain-test.com"; + String email = "valid.E-mail1@domain-sub.test.com"; boolean isValid = validator.isEmailValid(email); assertTrue(String.format("Email: '%s' should be valid.", email), isValid == true); -- GitLab From 3142c2ef0af3ea4f6d2c5840eab8dcffe4335020 Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Tue, 7 Aug 2018 12:00:19 +0300 Subject: [PATCH 15/37] NY-2216: [BE] SQL files for DB schema and tables Initial implementation for storing basic user data Signed-off-by: Ralitsa Todorova --- .../grpc/configuration/CassandraConfig.java | 2 + .../grpc/models/AuthenticationType.java | 8 + .../nynja/account/grpc/models/UserInfo.java | 225 ++++++++++++++++++ .../grpc/repositories/UserInfoRepository.java | 14 ++ 4 files changed, 249 insertions(+) create mode 100644 src/main/java/biz/nynja/account/grpc/models/AuthenticationType.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/UserInfo.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/UserInfoRepository.java diff --git a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java index c7671ad..11d22f1 100644 --- a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java +++ b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java @@ -11,8 +11,10 @@ import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.config.AbstractCassandraConfiguration; import org.springframework.data.cassandra.config.SchemaAction; import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification; +import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; @Configuration +@EnableCassandraRepositories public class CassandraConfig extends AbstractCassandraConfiguration { @Value("${spring.data.cassandra.keyspace-name}") diff --git a/src/main/java/biz/nynja/account/grpc/models/AuthenticationType.java b/src/main/java/biz/nynja/account/grpc/models/AuthenticationType.java new file mode 100644 index 0000000..c8afc57 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/AuthenticationType.java @@ -0,0 +1,8 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +public enum AuthenticationType { + PHONE, EMAIL, FACEBOOK, GOOGLEPLUS +} diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfo.java b/src/main/java/biz/nynja/account/grpc/models/UserInfo.java new file mode 100644 index 0000000..d0ea474 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfo.java @@ -0,0 +1,225 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.util.UUID; + +import org.springframework.data.cassandra.core.cql.PrimaryKeyType; +import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; +import org.springframework.data.cassandra.core.mapping.Table; + +@Table +public class UserInfo { + + @PrimaryKeyColumn(name = "profile_id", ordinal = 0, type = PrimaryKeyType.PARTITIONED) + private UUID profileId; + @PrimaryKeyColumn(name = "account_id", ordinal = 1, type = PrimaryKeyType.CLUSTERED) + private UUID accountId; + private String firstName; + private String lastName; + private String middleName; + @PrimaryKeyColumn(name = "username", ordinal = 2, type = PrimaryKeyType.CLUSTERED) + private String username; + private String phoneNumber; + private String emailAddress; + private Status status; + @PrimaryKeyColumn(name = "authentication_type", ordinal = 3, type = PrimaryKeyType.CLUSTERED) + private AuthenticationType authenticationType; + @PrimaryKeyColumn(name = "authnetication_provider_id", ordinal = 4, type = PrimaryKeyType.CLUSTERED) + private String authenticationProviderId; + private String communicationProcviderId; + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getMiddleName() { + return middleName; + } + + public void setMiddleName(String middleName) { + this.middleName = middleName; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public String getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public AuthenticationType getAuthenticationType() { + return authenticationType; + } + + public void setAuthenticationType(AuthenticationType authenticationType) { + this.authenticationType = authenticationType; + } + + public String getAuthenticationProviderId() { + return authenticationProviderId; + } + + public void setAuthenticationProviderId(String authenticationProviderId) { + this.authenticationProviderId = authenticationProviderId; + } + + public String getCommunicationProcviderId() { + return communicationProcviderId; + } + + public void setCommunicationProcviderId(String communicationProcviderId) { + this.communicationProcviderId = communicationProcviderId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((authenticationProviderId == null) ? 0 : authenticationProviderId.hashCode()); + result = prime * result + ((authenticationType == null) ? 0 : authenticationType.hashCode()); + result = prime * result + ((communicationProcviderId == null) ? 0 : communicationProcviderId.hashCode()); + result = prime * result + ((emailAddress == null) ? 0 : emailAddress.hashCode()); + result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); + result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); + result = prime * result + ((middleName == null) ? 0 : middleName.hashCode()); + result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + UserInfo other = (UserInfo) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (authenticationProviderId == null) { + if (other.authenticationProviderId != null) + return false; + } else if (!authenticationProviderId.equals(other.authenticationProviderId)) + return false; + if (authenticationType != other.authenticationType) + return false; + if (communicationProcviderId == null) { + if (other.communicationProcviderId != null) + return false; + } else if (!communicationProcviderId.equals(other.communicationProcviderId)) + return false; + if (emailAddress == null) { + if (other.emailAddress != null) + return false; + } else if (!emailAddress.equals(other.emailAddress)) + return false; + if (firstName == null) { + if (other.firstName != null) + return false; + } else if (!firstName.equals(other.firstName)) + return false; + if (lastName == null) { + if (other.lastName != null) + return false; + } else if (!lastName.equals(other.lastName)) + return false; + if (middleName == null) { + if (other.middleName != null) + return false; + } else if (!middleName.equals(other.middleName)) + return false; + if (phoneNumber == null) { + if (other.phoneNumber != null) + return false; + } else if (!phoneNumber.equals(other.phoneNumber)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + if (status != other.status) + return false; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("UserInfo [profileId=").append(profileId).append(", accountId=").append(accountId) + .append(", firstName=").append(firstName).append(", lastName=").append(lastName).append(", middleName=") + .append(middleName).append(", username=").append(username).append(", phoneNumber=").append(phoneNumber) + .append(", emailAddress=").append(emailAddress).append(", status=").append(status) + .append(", authenticationType=").append(authenticationType).append(", authenticationProviderId=") + .append(authenticationProviderId).append(", avatar=[]").append(", communicationProcviderId=") + .append(communicationProcviderId).append("]").toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/UserInfoRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoRepository.java new file mode 100644 index 0000000..4612add --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoRepository.java @@ -0,0 +1,14 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import java.util.UUID; + +import org.springframework.data.repository.CrudRepository; + +import biz.nynja.account.grpc.models.UserInfo; + +public interface UserInfoRepository extends CrudRepository { + +} -- GitLab From 18053b06e28f96aeea8ae2e8a00118065e709c1a Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Wed, 8 Aug 2018 11:15:46 +0300 Subject: [PATCH 16/37] NY-2214: [BE] Get all accounts by email Create Cassandra Materialized view on startup that will be used to retrieve accounts by email Signed-off-by: Ralitsa Todorova --- .../grpc/configuration/CassandraConfig.java | 12 + .../account/grpc/models/UserInfoByEmail.java | 226 ++++++++++++++++++ .../UserInfoByEmailRepository.java | 17 ++ 3 files changed, 255 insertions(+) create mode 100644 src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/UserInfoByEmailRepository.java diff --git a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java index 11d22f1..b8369a6 100644 --- a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java +++ b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java @@ -42,4 +42,16 @@ public class CassandraConfig extends AbstractCassandraConfiguration { return Arrays.asList(specification); } + @Override + protected List getStartupScripts() { + + String script = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + getKeyspaceName() + + ".userinfo_by_email AS SELECT * FROM " + getKeyspaceName() + ".userinfo " + + "WHERE emailaddress IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " + + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(emailaddress, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; + + return Arrays.asList(script); + } } diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java b/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java new file mode 100644 index 0000000..0336dc2 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java @@ -0,0 +1,226 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.util.UUID; + +import org.springframework.data.cassandra.core.cql.PrimaryKeyType; +import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; +import org.springframework.data.cassandra.core.mapping.Table; + +@Table("userinfo_by_email") +public class UserInfoByEmail { + + @PrimaryKeyColumn(name = "emailaddress", ordinal = 0, type = PrimaryKeyType.PARTITIONED) + private String emailAddress; + @PrimaryKeyColumn(name = "profile_id", ordinal = 1, type = PrimaryKeyType.CLUSTERED) + private UUID profileId; + @PrimaryKeyColumn(name = "account_id", ordinal = 2, type = PrimaryKeyType.CLUSTERED) + private UUID accountId; + @PrimaryKeyColumn(name = "username", ordinal = 3, type = PrimaryKeyType.CLUSTERED) + private String username; + @PrimaryKeyColumn(name = "authentication_type", ordinal = 4, type = PrimaryKeyType.CLUSTERED) + private AuthenticationType authenticationType; + @PrimaryKeyColumn(name = "authnetication_provider_id", ordinal = 5, type = PrimaryKeyType.CLUSTERED) + private String authenticationProviderId; + private String firstName; + private String lastName; + private String middleName; + private String phoneNumber; + private Status status; + private String communicationProcviderId; + + public String getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public AuthenticationType getAuthenticationType() { + return authenticationType; + } + + public void setAuthenticationType(AuthenticationType authenticationType) { + this.authenticationType = authenticationType; + } + + public String getAuthenticationProviderId() { + return authenticationProviderId; + } + + public void setAuthenticationProviderId(String authenticationProviderId) { + this.authenticationProviderId = authenticationProviderId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getMiddleName() { + return middleName; + } + + public void setMiddleName(String middleName) { + this.middleName = middleName; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public String getCommunicationProcviderId() { + return communicationProcviderId; + } + + public void setCommunicationProcviderId(String communicationProcviderId) { + this.communicationProcviderId = communicationProcviderId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((authenticationProviderId == null) ? 0 : authenticationProviderId.hashCode()); + result = prime * result + ((authenticationType == null) ? 0 : authenticationType.hashCode()); + result = prime * result + ((communicationProcviderId == null) ? 0 : communicationProcviderId.hashCode()); + result = prime * result + ((emailAddress == null) ? 0 : emailAddress.hashCode()); + result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); + result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); + result = prime * result + ((middleName == null) ? 0 : middleName.hashCode()); + result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + UserInfoByEmail other = (UserInfoByEmail) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (authenticationProviderId == null) { + if (other.authenticationProviderId != null) + return false; + } else if (!authenticationProviderId.equals(other.authenticationProviderId)) + return false; + if (authenticationType != other.authenticationType) + return false; + if (communicationProcviderId == null) { + if (other.communicationProcviderId != null) + return false; + } else if (!communicationProcviderId.equals(other.communicationProcviderId)) + return false; + if (emailAddress == null) { + if (other.emailAddress != null) + return false; + } else if (!emailAddress.equals(other.emailAddress)) + return false; + if (firstName == null) { + if (other.firstName != null) + return false; + } else if (!firstName.equals(other.firstName)) + return false; + if (lastName == null) { + if (other.lastName != null) + return false; + } else if (!lastName.equals(other.lastName)) + return false; + if (middleName == null) { + if (other.middleName != null) + return false; + } else if (!middleName.equals(other.middleName)) + return false; + if (phoneNumber == null) { + if (other.phoneNumber != null) + return false; + } else if (!phoneNumber.equals(other.phoneNumber)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + if (status != other.status) + return false; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("UserInfoByEmail [emailAddress=").append(emailAddress).append(", profileId=") + .append(profileId).append(", accountId=").append(accountId).append(", username=").append(username) + .append(", authenticationType=").append(authenticationType).append(", authenticationProviderId=") + .append(authenticationProviderId).append(", firstName=").append(firstName).append(", lastName=") + .append(lastName).append(", middleName=").append(middleName).append(", phoneNumber=") + .append(phoneNumber).append(", status=").append(status).append(", communicationProcviderId=") + .append(communicationProcviderId).append("]").toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByEmailRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByEmailRepository.java new file mode 100644 index 0000000..56acce7 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByEmailRepository.java @@ -0,0 +1,17 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import java.util.List; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.UserInfoByEmail; + +@Repository +public interface UserInfoByEmailRepository extends CassandraRepository { + + List findByEmailAddress(String email); +} -- GitLab From 41b3b780dcad840941239ecb2ebc3656fd42f985 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Wed, 8 Aug 2018 19:47:47 +0300 Subject: [PATCH 17/37] NY-2208 [BE] Endpoint for creation of account and profile - update user info fields; - add create account endpoint; - add dependency for account.proto; Signed-off-by: abotev-intracol --- pom.xml | 2 +- .../GrpcServerHealthIndicator.java | 2 +- .../grpc/models/AuthenticationType.java | 8 --- .../grpc/models/CommunicationProvider.java | 2 + .../nynja/account/grpc/models/Profile.java | 2 + .../account/grpc/models/SocialIdentity.java | 2 + .../biz/nynja/account/grpc/models/Status.java | 8 --- .../account/grpc/models/UserAccount.java | 2 + .../nynja/account/grpc/models/UserInfo.java | 50 +++++++++++++++-- .../account/grpc/models/UserInfoByEmail.java | 3 ++ .../grpc/services/AccountServiceImpl.java | 53 ++++++++++++------- 11 files changed, 93 insertions(+), 41 deletions(-) delete mode 100644 src/main/java/biz/nynja/account/grpc/models/AuthenticationType.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/Status.java diff --git a/pom.xml b/pom.xml index 52f7604..7c49ac0 100644 --- a/pom.xml +++ b/pom.xml @@ -101,7 +101,7 @@ libs-snapshot-local.biz.nynja.protos - blueprint-java-intracoldev + account-service-intracoldev 1.0-SNAPSHOT diff --git a/src/main/java/biz/nynja/account/grpc/healthindicators/GrpcServerHealthIndicator.java b/src/main/java/biz/nynja/account/grpc/healthindicators/GrpcServerHealthIndicator.java index cf6cd2d..a20f920 100644 --- a/src/main/java/biz/nynja/account/grpc/healthindicators/GrpcServerHealthIndicator.java +++ b/src/main/java/biz/nynja/account/grpc/healthindicators/GrpcServerHealthIndicator.java @@ -14,7 +14,7 @@ import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; import biz.nynja.account.grpc.services.AccountServiceImpl; -import biz.nynja.blueprint.grpc.AccountServiceGrpc; +import biz.nynja.account.grpc.AccountServiceGrpc; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.health.v1.HealthCheckRequest; diff --git a/src/main/java/biz/nynja/account/grpc/models/AuthenticationType.java b/src/main/java/biz/nynja/account/grpc/models/AuthenticationType.java deleted file mode 100644 index c8afc57..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/AuthenticationType.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -public enum AuthenticationType { - PHONE, EMAIL, FACEBOOK, GOOGLEPLUS -} diff --git a/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java index ad76dbe..609050a 100644 --- a/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java +++ b/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java @@ -5,6 +5,8 @@ package biz.nynja.account.grpc.models; import java.util.UUID; +import biz.nynja.account.grpc.Status; + public class CommunicationProvider { private UUID providerId; diff --git a/src/main/java/biz/nynja/account/grpc/models/Profile.java b/src/main/java/biz/nynja/account/grpc/models/Profile.java index c07c871..1ed9fa1 100644 --- a/src/main/java/biz/nynja/account/grpc/models/Profile.java +++ b/src/main/java/biz/nynja/account/grpc/models/Profile.java @@ -6,6 +6,8 @@ package biz.nynja.account.grpc.models; import java.util.List; import java.util.UUID; +import biz.nynja.account.grpc.Status; + public class Profile { private UUID profileId; diff --git a/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java index 702969b..8024b04 100644 --- a/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java +++ b/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java @@ -5,6 +5,8 @@ package biz.nynja.account.grpc.models; import java.util.UUID; +import biz.nynja.account.grpc.Status; + public class SocialIdentity { private UUID socialIdentityId; diff --git a/src/main/java/biz/nynja/account/grpc/models/Status.java b/src/main/java/biz/nynja/account/grpc/models/Status.java deleted file mode 100644 index ff6d639..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/Status.java +++ /dev/null @@ -1,8 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -public enum Status { - ACTIVE, SUSPENDED, TERMINATED -} diff --git a/src/main/java/biz/nynja/account/grpc/models/UserAccount.java b/src/main/java/biz/nynja/account/grpc/models/UserAccount.java index 8917c07..b1ed1f1 100644 --- a/src/main/java/biz/nynja/account/grpc/models/UserAccount.java +++ b/src/main/java/biz/nynja/account/grpc/models/UserAccount.java @@ -8,6 +8,8 @@ import java.util.UUID; import javax.validation.constraints.Size; +import biz.nynja.account.grpc.Status; + public class UserAccount { private UUID accountId; diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfo.java b/src/main/java/biz/nynja/account/grpc/models/UserInfo.java index d0ea474..6e36309 100644 --- a/src/main/java/biz/nynja/account/grpc/models/UserInfo.java +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfo.java @@ -9,6 +9,10 @@ import org.springframework.data.cassandra.core.cql.PrimaryKeyType; import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; import org.springframework.data.cassandra.core.mapping.Table; +import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.CreateAccountRequest; +import biz.nynja.account.grpc.Status; + @Table public class UserInfo { @@ -23,6 +27,7 @@ public class UserInfo { private String username; private String phoneNumber; private String emailAddress; + private String password; private Status status; @PrimaryKeyColumn(name = "authentication_type", ordinal = 3, type = PrimaryKeyType.CLUSTERED) private AuthenticationType authenticationType; @@ -94,6 +99,14 @@ public class UserInfo { this.emailAddress = emailAddress; } + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + public Status getStatus() { return status; } @@ -142,6 +155,7 @@ public class UserInfo { result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); result = prime * result + ((status == null) ? 0 : status.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); + result = prime * result + ((password == null) ? 0 : password.hashCode()); return result; } @@ -208,6 +222,11 @@ public class UserInfo { return false; } else if (!username.equals(other.username)) return false; + if (password == null) { + if (other.password != null) + return false; + } else if (!password.equals(other.password)) + return false; return true; } @@ -215,11 +234,32 @@ public class UserInfo { public String toString() { return new StringBuilder("UserInfo [profileId=").append(profileId).append(", accountId=").append(accountId) .append(", firstName=").append(firstName).append(", lastName=").append(lastName).append(", middleName=") - .append(middleName).append(", username=").append(username).append(", phoneNumber=").append(phoneNumber) - .append(", emailAddress=").append(emailAddress).append(", status=").append(status) - .append(", authenticationType=").append(authenticationType).append(", authenticationProviderId=") - .append(authenticationProviderId).append(", avatar=[]").append(", communicationProcviderId=") - .append(communicationProcviderId).append("]").toString(); + .append(middleName).append(", username=").append(username).append(", password=").append(password) + .append(", phoneNumber=").append(phoneNumber).append(", emailAddress=").append(emailAddress) + .append(", status=").append(status).append(", authenticationType=").append(authenticationType) + .append(", authenticationProviderId=").append(authenticationProviderId).append(", avatar=[]") + .append(", communicationProcviderId=").append(communicationProcviderId).append("]").toString(); } + public static UserInfo fromProto(CreateAccountRequest proto) { + UserInfo userInfo = new UserInfo(); + userInfo.setUsername(proto.getUsername()); + userInfo.setEmailAddress(proto.getEmailAddress()); + userInfo.setFirstName(proto.getFirstName()); + userInfo.setLastName(proto.getLastName()); + userInfo.setMiddleName(proto.getMiddleName()); + userInfo.setPhoneNumber(proto.getPhoneNumber()); + userInfo.setAuthenticationType(proto.getAuthenticationType()); + userInfo.setStatus(proto.getStatus()); + userInfo.setPassword(proto.getPassword()); + return userInfo; + } + + public biz.nynja.account.grpc.AccountDetails toProto() { + return biz.nynja.account.grpc.AccountDetails.newBuilder().setAccountId(getAccountId().toString()) + .setProfileId(getProfileId().toString()).setUsername(getUsername()) + .setAuthenticationType(getAuthenticationType()).setEmailAddress(getEmailAddress()) + .setFirstName(getFirstName()).setLastName(getLastName()).setMiddleName(getMiddleName()) + .setPhoneNumber(getPhoneNumber()).setStatus(getStatus()).build(); + } } diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java b/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java index 0336dc2..f2b1e9b 100644 --- a/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java @@ -9,6 +9,9 @@ import org.springframework.data.cassandra.core.cql.PrimaryKeyType; import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; import org.springframework.data.cassandra.core.mapping.Table; +import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.Status; + @Table("userinfo_by_email") public class UserInfoByEmail { diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index d139529..d2c373e 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -3,18 +3,22 @@ */ package biz.nynja.account.grpc.services; +import java.util.UUID; + import org.lognet.springboot.grpc.GRpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import biz.nynja.account.grpc.models.AccountInfo; +import biz.nynja.account.grpc.AccountServiceGrpc; +import biz.nynja.account.grpc.CreateAccountRequest; +import biz.nynja.account.grpc.CreateAccountResponse; +import biz.nynja.account.grpc.ErrorResponse; +import biz.nynja.account.grpc.ErrorResponse.Cause; +import biz.nynja.account.grpc.models.UserInfo; import biz.nynja.account.grpc.repositories.AccountInfoRepository; -import biz.nynja.blueprint.grpc.AccountServiceGrpc; -import biz.nynja.blueprint.grpc.RegisterError; -import biz.nynja.blueprint.grpc.RegisterError.Cause; -import biz.nynja.blueprint.grpc.RegisterRequest; -import biz.nynja.blueprint.grpc.RegisterResponse; +import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; +import biz.nynja.account.grpc.repositories.UserInfoRepository; import io.grpc.stub.StreamObserver; /** @@ -25,32 +29,45 @@ import io.grpc.stub.StreamObserver; @GRpcService public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBase { - private static final Logger LOGGER = LoggerFactory.getLogger(AccountServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(AccountServiceImpl.class); + + @Autowired + private UserInfoRepository userInfoRepository; @Autowired - private AccountInfoRepository accountInfoRepository; + private UserInfoByEmailRepository userInfoByEmailRepository; @Override - public void register(RegisterRequest request, StreamObserver responseObserver) { + public void createAccount(CreateAccountRequest request, StreamObserver responseObserver) { - LOGGER.info("Got a gRPC service request: {}", request); + logger.info("Creating account..."); + logger.debug("Creating account: {} ...", request); - AccountInfo accountInfo = AccountInfo.fromProto(request); + UserInfo userInfo = UserInfo.fromProto(request); + userInfo.setProfileId(UUID.randomUUID()); + userInfo.setAccountId(UUID.randomUUID()); + // TODO set authentication provider + userInfo.setAuthenticationProviderId("id"); - if (accountInfoRepository.findByEmail(accountInfo.getEmail()) != null) { - responseObserver.onNext(RegisterResponse.newBuilder() - .setError(RegisterError.newBuilder().setCause(Cause.EMAIL_ALREADY_USED)).build()); + if (!userInfoByEmailRepository.findByEmailAddress(userInfo.getEmailAddress()).isEmpty()) { + responseObserver.onNext(CreateAccountResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.EMAIL_ALREADY_USED)).build()); responseObserver.onCompleted(); return; } - AccountInfo savedAccount = accountInfoRepository.save(accountInfo); - LOGGER.info("Account \"{}\" saved into the DB", savedAccount.toString()); + UserInfo savedAccount = userInfoRepository.save(userInfo); + logger.debug("Account \"{}\" saved into the DB", savedAccount.toString()); - RegisterResponse response = RegisterResponse.newBuilder().setAccount(savedAccount.toProto()).build(); - LOGGER.info("Response from gRPC service: \"{}\"", response); + CreateAccountResponse response = CreateAccountResponse.newBuilder().setAccountDetails(savedAccount.toProto()) + .build(); + logger.info("Account created successfully."); + logger.debug("Account: \"{}\" created successfully.", response); responseObserver.onNext(response); responseObserver.onCompleted(); + + return; } + } -- GitLab From 5ad976cf3a5ccf58ef30d861223af7aea03d5a89 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Wed, 8 Aug 2018 19:56:49 +0300 Subject: [PATCH 18/37] NY-2208 [BE] Endpoint for creation of account and profile - remove unnecessary unit tests; Signed-off-by: abotev-intracol --- .../account/grpc/models/AccountInfo.java | 72 ------------------- .../repositories/AccountInfoRepository.java | 18 ----- .../grpc/services/AccountServiceImpl.java | 1 - .../grpc/services/AccountServiceTests.java | 55 -------------- .../biz/nynja/account/grpc/utils/Util.java | 29 ++++---- 5 files changed, 15 insertions(+), 160 deletions(-) delete mode 100644 src/main/java/biz/nynja/account/grpc/models/AccountInfo.java delete mode 100644 src/main/java/biz/nynja/account/grpc/repositories/AccountInfoRepository.java diff --git a/src/main/java/biz/nynja/account/grpc/models/AccountInfo.java b/src/main/java/biz/nynja/account/grpc/models/AccountInfo.java deleted file mode 100644 index b87e269..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/AccountInfo.java +++ /dev/null @@ -1,72 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -import org.springframework.data.cassandra.core.mapping.PrimaryKey; -import org.springframework.data.cassandra.core.mapping.Table; - -import biz.nynja.blueprint.grpc.RegisterRequest; - -/** - * Account bean as represented in the corresponding Cassandra table. - */ -@Table -public class AccountInfo { - - @PrimaryKey - private Long id; - private String email; - private String password; - - public AccountInfo() { - } - - public AccountInfo(long id, String email, String password) { - this.id = id; - this.email = email; - this.password = password; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - @Override - public String toString() { - return "AccountInfo [id=" + id + ", email=" + email + ", password=" + password + "]"; - } - - public static AccountInfo fromProto(RegisterRequest proto) { - AccountInfo accountInfo = new AccountInfo(); - accountInfo.setId(proto.getId()); - accountInfo.setEmail(proto.getEmail()); - accountInfo.setPassword(proto.getPassword()); - return accountInfo; - } - - public biz.nynja.blueprint.grpc.AccountInfo toProto() { - return biz.nynja.blueprint.grpc.AccountInfo.newBuilder().setId(Long.toString(getId())).setEmail(getEmail()) - .build(); - } -} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/AccountInfoRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/AccountInfoRepository.java deleted file mode 100644 index 5fc9537..0000000 --- a/src/main/java/biz/nynja/account/grpc/repositories/AccountInfoRepository.java +++ /dev/null @@ -1,18 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.repositories; - -import org.springframework.data.cassandra.repository.Query; -import org.springframework.data.repository.CrudRepository; - -import biz.nynja.account.grpc.models.AccountInfo; - -/** - * Account repository for Cassandra operations/queries. - */ -public interface AccountInfoRepository extends CrudRepository { - - @Query(value = "SELECT * FROM AccountInfo WHERE email=?0 ALLOW FILTERING") - public AccountInfo findByEmail(String email); -} diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index d2c373e..cdc67c5 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -16,7 +16,6 @@ import biz.nynja.account.grpc.CreateAccountResponse; import biz.nynja.account.grpc.ErrorResponse; import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.models.UserInfo; -import biz.nynja.account.grpc.repositories.AccountInfoRepository; import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; import biz.nynja.account.grpc.repositories.UserInfoRepository; import io.grpc.stub.StreamObserver; diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index 43af81b..2c9ca7a 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -3,32 +3,17 @@ */ package biz.nynja.account.grpc.services; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; - -import java.util.Optional; import java.util.concurrent.ExecutionException; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; -import biz.nynja.account.grpc.models.AccountInfo; -import biz.nynja.account.grpc.repositories.AccountInfoRepository; import biz.nynja.account.grpc.utils.GrpcServerTestBase; import biz.nynja.account.grpc.utils.Util; -import biz.nynja.blueprint.grpc.AccountServiceGrpc; -import biz.nynja.blueprint.grpc.RegisterError; -import biz.nynja.blueprint.grpc.RegisterRequest; -import biz.nynja.blueprint.grpc.RegisterResponse; /** * AccountService unit tests. @@ -43,54 +28,14 @@ import biz.nynja.blueprint.grpc.RegisterResponse; @ActiveProfiles("dev") public class AccountServiceTests extends GrpcServerTestBase { - @MockBean - private AccountInfoRepository accountInfoRepository; - - @Autowired - @Qualifier("newAccount") - private AccountInfo accountInfo; - - @Autowired - @Qualifier("savedAccount") - private AccountInfo savedAccount; - @Test public void testRegister() throws ExecutionException, InterruptedException { - String testEmail = "email@test.com"; - final RegisterRequest request = RegisterRequest.newBuilder().setId(1).setEmail(testEmail).setPassword("abc") - .build(); - - given(accountInfoRepository.findByEmail(testEmail)).willReturn(null); - given(accountInfoRepository.save(any(AccountInfo.class))).willReturn(savedAccount); - - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc - .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - - final RegisterResponse reply = accountServiceBlockingStub.register(request); - - assertNotNull("Reply should not be null", reply); - assertTrue(String.format("Reply should contain email '%s'", testEmail), - reply.getAccount().getEmail().equals(testEmail)); } @Test public void testRegisterExistEmail() throws ExecutionException, InterruptedException { - String testEmail = "email@test.com"; - final RegisterRequest request = RegisterRequest.newBuilder().setId(1).setEmail(testEmail).setPassword("abc") - .build(); - - given(accountInfoRepository.findByEmail(testEmail)).willReturn(accountInfo); - - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc - .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - - final RegisterResponse reply = accountServiceBlockingStub.register(request); - - assertNotNull("Reply should not be null", reply); - assertTrue(String.format("Reply should contain cause '%s'", testEmail), - reply.getError().getCause().equals(RegisterError.Cause.EMAIL_ALREADY_USED)); } } diff --git a/src/test/java/biz/nynja/account/grpc/utils/Util.java b/src/test/java/biz/nynja/account/grpc/utils/Util.java index 101aa24..83873d4 100644 --- a/src/test/java/biz/nynja/account/grpc/utils/Util.java +++ b/src/test/java/biz/nynja/account/grpc/utils/Util.java @@ -4,10 +4,12 @@ package biz.nynja.account.grpc.utils; +import java.util.UUID; + import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; -import biz.nynja.account.grpc.models.AccountInfo;; +import biz.nynja.account.grpc.models.UserInfo;; /** * Unit tests variables, beans and help methods. @@ -16,25 +18,24 @@ import biz.nynja.account.grpc.models.AccountInfo;; @TestConfiguration public class Util { - public static final Long ID = 1L; + public static final String ID = "id1"; public static final String EMAIL = "email@test.com"; public static final String PASSWORD = "abc"; @Bean - public AccountInfo newAccount() { - AccountInfo accountInfo = new AccountInfo(); - accountInfo.setId(ID); - accountInfo.setEmail(EMAIL); - accountInfo.setPassword(PASSWORD); - return accountInfo; + public UserInfo newAccount() { + UserInfo userInfo = new UserInfo(); + userInfo.setEmailAddress(EMAIL); + userInfo.setPassword(PASSWORD); + return userInfo; } @Bean - public AccountInfo savedAccount() { - AccountInfo accountInfo = new AccountInfo(); - accountInfo.setId(ID); - accountInfo.setEmail(EMAIL); - accountInfo.setPassword(PASSWORD); - return accountInfo; + public UserInfo savedAccount() { + UserInfo userInfo = new UserInfo(); + userInfo.setAccountId(UUID.fromString(ID)); + userInfo.setEmailAddress(EMAIL); + userInfo.setPassword(PASSWORD); + return userInfo; } } -- GitLab From e618f6e2e04c07e8de2756edd526d0fc57695187 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Thu, 9 Aug 2018 11:19:46 +0300 Subject: [PATCH 19/37] NY-2213 [BE] Get all accounts by phone number - add materialized view; - add additional table; - add repository; - add object; - add email validation when create account; Signed-off-by: abotev-intracol --- .../grpc/configuration/CassandraConfig.java | 11 +- .../account/grpc/models/UserInfoByPhone.java | 226 ++++++++++++++++++ .../UserInfoByPhoneRepository.java | 15 ++ .../grpc/services/AccountServiceImpl.java | 17 +- 4 files changed, 266 insertions(+), 3 deletions(-) create mode 100644 src/main/java/biz/nynja/account/grpc/models/UserInfoByPhone.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/UserInfoByPhoneRepository.java diff --git a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java index b8369a6..babf740 100644 --- a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java +++ b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java @@ -45,13 +45,20 @@ public class CassandraConfig extends AbstractCassandraConfiguration { @Override protected List getStartupScripts() { - String script = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + getKeyspaceName() + String scriptViewEmail = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + getKeyspaceName() + ".userinfo_by_email AS SELECT * FROM " + getKeyspaceName() + ".userinfo " + "WHERE emailaddress IS NOT NULL and authnetication_provider_id IS NOT NULL " + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " + "and authentication_type IS NOT NULL " + "PRIMARY KEY(emailaddress, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; - return Arrays.asList(script); + String scriptViewPhone = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + getKeyspaceName() + + ".userinfo_by_phone AS SELECT * FROM " + getKeyspaceName() + ".userinfo " + + "WHERE phonenumber IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " + + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(phonenumber, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; + + return Arrays.asList(scriptViewEmail, scriptViewPhone); } } diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfoByPhone.java b/src/main/java/biz/nynja/account/grpc/models/UserInfoByPhone.java new file mode 100644 index 0000000..5dc4b17 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfoByPhone.java @@ -0,0 +1,226 @@ +package biz.nynja.account.grpc.models; + +import java.util.UUID; + +import org.springframework.data.cassandra.core.cql.PrimaryKeyType; +import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; +import org.springframework.data.cassandra.core.mapping.Table; + +import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.Status; + +@Table("userinfo_by_phone") +public class UserInfoByPhone { + + @PrimaryKeyColumn(name = "phonenumber", ordinal = 0, type = PrimaryKeyType.PARTITIONED) + private String phoneNumber; + @PrimaryKeyColumn(name = "profile_id", ordinal = 1, type = PrimaryKeyType.CLUSTERED) + private UUID profileId; + @PrimaryKeyColumn(name = "account_id", ordinal = 2, type = PrimaryKeyType.CLUSTERED) + private UUID accountId; + @PrimaryKeyColumn(name = "username", ordinal = 3, type = PrimaryKeyType.CLUSTERED) + private String username; + @PrimaryKeyColumn(name = "authentication_type", ordinal = 4, type = PrimaryKeyType.CLUSTERED) + private AuthenticationType authenticationType; + @PrimaryKeyColumn(name = "authnetication_provider_id", ordinal = 5, type = PrimaryKeyType.CLUSTERED) + private String authenticationProviderId; + private String firstName; + private String lastName; + private String middleName; + private String emailAddress; + private Status status; + private String communicationProcviderId; + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public AuthenticationType getAuthenticationType() { + return authenticationType; + } + + public void setAuthenticationType(AuthenticationType authenticationType) { + this.authenticationType = authenticationType; + } + + public String getAuthenticationProviderId() { + return authenticationProviderId; + } + + public void setAuthenticationProviderId(String authenticationProviderId) { + this.authenticationProviderId = authenticationProviderId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getMiddleName() { + return middleName; + } + + public void setMiddleName(String middleName) { + this.middleName = middleName; + } + + public String getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public String getCommunicationProcviderId() { + return communicationProcviderId; + } + + public void setCommunicationProcviderId(String communicationProcviderId) { + this.communicationProcviderId = communicationProcviderId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((authenticationProviderId == null) ? 0 : authenticationProviderId.hashCode()); + result = prime * result + ((authenticationType == null) ? 0 : authenticationType.hashCode()); + result = prime * result + ((communicationProcviderId == null) ? 0 : communicationProcviderId.hashCode()); + result = prime * result + ((emailAddress == null) ? 0 : emailAddress.hashCode()); + result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); + result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); + result = prime * result + ((middleName == null) ? 0 : middleName.hashCode()); + result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + UserInfoByPhone other = (UserInfoByPhone) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (authenticationProviderId == null) { + if (other.authenticationProviderId != null) + return false; + } else if (!authenticationProviderId.equals(other.authenticationProviderId)) + return false; + if (authenticationType != other.authenticationType) + return false; + if (communicationProcviderId == null) { + if (other.communicationProcviderId != null) + return false; + } else if (!communicationProcviderId.equals(other.communicationProcviderId)) + return false; + if (emailAddress == null) { + if (other.emailAddress != null) + return false; + } else if (!emailAddress.equals(other.emailAddress)) + return false; + if (firstName == null) { + if (other.firstName != null) + return false; + } else if (!firstName.equals(other.firstName)) + return false; + if (lastName == null) { + if (other.lastName != null) + return false; + } else if (!lastName.equals(other.lastName)) + return false; + if (middleName == null) { + if (other.middleName != null) + return false; + } else if (!middleName.equals(other.middleName)) + return false; + if (phoneNumber == null) { + if (other.phoneNumber != null) + return false; + } else if (!phoneNumber.equals(other.phoneNumber)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + if (status != other.status) + return false; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("UserInfoByEmail [emailAddress=").append(emailAddress).append(", profileId=") + .append(profileId).append(", accountId=").append(accountId).append(", username=").append(username) + .append(", authenticationType=").append(authenticationType).append(", authenticationProviderId=") + .append(authenticationProviderId).append(", firstName=").append(firstName).append(", lastName=") + .append(lastName).append(", middleName=").append(middleName).append(", phoneNumber=") + .append(phoneNumber).append(", status=").append(status).append(", communicationProcviderId=") + .append(communicationProcviderId).append("]").toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByPhoneRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByPhoneRepository.java new file mode 100644 index 0000000..756f1af --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByPhoneRepository.java @@ -0,0 +1,15 @@ +package biz.nynja.account.grpc.repositories; + +import java.util.List; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.UserInfoByPhone; + +@Repository +public interface UserInfoByPhoneRepository extends CassandraRepository{ + + List findByPhoneNumber(String email); + +} diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index cdc67c5..699c435 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -9,12 +9,14 @@ import org.lognet.springboot.grpc.GRpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; - import biz.nynja.account.grpc.AccountServiceGrpc; +import biz.nynja.account.grpc.AccountsByAuthenticationProviderRequest; import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.CreateAccountResponse; import biz.nynja.account.grpc.ErrorResponse; import biz.nynja.account.grpc.ErrorResponse.Cause; +import biz.nynja.account.grpc.GetAccountsResponse; +import biz.nynja.account.grpc.components.Validator; import biz.nynja.account.grpc.models.UserInfo; import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; import biz.nynja.account.grpc.repositories.UserInfoRepository; @@ -36,6 +38,9 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas @Autowired private UserInfoByEmailRepository userInfoByEmailRepository; + @Autowired + private Validator validator; + @Override public void createAccount(CreateAccountRequest request, StreamObserver responseObserver) { @@ -48,6 +53,13 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas // TODO set authentication provider userInfo.setAuthenticationProviderId("id"); + if(userInfo.getEmailAddress() == null || !validator.isEmailValid(userInfo.getEmailAddress())) { + responseObserver.onNext(CreateAccountResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.EMAIL_INVALID)).build()); + responseObserver.onCompleted(); + return; + } + if (!userInfoByEmailRepository.findByEmailAddress(userInfo.getEmailAddress()).isEmpty()) { responseObserver.onNext(CreateAccountResponse.newBuilder() .setError(ErrorResponse.newBuilder().setCause(Cause.EMAIL_ALREADY_USED)).build()); @@ -69,4 +81,7 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas return; } + @Override + public void getAllAccountsByAuthenticationProvider(AccountsByAuthenticationProviderRequest request, StreamObserver responseObserver) { + } } -- GitLab From f12bff6841508963b8939009e17090b59e0a7a37 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Thu, 9 Aug 2018 17:03:12 +0300 Subject: [PATCH 20/37] NY-2208 [BE] Endpoint for creation of account and profile - fix request validation; Signed-off-by: abotev-intracol --- pom.xml | 2 +- .../account/grpc/components/Validator.java | 44 ++++ .../grpc/configuration/CassandraConfig.java | 9 +- .../grpc/models/UserInfoByUsername.java | 226 ++++++++++++++++++ .../UserInfoByPhoneRepository.java | 2 +- .../UserInfoByUsernameRepository.java | 15 ++ .../grpc/services/AccountServiceImpl.java | 27 +-- 7 files changed, 304 insertions(+), 21 deletions(-) create mode 100644 src/main/java/biz/nynja/account/grpc/models/UserInfoByUsername.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/UserInfoByUsernameRepository.java diff --git a/pom.xml b/pom.xml index 7c49ac0..b28a877 100644 --- a/pom.xml +++ b/pom.xml @@ -22,7 +22,7 @@ UTF-8 UTF-8 - 1.10 + 10 2.3.2 diff --git a/src/main/java/biz/nynja/account/grpc/components/Validator.java b/src/main/java/biz/nynja/account/grpc/components/Validator.java index 0565c49..ac1366d 100644 --- a/src/main/java/biz/nynja/account/grpc/components/Validator.java +++ b/src/main/java/biz/nynja/account/grpc/components/Validator.java @@ -15,11 +15,17 @@ import javax.annotation.PostConstruct; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Component; +import biz.nynja.account.grpc.CreateAccountRequest; +import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.models.CountryInfo; +import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; +import biz.nynja.account.grpc.repositories.UserInfoByPhoneRepository; +import biz.nynja.account.grpc.repositories.UserInfoByUsernameRepository; /** * Component which contains all validation methods. @@ -32,6 +38,15 @@ public class Validator { private HashMap countryInfoMap; + @Autowired + private UserInfoByEmailRepository userInfoByEmailRepository; + + @Autowired + private UserInfoByPhoneRepository userInfoByPhoneRepository; + + @Autowired + private UserInfoByUsernameRepository userInfoByUsernameRepository; + @PostConstruct public void loadPhonesBook() { @@ -134,4 +149,33 @@ public class Validator { return isValid; } + public Cause validateCreateAccountRequest(CreateAccountRequest request) { + + if (request.getUsername() != null && !request.getUsername().trim().isEmpty() + && !isUsernameValid(request.getUsername())) { + return Cause.USERNAME_INVALID; + } else if (request.getUsername() != null && !request.getUsername().trim().isEmpty() + && !userInfoByUsernameRepository.findByUsername(request.getUsername()).isEmpty()) { + return Cause.USERNAME_ALREADY_USED; + } + + if (request.getEmailAddress() != null && !request.getEmailAddress().trim().isEmpty() + && !isEmailValid(request.getEmailAddress())) { + return Cause.EMAIL_INVALID; + } else if (request.getEmailAddress() != null && !request.getEmailAddress().trim().isEmpty() + && !userInfoByEmailRepository.findByEmailAddress(request.getEmailAddress()).isEmpty()) { + return Cause.EMAIL_ALREADY_USED; + } + + if (request.getPhoneNumber() != null && !request.getPhoneNumber().trim().isEmpty() + && !isPhoneNumberValid(request.getPhoneNumber(), request.getCountryCode())) { + return Cause.PHONE_NUMBER_INVALID; + } else if (request.getPhoneNumber() != null && !request.getPhoneNumber().trim().isEmpty() + && !userInfoByPhoneRepository.findByPhoneNumber(request.getPhoneNumber()).isEmpty()) { + return Cause.PHONE_NUMBER_ALREADY_USED; + } + + return null; + } + } diff --git a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java index babf740..c12435f 100644 --- a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java +++ b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java @@ -59,6 +59,13 @@ public class CassandraConfig extends AbstractCassandraConfiguration { + "and authentication_type IS NOT NULL " + "PRIMARY KEY(phonenumber, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; - return Arrays.asList(scriptViewEmail, scriptViewPhone); + String scriptViewUsername = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + getKeyspaceName() + + ".userinfo_by_username AS SELECT * FROM " + getKeyspaceName() + ".userinfo " + + "WHERE username IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL " + + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(username, authnetication_provider_id, profile_id, account_id, authentication_type) "; + + return Arrays.asList(scriptViewEmail, scriptViewPhone, scriptViewUsername); } } diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfoByUsername.java b/src/main/java/biz/nynja/account/grpc/models/UserInfoByUsername.java new file mode 100644 index 0000000..2b22a75 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfoByUsername.java @@ -0,0 +1,226 @@ +package biz.nynja.account.grpc.models; + +import java.util.UUID; + +import org.springframework.data.cassandra.core.cql.PrimaryKeyType; +import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; +import org.springframework.data.cassandra.core.mapping.Table; + +import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.Status; + +@Table("userinfo_by_username") +public class UserInfoByUsername { + + @PrimaryKeyColumn(name = "username", ordinal = 0, type = PrimaryKeyType.PARTITIONED) + private String username; + @PrimaryKeyColumn(name = "profile_id", ordinal = 1, type = PrimaryKeyType.CLUSTERED) + private UUID profileId; + @PrimaryKeyColumn(name = "account_id", ordinal = 2, type = PrimaryKeyType.CLUSTERED) + private UUID accountId; + @PrimaryKeyColumn(name = "phonenumber", ordinal = 3, type = PrimaryKeyType.CLUSTERED) + private String phoneNumber; + @PrimaryKeyColumn(name = "authentication_type", ordinal = 4, type = PrimaryKeyType.CLUSTERED) + private AuthenticationType authenticationType; + @PrimaryKeyColumn(name = "authnetication_provider_id", ordinal = 5, type = PrimaryKeyType.CLUSTERED) + private String authenticationProviderId; + private String firstName; + private String lastName; + private String middleName; + private String emailAddress; + private Status status; + private String communicationProcviderId; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public String getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(String phoneNumber) { + this.phoneNumber = phoneNumber; + } + + public AuthenticationType getAuthenticationType() { + return authenticationType; + } + + public void setAuthenticationType(AuthenticationType authenticationType) { + this.authenticationType = authenticationType; + } + + public String getAuthenticationProviderId() { + return authenticationProviderId; + } + + public void setAuthenticationProviderId(String authenticationProviderId) { + this.authenticationProviderId = authenticationProviderId; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getMiddleName() { + return middleName; + } + + public void setMiddleName(String middleName) { + this.middleName = middleName; + } + + public String getEmailAddress() { + return emailAddress; + } + + public void setEmailAddress(String emailAddress) { + this.emailAddress = emailAddress; + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } + + public String getCommunicationProcviderId() { + return communicationProcviderId; + } + + public void setCommunicationProcviderId(String communicationProcviderId) { + this.communicationProcviderId = communicationProcviderId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((authenticationProviderId == null) ? 0 : authenticationProviderId.hashCode()); + result = prime * result + ((authenticationType == null) ? 0 : authenticationType.hashCode()); + result = prime * result + ((communicationProcviderId == null) ? 0 : communicationProcviderId.hashCode()); + result = prime * result + ((emailAddress == null) ? 0 : emailAddress.hashCode()); + result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); + result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); + result = prime * result + ((middleName == null) ? 0 : middleName.hashCode()); + result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + result = prime * result + ((status == null) ? 0 : status.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + UserInfoByUsername other = (UserInfoByUsername) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (authenticationProviderId == null) { + if (other.authenticationProviderId != null) + return false; + } else if (!authenticationProviderId.equals(other.authenticationProviderId)) + return false; + if (authenticationType != other.authenticationType) + return false; + if (communicationProcviderId == null) { + if (other.communicationProcviderId != null) + return false; + } else if (!communicationProcviderId.equals(other.communicationProcviderId)) + return false; + if (emailAddress == null) { + if (other.emailAddress != null) + return false; + } else if (!emailAddress.equals(other.emailAddress)) + return false; + if (firstName == null) { + if (other.firstName != null) + return false; + } else if (!firstName.equals(other.firstName)) + return false; + if (lastName == null) { + if (other.lastName != null) + return false; + } else if (!lastName.equals(other.lastName)) + return false; + if (middleName == null) { + if (other.middleName != null) + return false; + } else if (!middleName.equals(other.middleName)) + return false; + if (phoneNumber == null) { + if (other.phoneNumber != null) + return false; + } else if (!phoneNumber.equals(other.phoneNumber)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + if (status != other.status) + return false; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("UserInfoByEmail [emailAddress=").append(emailAddress).append(", profileId=") + .append(profileId).append(", accountId=").append(accountId).append(", username=").append(username) + .append(", authenticationType=").append(authenticationType).append(", authenticationProviderId=") + .append(authenticationProviderId).append(", firstName=").append(firstName).append(", lastName=") + .append(lastName).append(", middleName=").append(middleName).append(", phoneNumber=") + .append(phoneNumber).append(", status=").append(status).append(", communicationProcviderId=") + .append(communicationProcviderId).append("]").toString(); + } + +} \ No newline at end of file diff --git a/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByPhoneRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByPhoneRepository.java index 756f1af..a05e46e 100644 --- a/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByPhoneRepository.java +++ b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByPhoneRepository.java @@ -10,6 +10,6 @@ import biz.nynja.account.grpc.models.UserInfoByPhone; @Repository public interface UserInfoByPhoneRepository extends CassandraRepository{ - List findByPhoneNumber(String email); + List findByPhoneNumber(String phoneNumber); } diff --git a/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByUsernameRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByUsernameRepository.java new file mode 100644 index 0000000..32f381e --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoByUsernameRepository.java @@ -0,0 +1,15 @@ +package biz.nynja.account.grpc.repositories; + +import java.util.List; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.UserInfoByUsername; + +@Repository +public interface UserInfoByUsernameRepository extends CassandraRepository { + + List findByUsername(String username); + +} diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index 699c435..87874da 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -9,6 +9,7 @@ import org.lognet.springboot.grpc.GRpcService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; + import biz.nynja.account.grpc.AccountServiceGrpc; import biz.nynja.account.grpc.AccountsByAuthenticationProviderRequest; import biz.nynja.account.grpc.CreateAccountRequest; @@ -18,7 +19,6 @@ import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.GetAccountsResponse; import biz.nynja.account.grpc.components.Validator; import biz.nynja.account.grpc.models.UserInfo; -import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; import biz.nynja.account.grpc.repositories.UserInfoRepository; import io.grpc.stub.StreamObserver; @@ -35,9 +35,6 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas @Autowired private UserInfoRepository userInfoRepository; - @Autowired - private UserInfoByEmailRepository userInfoByEmailRepository; - @Autowired private Validator validator; @@ -47,25 +44,19 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas logger.info("Creating account..."); logger.debug("Creating account: {} ...", request); - UserInfo userInfo = UserInfo.fromProto(request); - userInfo.setProfileId(UUID.randomUUID()); - userInfo.setAccountId(UUID.randomUUID()); - // TODO set authentication provider - userInfo.setAuthenticationProviderId("id"); - - if(userInfo.getEmailAddress() == null || !validator.isEmailValid(userInfo.getEmailAddress())) { + Cause validationCause = validator.validateCreateAccountRequest(request); + if (validationCause != null) { responseObserver.onNext(CreateAccountResponse.newBuilder() - .setError(ErrorResponse.newBuilder().setCause(Cause.EMAIL_INVALID)).build()); + .setError(ErrorResponse.newBuilder().setCause(validationCause)).build()); responseObserver.onCompleted(); return; } - if (!userInfoByEmailRepository.findByEmailAddress(userInfo.getEmailAddress()).isEmpty()) { - responseObserver.onNext(CreateAccountResponse.newBuilder() - .setError(ErrorResponse.newBuilder().setCause(Cause.EMAIL_ALREADY_USED)).build()); - responseObserver.onCompleted(); - return; - } + UserInfo userInfo = UserInfo.fromProto(request); + userInfo.setProfileId(UUID.randomUUID()); + userInfo.setAccountId(UUID.randomUUID()); + // TODO set authentication provider + userInfo.setAuthenticationProviderId("id"); UserInfo savedAccount = userInfoRepository.save(userInfo); logger.debug("Account \"{}\" saved into the DB", savedAccount.toString()); -- GitLab From df8181d6bf0175b23e98279e3a0dd28d53fa97ec Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Thu, 9 Aug 2018 18:43:57 +0300 Subject: [PATCH 21/37] NY-2212: [BE] Get all accounts by profile ID Signed-off-by: Stanimir Penkov --- .../nynja/account/grpc/models/UserInfo.java | 3 ++ .../grpc/repositories/UserInfoRepository.java | 7 ++-- .../grpc/services/AccountServiceImpl.java | 35 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfo.java b/src/main/java/biz/nynja/account/grpc/models/UserInfo.java index 6e36309..027a652 100644 --- a/src/main/java/biz/nynja/account/grpc/models/UserInfo.java +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfo.java @@ -35,6 +35,9 @@ public class UserInfo { private String authenticationProviderId; private String communicationProcviderId; + public UserInfo() { + } + public UUID getProfileId() { return profileId; } diff --git a/src/main/java/biz/nynja/account/grpc/repositories/UserInfoRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoRepository.java index 4612add..550cac2 100644 --- a/src/main/java/biz/nynja/account/grpc/repositories/UserInfoRepository.java +++ b/src/main/java/biz/nynja/account/grpc/repositories/UserInfoRepository.java @@ -3,12 +3,15 @@ */ package biz.nynja.account.grpc.repositories; +import java.util.List; import java.util.UUID; -import org.springframework.data.repository.CrudRepository; +import org.springframework.data.cassandra.repository.CassandraRepository; import biz.nynja.account.grpc.models.UserInfo; -public interface UserInfoRepository extends CrudRepository { +public interface UserInfoRepository extends CassandraRepository { + + public List findAllByProfileId(UUID profileId); } diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index 87874da..36d992c 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -3,6 +3,8 @@ */ package biz.nynja.account.grpc.services; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; import org.lognet.springboot.grpc.GRpcService; @@ -10,8 +12,11 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import biz.nynja.account.grpc.AccountDetails; import biz.nynja.account.grpc.AccountServiceGrpc; import biz.nynja.account.grpc.AccountsByAuthenticationProviderRequest; +import biz.nynja.account.grpc.AccountsByProfileIdRequest; +import biz.nynja.account.grpc.AccountsResponse; import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.CreateAccountResponse; import biz.nynja.account.grpc.ErrorResponse; @@ -75,4 +80,34 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas @Override public void getAllAccountsByAuthenticationProvider(AccountsByAuthenticationProviderRequest request, StreamObserver responseObserver) { } + + @Override + public void getAllAccountsByProfileId(AccountsByProfileIdRequest request, StreamObserver responseObserver) { + + logger.info("Getting accounts by profile ID..."); + + List listAccountsByProfileId = userInfoRepository.findAllByProfileId(UUID.fromString(request.getProfileId())); + List responseList = new ArrayList (); + + if (listAccountsByProfileId.size() == 0) { + responseObserver.onNext(GetAccountsResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(Cause.ACCOUNT_NOT_FOUND)).build()); + responseObserver.onCompleted(); + return; + } + + for(UserInfo userInfo : listAccountsByProfileId) { + responseList.add(userInfo.toProto()); + } + + GetAccountsResponse response = GetAccountsResponse.newBuilder().setAccountsResponse(AccountsResponse.newBuilder().addAllAccountDetails(responseList)).build(); + + logger.debug("Returned response: \"{}\".", response); + + responseObserver.onNext(response); + responseObserver.onCompleted(); + + return; + + } + } -- GitLab From a36c8a4605b2e26d0fb08b89b8cf44e55a2b79ce Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Thu, 9 Aug 2018 20:03:53 +0300 Subject: [PATCH 22/37] NY-2213, NY-2214: Get all accounts by Authentication provider Added implementation to get all accounts by email or phone number Signed-off-by: Ralitsa Todorova --- .../account/grpc/models/UserInfoByEmail.java | 21 ++++ .../account/grpc/models/UserInfoByPhone.java | 21 ++++ .../grpc/services/AccountServiceImpl.java | 118 +++++++++++++++--- 3 files changed, 142 insertions(+), 18 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java b/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java index f2b1e9b..bbdf000 100644 --- a/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfoByEmail.java @@ -10,6 +10,7 @@ import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; import org.springframework.data.cassandra.core.mapping.Table; import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.Status; @Table("userinfo_by_email") @@ -226,4 +227,24 @@ public class UserInfoByEmail { .append(communicationProcviderId).append("]").toString(); } + public static UserInfoByEmail fromProto(CreateAccountRequest proto) { + UserInfoByEmail userInfo = new UserInfoByEmail(); + userInfo.setUsername(proto.getUsername()); + userInfo.setEmailAddress(proto.getEmailAddress()); + userInfo.setFirstName(proto.getFirstName()); + userInfo.setLastName(proto.getLastName()); + userInfo.setMiddleName(proto.getMiddleName()); + userInfo.setPhoneNumber(proto.getPhoneNumber()); + userInfo.setAuthenticationType(proto.getAuthenticationType()); + userInfo.setStatus(proto.getStatus()); + return userInfo; + } + + public biz.nynja.account.grpc.AccountDetails toProto() { + return biz.nynja.account.grpc.AccountDetails.newBuilder().setAccountId(getAccountId().toString()) + .setProfileId(getProfileId().toString()).setUsername(getUsername()) + .setAuthenticationType(getAuthenticationType()).setEmailAddress(getEmailAddress()) + .setFirstName(getFirstName()).setLastName(getLastName()).setMiddleName(getMiddleName()) + .setPhoneNumber(getPhoneNumber()).setStatus(getStatus()).build(); + } } diff --git a/src/main/java/biz/nynja/account/grpc/models/UserInfoByPhone.java b/src/main/java/biz/nynja/account/grpc/models/UserInfoByPhone.java index 5dc4b17..5b80bd4 100644 --- a/src/main/java/biz/nynja/account/grpc/models/UserInfoByPhone.java +++ b/src/main/java/biz/nynja/account/grpc/models/UserInfoByPhone.java @@ -7,6 +7,7 @@ import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; import org.springframework.data.cassandra.core.mapping.Table; import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.Status; @Table("userinfo_by_phone") @@ -223,4 +224,24 @@ public class UserInfoByPhone { .append(communicationProcviderId).append("]").toString(); } + public static UserInfoByPhone fromProto(CreateAccountRequest proto) { + UserInfoByPhone userInfo = new UserInfoByPhone(); + userInfo.setUsername(proto.getUsername()); + userInfo.setEmailAddress(proto.getEmailAddress()); + userInfo.setFirstName(proto.getFirstName()); + userInfo.setLastName(proto.getLastName()); + userInfo.setMiddleName(proto.getMiddleName()); + userInfo.setPhoneNumber(proto.getPhoneNumber()); + userInfo.setAuthenticationType(proto.getAuthenticationType()); + userInfo.setStatus(proto.getStatus()); + return userInfo; + } + + public biz.nynja.account.grpc.AccountDetails toProto() { + return biz.nynja.account.grpc.AccountDetails.newBuilder().setAccountId(getAccountId().toString()) + .setProfileId(getProfileId().toString()).setUsername(getUsername()) + .setAuthenticationType(getAuthenticationType()).setEmailAddress(getEmailAddress()) + .setFirstName(getFirstName()).setLastName(getLastName()).setMiddleName(getMiddleName()) + .setPhoneNumber(getPhoneNumber()).setStatus(getStatus()).build(); + } } diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index 36d992c..0c809c0 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -24,6 +24,10 @@ import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.GetAccountsResponse; import biz.nynja.account.grpc.components.Validator; import biz.nynja.account.grpc.models.UserInfo; +import biz.nynja.account.grpc.models.UserInfoByEmail; +import biz.nynja.account.grpc.models.UserInfoByPhone; +import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; +import biz.nynja.account.grpc.repositories.UserInfoByPhoneRepository; import biz.nynja.account.grpc.repositories.UserInfoRepository; import io.grpc.stub.StreamObserver; @@ -40,6 +44,12 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas @Autowired private UserInfoRepository userInfoRepository; + @Autowired + private UserInfoByEmailRepository userInfoByEmailRepository; + + @Autowired + private UserInfoByPhoneRepository userInfoByPhoneRepository; + @Autowired private Validator validator; @@ -78,35 +88,107 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas } @Override - public void getAllAccountsByAuthenticationProvider(AccountsByAuthenticationProviderRequest request, StreamObserver responseObserver) { + public void getAllAccountsByAuthenticationProvider(AccountsByAuthenticationProviderRequest request, + StreamObserver responseObserver) { + + logger.info("New get all accounts by authentication provider request recieved."); + logger.debug("New get all accounts by authentication provider request recieved: {}", request); + if (request.getAuthenticationType() == null) { + responseObserver.onNext(GetAccountsResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_AUTH_PROVIDER_TYPE)).build()); + responseObserver.onCompleted(); + return; + } + if (request.getAuthenticationIdentifier() == null) { + responseObserver.onNext(GetAccountsResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_AUTH_PROVIDER_IDENTIFIER)).build()); + responseObserver.onCompleted(); + return; + } + + List accountDetails = new ArrayList() { + }; + + switch (request.getAuthenticationType()) { + case PHONE: { + + List relatedAccounts = userInfoByPhoneRepository + .findByPhoneNumber(request.getAuthenticationIdentifier()); + if (relatedAccounts.isEmpty()) { + responseObserver.onNext(GetAccountsResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.ACCOUNT_NOT_FOUND)).build()); + responseObserver.onCompleted(); + return; + } + + for (UserInfoByPhone account : relatedAccounts) { + accountDetails.add(account.toProto()); + } + GetAccountsResponse response = GetAccountsResponse.newBuilder() + .setAccountsResponse(AccountsResponse.newBuilder().addAllAccountDetails(accountDetails)).build(); + responseObserver.onNext(response); + responseObserver.onCompleted(); + } + case EMAIL: { + List relatedAccounts = userInfoByEmailRepository + .findByEmailAddress(request.getAuthenticationIdentifier()); + if (relatedAccounts.isEmpty()) { + responseObserver.onNext(GetAccountsResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.ACCOUNT_NOT_FOUND)).build()); + responseObserver.onCompleted(); + return; + } + for (UserInfoByEmail account : relatedAccounts) { + accountDetails.add(account.toProto()); + } + } + case FACEBOOK: + break; + case GOOGLEPLUS: + break; + case UNRECOGNIZED: + break; + default: + break; + + } + GetAccountsResponse response = GetAccountsResponse.newBuilder() + .setAccountsResponse(AccountsResponse.newBuilder().addAllAccountDetails(accountDetails)).build(); + + responseObserver.onNext(response); + responseObserver.onCompleted(); } @Override - public void getAllAccountsByProfileId(AccountsByProfileIdRequest request, StreamObserver responseObserver) { + public void getAllAccountsByProfileId(AccountsByProfileIdRequest request, + StreamObserver responseObserver) { - logger.info("Getting accounts by profile ID..."); + logger.info("Getting accounts by profile ID..."); - List listAccountsByProfileId = userInfoRepository.findAllByProfileId(UUID.fromString(request.getProfileId())); - List responseList = new ArrayList (); + List listAccountsByProfileId = userInfoRepository + .findAllByProfileId(UUID.fromString(request.getProfileId())); + List responseList = new ArrayList(); - if (listAccountsByProfileId.size() == 0) { - responseObserver.onNext(GetAccountsResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(Cause.ACCOUNT_NOT_FOUND)).build()); - responseObserver.onCompleted(); - return; - } + if (listAccountsByProfileId.size() == 0) { + responseObserver.onNext(GetAccountsResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.ACCOUNT_NOT_FOUND)).build()); + responseObserver.onCompleted(); + return; + } - for(UserInfo userInfo : listAccountsByProfileId) { - responseList.add(userInfo.toProto()); - } + for (UserInfo userInfo : listAccountsByProfileId) { + responseList.add(userInfo.toProto()); + } - GetAccountsResponse response = GetAccountsResponse.newBuilder().setAccountsResponse(AccountsResponse.newBuilder().addAllAccountDetails(responseList)).build(); + GetAccountsResponse response = GetAccountsResponse.newBuilder() + .setAccountsResponse(AccountsResponse.newBuilder().addAllAccountDetails(responseList)).build(); - logger.debug("Returned response: \"{}\".", response); + logger.debug("Returned response: \"{}\".", response); - responseObserver.onNext(response); - responseObserver.onCompleted(); + responseObserver.onNext(response); + responseObserver.onCompleted(); - return; + return; } -- GitLab From d1e4bd3ca251b9a307add989ae9d5ad51de20578 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Fri, 10 Aug 2018 11:10:00 +0300 Subject: [PATCH 23/37] NY-2556 Unit tests - fix cassandra config in junit tests; - add valid unit test for creating account with email; - add unit test for already exist email; Signed-off-by: abotev-intracol --- .../grpc/configuration/CassandraConfig.java | 2 + .../grpc/components/ValidatorTests.java | 14 +++ .../grpc/services/AccountServiceTests.java | 94 ++++++++++++++++++- .../grpc/utils/GrpcServerTestBase.java | 2 +- .../biz/nynja/account/grpc/utils/Util.java | 29 +++++- 5 files changed, 133 insertions(+), 8 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java index c12435f..a4252ac 100644 --- a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java +++ b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java @@ -7,6 +7,7 @@ import java.util.Arrays; import java.util.List; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.context.annotation.Configuration; import org.springframework.data.cassandra.config.AbstractCassandraConfiguration; import org.springframework.data.cassandra.config.SchemaAction; @@ -15,6 +16,7 @@ import org.springframework.data.cassandra.repository.config.EnableCassandraRepos @Configuration @EnableCassandraRepositories +@ConditionalOnMissingClass("org.springframework.test.context.junit4.SpringRunner") public class CassandraConfig extends AbstractCassandraConfiguration { @Value("${spring.data.cassandra.keyspace-name}") diff --git a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java index 6ef7367..8e91e0d 100644 --- a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java +++ b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java @@ -10,9 +10,14 @@ import static org.junit.Assert.assertTrue; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; +import biz.nynja.account.grpc.repositories.UserInfoByPhoneRepository; +import biz.nynja.account.grpc.repositories.UserInfoByUsernameRepository; + /** * Components unit tests. */ @@ -24,6 +29,15 @@ public class ValidatorTests { @Autowired private Validator validator; + @MockBean + private UserInfoByEmailRepository userInfoByEmailRepository; + + @MockBean + private UserInfoByPhoneRepository userInfoByPhoneRepository; + + @MockBean + private UserInfoByUsernameRepository userInfoByUsernameRepository; + @Test public void validPhoneNumberTest() { diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index 2c9ca7a..b9e6be0 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -3,15 +3,39 @@ */ package biz.nynja.account.grpc.services; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.BDDMockito.given; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import java.util.concurrent.ExecutionException; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; +import biz.nynja.account.grpc.AccountServiceGrpc; +import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.CreateAccountRequest; +import biz.nynja.account.grpc.CreateAccountResponse; +import biz.nynja.account.grpc.ErrorResponse.Cause; +import biz.nynja.account.grpc.Status; +import biz.nynja.account.grpc.models.UserInfo; +import biz.nynja.account.grpc.models.UserInfoByEmail; +import biz.nynja.account.grpc.models.UserInfoByPhone; +import biz.nynja.account.grpc.models.UserInfoByUsername; +import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; +import biz.nynja.account.grpc.repositories.UserInfoByPhoneRepository; +import biz.nynja.account.grpc.repositories.UserInfoByUsernameRepository; +import biz.nynja.account.grpc.repositories.UserInfoRepository; import biz.nynja.account.grpc.utils.GrpcServerTestBase; import biz.nynja.account.grpc.utils.Util; @@ -21,21 +45,83 @@ import biz.nynja.account.grpc.utils.Util; @RunWith(SpringRunner.class) @SpringBootTest(classes = Util.class, - webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration", "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration" }) @ActiveProfiles("dev") public class AccountServiceTests extends GrpcServerTestBase { + @MockBean + private UserInfoRepository userInfoRepository; + + @MockBean + private UserInfoByEmailRepository userInfoByEmailRepository; + + @MockBean + private UserInfoByPhoneRepository userInfoByPhoneRepository; + + @MockBean + private UserInfoByUsernameRepository userInfoByUsernameRepository; + + @Autowired + @Qualifier("newAccount") + private UserInfo newAccount; + + @Autowired + @Qualifier("savedAccount") + private UserInfo savedAccount; + @Test - public void testRegister() throws ExecutionException, InterruptedException { + public void testCreateAccount() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.EMAIL).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setEmailAddress(Util.EMAIL).setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG") + .setStatus(Status.SUSPENDED).build(); + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain email '%s'", Util.EMAIL), + reply.getAccountDetails().getEmailAddress().equals(Util.EMAIL)); } @Test - public void testRegisterExistEmail() throws ExecutionException, InterruptedException { + public void testCreateAccountExistEmail() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.EMAIL).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setEmailAddress(Util.EMAIL).setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG") + .setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + usersByEmail.add(new UserInfoByEmail()); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Util.EMAIL), + reply.getError().getCause().equals(Cause.EMAIL_ALREADY_USED)); } } diff --git a/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java b/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java index 0a04113..2db24e8 100644 --- a/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java +++ b/src/test/java/biz/nynja/account/grpc/utils/GrpcServerTestBase.java @@ -72,4 +72,4 @@ public abstract class GrpcServerTestBase { Optional.ofNullable(channel).ifPresent(ManagedChannel::shutdownNow); Optional.ofNullable(inProcChannel).ifPresent(ManagedChannel::shutdownNow); } -} +} \ No newline at end of file diff --git a/src/test/java/biz/nynja/account/grpc/utils/Util.java b/src/test/java/biz/nynja/account/grpc/utils/Util.java index 83873d4..7f84ce3 100644 --- a/src/test/java/biz/nynja/account/grpc/utils/Util.java +++ b/src/test/java/biz/nynja/account/grpc/utils/Util.java @@ -9,6 +9,8 @@ import java.util.UUID; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; +import biz.nynja.account.grpc.AuthenticationType; +import biz.nynja.account.grpc.Status; import biz.nynja.account.grpc.models.UserInfo;; /** @@ -20,22 +22,43 @@ public class Util { public static final String ID = "id1"; public static final String EMAIL = "email@test.com"; - public static final String PASSWORD = "abc"; + public static final String USERNAME = "jdoe"; + public static final String PASSWORD = "abc123"; + public static final String FIRST_NAME = "John"; + public static final String LAST_NAME = "Doe"; + public static final String MIDDLE_NAME = "Kass"; + public static final String PHONE_NUMBER = "+359887434646"; @Bean public UserInfo newAccount() { UserInfo userInfo = new UserInfo(); - userInfo.setEmailAddress(EMAIL); + userInfo.setUsername(USERNAME); userInfo.setPassword(PASSWORD); + userInfo.setEmailAddress(EMAIL); + userInfo.setFirstName(FIRST_NAME); + userInfo.setLastName(LAST_NAME); + userInfo.setMiddleName(MIDDLE_NAME); + userInfo.setPhoneNumber(PHONE_NUMBER); + userInfo.setAuthenticationType(AuthenticationType.EMAIL); + userInfo.setStatus(Status.SUSPENDED); return userInfo; } @Bean public UserInfo savedAccount() { UserInfo userInfo = new UserInfo(); - userInfo.setAccountId(UUID.fromString(ID)); + userInfo.setAccountId(UUID.randomUUID()); + userInfo.setProfileId(UUID.randomUUID()); userInfo.setEmailAddress(EMAIL); + userInfo.setUsername(USERNAME); userInfo.setPassword(PASSWORD); + userInfo.setFirstName(FIRST_NAME); + userInfo.setLastName(LAST_NAME); + userInfo.setMiddleName(MIDDLE_NAME); + userInfo.setPhoneNumber(PHONE_NUMBER); + userInfo.setAuthenticationType(AuthenticationType.EMAIL); + userInfo.setStatus(Status.SUSPENDED); return userInfo; } + } -- GitLab From 53cfb45a6ad8215c8b69a624f8f4928c66caba30 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Fri, 10 Aug 2018 12:35:08 +0300 Subject: [PATCH 24/37] NY-2500 Unit tests - add unit tests for get all accounts by phone; - add unit tests for get all accounts by email; - remove brackets; Signed-off-by: abotev-intracol --- .../grpc/services/AccountServiceImpl.java | 3 +- .../grpc/services/AccountServiceTests.java | 116 +++++++++++++++++- .../biz/nynja/account/grpc/utils/Util.java | 36 +++++- 3 files changed, 151 insertions(+), 4 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index 0c809c0..eea4642 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -106,8 +106,7 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas return; } - List accountDetails = new ArrayList() { - }; + List accountDetails = new ArrayList(); switch (request.getAuthenticationType()) { case PHONE: { diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index b9e6be0..c917467 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -23,10 +23,12 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; import biz.nynja.account.grpc.AccountServiceGrpc; +import biz.nynja.account.grpc.AccountsByAuthenticationProviderRequest; import biz.nynja.account.grpc.AuthenticationType; import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.CreateAccountResponse; import biz.nynja.account.grpc.ErrorResponse.Cause; +import biz.nynja.account.grpc.GetAccountsResponse; import biz.nynja.account.grpc.Status; import biz.nynja.account.grpc.models.UserInfo; import biz.nynja.account.grpc.models.UserInfoByEmail; @@ -71,6 +73,12 @@ public class AccountServiceTests extends GrpcServerTestBase { @Qualifier("savedAccount") private UserInfo savedAccount; + @Autowired + private UserInfoByEmail userInfoByEmail; + + @Autowired + private UserInfoByPhone userInfoByPhone; + @Test public void testCreateAccount() throws ExecutionException, InterruptedException { final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) @@ -120,8 +128,114 @@ public class AccountServiceTests extends GrpcServerTestBase { final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); assertNotNull("Reply should not be null", reply); - assertTrue(String.format("Reply should contain cause '%s'", Util.EMAIL), + assertTrue(String.format("Reply should contain cause '%s'", Cause.EMAIL_ALREADY_USED), reply.getError().getCause().equals(Cause.EMAIL_ALREADY_USED)); } + @Test + public void testGetAccountsByEmail() throws ExecutionException, InterruptedException { + final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() + .setAuthenticationType(AuthenticationType.EMAIL).setAuthenticationIdentifier(Util.EMAIL).build(); + + List usersByEmail = new ArrayList<>(); + usersByEmail.add(userInfoByEmail); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByAuthenticationProvider(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain email '%s'", Util.EMAIL), + reply.getAccountsResponse().getAccountDetails(0).getEmailAddress().equals(Util.EMAIL)); + } + + @Test + public void testGetAccountsByEmailNotFound() throws ExecutionException, InterruptedException { + final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() + .setAuthenticationType(AuthenticationType.EMAIL).setAuthenticationIdentifier(Util.EMAIL).build(); + + List usersByEmail = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByAuthenticationProvider(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.ACCOUNT_NOT_FOUND), + reply.getError().getCause().equals(Cause.ACCOUNT_NOT_FOUND)); + } + + @Test + public void testGetAccountsByEmailBadRequest() + throws ExecutionException, InterruptedException { + final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() + .setAuthenticationType(AuthenticationType.EMAIL).build(); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByAuthenticationProvider(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.ACCOUNT_NOT_FOUND), + reply.getError().getCause().equals(Cause.ACCOUNT_NOT_FOUND)); + } + + @Test + public void testGetAccountsByPhone() throws ExecutionException, InterruptedException { + final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() + .setAuthenticationType(AuthenticationType.PHONE).setAuthenticationIdentifier(Util.PHONE_NUMBER).build(); + + List usersByPhone = new ArrayList<>(); + usersByPhone.add(userInfoByPhone); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByAuthenticationProvider(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain phone '%s'", Util.PHONE_NUMBER), + reply.getAccountsResponse().getAccountDetails(0).getPhoneNumber().equals(Util.PHONE_NUMBER)); + } + + @Test + public void testGetAccountsByPhoneNotFound() throws ExecutionException, InterruptedException { + final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() + .setAuthenticationType(AuthenticationType.PHONE).setAuthenticationIdentifier(Util.PHONE_NUMBER).build(); + + List usersByPhone = new ArrayList<>(); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByAuthenticationProvider(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.ACCOUNT_NOT_FOUND), + reply.getError().getCause().equals(Cause.ACCOUNT_NOT_FOUND)); + } + + @Test + public void testGetAccountsByPhoneBadRequest() + throws ExecutionException, InterruptedException { + final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() + .setAuthenticationType(AuthenticationType.PHONE).build(); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByAuthenticationProvider(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.ACCOUNT_NOT_FOUND), + reply.getError().getCause().equals(Cause.ACCOUNT_NOT_FOUND)); + } + } diff --git a/src/test/java/biz/nynja/account/grpc/utils/Util.java b/src/test/java/biz/nynja/account/grpc/utils/Util.java index 7f84ce3..bafd7b4 100644 --- a/src/test/java/biz/nynja/account/grpc/utils/Util.java +++ b/src/test/java/biz/nynja/account/grpc/utils/Util.java @@ -11,7 +11,9 @@ import org.springframework.context.annotation.Bean; import biz.nynja.account.grpc.AuthenticationType; import biz.nynja.account.grpc.Status; -import biz.nynja.account.grpc.models.UserInfo;; +import biz.nynja.account.grpc.models.UserInfo; +import biz.nynja.account.grpc.models.UserInfoByEmail; +import biz.nynja.account.grpc.models.UserInfoByPhone;; /** * Unit tests variables, beans and help methods. @@ -61,4 +63,36 @@ public class Util { return userInfo; } + @Bean + public UserInfoByEmail accountByEmail() { + UserInfoByEmail userInfoByEmail = new UserInfoByEmail(); + userInfoByEmail.setEmailAddress(EMAIL); + userInfoByEmail.setAccountId(UUID.randomUUID()); + userInfoByEmail.setProfileId(UUID.randomUUID()); + userInfoByEmail.setUsername(USERNAME); + userInfoByEmail.setFirstName(FIRST_NAME); + userInfoByEmail.setLastName(LAST_NAME); + userInfoByEmail.setMiddleName(MIDDLE_NAME); + userInfoByEmail.setPhoneNumber(PHONE_NUMBER); + userInfoByEmail.setAuthenticationType(AuthenticationType.EMAIL); + userInfoByEmail.setStatus(Status.SUSPENDED); + return userInfoByEmail; + } + + @Bean + public UserInfoByPhone accountByPhone() { + UserInfoByPhone userInfoByPhone = new UserInfoByPhone(); + userInfoByPhone.setPhoneNumber(PHONE_NUMBER); + userInfoByPhone.setEmailAddress(EMAIL); + userInfoByPhone.setAccountId(UUID.randomUUID()); + userInfoByPhone.setProfileId(UUID.randomUUID()); + userInfoByPhone.setUsername(USERNAME); + userInfoByPhone.setFirstName(FIRST_NAME); + userInfoByPhone.setLastName(LAST_NAME); + userInfoByPhone.setMiddleName(MIDDLE_NAME); + userInfoByPhone.setAuthenticationType(AuthenticationType.EMAIL); + userInfoByPhone.setStatus(Status.SUSPENDED); + return userInfoByPhone; + } + } -- GitLab From e2b86429eb823ae88a9b8617cb2e571420dcd9b3 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Fri, 10 Aug 2018 13:36:38 +0300 Subject: [PATCH 25/37] NY-2556 Unit tests - add unit tests for exist, non exists and not valid requests for account creation; Signed-off-by: abotev-intracol --- .../grpc/services/AccountServiceTests.java | 164 +++++++++++++++++- 1 file changed, 157 insertions(+), 7 deletions(-) diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index c917467..5886cf1 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -80,12 +80,11 @@ public class AccountServiceTests extends GrpcServerTestBase { private UserInfoByPhone userInfoByPhone; @Test - public void testCreateAccount() throws ExecutionException, InterruptedException { + public void testCreateAccountByEmail() throws ExecutionException, InterruptedException { final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) .setAuthenticationType(AuthenticationType.EMAIL).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) - .setEmailAddress(Util.EMAIL).setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG") - .setStatus(Status.SUSPENDED).build(); + .setEmailAddress(Util.EMAIL).setStatus(Status.SUSPENDED).build(); List usersByPhone = new ArrayList<>(); List usersByEmail = new ArrayList<>(); @@ -132,6 +131,159 @@ public class AccountServiceTests extends GrpcServerTestBase { reply.getError().getCause().equals(Cause.EMAIL_ALREADY_USED)); } + @Test + public void testCreateAccountByPhone() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.PHONE).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG").setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain phone '%s'", Util.PHONE_NUMBER), + reply.getAccountDetails().getPhoneNumber().equals(Util.PHONE_NUMBER)); + } + + @Test + public void testCreateAccountExistPhone() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.PHONE).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG").setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + usersByPhone.add(new UserInfoByPhone()); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.PHONE_NUMBER_ALREADY_USED), + reply.getError().getCause().equals(Cause.PHONE_NUMBER_ALREADY_USED)); + } + + @Test + public void testCreateAccountExistUsername() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.PHONE).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG").setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + usersByUsername.add(new UserInfoByUsername()); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.USERNAME_ALREADY_USED), + reply.getError().getCause().equals(Cause.USERNAME_ALREADY_USED)); + } + + @Test + public void testCreateAccountInvalidUsername() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.PHONE).setUsername(".Invalid-Username.") + .setPassword(Util.PASSWORD).setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG") + .setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.USERNAME_INVALID), + reply.getError().getCause().equals(Cause.USERNAME_INVALID)); + } + + @Test + public void testCreateAccountInvalidEmail() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.EMAIL).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setEmailAddress(".invalid@email-.com").setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.EMAIL_INVALID), + reply.getError().getCause().equals(Cause.EMAIL_INVALID)); + } + + @Test + public void testCreateAccountInvalidPhone() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.PHONE).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setPhoneNumber("+45955588833648946512957").setCountryCode("BG").setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.PHONE_NUMBER_INVALID), + reply.getError().getCause().equals(Cause.PHONE_NUMBER_INVALID)); + } + @Test public void testGetAccountsByEmail() throws ExecutionException, InterruptedException { final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() @@ -170,8 +322,7 @@ public class AccountServiceTests extends GrpcServerTestBase { } @Test - public void testGetAccountsByEmailBadRequest() - throws ExecutionException, InterruptedException { + public void testGetAccountsByEmailBadRequest() throws ExecutionException, InterruptedException { final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() .setAuthenticationType(AuthenticationType.EMAIL).build(); @@ -223,8 +374,7 @@ public class AccountServiceTests extends GrpcServerTestBase { } @Test - public void testGetAccountsByPhoneBadRequest() - throws ExecutionException, InterruptedException { + public void testGetAccountsByPhoneBadRequest() throws ExecutionException, InterruptedException { final AccountsByAuthenticationProviderRequest request = AccountsByAuthenticationProviderRequest.newBuilder() .setAuthenticationType(AuthenticationType.PHONE).build(); -- GitLab From a96af439c3febb51eb172c289760c9f79d55f3e5 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Fri, 10 Aug 2018 14:50:31 +0300 Subject: [PATCH 26/37] NY-2212: [BE] Get all accounts by profile ID - checks for missing profile id; - unit tests; Signed-off-by: Stanimir Penkov --- .../grpc/services/AccountServiceImpl.java | 16 ++++-- .../grpc/services/AccountServiceTests.java | 55 +++++++++++++++++++ .../biz/nynja/account/grpc/utils/Util.java | 15 ++--- 3 files changed, 73 insertions(+), 13 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index eea4642..8e3808d 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -159,14 +159,18 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas } @Override - public void getAllAccountsByProfileId(AccountsByProfileIdRequest request, - StreamObserver responseObserver) { + public void getAllAccountsByProfileId(AccountsByProfileIdRequest request, StreamObserver responseObserver) { + + logger.info("Getting accounts by profile ID..."); - logger.info("Getting accounts by profile ID..."); + if ((request.getProfileId() == null) || (request.getProfileId().isEmpty())) { + responseObserver.onNext(GetAccountsResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_PROFILE_ID)).build()); + responseObserver.onCompleted(); + return; + } - List listAccountsByProfileId = userInfoRepository - .findAllByProfileId(UUID.fromString(request.getProfileId())); - List responseList = new ArrayList(); + List listAccountsByProfileId = userInfoRepository.findAllByProfileId(UUID.fromString(request.getProfileId())); + List responseList = new ArrayList (); if (listAccountsByProfileId.size() == 0) { responseObserver.onNext(GetAccountsResponse.newBuilder() diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index 5886cf1..d5352d1 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -11,6 +11,7 @@ import static org.mockito.BDDMockito.given; import java.util.ArrayList; import java.util.List; import java.util.Optional; +import java.util.UUID; import java.util.concurrent.ExecutionException; import org.junit.Test; @@ -24,6 +25,7 @@ import org.springframework.test.context.junit4.SpringRunner; import biz.nynja.account.grpc.AccountServiceGrpc; import biz.nynja.account.grpc.AccountsByAuthenticationProviderRequest; +import biz.nynja.account.grpc.AccountsByProfileIdRequest; import biz.nynja.account.grpc.AuthenticationType; import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.CreateAccountResponse; @@ -388,4 +390,57 @@ public class AccountServiceTests extends GrpcServerTestBase { reply.getError().getCause().equals(Cause.ACCOUNT_NOT_FOUND)); } + @Test + public void testGetAccountsByProfileId() throws ExecutionException, InterruptedException { + final AccountsByProfileIdRequest request = AccountsByProfileIdRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()).build(); + + List usersInfo = new ArrayList<>(); + usersInfo.add(savedAccount); + given(userInfoRepository.findAllByProfileId(UUID.fromString(request.getProfileId()))).willReturn(usersInfo); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByProfileId(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain profile ID '%s'", Util.PROFILE_ID.toString()), + reply.getAccountsResponse().getAccountDetails(0).getProfileId().equals(Util.PROFILE_ID.toString())); + } + + @Test + public void testGetAccountsByProfileIdNotFound() throws ExecutionException, InterruptedException { + final AccountsByProfileIdRequest request = AccountsByProfileIdRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()).build(); + + List usersInfo = new ArrayList<>(); + + given(userInfoRepository.findAllByProfileId(Util.PROFILE_ID)).willReturn(usersInfo); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByProfileId(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.ACCOUNT_NOT_FOUND), + reply.getError().getCause().equals(Cause.ACCOUNT_NOT_FOUND)); + } + + @Test + public void testGetAccountsByProfileIdBadRequest() + throws ExecutionException, InterruptedException { + final AccountsByProfileIdRequest request = AccountsByProfileIdRequest.newBuilder().build(); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final GetAccountsResponse reply = accountServiceBlockingStub.getAllAccountsByProfileId(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_PROFILE_ID), + reply.getError().getCause().equals(Cause.MISSING_PROFILE_ID)); + } + } diff --git a/src/test/java/biz/nynja/account/grpc/utils/Util.java b/src/test/java/biz/nynja/account/grpc/utils/Util.java index bafd7b4..8d87038 100644 --- a/src/test/java/biz/nynja/account/grpc/utils/Util.java +++ b/src/test/java/biz/nynja/account/grpc/utils/Util.java @@ -22,7 +22,8 @@ import biz.nynja.account.grpc.models.UserInfoByPhone;; @TestConfiguration public class Util { - public static final String ID = "id1"; + public static final UUID PROFILE_ID = UUID.fromString("12352345-e89b-43d3-d156-456732452200"); + public static final UUID ACCOUNT_ID = UUID.fromString("44532732-12b3-132d-e156-223732152200"); public static final String EMAIL = "email@test.com"; public static final String USERNAME = "jdoe"; public static final String PASSWORD = "abc123"; @@ -49,8 +50,8 @@ public class Util { @Bean public UserInfo savedAccount() { UserInfo userInfo = new UserInfo(); - userInfo.setAccountId(UUID.randomUUID()); - userInfo.setProfileId(UUID.randomUUID()); + userInfo.setAccountId(ACCOUNT_ID); + userInfo.setProfileId(PROFILE_ID); userInfo.setEmailAddress(EMAIL); userInfo.setUsername(USERNAME); userInfo.setPassword(PASSWORD); @@ -67,8 +68,8 @@ public class Util { public UserInfoByEmail accountByEmail() { UserInfoByEmail userInfoByEmail = new UserInfoByEmail(); userInfoByEmail.setEmailAddress(EMAIL); - userInfoByEmail.setAccountId(UUID.randomUUID()); - userInfoByEmail.setProfileId(UUID.randomUUID()); + userInfoByEmail.setAccountId(ACCOUNT_ID); + userInfoByEmail.setProfileId(PROFILE_ID); userInfoByEmail.setUsername(USERNAME); userInfoByEmail.setFirstName(FIRST_NAME); userInfoByEmail.setLastName(LAST_NAME); @@ -84,8 +85,8 @@ public class Util { UserInfoByPhone userInfoByPhone = new UserInfoByPhone(); userInfoByPhone.setPhoneNumber(PHONE_NUMBER); userInfoByPhone.setEmailAddress(EMAIL); - userInfoByPhone.setAccountId(UUID.randomUUID()); - userInfoByPhone.setProfileId(UUID.randomUUID()); + userInfoByPhone.setAccountId(ACCOUNT_ID); + userInfoByPhone.setProfileId(PROFILE_ID); userInfoByPhone.setUsername(USERNAME); userInfoByPhone.setFirstName(FIRST_NAME); userInfoByPhone.setLastName(LAST_NAME); -- GitLab From 23e6198ee74704c4276afeb3a9e9fffba225b9c5 Mon Sep 17 00:00:00 2001 From: Georgi Samardzhiev Date: Wed, 15 Aug 2018 14:07:59 +0300 Subject: [PATCH 27/37] NY-2666: Enrich profile data - Added validations for first/last name; - Updated unit tests. Signed-off-by: Ralitsa Todorova --- .../account/grpc/components/Validator.java | 31 +++++++ .../grpc/components/ValidatorTests.java | 52 ++++++++++-- .../grpc/services/AccountServiceTests.java | 84 +++++++++++++++++-- 3 files changed, 154 insertions(+), 13 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/components/Validator.java b/src/main/java/biz/nynja/account/grpc/components/Validator.java index ac1366d..edcda5f 100644 --- a/src/main/java/biz/nynja/account/grpc/components/Validator.java +++ b/src/main/java/biz/nynja/account/grpc/components/Validator.java @@ -149,6 +149,26 @@ public class Validator { return isValid; } + public boolean isFirstNameValid(String firstName) { + logger.debug("Checking First Name: {}", firstName); + + int len = firstName.length(); + boolean isValid = 2 <= len && len <= 32; + + logger.debug("First Name: {} is valid: {}", firstName, isValid); + return isValid; + } + + public boolean isLastNameValid(String lastName) { + logger.debug("Checking Last Name: {}", lastName); + + int len = lastName.length(); + boolean isValid = 0 <= len && len <= 32; + + logger.debug("Last Name: {} is valid: {}", lastName, isValid); + return isValid; + } + public Cause validateCreateAccountRequest(CreateAccountRequest request) { if (request.getUsername() != null && !request.getUsername().trim().isEmpty() @@ -175,6 +195,17 @@ public class Validator { return Cause.PHONE_NUMBER_ALREADY_USED; } + if (request.getFirstName() != null && request.getFirstName().trim().isEmpty()) { + return Cause.MISSING_FIRST_NAME; + } else if (!isFirstNameValid(request.getFirstName())) { + return Cause.INVALID_FIRST_NAME; + } + + if (request.getLastName() != null && !request.getLastName().trim().isEmpty() + && !isLastNameValid(request.getLastName())) { + return Cause.INVALID_LAST_NAME; + } + return null; } diff --git a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java index 8e91e0d..4800210 100644 --- a/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java +++ b/src/test/java/biz/nynja/account/grpc/components/ValidatorTests.java @@ -65,8 +65,7 @@ public class ValidatorTests { String username = "VALID_username1"; boolean isValid = validator.isUsernameValid(username); - assertTrue(String.format("Username: '%s' should be valid.", username), - isValid == true); + assertTrue(String.format("Username: '%s' should be valid.", username), isValid == true); } @@ -75,8 +74,7 @@ public class ValidatorTests { String username = "INVALID-username1"; boolean isValid = validator.isUsernameValid(username); - assertFalse(String.format("Username: '%s' should be invalid.", username), - isValid == true); + assertFalse(String.format("Username: '%s' should be invalid.", username), isValid == true); } @@ -85,8 +83,7 @@ public class ValidatorTests { String email = "valid.E-mail1@domain-sub.test.com"; boolean isValid = validator.isEmailValid(email); - assertTrue(String.format("Email: '%s' should be valid.", email), - isValid == true); + assertTrue(String.format("Email: '%s' should be valid.", email), isValid == true); } @@ -95,8 +92,47 @@ public class ValidatorTests { String email = "invalid.E-mail1.@domain_test.com1"; boolean isValid = validator.isEmailValid(email); - assertFalse(String.format("Email: '%s' should be invalid.", email), - isValid == true); + assertFalse(String.format("Email: '%s' should be invalid.", email), isValid == true); + + } + + @Test + public void validFistNameTest() { + + String fistName = "Validfirstname"; + boolean isValid = validator.isFirstNameValid(fistName); + assertTrue(String.format("First name: '%s' should be valid.", fistName), isValid == true); + + } + + @Test + public void invalidFirstNameTest() { + + String fistName = "VeryINVALIDFirstNameThisIsForReal"; + boolean isValid = validator.isFirstNameValid(fistName); + assertFalse(String.format("First name: '%s' should be invalid.", fistName), isValid == true); + + } + + @Test + public void validLastNameTest() { + + String lastName = "Validlastname"; + boolean isValid = validator.isLastNameValid(lastName); + assertTrue(String.format("Username: '%s' should be valid.", lastName), isValid == true); + + lastName = ""; + isValid = validator.isLastNameValid(lastName); + assertTrue(String.format("Last name: '%s' should be valid.", lastName), isValid == true); + + } + + @Test + public void invalidlastNameTest() { + + String lastName = "VeryINVALIDFirstNameThisIsForReal"; + boolean isValid = validator.isLastNameValid(lastName); + assertFalse(String.format("Last name: '%s' should be invalid.", lastName), isValid == true); } } diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index d5352d1..0d6ee65 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -236,6 +236,81 @@ public class AccountServiceTests extends GrpcServerTestBase { reply.getError().getCause().equals(Cause.USERNAME_INVALID)); } + @Test + public void testCreateAccountMissingFirstName() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setLastName(Util.LAST_NAME) + .setMiddleName(Util.MIDDLE_NAME).setAuthenticationType(AuthenticationType.PHONE) + .setUsername(Util.USERNAME).setPassword(Util.PASSWORD).setPhoneNumber(Util.PHONE_NUMBER) + .setCountryCode("BG").setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.MISSING_FIRST_NAME), + reply.getError().getCause().equals(Cause.MISSING_FIRST_NAME)); + } + + @Test + public void testCreateAccountInvalidFirstName() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName("n") + .setLastName(Util.LAST_NAME).setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.PHONE).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG").setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.INVALID_FIRST_NAME), + reply.getError().getCause().equals(Cause.INVALID_FIRST_NAME)); + } + + @Test + public void testCreateAccountInvalidLastName() throws ExecutionException, InterruptedException { + final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) + .setLastName("ThisIsNotaValidLastNameIndeedAndItIsNotReal").setMiddleName(Util.MIDDLE_NAME) + .setAuthenticationType(AuthenticationType.PHONE).setUsername(Util.USERNAME).setPassword(Util.PASSWORD) + .setPhoneNumber(Util.PHONE_NUMBER).setCountryCode("BG").setStatus(Status.SUSPENDED).build(); + + List usersByPhone = new ArrayList<>(); + List usersByEmail = new ArrayList<>(); + List usersByUsername = new ArrayList<>(); + given(userInfoByEmailRepository.findByEmailAddress(Util.EMAIL)).willReturn(usersByEmail); + given(userInfoByPhoneRepository.findByPhoneNumber(Util.PHONE_NUMBER)).willReturn(usersByPhone); + given(userInfoByUsernameRepository.findByUsername(Util.USERNAME)).willReturn(usersByUsername); + given(userInfoRepository.save(any(UserInfo.class))).willReturn(savedAccount); + + final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc + .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); + + final CreateAccountResponse reply = accountServiceBlockingStub.createAccount(request); + + assertNotNull("Reply should not be null", reply); + assertTrue(String.format("Reply should contain cause '%s'", Cause.INVALID_LAST_NAME), + reply.getError().getCause().equals(Cause.INVALID_LAST_NAME)); + } + @Test public void testCreateAccountInvalidEmail() throws ExecutionException, InterruptedException { final CreateAccountRequest request = CreateAccountRequest.newBuilder().setFirstName(Util.FIRST_NAME) @@ -392,8 +467,8 @@ public class AccountServiceTests extends GrpcServerTestBase { @Test public void testGetAccountsByProfileId() throws ExecutionException, InterruptedException { - final AccountsByProfileIdRequest request = AccountsByProfileIdRequest.newBuilder() - .setProfileId(Util.PROFILE_ID.toString()).build(); + final AccountsByProfileIdRequest request = AccountsByProfileIdRequest.newBuilder() + .setProfileId(Util.PROFILE_ID.toString()).build(); List usersInfo = new ArrayList<>(); usersInfo.add(savedAccount); @@ -412,7 +487,7 @@ public class AccountServiceTests extends GrpcServerTestBase { @Test public void testGetAccountsByProfileIdNotFound() throws ExecutionException, InterruptedException { final AccountsByProfileIdRequest request = AccountsByProfileIdRequest.newBuilder() - .setProfileId(Util.PROFILE_ID.toString()).build(); + .setProfileId(Util.PROFILE_ID.toString()).build(); List usersInfo = new ArrayList<>(); @@ -429,8 +504,7 @@ public class AccountServiceTests extends GrpcServerTestBase { } @Test - public void testGetAccountsByProfileIdBadRequest() - throws ExecutionException, InterruptedException { + public void testGetAccountsByProfileIdBadRequest() throws ExecutionException, InterruptedException { final AccountsByProfileIdRequest request = AccountsByProfileIdRequest.newBuilder().build(); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc -- GitLab From 9e4f83f517a5b0f0a296200254d7eac6c2e81773 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Wed, 15 Aug 2018 15:32:02 +0300 Subject: [PATCH 28/37] NY-2638: Support for multiple accounts for a profile - use the profileId from the request; Signed-off-by: Stanimir Penkov --- .../biz/nynja/account/grpc/services/AccountServiceImpl.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index 8e3808d..c06283e 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -68,7 +68,11 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas } UserInfo userInfo = UserInfo.fromProto(request); - userInfo.setProfileId(UUID.randomUUID()); + if (request.getProfileId() == null || request.getProfileId().isEmpty()) { + userInfo.setProfileId(UUID.randomUUID()); + } else { + userInfo.setProfileId(UUID.fromString(request.getProfileId())); + } userInfo.setAccountId(UUID.randomUUID()); // TODO set authentication provider userInfo.setAuthenticationProviderId("id"); -- GitLab From 909810ebe896ee626eeb7b6812226c27107e78da Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Tue, 21 Aug 2018 15:41:34 +0300 Subject: [PATCH 29/37] NY-2697: Implementation of the new Cassandra DB model Signed-off-by: Ralitsa Todorova --- .../nynja/account/grpc/models/Account.java | 262 ++++++++++++++++++ .../grpc/models/AuthenticationProvider.java | 67 +++++ .../grpc/models/CommunicationProvider.java | 96 ------- .../grpc/models/EmailSocialIdentity.java | 51 ---- .../models/FacebookCommunicationProvider.java | 82 ------ .../grpc/models/FacebookSocialIdentity.java | 85 ------ .../models/PhoneCommunicationProvider.java | 67 ----- .../grpc/models/PhoneSocialIdentity.java | 67 ----- .../nynja/account/grpc/models/Profile.java | 88 ++++-- .../ProfileByAuthenticationProvider.java | 81 ++++++ .../account/grpc/models/SocialIdentity.java | 96 ------- .../account/grpc/models/UserAccount.java | 211 -------------- .../models/UsernameCommunicationProvider.java | 50 ---- 13 files changed, 473 insertions(+), 830 deletions(-) create mode 100644 src/main/java/biz/nynja/account/grpc/models/Account.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/EmailSocialIdentity.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/FacebookCommunicationProvider.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/FacebookSocialIdentity.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/PhoneCommunicationProvider.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/PhoneSocialIdentity.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/ProfileByAuthenticationProvider.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/UserAccount.java delete mode 100644 src/main/java/biz/nynja/account/grpc/models/UsernameCommunicationProvider.java diff --git a/src/main/java/biz/nynja/account/grpc/models/Account.java b/src/main/java/biz/nynja/account/grpc/models/Account.java new file mode 100644 index 0000000..4e0a058 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/Account.java @@ -0,0 +1,262 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.nio.ByteBuffer; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +import javax.validation.constraints.Size; + +import org.springframework.data.cassandra.core.mapping.PrimaryKey; +import org.springframework.data.cassandra.core.mapping.Table; + +import biz.nynja.account.grpc.Status; + +@Table +public class Account { + + @PrimaryKey + private UUID accountId; + private UUID profileId; + private String accountMark; + private String authenticationProvider; + private String authenticationProviderType; + private String firstName; + private String lastName; + private ByteBuffer avatar; + private String accountName; + private String username; + private Status accountStatus; + private Long creationTimestamp; + private Long lastUpdateTimestamp; + private Set communicationProviders; + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + public String getAccountMark() { + return accountMark; + } + + public void setAccountMark(String accountMark) { + this.accountMark = accountMark; + } + + public String getAuthenticationProvider() { + return authenticationProvider; + } + + public void setAuthenticationProvider(String authenticationProvider) { + this.authenticationProvider = authenticationProvider; + } + + public String getAuthenticationProviderType() { + return authenticationProviderType; + } + + public void setAuthenticationProviderType(String authenticationProviderType) { + this.authenticationProviderType = authenticationProviderType; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public ByteBuffer getAvatar() { + return avatar; + } + + public void setAvatar(ByteBuffer avatar) { + this.avatar = avatar; + } + + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public Status getAccountStatus() { + return accountStatus; + } + + public void setAccountStatus(Status accountStatus) { + this.accountStatus = accountStatus; + } + + public Long getCreationTimestamp() { + return creationTimestamp; + } + + public void setCreationTimestamp(Long creationTimestamp) { + this.creationTimestamp = creationTimestamp; + } + + public Long getLastUpdateTimestamp() { + return lastUpdateTimestamp; + } + + public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { + this.lastUpdateTimestamp = lastUpdateTimestamp; + } + + public Set getCommunicationProviders() { + return communicationProviders; + } + + public void setCommunicationProviders(Set communicationProviders) { + this.communicationProviders = communicationProviders; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((accountMark == null) ? 0 : accountMark.hashCode()); + result = prime * result + ((accountName == null) ? 0 : accountName.hashCode()); + result = prime * result + ((accountStatus == null) ? 0 : accountStatus.hashCode()); + result = prime * result + ((authenticationProvider == null) ? 0 : authenticationProvider.hashCode()); + result = prime * result + ((authenticationProviderType == null) ? 0 : authenticationProviderType.hashCode()); + result = prime * result + ((avatar == null) ? 0 : avatar.hashCode()); + result = prime * result + ((communicationProviders == null) ? 0 : communicationProviders.hashCode()); + result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); + result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); + result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); + result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Account other = (Account) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (accountMark == null) { + if (other.accountMark != null) + return false; + } else if (!accountMark.equals(other.accountMark)) + return false; + if (accountName == null) { + if (other.accountName != null) + return false; + } else if (!accountName.equals(other.accountName)) + return false; + if (accountStatus != other.accountStatus) + return false; + if (authenticationProvider == null) { + if (other.authenticationProvider != null) + return false; + } else if (!authenticationProvider.equals(other.authenticationProvider)) + return false; + if (authenticationProviderType == null) { + if (other.authenticationProviderType != null) + return false; + } else if (!authenticationProviderType.equals(other.authenticationProviderType)) + return false; + if (avatar == null) { + if (other.avatar != null) + return false; + } else if (!avatar.equals(other.avatar)) + return false; + if (communicationProviders == null) { + if (other.communicationProviders != null) + return false; + } else if (!communicationProviders.equals(other.communicationProviders)) + return false; + if (creationTimestamp == null) { + if (other.creationTimestamp != null) + return false; + } else if (!creationTimestamp.equals(other.creationTimestamp)) + return false; + if (firstName == null) { + if (other.firstName != null) + return false; + } else if (!firstName.equals(other.firstName)) + return false; + if (lastName == null) { + if (other.lastName != null) + return false; + } else if (!lastName.equals(other.lastName)) + return false; + if (lastUpdateTimestamp == null) { + if (other.lastUpdateTimestamp != null) + return false; + } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("Account [accountId=").append(accountId).append(", profileId=").append(profileId) + .append(", accountMark=").append(accountMark).append(", authenticationProvider=") + .append(authenticationProvider).append(", authenticationProviderType=") + .append(authenticationProviderType).append(", firstName=").append(firstName).append(", lastName=") + .append(lastName).append(", avatar=").append(avatar).append(", accountName=").append(accountName) + .append(", username=").append(username).append(", accountStatus=").append(accountStatus) + .append(", creationTimestamp=").append(creationTimestamp).append(", lastUpdateTimestamp=") + .append(lastUpdateTimestamp).append(", communicationProviders=").append(communicationProviders) + .append("]").toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java b/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java new file mode 100644 index 0000000..aaeadc1 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java @@ -0,0 +1,67 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import org.springframework.data.cassandra.core.mapping.UserDefinedType; + +@UserDefinedType +public class AuthenticationProvider { + + private String type; + private String value; + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((type == null) ? 0 : type.hashCode()); + result = prime * result + ((value == null) ? 0 : value.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AuthenticationProvider other = (AuthenticationProvider) obj; + if (type == null) { + if (other.type != null) + return false; + } else if (!type.equals(other.type)) + return false; + if (value == null) { + if (other.value != null) + return false; + } else if (!value.equals(other.value)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("AuthenticationProvider [type=").append(type).append(", value=").append(value) + .append("]").toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java deleted file mode 100644 index 609050a..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/CommunicationProvider.java +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -import java.util.UUID; - -import biz.nynja.account.grpc.Status; - -public class CommunicationProvider { - - private UUID providerId; - private Long creationTimestamp; - private Long lastUpdateTimestamp; - private Status status; - - public UUID getProviderId() { - return providerId; - } - - public void setProviderId(UUID providerId) { - this.providerId = providerId; - } - - public Long getCreationTimestamp() { - return creationTimestamp; - } - - public void setCreationTimestamp(Long creationTimestamp) { - this.creationTimestamp = creationTimestamp; - } - - public Long getLastUpdateTimestamp() { - return lastUpdateTimestamp; - } - - public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { - this.lastUpdateTimestamp = lastUpdateTimestamp; - } - - public Status getStatus() { - return status; - } - - public void setStatus(Status status) { - this.status = status; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); - result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); - result = prime * result + ((providerId == null) ? 0 : providerId.hashCode()); - result = prime * result + ((status == null) ? 0 : status.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - CommunicationProvider other = (CommunicationProvider) obj; - if (creationTimestamp == null) { - if (other.creationTimestamp != null) - return false; - } else if (!creationTimestamp.equals(other.creationTimestamp)) - return false; - if (lastUpdateTimestamp == null) { - if (other.lastUpdateTimestamp != null) - return false; - } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) - return false; - if (providerId == null) { - if (other.providerId != null) - return false; - } else if (!providerId.equals(other.providerId)) - return false; - if (status != other.status) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("CommunicationProvider [providerId=").append(providerId).append(", creationTimestamp=") - .append(creationTimestamp).append(", lastUpdateTimestamp=").append(lastUpdateTimestamp) - .append(", status=").append(status).append("]").toString(); - } - -} diff --git a/src/main/java/biz/nynja/account/grpc/models/EmailSocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/EmailSocialIdentity.java deleted file mode 100644 index c00f524..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/EmailSocialIdentity.java +++ /dev/null @@ -1,51 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -public class EmailSocialIdentity extends SocialIdentity { - - private String email; - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + ((email == null) ? 0 : email.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - EmailSocialIdentity other = (EmailSocialIdentity) obj; - if (email == null) { - if (other.email != null) - return false; - } else if (!email.equals(other.email)) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("EmailSocialIdentity [email=").append(email).append(", socialIdentityId=") - .append(this.getSocialIdentityId()).append(", creationTimestamp=").append(this.getCreationTimestamp()) - .append(", lastUpdateTimestamp=").append(this.getLastUpdateTimestamp()).append(", status=") - .append(this.getStatus()).append("]").toString(); - } - -} diff --git a/src/main/java/biz/nynja/account/grpc/models/FacebookCommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/FacebookCommunicationProvider.java deleted file mode 100644 index 2260123..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/FacebookCommunicationProvider.java +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -public class FacebookCommunicationProvider extends CommunicationProvider { - - private String facebookId; - private String token; - private Long tokenExpirationTimestamp; - - public String getFacebookId() { - return facebookId; - } - - public void setFacebookId(String facebookId) { - this.facebookId = facebookId; - } - - public String getToken() { - return token; - } - - public void setToken(String token) { - this.token = token; - } - - public Long getTokenExpirationTimestamp() { - return tokenExpirationTimestamp; - } - - public void setTokenExpirationTimestamp(Long tokenExpirationTimestamp) { - this.tokenExpirationTimestamp = tokenExpirationTimestamp; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + ((facebookId == null) ? 0 : facebookId.hashCode()); - result = prime * result + ((token == null) ? 0 : token.hashCode()); - result = prime * result + ((tokenExpirationTimestamp == null) ? 0 : tokenExpirationTimestamp.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - FacebookCommunicationProvider other = (FacebookCommunicationProvider) obj; - if (facebookId == null) { - if (other.facebookId != null) - return false; - } else if (!facebookId.equals(other.facebookId)) - return false; - if (token == null) { - if (other.token != null) - return false; - } else if (!token.equals(other.token)) - return false; - if (tokenExpirationTimestamp == null) { - if (other.tokenExpirationTimestamp != null) - return false; - } else if (!tokenExpirationTimestamp.equals(other.tokenExpirationTimestamp)) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("FacebookCommunicationProvider [facebookId=").append(facebookId).append(", token=") - .append(token).append(", tokenExpirationTimestamp=").append(tokenExpirationTimestamp) - .append(", providerId=").append(this.getProviderId()).append(", creationTimestamp=") - .append(this.getCreationTimestamp()).append(", lastUpdateTimestamp=") - .append(this.getLastUpdateTimestamp()).append(", status=").append(this.getStatus()).append("]") - .toString(); - } -} diff --git a/src/main/java/biz/nynja/account/grpc/models/FacebookSocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/FacebookSocialIdentity.java deleted file mode 100644 index 73f6098..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/FacebookSocialIdentity.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -public class FacebookSocialIdentity extends SocialIdentity { - - private String facebookId; - private String token; - private Long tokenExpirationTimestamp; - - public String getFacebookId() { - return facebookId; - } - - public void setFacebookId(String facebookId) { - this.facebookId = facebookId; - } - - public String getToken() { - return token; - } - - public void setToken(String token) { - this.token = token; - } - - public Long getTokenExpirationTimestamp() { - return tokenExpirationTimestamp; - } - - public void setTokenExpirationTimestamp(Long tokenExpirationTimestamp) { - this.tokenExpirationTimestamp = tokenExpirationTimestamp; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + ((facebookId == null) ? 0 : facebookId.hashCode()); - result = prime * result + ((token == null) ? 0 : token.hashCode()); - result = prime * result + ((tokenExpirationTimestamp == null) ? 0 : tokenExpirationTimestamp.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - FacebookSocialIdentity other = (FacebookSocialIdentity) obj; - if (facebookId == null) { - if (other.facebookId != null) - return false; - } else if (!facebookId.equals(other.facebookId)) - return false; - if (token == null) { - if (other.token != null) - return false; - } else if (!token.equals(other.token)) - return false; - if (tokenExpirationTimestamp == null) { - if (other.tokenExpirationTimestamp != null) - return false; - } else if (!tokenExpirationTimestamp.equals(other.tokenExpirationTimestamp)) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("FacebookSocialIdentity [facebookId=").append(facebookId).append(", token=") - .append(token).append(", tokenExpirationTimestamp=").append(tokenExpirationTimestamp) - .append(", socialIdentityId=").append(this.getSocialIdentityId()).append(", creationTimestamp=") - .append(this.getCreationTimestamp()).append(", lastUpdateTimestamp=") - .append(this.getLastUpdateTimestamp()).append(", status=").append(this.getStatus()).append("]") - .toString(); - } - -} diff --git a/src/main/java/biz/nynja/account/grpc/models/PhoneCommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/PhoneCommunicationProvider.java deleted file mode 100644 index 33c0a26..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/PhoneCommunicationProvider.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -public class PhoneCommunicationProvider extends CommunicationProvider { - - private String phoneNumber; - private String country; - - public String getPhoneNumber() { - return phoneNumber; - } - - public void setPhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; - } - - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + ((country == null) ? 0 : country.hashCode()); - result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - PhoneCommunicationProvider other = (PhoneCommunicationProvider) obj; - if (country == null) { - if (other.country != null) - return false; - } else if (!country.equals(other.country)) - return false; - if (phoneNumber == null) { - if (other.phoneNumber != null) - return false; - } else if (!phoneNumber.equals(other.phoneNumber)) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("PhoneCommunicationProvider [phoneNumber=").append(phoneNumber).append(", country=") - .append(country).append(", providerId=").append(this.getProviderId()).append(", creationTimestamp=") - .append(this.getCreationTimestamp()).append(", lastUpdateTimestamp=") - .append(this.getLastUpdateTimestamp()).append(", status=").append(this.getStatus()).append("]") - .toString(); - } - -} diff --git a/src/main/java/biz/nynja/account/grpc/models/PhoneSocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/PhoneSocialIdentity.java deleted file mode 100644 index 7986f8b..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/PhoneSocialIdentity.java +++ /dev/null @@ -1,67 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -public class PhoneSocialIdentity extends SocialIdentity { - - private String phoneNumber; - private String country; - - public String getPhoneNumber() { - return phoneNumber; - } - - public void setPhoneNumber(String phoneNumber) { - this.phoneNumber = phoneNumber; - } - - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + ((country == null) ? 0 : country.hashCode()); - result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - PhoneSocialIdentity other = (PhoneSocialIdentity) obj; - if (country == null) { - if (other.country != null) - return false; - } else if (!country.equals(other.country)) - return false; - if (phoneNumber == null) { - if (other.phoneNumber != null) - return false; - } else if (!phoneNumber.equals(other.phoneNumber)) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("PhoneSocialIdentity [phoneNumber=").append(phoneNumber).append(", country=") - .append(country).append(", socialIdentityId=").append(this.getSocialIdentityId()) - .append(", creationTimestamp=").append(this.getCreationTimestamp()).append(", lastUpdateTimestamp=") - .append(this.getLastUpdateTimestamp()).append(", status=").append(this.getStatus()).append("]") - .toString(); - } - -} diff --git a/src/main/java/biz/nynja/account/grpc/models/Profile.java b/src/main/java/biz/nynja/account/grpc/models/Profile.java index 1ed9fa1..2ef9957 100644 --- a/src/main/java/biz/nynja/account/grpc/models/Profile.java +++ b/src/main/java/biz/nynja/account/grpc/models/Profile.java @@ -3,18 +3,23 @@ */ package biz.nynja.account.grpc.models; -import java.util.List; +import java.util.Set; import java.util.UUID; -import biz.nynja.account.grpc.Status; +import org.springframework.data.cassandra.core.mapping.PrimaryKey; +import org.springframework.data.cassandra.core.mapping.Table; +@Table public class Profile { + @PrimaryKey private UUID profileId; + private Set authenticationProviders; + private AuthenticationProvider backupAuthenticationProvider; + private String passcode; + private UUID defaultAccount; private Long creationTimestamp; private Long lastUpdateTimestamp; - private Status status; - List userAccounts; public UUID getProfileId() { return profileId; @@ -24,6 +29,14 @@ public class Profile { this.profileId = profileId; } + public Set getAuthenticationProviders() { + return authenticationProviders; + } + + public void setAuthenticationProviders(Set authenticationProviders) { + this.authenticationProviders = authenticationProviders; + } + public Long getCreationTimestamp() { return creationTimestamp; } @@ -40,31 +53,42 @@ public class Profile { this.lastUpdateTimestamp = lastUpdateTimestamp; } - public Status getStatus() { - return status; + public String getPasscode() { + return passcode; + } + + public void setPasscode(String passcode) { + this.passcode = passcode; + } + + public UUID getDefaultAccount() { + return defaultAccount; } - public void setStatus(Status status) { - this.status = status; + public void setDefaultAccount(UUID defaultAccount) { + this.defaultAccount = defaultAccount; } - public List getUserAccounts() { - return userAccounts; + public AuthenticationProvider getBackupAuthenticationProvider() { + return backupAuthenticationProvider; } - public void setUserAccounts(List userAccounts) { - this.userAccounts = userAccounts; + public void setBackupAuthenticationProvider(AuthenticationProvider backupAuthenticationProvider) { + this.backupAuthenticationProvider = backupAuthenticationProvider; } @Override public int hashCode() { final int prime = 31; int result = 1; + result = prime * result + ((authenticationProviders == null) ? 0 : authenticationProviders.hashCode()); + result = prime * result + + ((backupAuthenticationProvider == null) ? 0 : backupAuthenticationProvider.hashCode()); result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); + result = prime * result + ((defaultAccount == null) ? 0 : defaultAccount.hashCode()); result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); + result = prime * result + ((passcode == null) ? 0 : passcode.hashCode()); result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); - result = prime * result + ((status == null) ? 0 : status.hashCode()); - result = prime * result + ((userAccounts == null) ? 0 : userAccounts.hashCode()); return result; } @@ -77,37 +101,51 @@ public class Profile { if (getClass() != obj.getClass()) return false; Profile other = (Profile) obj; + if (authenticationProviders == null) { + if (other.authenticationProviders != null) + return false; + } else if (!authenticationProviders.equals(other.authenticationProviders)) + return false; + if (backupAuthenticationProvider == null) { + if (other.backupAuthenticationProvider != null) + return false; + } else if (!backupAuthenticationProvider.equals(other.backupAuthenticationProvider)) + return false; if (creationTimestamp == null) { if (other.creationTimestamp != null) return false; } else if (!creationTimestamp.equals(other.creationTimestamp)) return false; + if (defaultAccount == null) { + if (other.defaultAccount != null) + return false; + } else if (!defaultAccount.equals(other.defaultAccount)) + return false; if (lastUpdateTimestamp == null) { if (other.lastUpdateTimestamp != null) return false; } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) return false; + if (passcode == null) { + if (other.passcode != null) + return false; + } else if (!passcode.equals(other.passcode)) + return false; if (profileId == null) { if (other.profileId != null) return false; } else if (!profileId.equals(other.profileId)) return false; - if (status != other.status) - return false; - if (userAccounts == null) { - if (other.userAccounts != null) - return false; - } else if (!userAccounts.equals(other.userAccounts)) - return false; return true; } @Override public String toString() { - return new StringBuilder("Profile [profileId=").append(profileId).append(", creationTimestamp=") - .append(creationTimestamp).append(", lastUpdateTimestamp=").append(lastUpdateTimestamp) - .append(", status=").append(status).append(", userAccounts=").append(userAccounts).append("]") - .toString(); + return new StringBuilder("Profile [profileId=").append(profileId).append(", authenticationProviders=") + .append(authenticationProviders).append(", creationTimestamp=").append(creationTimestamp) + .append(", lastUpdateTimestamp=").append(lastUpdateTimestamp).append(", passcode=******") + .append(", defaultAccount=").append(defaultAccount).append(", backupAuthenticationProvider=") + .append(backupAuthenticationProvider).append("]").toString(); } } diff --git a/src/main/java/biz/nynja/account/grpc/models/ProfileByAuthenticationProvider.java b/src/main/java/biz/nynja/account/grpc/models/ProfileByAuthenticationProvider.java new file mode 100644 index 0000000..4a3b8cc --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/ProfileByAuthenticationProvider.java @@ -0,0 +1,81 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.util.UUID; + +public class ProfileByAuthenticationProvider { + + private String authenticationProvider; + private String authenticationProviderType; + private UUID profileId; + + public String getAuthenticationProvider() { + return authenticationProvider; + } + + public void setAuthenticationProvider(String authenticationProvider) { + this.authenticationProvider = authenticationProvider; + } + + public String getAuthenticationProviderType() { + return authenticationProviderType; + } + + public void setAuthenticationProviderType(String authenticationProviderType) { + this.authenticationProviderType = authenticationProviderType; + } + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((authenticationProvider == null) ? 0 : authenticationProvider.hashCode()); + result = prime * result + ((authenticationProviderType == null) ? 0 : authenticationProviderType.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ProfileByAuthenticationProvider other = (ProfileByAuthenticationProvider) obj; + if (authenticationProvider == null) { + if (other.authenticationProvider != null) + return false; + } else if (!authenticationProvider.equals(other.authenticationProvider)) + return false; + if (authenticationProviderType == null) { + if (other.authenticationProviderType != null) + return false; + } else if (!authenticationProviderType.equals(other.authenticationProviderType)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("ProfileByAuthenticationProvider [authenticationProvider=") + .append(authenticationProvider).append(", authenticationProviderType=") + .append(authenticationProviderType).append(", profileId=").append(profileId).append("]").toString(); + } +} diff --git a/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java b/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java deleted file mode 100644 index 8024b04..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/SocialIdentity.java +++ /dev/null @@ -1,96 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -import java.util.UUID; - -import biz.nynja.account.grpc.Status; - -public class SocialIdentity { - - private UUID socialIdentityId; - private Long creationTimestamp; - private Long lastUpdateTimestamp; - private Status status; - - public UUID getSocialIdentityId() { - return socialIdentityId; - } - - public void setSocialIdentityId(UUID socialIdentityId) { - this.socialIdentityId = socialIdentityId; - } - - public Long getCreationTimestamp() { - return creationTimestamp; - } - - public void setCreationTimestamp(Long creationTimestamp) { - this.creationTimestamp = creationTimestamp; - } - - public Long getLastUpdateTimestamp() { - return lastUpdateTimestamp; - } - - public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { - this.lastUpdateTimestamp = lastUpdateTimestamp; - } - - public Status getStatus() { - return status; - } - - public void setStatus(Status status) { - this.status = status; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); - result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); - result = prime * result + ((socialIdentityId == null) ? 0 : socialIdentityId.hashCode()); - result = prime * result + ((status == null) ? 0 : status.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - SocialIdentity other = (SocialIdentity) obj; - if (creationTimestamp == null) { - if (other.creationTimestamp != null) - return false; - } else if (!creationTimestamp.equals(other.creationTimestamp)) - return false; - if (lastUpdateTimestamp == null) { - if (other.lastUpdateTimestamp != null) - return false; - } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) - return false; - if (socialIdentityId == null) { - if (other.socialIdentityId != null) - return false; - } else if (!socialIdentityId.equals(other.socialIdentityId)) - return false; - if (status != other.status) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("SocialIdentity [socialIdentityId=").append(socialIdentityId) - .append(", creationTimestamp=").append(creationTimestamp).append(", lastUpdateTimestamp=") - .append(lastUpdateTimestamp).append(", status=").append(status).append("]").toString(); - } - -} diff --git a/src/main/java/biz/nynja/account/grpc/models/UserAccount.java b/src/main/java/biz/nynja/account/grpc/models/UserAccount.java deleted file mode 100644 index b1ed1f1..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/UserAccount.java +++ /dev/null @@ -1,211 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -import java.util.List; -import java.util.UUID; - -import javax.validation.constraints.Size; - -import biz.nynja.account.grpc.Status; - -public class UserAccount { - - private UUID accountId; - @Size(max = 256) - private String firstName; - @Size(max = 256) - private String lastName; - @Size(max = 256) - private String middleName; - @Size(max = 256) - private String username; - private String password; - private Long creationTimestamp; - private Long lastUpdateTimestamp; - private Status status; - private List socialIdentities; - private List communicationProviders; - - public UUID getAccountId() { - return accountId; - } - - public void setAccountId(UUID accountId) { - this.accountId = accountId; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } - - public String getMiddleName() { - return middleName; - } - - public void setMiddleName(String middleName) { - this.middleName = middleName; - } - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public Long getCreationTimestamp() { - return creationTimestamp; - } - - public void setCreationTimestamp(Long creationTimestamp) { - this.creationTimestamp = creationTimestamp; - } - - public Long getLastUpdateTimestamp() { - return lastUpdateTimestamp; - } - - public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { - this.lastUpdateTimestamp = lastUpdateTimestamp; - } - - public Status getStatus() { - return status; - } - - public void setStatus(Status status) { - this.status = status; - } - - public List getSocialIdentities() { - return socialIdentities; - } - - public void setSocialIdentities(List socialIdentities) { - this.socialIdentities = socialIdentities; - } - - public List getCommunicationProviders() { - return communicationProviders; - } - - public void setCommunicationProviders(List communicationProviders) { - this.communicationProviders = communicationProviders; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); - result = prime * result + ((communicationProviders == null) ? 0 : communicationProviders.hashCode()); - result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); - result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); - result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); - result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); - result = prime * result + ((middleName == null) ? 0 : middleName.hashCode()); - result = prime * result + ((password == null) ? 0 : password.hashCode()); - result = prime * result + ((socialIdentities == null) ? 0 : socialIdentities.hashCode()); - result = prime * result + ((status == null) ? 0 : status.hashCode()); - result = prime * result + ((username == null) ? 0 : username.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - UserAccount other = (UserAccount) obj; - if (accountId == null) { - if (other.accountId != null) - return false; - } else if (!accountId.equals(other.accountId)) - return false; - if (communicationProviders == null) { - if (other.communicationProviders != null) - return false; - } else if (!communicationProviders.equals(other.communicationProviders)) - return false; - if (creationTimestamp == null) { - if (other.creationTimestamp != null) - return false; - } else if (!creationTimestamp.equals(other.creationTimestamp)) - return false; - if (firstName == null) { - if (other.firstName != null) - return false; - } else if (!firstName.equals(other.firstName)) - return false; - if (lastName == null) { - if (other.lastName != null) - return false; - } else if (!lastName.equals(other.lastName)) - return false; - if (lastUpdateTimestamp == null) { - if (other.lastUpdateTimestamp != null) - return false; - } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) - return false; - if (middleName == null) { - if (other.middleName != null) - return false; - } else if (!middleName.equals(other.middleName)) - return false; - if (password == null) { - if (other.password != null) - return false; - } else if (!password.equals(other.password)) - return false; - if (socialIdentities == null) { - if (other.socialIdentities != null) - return false; - } else if (!socialIdentities.equals(other.socialIdentities)) - return false; - if (status != other.status) - return false; - if (username == null) { - if (other.username != null) - return false; - } else if (!username.equals(other.username)) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("UserAccount [accountId=").append(accountId).append(", firstName=").append(firstName) - .append(", lastName=").append(lastName).append(", middleName=").append(middleName).append(", username=") - .append(username).append(", password=[]").append(", creationTimestamp=").append(creationTimestamp) - .append(", lastUpdateTimestamp=").append(lastUpdateTimestamp).append(", status=").append(status) - .append(", socialIdentities=").append(socialIdentities).append(", communicationProviders=") - .append(communicationProviders).append("]").toString(); - } - -} diff --git a/src/main/java/biz/nynja/account/grpc/models/UsernameCommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/UsernameCommunicationProvider.java deleted file mode 100644 index 0a27664..0000000 --- a/src/main/java/biz/nynja/account/grpc/models/UsernameCommunicationProvider.java +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright (C) 2018 Nynja Inc. All rights reserved. - */ -package biz.nynja.account.grpc.models; - -public class UsernameCommunicationProvider extends CommunicationProvider { - - private String username; - - public String getUsername() { - return username; - } - - public void setUsername(String username) { - this.username = username; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = super.hashCode(); - result = prime * result + ((username == null) ? 0 : username.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (!super.equals(obj)) - return false; - if (getClass() != obj.getClass()) - return false; - UsernameCommunicationProvider other = (UsernameCommunicationProvider) obj; - if (username == null) { - if (other.username != null) - return false; - } else if (!username.equals(other.username)) - return false; - return true; - } - - @Override - public String toString() { - return new StringBuilder("UsernameCommunicationProvider [username=").append(username).append(", providerId=") - .append(this.getProviderId()).append(", creationTimestamp=").append(this.getCreationTimestamp()) - .append(", lastUpdateTimestamp=").append(this.getLastUpdateTimestamp()).append(", status=") - .append(this.getStatus()).append("]").toString(); - } -} -- GitLab From 0e9ba795501742c042a6148039568525500eacdb Mon Sep 17 00:00:00 2001 From: Dragomir Todorov Date: Wed, 22 Aug 2018 15:45:02 +0300 Subject: [PATCH 30/37] NY-2951: Added spring startup listener to execute cassandra scripts --- .../account/grpc/StartupScriptsListener.java | 61 +++++++++++++++++++ .../grpc/configuration/CassandraConfig.java | 46 +++++--------- .../grpc/services/AccountServiceTests.java | 5 ++ 3 files changed, 81 insertions(+), 31 deletions(-) create mode 100644 src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java diff --git a/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java b/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java new file mode 100644 index 0000000..99141da --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java @@ -0,0 +1,61 @@ +package biz.nynja.account.grpc; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +import com.datastax.driver.core.Session; + +import biz.nynja.account.grpc.configuration.CassandraConfig; + +/** + * This acts as {@link CassandraConfig} startupScripts executor + * but activated after the spring has setup the needed tables though JPA + * @author dragomir.todorov + * + */ +@Component +public class StartupScriptsListener { + + private String keyspace; + + @Autowired + private Session session; + + @EventListener(ContextRefreshedEvent.class) + public void contextRefreshedEvent() { + keyspace = session.getLoggedKeyspace(); + + for (String script : getStartupScripts()) { + session.execute(script); + } + } + + private List getStartupScripts() { + String scriptViewEmail = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + ".userinfo_by_email AS SELECT * FROM " + keyspace + ".userinfo " + + "WHERE emailaddress IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " + + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(emailaddress, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; + + String scriptViewPhone = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + ".userinfo_by_phone AS SELECT * FROM " + keyspace + ".userinfo " + + "WHERE phonenumber IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " + + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(phonenumber, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; + + String scriptViewUsername = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + ".userinfo_by_username AS SELECT * FROM " + keyspace + ".userinfo " + + "WHERE username IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL " + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(username, authnetication_provider_id, profile_id, account_id, authentication_type) "; + + return Arrays.asList(scriptViewEmail, scriptViewPhone, scriptViewUsername); + } +} \ No newline at end of file diff --git a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java index a4252ac..0aad4ca 100644 --- a/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java +++ b/src/main/java/biz/nynja/account/grpc/configuration/CassandraConfig.java @@ -14,6 +14,8 @@ import org.springframework.data.cassandra.config.SchemaAction; import org.springframework.data.cassandra.core.cql.keyspace.CreateKeyspaceSpecification; import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories; +import biz.nynja.account.grpc.StartupScriptsListener; + @Configuration @EnableCassandraRepositories @ConditionalOnMissingClass("org.springframework.test.context.junit4.SpringRunner") @@ -26,48 +28,30 @@ public class CassandraConfig extends AbstractCassandraConfiguration { protected String getKeyspaceName() { return keyspace; } - + @Override public SchemaAction getSchemaAction() { return SchemaAction.CREATE_IF_NOT_EXISTS; } - @Override - public String[] getEntityBasePackages() { - return new String[] { "biz.nynja.account.grpc.models" }; - } - @Override protected List getKeyspaceCreations() { CreateKeyspaceSpecification specification = CreateKeyspaceSpecification.createKeyspace(getKeyspaceName()) .ifNotExists().withSimpleReplication(); return Arrays.asList(specification); } - + + @Override + public String[] getEntityBasePackages() { + return new String[] { "biz.nynja.account.grpc.models" }; + } + + /** + * See {@link StartupScriptsListener} for scripts + * that require JPA annotated tables + */ @Override protected List getStartupScripts() { - - String scriptViewEmail = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + getKeyspaceName() - + ".userinfo_by_email AS SELECT * FROM " + getKeyspaceName() + ".userinfo " - + "WHERE emailaddress IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " - + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(emailaddress, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; - - String scriptViewPhone = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + getKeyspaceName() - + ".userinfo_by_phone AS SELECT * FROM " + getKeyspaceName() + ".userinfo " - + "WHERE phonenumber IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " - + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(phonenumber, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; - - String scriptViewUsername = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + getKeyspaceName() - + ".userinfo_by_username AS SELECT * FROM " + getKeyspaceName() + ".userinfo " - + "WHERE username IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL " - + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(username, authnetication_provider_id, profile_id, account_id, authentication_type) "; - - return Arrays.asList(scriptViewEmail, scriptViewPhone, scriptViewUsername); + return super.getStartupScripts(); } -} +} \ No newline at end of file diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index 0d6ee65..8d63c00 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -23,6 +23,8 @@ import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringRunner; +import com.datastax.driver.core.Session; + import biz.nynja.account.grpc.AccountServiceGrpc; import biz.nynja.account.grpc.AccountsByAuthenticationProviderRequest; import biz.nynja.account.grpc.AccountsByProfileIdRequest; @@ -64,6 +66,9 @@ public class AccountServiceTests extends GrpcServerTestBase { @MockBean private UserInfoByPhoneRepository userInfoByPhoneRepository; + @MockBean + private Session session; + @MockBean private UserInfoByUsernameRepository userInfoByUsernameRepository; -- GitLab From 80c0c8455d4c802f2043d075ae0b4875b2a815b1 Mon Sep 17 00:00:00 2001 From: abotev-intracol Date: Wed, 22 Aug 2018 16:10:17 +0300 Subject: [PATCH 31/37] Start unit tests in random port; Signed-off-by: abotev-intracol --- .../biz/nynja/account/grpc/services/AccountServiceTests.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java index 8d63c00..a7a6820 100644 --- a/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/grpc/services/AccountServiceTests.java @@ -16,6 +16,7 @@ import java.util.concurrent.ExecutionException; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; @@ -51,6 +52,7 @@ import biz.nynja.account.grpc.utils.Util; @RunWith(SpringRunner.class) @SpringBootTest(classes = Util.class, + webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfiguration", "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration" }) -- GitLab From 12a8171cceb0745cf7dfdd158e31ac2e384bdf53 Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Mon, 27 Aug 2018 19:26:43 +0300 Subject: [PATCH 32/37] NY-2654: Create materialized views based on the new DB design Signed-off-by: Ralitsa Todorova --- .../account/grpc/StartupScriptsListener.java | 87 +++++++++++-------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java b/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java index 99141da..eb406e3 100644 --- a/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java +++ b/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java @@ -21,41 +21,54 @@ import biz.nynja.account.grpc.configuration.CassandraConfig; @Component public class StartupScriptsListener { - private String keyspace; - - @Autowired - private Session session; - - @EventListener(ContextRefreshedEvent.class) - public void contextRefreshedEvent() { - keyspace = session.getLoggedKeyspace(); - - for (String script : getStartupScripts()) { - session.execute(script); - } - } - - private List getStartupScripts() { - String scriptViewEmail = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".userinfo_by_email AS SELECT * FROM " + keyspace + ".userinfo " - + "WHERE emailaddress IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " - + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(emailaddress, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; - - String scriptViewPhone = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".userinfo_by_phone AS SELECT * FROM " + keyspace + ".userinfo " - + "WHERE phonenumber IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " - + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(phonenumber, authnetication_provider_id, profile_id, account_id, username, authentication_type) "; - - String scriptViewUsername = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".userinfo_by_username AS SELECT * FROM " + keyspace + ".userinfo " - + "WHERE username IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL " + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(username, authnetication_provider_id, profile_id, account_id, authentication_type) "; - - return Arrays.asList(scriptViewEmail, scriptViewPhone, scriptViewUsername); - } + private String keyspace; + + @Autowired + private Session session; + + @EventListener(ContextRefreshedEvent.class) + public void contextRefreshedEvent() { + keyspace = session.getLoggedKeyspace(); + + for (String script : getStartupScripts()) { + session.execute(script); + } + } + + private List getStartupScripts() { + String scriptAccountViewByProfileId = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + "account_by_profile_id AS SELECT * FROM account " + "WHERE profileid IS NOT NULL " + + "PRIMARY KEY (profileid, accountid);"; + + String scriptAccountViewByAuthProvider = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + "account_by_authentication_provider AS SELECT * FROM account " + + "WHERE authenticationprovider IS NOT NULL " + "PRIMARY KEY (authenticationprovider, accountid);"; + + String scriptAccountViewByАccountName = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + "account_by_account_name AS SELECT * FROM account " + "WHERE accountname IS NOT NULL " + + "PRIMARY KEY (accountname, accountid);"; + + String scriptViewEmail = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + ".userinfo_by_email AS SELECT * FROM " + keyspace + ".userinfo " + + "WHERE emailaddress IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " + + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(emailaddress, authnetication_provider_id, profile_id, account_id, username, authentication_type); "; + + String scriptViewPhone = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + ".userinfo_by_phone AS SELECT * FROM " + keyspace + ".userinfo " + + "WHERE phonenumber IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " + + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(phonenumber, authnetication_provider_id, profile_id, account_id, username, authentication_type); "; + + String scriptViewUsername = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace + + ".userinfo_by_username AS SELECT * FROM " + keyspace + ".userinfo " + + "WHERE username IS NOT NULL and authnetication_provider_id IS NOT NULL " + + "and profile_id IS NOT NULL and account_id IS NOT NULL " + "and authentication_type IS NOT NULL " + + "PRIMARY KEY(username, authnetication_provider_id, profile_id, account_id, authentication_type); "; + + return Arrays.asList(scriptAccountViewByProfileId, scriptAccountViewByAuthProvider, + scriptAccountViewByАccountName, scriptViewEmail, scriptViewPhone, scriptViewUsername); + } } \ No newline at end of file -- GitLab From 03dc3812348ba1fb87ab4eb0512bcb52d3b82695 Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Tue, 28 Aug 2018 12:41:48 +0300 Subject: [PATCH 33/37] Fix typo in Materialized view creation scripts Signed-off-by: Ralitsa Todorova --- .../java/biz/nynja/account/grpc/StartupScriptsListener.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java b/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java index eb406e3..473bba9 100644 --- a/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java +++ b/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java @@ -37,15 +37,15 @@ public class StartupScriptsListener { private List getStartupScripts() { String scriptAccountViewByProfileId = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + "account_by_profile_id AS SELECT * FROM account " + "WHERE profileid IS NOT NULL " + + ".account_by_profile_id AS SELECT * FROM account " + "WHERE profileid IS NOT NULL " + "PRIMARY KEY (profileid, accountid);"; String scriptAccountViewByAuthProvider = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + "account_by_authentication_provider AS SELECT * FROM account " + + ".account_by_authentication_provider AS SELECT * FROM account " + "WHERE authenticationprovider IS NOT NULL " + "PRIMARY KEY (authenticationprovider, accountid);"; String scriptAccountViewByАccountName = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + "account_by_account_name AS SELECT * FROM account " + "WHERE accountname IS NOT NULL " + + ".account_by_account_name AS SELECT * FROM account " + "WHERE accountname IS NOT NULL " + "PRIMARY KEY (accountname, accountid);"; String scriptViewEmail = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace -- GitLab From a866cd6a6bc624d24b0c8a59de80ddbb95d34844 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Tue, 28 Aug 2018 13:25:42 +0300 Subject: [PATCH 34/37] NY-2981: Implementation - implementation for additional tables and repositories according to the new DB model; - changes in existing tables and repositories; - implementation for pending account creation. Signed-off-by: Stanimir Penkov --- .../nynja/account/grpc/models/Account.java | 44 +++++- .../AccountByCommunicationProvider.java | 140 ++++++++++++++++++ .../grpc/models/AuthenticationProvider.java | 17 +++ .../account/grpc/models/PendingAccount.java | 137 +++++++++++++++++ .../ProfileByAuthenticationProvider.java | 10 ++ ...ountByCommunicationProviderRepository.java | 16 ++ .../grpc/repositories/AccountRepository.java | 18 +++ .../AccountRepositoryAdditional.java | 19 +++ .../AccountRepositoryAdditionalImpl.java | 130 ++++++++++++++++ .../PendingAccountRepository.java | 19 +++ ...ileByAuthenticationProviderRepository.java | 14 ++ .../grpc/repositories/ProfileRepository.java | 18 +++ 12 files changed, 574 insertions(+), 8 deletions(-) create mode 100644 src/main/java/biz/nynja/account/grpc/models/AccountByCommunicationProvider.java create mode 100644 src/main/java/biz/nynja/account/grpc/models/PendingAccount.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/AccountByCommunicationProviderRepository.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/AccountRepository.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditional.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditionalImpl.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/PendingAccountRepository.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/ProfileByAuthenticationProviderRepository.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/ProfileRepository.java diff --git a/src/main/java/biz/nynja/account/grpc/models/Account.java b/src/main/java/biz/nynja/account/grpc/models/Account.java index 4e0a058..a653f96 100644 --- a/src/main/java/biz/nynja/account/grpc/models/Account.java +++ b/src/main/java/biz/nynja/account/grpc/models/Account.java @@ -14,6 +14,7 @@ import org.springframework.data.cassandra.core.mapping.PrimaryKey; import org.springframework.data.cassandra.core.mapping.Table; import biz.nynja.account.grpc.Status; +import biz.nynja.account.grpc.CreatePendingAccountRequest; @Table public class Account { @@ -29,7 +30,8 @@ public class Account { private ByteBuffer avatar; private String accountName; private String username; - private Status accountStatus; + private String qrCode; + private String accountStatus; private Long creationTimestamp; private Long lastUpdateTimestamp; private Set communicationProviders; @@ -114,11 +116,19 @@ public class Account { this.username = username; } - public Status getAccountStatus() { + public String getQrCode() { + return qrCode; + } + + public void setQrCode(String qrCode) { + this.qrCode = qrCode; + } + + public String getAccountStatus() { return accountStatus; } - public void setAccountStatus(Status accountStatus) { + public void setAccountStatus(String accountStatus) { this.accountStatus = accountStatus; } @@ -163,6 +173,7 @@ public class Account { result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + result = prime * result + ((qrCode == null) ? 0 : qrCode.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @@ -191,7 +202,10 @@ public class Account { return false; } else if (!accountName.equals(other.accountName)) return false; - if (accountStatus != other.accountStatus) + if (accountStatus == null) { + if (other.accountStatus != null) + return false; + } else if (!accountStatus.equals(other.accountStatus)) return false; if (authenticationProvider == null) { if (other.authenticationProvider != null) @@ -238,6 +252,11 @@ public class Account { return false; } else if (!profileId.equals(other.profileId)) return false; + if (qrCode == null) { + if (other.qrCode != null) + return false; + } else if (!qrCode.equals(other.qrCode)) + return false; if (username == null) { if (other.username != null) return false; @@ -253,10 +272,19 @@ public class Account { .append(authenticationProvider).append(", authenticationProviderType=") .append(authenticationProviderType).append(", firstName=").append(firstName).append(", lastName=") .append(lastName).append(", avatar=").append(avatar).append(", accountName=").append(accountName) - .append(", username=").append(username).append(", accountStatus=").append(accountStatus) - .append(", creationTimestamp=").append(creationTimestamp).append(", lastUpdateTimestamp=") - .append(lastUpdateTimestamp).append(", communicationProviders=").append(communicationProviders) - .append("]").toString(); + .append(", username=").append(username).append(", qrCode=").append(qrCode).append(", accountStatus=") + .append(accountStatus).append(", creationTimestamp=").append(creationTimestamp) + .append(", lastUpdateTimestamp=").append(lastUpdateTimestamp).append(", communicationProviders=") + .append(communicationProviders).append("]").toString(); + } + + public static Account createPendingAccountFromProto(CreatePendingAccountRequest request) { + Account account = new Account(); + account.setAuthenticationProviderType(request.getAuthenticationType().toString()); + account.setAuthenticationProvider(request.getAuthenticationProvider()); + return account; + } + } } diff --git a/src/main/java/biz/nynja/account/grpc/models/AccountByCommunicationProvider.java b/src/main/java/biz/nynja/account/grpc/models/AccountByCommunicationProvider.java new file mode 100644 index 0000000..5c47680 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/AccountByCommunicationProvider.java @@ -0,0 +1,140 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.nio.ByteBuffer; +import java.util.UUID; + +import org.springframework.data.cassandra.core.cql.PrimaryKeyType; +import org.springframework.data.cassandra.core.mapping.Column; +import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; +import org.springframework.data.cassandra.core.mapping.Table; + +@Table("account_by_communication_provider") +public class AccountByCommunicationProvider { + + @PrimaryKeyColumn(name = "communication_provider", ordinal = 0, type = PrimaryKeyType.PARTITIONED) + private String communicationProvider; + @PrimaryKeyColumn(name = "communication_provider_type", ordinal = 1, type = PrimaryKeyType.PARTITIONED) + private String communicationProviderType; + @PrimaryKeyColumn(name = "account_id", ordinal = 2, type = PrimaryKeyType.CLUSTERED) + private UUID accountId; + @Column("account_name") + private String accountName; + @Column("first_name") + private String firstName; + private ByteBuffer avatar; + + public String getCommunicationProvider() { + return communicationProvider; + } + + public void setCommunicationProvider(String communicationProvider) { + this.communicationProvider = communicationProvider; + } + + public String getCommunicationProviderType() { + return communicationProviderType; + } + + public void setCommunicationProviderType(String communicationProviderType) { + this.communicationProviderType = communicationProviderType; + } + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public ByteBuffer getAvatar() { + return avatar; + } + + public void setAvatar(ByteBuffer avatar) { + this.avatar = avatar; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((accountName == null) ? 0 : accountName.hashCode()); + result = prime * result + ((avatar == null) ? 0 : avatar.hashCode()); + result = prime * result + ((communicationProvider == null) ? 0 : communicationProvider.hashCode()); + result = prime * result + ((communicationProviderType == null) ? 0 : communicationProviderType.hashCode()); + result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AccountByCommunicationProvider other = (AccountByCommunicationProvider) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (accountName == null) { + if (other.accountName != null) + return false; + } else if (!accountName.equals(other.accountName)) + return false; + if (avatar == null) { + if (other.avatar != null) + return false; + } else if (!avatar.equals(other.avatar)) + return false; + if (communicationProvider == null) { + if (other.communicationProvider != null) + return false; + } else if (!communicationProvider.equals(other.communicationProvider)) + return false; + if (communicationProviderType == null) { + if (other.communicationProviderType != null) + return false; + } else if (!communicationProviderType.equals(other.communicationProviderType)) + return false; + if (firstName == null) { + if (other.firstName != null) + return false; + } else if (!firstName.equals(other.firstName)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("AccountsByCommunicationProvider [communicationProvider=") + .append(communicationProvider).append(", communicationProviderType=").append(communicationProviderType) + .append(", accountId=").append(accountId).append(", accountName=").append(accountName) + .append(", firstName=").append(firstName).append(", avatar=").append(avatar).append("]").toString(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java b/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java index aaeadc1..8c67222 100644 --- a/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java +++ b/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java @@ -5,6 +5,9 @@ package biz.nynja.account.grpc.models; import org.springframework.data.cassandra.core.mapping.UserDefinedType; +import biz.nynja.account.grpc.AuthProviderDetails; +import biz.nynja.account.grpc.CreateAccountRequest; + @UserDefinedType public class AuthenticationProvider { @@ -64,4 +67,18 @@ public class AuthenticationProvider { .append("]").toString(); } + public static AuthenticationProvider createAuthenticationProviderFromProto(AuthProviderDetails authProviderDetails) { + AuthenticationProvider authenticationProvider = new AuthenticationProvider(); + authenticationProvider.setType(authProviderDetails.getAuthenticationType().toString()); + authenticationProvider.setValue(authProviderDetails.getAuthenticationProvider()); + return authenticationProvider; + } + + public static AuthenticationProvider createAuthenticationProviderFromStrings(String type, String value) { + AuthenticationProvider authenticationProvider = new AuthenticationProvider(); + authenticationProvider.setType(type); + authenticationProvider.setValue(value); + return authenticationProvider; + } + } diff --git a/src/main/java/biz/nynja/account/grpc/models/PendingAccount.java b/src/main/java/biz/nynja/account/grpc/models/PendingAccount.java new file mode 100644 index 0000000..a0bb463 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/PendingAccount.java @@ -0,0 +1,137 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.util.Date; +import java.util.UUID; + +import org.springframework.data.cassandra.core.mapping.Column; +import org.springframework.data.cassandra.core.mapping.PrimaryKey; +import org.springframework.data.cassandra.core.mapping.Table; + +import biz.nynja.account.grpc.CreatePendingAccountRequest; + +@Table("pending_account") +public class PendingAccount { + @PrimaryKey("account_id") + private UUID accountId; + @Column("profile_id") + private UUID profileId; + @Column("authentication_provider") + private String authenticationProvider; + @Column("authentication_provider_type") + private String authenticationProviderType; + @Column("creation_timestamp") + private Long creationTimestamp; + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + public String getAuthenticationProvider() { + return authenticationProvider; + } + + public void setAuthenticationProvider(String authenticationProvider) { + this.authenticationProvider = authenticationProvider; + } + + public String getAuthenticationProviderType() { + return authenticationProviderType; + } + + public void setAuthenticationProviderType(String authenticationProviderType) { + this.authenticationProviderType = authenticationProviderType; + } + + public Long getCreationTimestamp() { + return creationTimestamp; + } + + public void setCreationTimestamp(Long creationTimestamp) { + this.creationTimestamp = creationTimestamp; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((authenticationProvider == null) ? 0 : authenticationProvider.hashCode()); + result = prime * result + ((authenticationProviderType == null) ? 0 : authenticationProviderType.hashCode()); + result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + PendingAccount other = (PendingAccount) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (authenticationProvider == null) { + if (other.authenticationProvider != null) + return false; + } else if (!authenticationProvider.equals(other.authenticationProvider)) + return false; + if (authenticationProviderType == null) { + if (other.authenticationProviderType != null) + return false; + } else if (!authenticationProviderType.equals(other.authenticationProviderType)) + return false; + if (creationTimestamp == null) { + if (other.creationTimestamp != null) + return false; + } else if (!creationTimestamp.equals(other.creationTimestamp)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("PendingAccounts [accountId=").append(accountId).append(", profileId=") + .append(profileId).append(", authenticationProvider=").append(authenticationProvider) + .append(", authenticationProviderType=").append(authenticationProviderType) + .append(", creationTimestamp=").append(creationTimestamp).append("]").toString(); + } + + public static PendingAccount fromProto(CreatePendingAccountRequest proto) { + PendingAccount pendingAccount = new PendingAccount(); + pendingAccount.setAuthenticationProviderType(proto.getAuthenticationType().toString()); + pendingAccount.setAuthenticationProvider(proto.getAuthenticationProvider()); + return pendingAccount; + } + + public biz.nynja.account.grpc.PendingAccountDetails toProto() { + return biz.nynja.account.grpc.PendingAccountDetails.newBuilder().setAccountId(getAccountId().toString()) + .build(); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/ProfileByAuthenticationProvider.java b/src/main/java/biz/nynja/account/grpc/models/ProfileByAuthenticationProvider.java index 4a3b8cc..0558a4a 100644 --- a/src/main/java/biz/nynja/account/grpc/models/ProfileByAuthenticationProvider.java +++ b/src/main/java/biz/nynja/account/grpc/models/ProfileByAuthenticationProvider.java @@ -5,10 +5,19 @@ package biz.nynja.account.grpc.models; import java.util.UUID; +import org.springframework.data.cassandra.core.cql.PrimaryKeyType; +import org.springframework.data.cassandra.core.mapping.Column; +import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; +import org.springframework.data.cassandra.core.mapping.Table; + +@Table("profile_by_authentication_provider") public class ProfileByAuthenticationProvider { + @PrimaryKeyColumn(name = "authentication_provider", ordinal = 0, type = PrimaryKeyType.PARTITIONED) private String authenticationProvider; + @PrimaryKeyColumn(name = "authentication_provider_type", ordinal = 1, type = PrimaryKeyType.PARTITIONED) private String authenticationProviderType; + @Column("profile_id") private UUID profileId; public String getAuthenticationProvider() { @@ -78,4 +87,5 @@ public class ProfileByAuthenticationProvider { .append(authenticationProvider).append(", authenticationProviderType=") .append(authenticationProviderType).append(", profileId=").append(profileId).append("]").toString(); } + } diff --git a/src/main/java/biz/nynja/account/grpc/repositories/AccountByCommunicationProviderRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/AccountByCommunicationProviderRepository.java new file mode 100644 index 0000000..2b2ae6c --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/AccountByCommunicationProviderRepository.java @@ -0,0 +1,16 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import java.util.UUID; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.AccountByCommunicationProvider; + +@Repository +public interface AccountByCommunicationProviderRepository extends CassandraRepository { + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/AccountRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/AccountRepository.java new file mode 100644 index 0000000..058aadd --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/AccountRepository.java @@ -0,0 +1,18 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import java.util.UUID; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.Account; + +@Repository +public interface AccountRepository extends CassandraRepository { + + public Account findByAccountId(UUID accountId); + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditional.java b/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditional.java new file mode 100644 index 0000000..6fe0e52 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditional.java @@ -0,0 +1,19 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.CreateAccountRequest; +import biz.nynja.account.grpc.CreatePendingAccountRequest; +import biz.nynja.account.grpc.models.Account; +import biz.nynja.account.grpc.models.PendingAccount; + +@Repository +public interface AccountRepositoryAdditional { + + public Account completePendingAccountCreation(CompletePendingAccountCreationRequest request); + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditionalImpl.java new file mode 100644 index 0000000..ca38eb6 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditionalImpl.java @@ -0,0 +1,130 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import java.util.Date; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.cassandra.core.CassandraBatchOperations; +import org.springframework.data.cassandra.core.CassandraTemplate; +import org.springframework.data.cassandra.core.WriteResult; +import org.springframework.stereotype.Service; + +import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.models.Account; +import biz.nynja.account.grpc.models.AuthenticationProvider; +import biz.nynja.account.grpc.models.PendingAccount; +import biz.nynja.account.grpc.models.Profile; +import biz.nynja.account.grpc.models.ProfileByAuthenticationProvider; + +@Service +public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditional { + + private static final Logger logger = LoggerFactory.getLogger(AccountRepositoryAdditionalImpl.class); + + @Autowired + private CassandraTemplate cassandraTemplate; + + @Autowired + private AccountRepository accountRepository; + + @Autowired + private PendingAccountRepository pendingAccountRepository; + + @Override + public Account completePendingAccountCreation(CompletePendingAccountCreationRequest request) { + CassandraBatchOperations batchOperations = cassandraTemplate.batchOps(); + PendingAccount pendingAccount = pendingAccountRepository + .findByAccountId(UUID.fromString(request.getAccountId())); + if (pendingAccount == null) { + return null; + } else { + Long timeCreated = new Date().getTime(); + Long checkMinutes = timeCreated - pendingAccount.getCreationTimestamp(); + if (checkMinutes > 30 * 60 * 1000) { + logger.info("Account creation timeout expired."); + return null; + } else { + WriteResult wr = null; + try { + newAccountInsert(batchOperations, request, pendingAccount, timeCreated); + newProfileInsert(batchOperations, request, pendingAccount, timeCreated); + newProfileByAuthenticationProviderInsert(batchOperations, pendingAccount); + wr = batchOperations.execute(); + } catch (IllegalArgumentException | IllegalStateException e) { + logger.info("Exception while completing pending account creation."); + logger.debug("Exception while completing pending account creation: {} ...", e.getMessage()); + return null; + } + if (wr != null) { + boolean applied = wr.wasApplied(); + if (applied) { + pendingAccountRepository.deleteById(UUID.fromString(request.getAccountId())); + Account createdAccount = accountRepository + .findByAccountId(UUID.fromString(request.getAccountId())); + return createdAccount; + } + } + return null; + } + } + } + + private void newAccountInsert(CassandraBatchOperations batchOps, CompletePendingAccountCreationRequest request, + PendingAccount pendingAccount, Long creationTimestamp) { + Account newAccount = new Account(); + newAccount.setAccountId(pendingAccount.getAccountId()); + newAccount.setProfileId(pendingAccount.getProfileId()); + newAccount.setAccountMark(request.getAccountMark()); + newAccount.setAuthenticationProvider(pendingAccount.getAuthenticationProvider()); + newAccount.setAuthenticationProviderType(pendingAccount.getAuthenticationProviderType()); + newAccount.setFirstName(request.getFirstName()); + newAccount.setLastName(request.getLastName()); + newAccount.setAvatar(request.getAvatar().asReadOnlyByteBuffer()); + newAccount.setAccountName(request.getAccountName()); + newAccount.setAccountStatus(request.getAccountStatus()); + newAccount.setUsername(request.getUsername()); + newAccount.setCreationTimestamp(creationTimestamp); + Set communicationProvidersSet = new HashSet(); + communicationProvidersSet.add(AuthenticationProvider.createAuthenticationProviderFromStrings( + pendingAccount.getAuthenticationProviderType(), pendingAccount.getAuthenticationProvider())); + newAccount.setCommunicationProviders(communicationProvidersSet); + if (request.getCommunicationProvidersList() != null) { + for (int i = 0; i < request.getCommunicationProvidersList().size(); i++) { + communicationProvidersSet.add(AuthenticationProvider + .createAuthenticationProviderFromProto(request.getCommunicationProviders(i))); + } + newAccount.setCommunicationProviders(communicationProvidersSet); + } + newAccount.setCommunicationProviders(communicationProvidersSet); + newAccount.setQrCode(request.getQrCode()); + batchOps.insert(newAccount); + } + + private void newProfileInsert(CassandraBatchOperations batchOps, CompletePendingAccountCreationRequest request, + PendingAccount pendingAccount, Long creationTimestamp) { + Profile newProfile = new Profile(); + newProfile.setProfileId(pendingAccount.getProfileId()); + Set authenticationProvidersSet = new HashSet(); + authenticationProvidersSet.add(AuthenticationProvider.createAuthenticationProviderFromStrings( + pendingAccount.getAuthenticationProviderType(), pendingAccount.getAuthenticationProvider())); + newProfile.setAuthenticationProviders(authenticationProvidersSet); + newProfile.setCreationTimestamp(creationTimestamp); + batchOps.insert(newProfile); + } + + private void newProfileByAuthenticationProviderInsert(CassandraBatchOperations batchOps, + PendingAccount pendingAccount) { + ProfileByAuthenticationProvider newProfileByAuthenticationProvider = new ProfileByAuthenticationProvider(); + newProfileByAuthenticationProvider.setAuthenticationProvider(pendingAccount.getAuthenticationProvider()); + newProfileByAuthenticationProvider.setAuthenticationProviderType(pendingAccount.getAuthenticationProvider()); + newProfileByAuthenticationProvider.setProfileId(pendingAccount.getProfileId()); + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/PendingAccountRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/PendingAccountRepository.java new file mode 100644 index 0000000..ac63c8a --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/PendingAccountRepository.java @@ -0,0 +1,19 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import java.util.UUID; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.Account; +import biz.nynja.account.grpc.models.PendingAccount; + +@Repository +public interface PendingAccountRepository extends CassandraRepository { + + public PendingAccount findByAccountId(UUID accountId); + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/ProfileByAuthenticationProviderRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/ProfileByAuthenticationProviderRepository.java new file mode 100644 index 0000000..45df898 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/ProfileByAuthenticationProviderRepository.java @@ -0,0 +1,14 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.ProfileByAuthenticationProvider; + +@Repository +public interface ProfileByAuthenticationProviderRepository extends CassandraRepository{ + +} diff --git a/src/main/java/biz/nynja/account/grpc/repositories/ProfileRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/ProfileRepository.java new file mode 100644 index 0000000..88f4336 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/ProfileRepository.java @@ -0,0 +1,18 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import java.util.UUID; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.Profile; + +@Repository +public interface ProfileRepository extends CassandraRepository { + + public Profile findByProfileId(UUID profileId); + +} -- GitLab From 0100c98444d8af19dacfea69592279651044c5d5 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Tue, 28 Aug 2018 19:08:18 +0300 Subject: [PATCH 35/37] NY-2981: Implementation - added implementation for creation of pending account; - added implementation for completing account creation based on pending account. Signed-off-by: Stanimir Penkov --- .../AccountRepositoryAdditionalImpl.java | 7 +- .../grpc/services/AccountServiceImpl.java | 66 ++++++++++++++++++- 2 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditionalImpl.java index ca38eb6..191e926 100644 --- a/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/grpc/repositories/AccountRepositoryAdditionalImpl.java @@ -43,6 +43,8 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio PendingAccount pendingAccount = pendingAccountRepository .findByAccountId(UUID.fromString(request.getAccountId())); if (pendingAccount == null) { + logger.info("Existing pending account with the provided id was not found."); + logger.debug("Existing pending account with the provided id {} was not found.", request.getAccountId()); return null; } else { Long timeCreated = new Date().getTime(); @@ -92,8 +94,6 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio newAccount.setUsername(request.getUsername()); newAccount.setCreationTimestamp(creationTimestamp); Set communicationProvidersSet = new HashSet(); - communicationProvidersSet.add(AuthenticationProvider.createAuthenticationProviderFromStrings( - pendingAccount.getAuthenticationProviderType(), pendingAccount.getAuthenticationProvider())); newAccount.setCommunicationProviders(communicationProvidersSet); if (request.getCommunicationProvidersList() != null) { for (int i = 0; i < request.getCommunicationProvidersList().size(); i++) { @@ -123,8 +123,9 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio PendingAccount pendingAccount) { ProfileByAuthenticationProvider newProfileByAuthenticationProvider = new ProfileByAuthenticationProvider(); newProfileByAuthenticationProvider.setAuthenticationProvider(pendingAccount.getAuthenticationProvider()); - newProfileByAuthenticationProvider.setAuthenticationProviderType(pendingAccount.getAuthenticationProvider()); + newProfileByAuthenticationProvider.setAuthenticationProviderType(pendingAccount.getAuthenticationProviderType()); newProfileByAuthenticationProvider.setProfileId(pendingAccount.getProfileId()); + batchOps.insert(newProfileByAuthenticationProvider); } } diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index c06283e..7e2847b 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -4,6 +4,7 @@ package biz.nynja.account.grpc.services; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.UUID; @@ -17,15 +18,23 @@ import biz.nynja.account.grpc.AccountServiceGrpc; import biz.nynja.account.grpc.AccountsByAuthenticationProviderRequest; import biz.nynja.account.grpc.AccountsByProfileIdRequest; import biz.nynja.account.grpc.AccountsResponse; +import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.CompletePendingAccountCreationResponse; import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.CreateAccountResponse; +import biz.nynja.account.grpc.CreatePendingAccountRequest; +import biz.nynja.account.grpc.CreatePendingAccountResponse; import biz.nynja.account.grpc.ErrorResponse; import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.GetAccountsResponse; import biz.nynja.account.grpc.components.Validator; +import biz.nynja.account.grpc.models.Account; +import biz.nynja.account.grpc.models.PendingAccount; import biz.nynja.account.grpc.models.UserInfo; import biz.nynja.account.grpc.models.UserInfoByEmail; import biz.nynja.account.grpc.models.UserInfoByPhone; +import biz.nynja.account.grpc.repositories.AccountRepositoryAdditional; +import biz.nynja.account.grpc.repositories.PendingAccountRepository; import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; import biz.nynja.account.grpc.repositories.UserInfoByPhoneRepository; import biz.nynja.account.grpc.repositories.UserInfoRepository; @@ -47,6 +56,12 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas @Autowired private UserInfoByEmailRepository userInfoByEmailRepository; + @Autowired + private PendingAccountRepository pendingAccountRepository; + + @Autowired + private AccountRepositoryAdditional accountRepositoryAdditional; + @Autowired private UserInfoByPhoneRepository userInfoByPhoneRepository; @@ -199,4 +214,53 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas } -} + @Override + public void createPendingAccount(CreatePendingAccountRequest request, + StreamObserver responseObserver) { + + logger.info("Creating pending account..."); + logger.debug("Creating pending account: {} ...", request); + + PendingAccount pendingAccount = PendingAccount.fromProto(request); + pendingAccount.setAccountId(UUID.randomUUID()); + pendingAccount.setProfileId(UUID.randomUUID()); + pendingAccount.setCreationTimestamp(new Date().getTime()); + + PendingAccount savedPendingAccount = pendingAccountRepository.save(pendingAccount); + logger.debug("Pending account \"{}\" saved into the DB", savedPendingAccount.toString()); + CreatePendingAccountResponse response = CreatePendingAccountResponse.newBuilder() + .setPendingAccountDetails(savedPendingAccount.toProto()).build(); + logger.info("Pending account created successfully."); + logger.debug("Pending account: \"{}\" created successfully.", response); + + responseObserver.onNext(response); + responseObserver.onCompleted(); + + return; + } + + @Override + public void completePendingAccountCreation(CompletePendingAccountCreationRequest request, + StreamObserver responseObserver) { + logger.info("Complete pending account creation..."); + logger.debug("Complete pending account creation...: {} ...", request); + + Account createdAccount = accountRepositoryAdditional.completePendingAccountCreation(request); + + if (createdAccount == null) { + responseObserver.onNext(CompletePendingAccountCreationResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.ERROR_CREATING_ACCOUNT)).build()); + responseObserver.onCompleted(); + return; + } else { + logger.debug("Account \"{}\" saved into the DB", createdAccount.toString()); + CompletePendingAccountCreationResponse response = CompletePendingAccountCreationResponse.newBuilder() + .setAccountDetails(createdAccount.toCompletePendingAccountProto()).build(); + logger.debug("Account: \"{}\" created successfully.", response); + + responseObserver.onNext(response); + responseObserver.onCompleted(); + return; + } + } +} \ No newline at end of file -- GitLab From 0ab7dfa1deb9f014789c971eeff5939c04fc17bf Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Wed, 29 Aug 2018 10:07:04 +0300 Subject: [PATCH 36/37] NY-2697: Rename materialized views according to default nameing convention - remove unnecessary startup scripts. Signed-off-by: Ralitsa Todorova --- .../account/grpc/StartupScriptsListener.java | 28 +++---------------- 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java b/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java index 473bba9..aa64421 100644 --- a/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java +++ b/src/main/java/biz/nynja/account/grpc/StartupScriptsListener.java @@ -37,38 +37,18 @@ public class StartupScriptsListener { private List getStartupScripts() { String scriptAccountViewByProfileId = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".account_by_profile_id AS SELECT * FROM account " + "WHERE profileid IS NOT NULL " + + ".accountbyprofileid AS SELECT * FROM account " + "WHERE profileid IS NOT NULL " + "PRIMARY KEY (profileid, accountid);"; String scriptAccountViewByAuthProvider = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".account_by_authentication_provider AS SELECT * FROM account " + + ".accountbyauthenticationprovider AS SELECT * FROM account " + "WHERE authenticationprovider IS NOT NULL " + "PRIMARY KEY (authenticationprovider, accountid);"; String scriptAccountViewByАccountName = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".account_by_account_name AS SELECT * FROM account " + "WHERE accountname IS NOT NULL " + + ".accountbyaccountname AS SELECT * FROM account " + "WHERE accountname IS NOT NULL " + "PRIMARY KEY (accountname, accountid);"; - String scriptViewEmail = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".userinfo_by_email AS SELECT * FROM " + keyspace + ".userinfo " - + "WHERE emailaddress IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " - + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(emailaddress, authnetication_provider_id, profile_id, account_id, username, authentication_type); "; - - String scriptViewPhone = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".userinfo_by_phone AS SELECT * FROM " + keyspace + ".userinfo " - + "WHERE phonenumber IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL and username IS NOT NULL " - + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(phonenumber, authnetication_provider_id, profile_id, account_id, username, authentication_type); "; - - String scriptViewUsername = "CREATE MATERIALIZED VIEW IF NOT EXISTS " + keyspace - + ".userinfo_by_username AS SELECT * FROM " + keyspace + ".userinfo " - + "WHERE username IS NOT NULL and authnetication_provider_id IS NOT NULL " - + "and profile_id IS NOT NULL and account_id IS NOT NULL " + "and authentication_type IS NOT NULL " - + "PRIMARY KEY(username, authnetication_provider_id, profile_id, account_id, authentication_type); "; - return Arrays.asList(scriptAccountViewByProfileId, scriptAccountViewByAuthProvider, - scriptAccountViewByАccountName, scriptViewEmail, scriptViewPhone, scriptViewUsername); + scriptAccountViewByАccountName); } } \ No newline at end of file -- GitLab From d7cb108380bdf4109c34094793c70857ee9bc4e2 Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Wed, 29 Aug 2018 10:21:01 +0300 Subject: [PATCH 37/37] NY-3044: Update account retrieval - Update getting accounts by profile id Signed-off-by: Ralitsa Todorova --- .../nynja/account/grpc/models/Account.java | 25 +- .../grpc/models/AccountByProfileId.java | 295 ++++++++++++++++++ .../grpc/models/AuthenticationProvider.java | 24 +- .../AccountByProfileIdRepository.java | 19 ++ .../grpc/services/AccountServiceImpl.java | 38 ++- 5 files changed, 381 insertions(+), 20 deletions(-) create mode 100644 src/main/java/biz/nynja/account/grpc/models/AccountByProfileId.java create mode 100644 src/main/java/biz/nynja/account/grpc/repositories/AccountByProfileIdRepository.java diff --git a/src/main/java/biz/nynja/account/grpc/models/Account.java b/src/main/java/biz/nynja/account/grpc/models/Account.java index a653f96..415fc55 100644 --- a/src/main/java/biz/nynja/account/grpc/models/Account.java +++ b/src/main/java/biz/nynja/account/grpc/models/Account.java @@ -4,16 +4,14 @@ package biz.nynja.account.grpc.models; import java.nio.ByteBuffer; -import java.util.List; import java.util.Set; import java.util.UUID; -import javax.validation.constraints.Size; - import org.springframework.data.cassandra.core.mapping.PrimaryKey; import org.springframework.data.cassandra.core.mapping.Table; -import biz.nynja.account.grpc.Status; +import biz.nynja.account.grpc.AccountDetails; +import biz.nynja.account.grpc.AccountDetails.Builder; import biz.nynja.account.grpc.CreatePendingAccountRequest; @Table @@ -285,6 +283,23 @@ public class Account { return account; } - } + public AccountDetails toProto() { + + Builder builder = AccountDetails.newBuilder().setAccountId(getAccountId().toString()) + .setProfileId(getProfileId().toString()).setAccountMark(getAccountMark()) + .setAccountName(getAccountName()).setFirstName(getFirstName()).setLastName(getLastName()) + .setUsername(getUsername()).setAccountStatus(getAccountStatus()).setQrCode(getQrCode()); + if (avatar != null) { + builder.setAvatar(com.google.protobuf.ByteString.copyFrom(avatar)); + } + if (communicationProviders != null) { + for (AuthenticationProvider ap : communicationProviders) { + builder.addCommunicationProviders(ap.toProto()); + } + } + + return builder.build(); + + } } diff --git a/src/main/java/biz/nynja/account/grpc/models/AccountByProfileId.java b/src/main/java/biz/nynja/account/grpc/models/AccountByProfileId.java new file mode 100644 index 0000000..dc8abfd --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/models/AccountByProfileId.java @@ -0,0 +1,295 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.models; + +import java.nio.ByteBuffer; +import java.util.Set; +import java.util.UUID; + +import biz.nynja.account.grpc.AccountDetails.Builder; + +public class AccountByProfileId { + + private UUID profileId; + private UUID accountId; + private String accountMark; + private String authenticationProvider; + private String authenticationProviderType; + private String firstName; + private String lastName; + private ByteBuffer avatar; + private String accountName; + private String username; + private String accountStatus; + private Long creationTimestamp; + private Long lastUpdateTimestamp; + private Set communicationProviders; + private String qrCode; + + public UUID getAccountId() { + return accountId; + } + + public void setAccountId(UUID accountId) { + this.accountId = accountId; + } + + public UUID getProfileId() { + return profileId; + } + + public void setProfileId(UUID profileId) { + this.profileId = profileId; + } + + public String getAccountMark() { + return accountMark; + } + + public void setAccountMark(String accountMark) { + this.accountMark = accountMark; + } + + public String getAuthenticationProvider() { + return authenticationProvider; + } + + public void setAuthenticationProvider(String authenticationProvider) { + this.authenticationProvider = authenticationProvider; + } + + public String getAuthenticationProviderType() { + return authenticationProviderType; + } + + public void setAuthenticationProviderType(String authenticationProviderType) { + this.authenticationProviderType = authenticationProviderType; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public ByteBuffer getAvatar() { + return avatar; + } + + public void setAvatar(ByteBuffer avatar) { + this.avatar = avatar; + } + + public String getAccountName() { + return accountName; + } + + public void setAccountName(String accountName) { + this.accountName = accountName; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getAccountStatus() { + return accountStatus; + } + + public void setAccountStatus(String accountStatus) { + this.accountStatus = accountStatus; + } + + public Long getCreationTimestamp() { + return creationTimestamp; + } + + public void setCreationTimestamp(Long creationTimestamp) { + this.creationTimestamp = creationTimestamp; + } + + public Long getLastUpdateTimestamp() { + return lastUpdateTimestamp; + } + + public void setLastUpdateTimestamp(Long lastUpdateTimestamp) { + this.lastUpdateTimestamp = lastUpdateTimestamp; + } + + public Set getCommunicationProviders() { + return communicationProviders; + } + + public void setCommunicationProviders(Set communicationProviders) { + this.communicationProviders = communicationProviders; + } + + public String getQrCode() { + return qrCode; + } + + public void setQrCode(String qrCode) { + this.qrCode = qrCode; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((accountId == null) ? 0 : accountId.hashCode()); + result = prime * result + ((accountMark == null) ? 0 : accountMark.hashCode()); + result = prime * result + ((accountName == null) ? 0 : accountName.hashCode()); + result = prime * result + ((accountStatus == null) ? 0 : accountStatus.hashCode()); + result = prime * result + ((authenticationProvider == null) ? 0 : authenticationProvider.hashCode()); + result = prime * result + ((authenticationProviderType == null) ? 0 : authenticationProviderType.hashCode()); + result = prime * result + ((avatar == null) ? 0 : avatar.hashCode()); + result = prime * result + ((communicationProviders == null) ? 0 : communicationProviders.hashCode()); + result = prime * result + ((creationTimestamp == null) ? 0 : creationTimestamp.hashCode()); + result = prime * result + ((firstName == null) ? 0 : firstName.hashCode()); + result = prime * result + ((lastName == null) ? 0 : lastName.hashCode()); + result = prime * result + ((lastUpdateTimestamp == null) ? 0 : lastUpdateTimestamp.hashCode()); + result = prime * result + ((profileId == null) ? 0 : profileId.hashCode()); + result = prime * result + ((qrCode == null) ? 0 : qrCode.hashCode()); + result = prime * result + ((username == null) ? 0 : username.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + AccountByProfileId other = (AccountByProfileId) obj; + if (accountId == null) { + if (other.accountId != null) + return false; + } else if (!accountId.equals(other.accountId)) + return false; + if (accountMark == null) { + if (other.accountMark != null) + return false; + } else if (!accountMark.equals(other.accountMark)) + return false; + if (accountName == null) { + if (other.accountName != null) + return false; + } else if (!accountName.equals(other.accountName)) + return false; + if (accountStatus == null) { + if (other.accountStatus != null) + return false; + } else if (!accountStatus.equals(other.accountStatus)) + return false; + if (authenticationProvider == null) { + if (other.authenticationProvider != null) + return false; + } else if (!authenticationProvider.equals(other.authenticationProvider)) + return false; + if (authenticationProviderType == null) { + if (other.authenticationProviderType != null) + return false; + } else if (!authenticationProviderType.equals(other.authenticationProviderType)) + return false; + if (avatar == null) { + if (other.avatar != null) + return false; + } else if (!avatar.equals(other.avatar)) + return false; + if (communicationProviders == null) { + if (other.communicationProviders != null) + return false; + } else if (!communicationProviders.equals(other.communicationProviders)) + return false; + if (creationTimestamp == null) { + if (other.creationTimestamp != null) + return false; + } else if (!creationTimestamp.equals(other.creationTimestamp)) + return false; + if (firstName == null) { + if (other.firstName != null) + return false; + } else if (!firstName.equals(other.firstName)) + return false; + if (lastName == null) { + if (other.lastName != null) + return false; + } else if (!lastName.equals(other.lastName)) + return false; + if (lastUpdateTimestamp == null) { + if (other.lastUpdateTimestamp != null) + return false; + } else if (!lastUpdateTimestamp.equals(other.lastUpdateTimestamp)) + return false; + if (profileId == null) { + if (other.profileId != null) + return false; + } else if (!profileId.equals(other.profileId)) + return false; + if (qrCode == null) { + if (other.qrCode != null) + return false; + } else if (!qrCode.equals(other.qrCode)) + return false; + if (username == null) { + if (other.username != null) + return false; + } else if (!username.equals(other.username)) + return false; + return true; + } + + @Override + public String toString() { + return new StringBuilder("Account [accountId=").append(accountId).append(", profileId=").append(profileId) + .append(", accountMark=").append(accountMark).append(", authenticationProvider=") + .append(authenticationProvider).append(", authenticationProviderType=") + .append(authenticationProviderType).append(", firstName=").append(firstName).append(", lastName=") + .append(lastName).append(", avatar=").append(avatar).append(", accountName=").append(accountName) + .append(", username=").append(username).append(", accountStatus=").append(accountStatus) + .append(", creationTimestamp=").append(creationTimestamp).append(", lastUpdateTimestamp=") + .append(lastUpdateTimestamp).append(", communicationProviders=").append(communicationProviders) + .append("]").toString(); + } + + public biz.nynja.account.grpc.AccountDetails toProto() { + + Builder builder = biz.nynja.account.grpc.AccountDetails.newBuilder().setAccountId(getAccountId().toString()) + .setProfileId(getProfileId().toString()).setAccountMark(getAccountMark()) + .setAccountName(getAccountName()).setFirstName(getFirstName()).setLastName(getLastName()) + .setUsername(getUsername()).setAccountStatus(getAccountStatus()); + + if (getQrCode() != null) { + builder.setQrCode(getQrCode()); + } + if (avatar != null) { + builder.setAvatar(com.google.protobuf.ByteString.copyFrom(avatar)); + } + if (communicationProviders != null) { + for (AuthenticationProvider ap : communicationProviders) { + builder.addCommunicationProviders(ap.toProto()); + } + } + + return builder.build(); + + } + +} diff --git a/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java b/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java index 8c67222..a4a1346 100644 --- a/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java +++ b/src/main/java/biz/nynja/account/grpc/models/AuthenticationProvider.java @@ -6,7 +6,7 @@ package biz.nynja.account.grpc.models; import org.springframework.data.cassandra.core.mapping.UserDefinedType; import biz.nynja.account.grpc.AuthProviderDetails; -import biz.nynja.account.grpc.CreateAccountRequest; +import biz.nynja.account.grpc.AuthenticationType; @UserDefinedType public class AuthenticationProvider { @@ -67,7 +67,8 @@ public class AuthenticationProvider { .append("]").toString(); } - public static AuthenticationProvider createAuthenticationProviderFromProto(AuthProviderDetails authProviderDetails) { + public static AuthenticationProvider createAuthenticationProviderFromProto( + AuthProviderDetails authProviderDetails) { AuthenticationProvider authenticationProvider = new AuthenticationProvider(); authenticationProvider.setType(authProviderDetails.getAuthenticationType().toString()); authenticationProvider.setValue(authProviderDetails.getAuthenticationProvider()); @@ -81,4 +82,23 @@ public class AuthenticationProvider { return authenticationProvider; } + public AuthProviderDetails toProto() { + AuthenticationType protoType = null; + switch (getType()) { + case "PHONE": + protoType = AuthenticationType.PHONE; + break; + case "EMAIL": + protoType = AuthenticationType.EMAIL; + break; + case "FACEBOOK": + protoType = AuthenticationType.FACEBOOK; + break; + case "GOOGLEPLUS": + protoType = AuthenticationType.GOOGLEPLUS; + break; + } + return AuthProviderDetails.newBuilder().setAuthenticationProvider(getValue()).setAuthenticationType(protoType) + .build(); + } } diff --git a/src/main/java/biz/nynja/account/grpc/repositories/AccountByProfileIdRepository.java b/src/main/java/biz/nynja/account/grpc/repositories/AccountByProfileIdRepository.java new file mode 100644 index 0000000..638ba37 --- /dev/null +++ b/src/main/java/biz/nynja/account/grpc/repositories/AccountByProfileIdRepository.java @@ -0,0 +1,19 @@ +/** + * Copyright (C) 2018 Nynja Inc. All rights reserved. + */ +package biz.nynja.account.grpc.repositories; + +import java.util.List; +import java.util.UUID; + +import org.springframework.data.cassandra.repository.CassandraRepository; +import org.springframework.stereotype.Repository; + +import biz.nynja.account.grpc.models.AccountByProfileId; + +@Repository +public interface AccountByProfileIdRepository extends CassandraRepository { + + List findAllByProfileId(UUID profileId); + +} diff --git a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java index 7e2847b..4658800 100644 --- a/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/grpc/services/AccountServiceImpl.java @@ -35,6 +35,11 @@ import biz.nynja.account.grpc.models.UserInfoByEmail; import biz.nynja.account.grpc.models.UserInfoByPhone; import biz.nynja.account.grpc.repositories.AccountRepositoryAdditional; import biz.nynja.account.grpc.repositories.PendingAccountRepository; +import biz.nynja.account.grpc.models.AccountByProfileId; +import biz.nynja.account.grpc.models.AuthenticationProvider; +import biz.nynja.account.grpc.models.UserInfoByPhone; +import biz.nynja.account.grpc.repositories.AccountByProfileIdRepository; +import biz.nynja.account.grpc.repositories.AccountRepository; import biz.nynja.account.grpc.repositories.UserInfoByEmailRepository; import biz.nynja.account.grpc.repositories.UserInfoByPhoneRepository; import biz.nynja.account.grpc.repositories.UserInfoRepository; @@ -53,6 +58,12 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas @Autowired private UserInfoRepository userInfoRepository; + @Autowired + private AccountRepository accountRepository; + + @Autowired + private AccountByProfileIdRepository accountByProfileIdRepository; + @Autowired private UserInfoByEmailRepository userInfoByEmailRepository; @@ -178,18 +189,21 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas } @Override - public void getAllAccountsByProfileId(AccountsByProfileIdRequest request, StreamObserver responseObserver) { + public void getAllAccountsByProfileId(AccountsByProfileIdRequest request, + StreamObserver responseObserver) { + logger.info("Getting accounts by profile ID..."); - logger.info("Getting accounts by profile ID..."); + if ((request.getProfileId() == null) || (request.getProfileId().isEmpty())) { + responseObserver.onNext(GetAccountsResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_PROFILE_ID)).build()); + responseObserver.onCompleted(); + return; + } - if ((request.getProfileId() == null) || (request.getProfileId().isEmpty())) { - responseObserver.onNext(GetAccountsResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_PROFILE_ID)).build()); - responseObserver.onCompleted(); - return; - } + List listAccountsByProfileId = accountByProfileIdRepository + .findAllByProfileId(UUID.fromString(request.getProfileId())); - List listAccountsByProfileId = userInfoRepository.findAllByProfileId(UUID.fromString(request.getProfileId())); - List responseList = new ArrayList (); + List responseList = new ArrayList(); if (listAccountsByProfileId.size() == 0) { responseObserver.onNext(GetAccountsResponse.newBuilder() @@ -198,8 +212,8 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas return; } - for (UserInfo userInfo : listAccountsByProfileId) { - responseList.add(userInfo.toProto()); + for (AccountByProfileId account : listAccountsByProfileId) { + responseList.add(account.toProto()); } GetAccountsResponse response = GetAccountsResponse.newBuilder() @@ -209,9 +223,7 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas responseObserver.onNext(response); responseObserver.onCompleted(); - return; - } @Override -- GitLab