From 7390db167c8e31d04316c0ed9e3bdc3db1ea1efb Mon Sep 17 00:00:00 2001 From: Ralitsa Todorova Date: Mon, 8 Oct 2018 17:38:20 +0300 Subject: [PATCH] NY-4082: Remove logic for updating auth providers in update profile - Remove logic for updating auth providers in update profile - Renamed ProfileResponse to ProfileDetails - Renamed UpdateProfileResponse to ProfileResponse Signed-off-by: Ralitsa Todorova --- .../nynja/account/components/Validator.java | 15 +---- .../biz/nynja/account/models/Profile.java | 9 ++- .../AccountRepositoryAdditionalImpl.java | 60 ------------------- .../account/services/AccountServiceImpl.java | 22 +++---- .../account/services/AccountServiceTests.java | 30 ++-------- 5 files changed, 19 insertions(+), 117 deletions(-) diff --git a/src/main/java/biz/nynja/account/components/Validator.java b/src/main/java/biz/nynja/account/components/Validator.java index ff8899b..a566b02 100644 --- a/src/main/java/biz/nynja/account/components/Validator.java +++ b/src/main/java/biz/nynja/account/components/Validator.java @@ -8,7 +8,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; -import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -16,7 +15,6 @@ 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; @@ -33,7 +31,6 @@ import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.grpc.UpdateProfileRequest; import biz.nynja.account.models.CountryInfo; -import biz.nynja.account.repositories.AccountRepositoryAdditional; /** * Component which contains all validation methods. @@ -53,9 +50,6 @@ public class Validator { private HashMap countryInfoMap; - @Autowired - private AccountRepositoryAdditional accountRepositoryAdditional; - @PostConstruct public void loadPhonesBook() { @@ -316,11 +310,8 @@ public class Validator { } public Cause validateUpdateProfileRequest(UpdateProfileRequest request) { - for (AuthProviderDetails details : request.getAuthProvidersList()) { - Cause cause = validateAuthProvider(details.getAuthenticationType(), details.getAuthenticationProvider()); - if (cause != null) { - return cause; - } + if (!isValidUuid(request.getProfileId())) { + return Cause.INVALID_PROFILE_ID; } return null; } @@ -329,7 +320,7 @@ public class Validator { if (!isValidUuid(request.getProfileId())) { return Cause.INVALID_PROFILE_ID; } - return validateAuthProvider(request.getAuthenticationProvider().getAuthenticationType(), + return validateAuthProvider(request.getAuthenticationProvider().getAuthenticationType(), request.getAuthenticationProvider().getAuthenticationProvider()); } diff --git a/src/main/java/biz/nynja/account/models/Profile.java b/src/main/java/biz/nynja/account/models/Profile.java index 63e09dc..03ad158 100644 --- a/src/main/java/biz/nynja/account/models/Profile.java +++ b/src/main/java/biz/nynja/account/models/Profile.java @@ -8,9 +8,8 @@ import java.util.UUID; import org.springframework.data.cassandra.core.mapping.PrimaryKey; import org.springframework.data.cassandra.core.mapping.Table; - -import biz.nynja.account.grpc.ProfileResponse; -import biz.nynja.account.grpc.ProfileResponse.Builder; +import biz.nynja.account.grpc.ProfileDetails; +import biz.nynja.account.grpc.ProfileDetails.Builder; @Table public class Profile { @@ -151,8 +150,8 @@ public class Profile { .append(backupAuthenticationProvider).append("]").toString(); } - public ProfileResponse toProto() { - Builder builder = ProfileResponse.newBuilder(); + public ProfileDetails toProto() { + Builder builder = ProfileDetails.newBuilder(); if (getProfileId() != null) { builder.setProfileId(getProfileId().toString()); } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index b886809..9a7e97c 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -25,7 +25,6 @@ import com.datastax.driver.core.SimpleStatement; import com.datastax.driver.core.Statement; import biz.nynja.account.components.AccountServiceHelper; -import biz.nynja.account.grpc.AccountDetails; import biz.nynja.account.grpc.AuthProviderDetails; import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; import biz.nynja.account.grpc.UpdateAccountRequest; @@ -171,7 +170,6 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } public Profile updateProfile(UpdateProfileRequest request) { - CassandraBatchOperations batchOperations = cassandraTemplate.batchOps(); Profile existingProfile = profileRepository.findByProfileId(UUID.fromString(request.getProfileId())); if (existingProfile == null) { @@ -180,44 +178,9 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return null; } Long timeUpdated = new Date().getTime(); - Set existingAuthenticationProvidersSet = new HashSet(); - if (existingProfile.getAuthenticationProviders() != null) { - existingAuthenticationProvidersSet = new HashSet( - existingProfile.getAuthenticationProviders()); - } - Set requestedAuthenticationProvidersSet = new HashSet(); - Set sameAuthenticationProvidersSet = new HashSet(); - Set oldAuthenticationProvidersSet = new HashSet(); - Set newAuthenticationProvidersSet = new HashSet(); - - if (request.getAuthProvidersList() != null) { - for (AuthProviderDetails authProviderDetails : request.getAuthProvidersList()) { - requestedAuthenticationProvidersSet - .add(AuthenticationProvider.createAuthenticationProviderFromProto(authProviderDetails)); - } - } - - try { - sameAuthenticationProvidersSet = new HashSet(requestedAuthenticationProvidersSet); - oldAuthenticationProvidersSet = new HashSet(existingAuthenticationProvidersSet); - newAuthenticationProvidersSet = new HashSet(requestedAuthenticationProvidersSet); - - sameAuthenticationProvidersSet.retainAll(existingAuthenticationProvidersSet); - oldAuthenticationProvidersSet.removeAll(sameAuthenticationProvidersSet); - newAuthenticationProvidersSet.removeAll(sameAuthenticationProvidersSet); - } catch (Exception e) { - logger.info("Exception while setting authentication providers."); - logger.debug("Exception while setting authentication providers: {} ...", e.getMessage()); - return null; - } - WriteResult wr = null; try { updateProfileData(batchOperations, request, existingProfile, timeUpdated); - insertNewAuthenticationProvidersToProfile(batchOperations, existingProfile.getProfileId(), - newAuthenticationProvidersSet); - deleteAuthenticationProvidersFromProfile(batchOperations, existingProfile.getProfileId(), - oldAuthenticationProvidersSet); wr = batchOperations.execute(); } catch (IllegalArgumentException | IllegalStateException e) { logger.info("Exception while updating profile."); @@ -415,13 +378,6 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio private void updateProfileData(CassandraBatchOperations batchOps, UpdateProfileRequest request, Profile existingProfile, Long lastUpdateTimestamp) { Profile updatedProfile = existingProfile; - Set authProvidersSet = new HashSet(); - if (request.getAuthProvidersList() != null) { - for (AuthProviderDetails authProviderDetails : request.getAuthProvidersList()) { - authProvidersSet.add(AuthenticationProvider.createAuthenticationProviderFromProto(authProviderDetails)); - } - updatedProfile.setAuthenticationProviders(authProvidersSet); - } if (!request.getBackupAuthProvider().getAuthenticationProvider().trim().isEmpty()) { updatedProfile.setBackupAuthenticationProvider( AuthenticationProvider.createAuthenticationProviderFromProto(request.getBackupAuthProvider())); @@ -493,22 +449,6 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } } - private void insertNewAuthenticationProvidersToProfile(CassandraBatchOperations batchOps, UUID profileId, - Set newAuthenticationProvidersSet) { - for (AuthenticationProvider authProvider : newAuthenticationProvidersSet) { - insertNewProfileByAuthenticationProvider(batchOps, profileId, authProvider); - } - } - - private void insertNewProfileByAuthenticationProvider(CassandraBatchOperations batchOps, UUID profileId, - AuthenticationProvider authProvider) { - ProfileByAuthenticationProvider newProfileByAuthenticationProvider = new ProfileByAuthenticationProvider(); - newProfileByAuthenticationProvider.setAuthenticationProvider(authProvider.getValue()); - newProfileByAuthenticationProvider.setAuthenticationProviderType(authProvider.getType()); - newProfileByAuthenticationProvider.setProfileId(profileId); - batchOps.insert(newProfileByAuthenticationProvider); - } - private void deleteAuthenticationProvidersFromProfile(CassandraBatchOperations batchOps, UUID profileId, Set authenticationProvidersSetToDelete) { for (AuthenticationProvider authProvider : authenticationProvidersSetToDelete) { diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index c1acf9d..fff5366 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -52,7 +52,7 @@ import biz.nynja.account.repositories.ProfileByAuthenticationProviderRepository; import biz.nynja.account.repositories.ProfileRepository; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.grpc.UpdateProfileRequest; -import biz.nynja.account.grpc.UpdateProfileResponse; +import biz.nynja.account.grpc.ProfileResponse; import io.grpc.stub.StreamObserver; /** @@ -332,30 +332,22 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas } @Override - public void updateProfile(UpdateProfileRequest request, StreamObserver responseObserver) { + public void updateProfile(UpdateProfileRequest request, StreamObserver responseObserver) { logger.info("Updating profile..."); logger.debug("Updating profile...: {}", request); if ((request.getProfileId() == null) || (request.getProfileId().isEmpty())) { - responseObserver.onNext(UpdateProfileResponse.newBuilder() + responseObserver.onNext(ProfileResponse.newBuilder() .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_PROFILE_ID)).build()); responseObserver.onCompleted(); return; } - if (request.getAuthProvidersList().size() < MIN_NUMBER_OF_AUTH_PROVIDERS_IN_PROFILE) { - logger.info("Error updating profile. Check the number of authentication providers."); - logger.debug("Error updating profile. Check the number of authentication providers: {}", request); - responseObserver.onNext(UpdateProfileResponse.newBuilder() - .setError(ErrorResponse.newBuilder().setCause(Cause.ERROR_UPDATING_PROFILE)).build()); - responseObserver.onCompleted(); - return; - } Cause cause = validator.validateUpdateProfileRequest(request); if (cause != null) { - responseObserver.onNext( - UpdateProfileResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(cause)).build()); + responseObserver.onNext(ProfileResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(cause)).build()); responseObserver.onCompleted(); return; } @@ -363,14 +355,14 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas Profile updatedProfile = accountRepositoryAdditional.updateProfile(request); if (updatedProfile == null) { - responseObserver.onNext(UpdateProfileResponse.newBuilder() + responseObserver.onNext(ProfileResponse.newBuilder() .setError(ErrorResponse.newBuilder().setCause(Cause.ERROR_UPDATING_PROFILE)).build()); responseObserver.onCompleted(); return; } logger.debug("Profile \"{}\" updated in the DB", updatedProfile.toString()); logger.debug("Profile: \"{}\" updated successfully.", updatedProfile); - UpdateProfileResponse response = UpdateProfileResponse.newBuilder().setProfileResponse(updatedProfile.toProto()) + ProfileResponse response = ProfileResponse.newBuilder().setProfileDetails (updatedProfile.toProto()) .build(); responseObserver.onNext(response); responseObserver.onCompleted(); diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index ab73bdc..851446e 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -9,10 +9,8 @@ import static org.junit.Assert.assertTrue; import static org.mockito.BDDMockito.given; import java.util.ArrayList; -import java.util.HashSet; import java.util.List; import java.util.Optional; -import java.util.Set; import java.util.UUID; import java.util.concurrent.ExecutionException; @@ -49,7 +47,7 @@ import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.grpc.StatusResponse; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.grpc.UpdateProfileRequest; -import biz.nynja.account.grpc.UpdateProfileResponse; +import biz.nynja.account.grpc.ProfileResponse; import biz.nynja.account.models.Account; import biz.nynja.account.models.AccountByAuthenticationProvider; import biz.nynja.account.repositories.AccountByAuthenticationProviderRepository; @@ -438,58 +436,40 @@ public class AccountServiceTests extends GrpcServerTestBase { @Test public void testUpdateProfileAddBackupAuthProvider() throws ExecutionException, InterruptedException { final UpdateProfileRequest request = UpdateProfileRequest.newBuilder().setProfileId(Util.PROFILE_ID.toString()) - .addAuthProviders(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_PROVIDER) - .setAuthenticationType(AuthenticationType.PHONE).build()) .setBackupAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_PROVIDER) .setAuthenticationType(AuthenticationType.PHONE).build()) .build(); given(accountRepositoryAdditional.updateProfile(request)).willReturn(updatedProfile); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final UpdateProfileResponse reply = accountServiceBlockingStub.updateProfile(request); + final ProfileResponse reply = accountServiceBlockingStub.updateProfile(request); assertNotNull("Reply should not be null", reply); assertTrue( String.format("Reply should contain backup auth provider type '%s'", request.getBackupAuthProvider().getAuthenticationType().name()), - reply.getProfileResponse().getBackupAuthProvider().getAuthenticationType().toString() + reply.getProfileDetails().getBackupAuthProvider().getAuthenticationType().toString() .equals(AuthenticationType.PHONE.name())); assertTrue( String.format("Reply should contain backup auth provider '%s'", request.getBackupAuthProvider().getAuthenticationProvider()), - reply.getProfileResponse().getBackupAuthProvider().getAuthenticationProvider() + reply.getProfileDetails().getBackupAuthProvider().getAuthenticationProvider() .equals(Util.PHONE_NUMBER)); } @Test public void testUpdateProfileMissingProfileId() throws ExecutionException, InterruptedException { final UpdateProfileRequest request = UpdateProfileRequest.newBuilder() - .addAuthProviders(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_NUMBER) - .setAuthenticationType(AuthenticationType.PHONE).build()) .setBackupAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_NUMBER) .setAuthenticationType(AuthenticationType.PHONE).build()) .build(); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final UpdateProfileResponse reply = accountServiceBlockingStub.updateProfile(request); + final ProfileResponse reply = accountServiceBlockingStub.updateProfile(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)); } - @Test - public void testUpdateProfileNoAuthProviders() throws ExecutionException, InterruptedException { - final UpdateProfileRequest request = UpdateProfileRequest.newBuilder().setProfileId(Util.PROFILE_ID.toString()) - .setBackupAuthProvider(AuthProviderDetails.newBuilder().setAuthenticationProvider(Util.PHONE_NUMBER) - .setAuthenticationType(AuthenticationType.PHONE).build()) - .build(); - final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc - .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); - final UpdateProfileResponse reply = accountServiceBlockingStub.updateProfile(request); - assertNotNull("Reply should not be null", reply); - assertTrue(String.format("Reply should contain cause '%s'", Cause.ERROR_UPDATING_PROFILE), - reply.getError().getCause().equals(Cause.ERROR_UPDATING_PROFILE)); - } - @Test public void testCreatePendingAccountOK() { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() -- GitLab