From 7693604dcee5c4d007f7c309bddd8ada7b81f134 Mon Sep 17 00:00:00 2001 From: sergeyPensov Date: Tue, 13 Nov 2018 12:24:34 +0200 Subject: [PATCH] Implement ttl --- .../AccountRepositoryAdditional.java | 3 +++ .../AccountRepositoryAdditionalImpl.java | 18 ++++++++++-------- .../repositories/PendingAccountRepository.java | 4 +++- .../account/services/AccountServiceImpl.java | 4 ++-- .../account/services/AccountServiceTests.java | 8 ++++---- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java index 3503b42..80a8f85 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java @@ -15,6 +15,7 @@ import biz.nynja.account.models.AuthenticationProvider; import biz.nynja.account.models.ContactInfo; import biz.nynja.account.models.PendingAccountByAuthenticationProvider; import biz.nynja.account.models.Profile; +import biz.nynja.account.models.PendingAccount; @Repository public interface AccountRepositoryAdditional { @@ -44,4 +45,6 @@ public interface AccountRepositoryAdditional { boolean deleteContactInfo(UUID accountId, ContactInfo contactInfo); boolean editContactInfo(UUID accountId, ContactInfo oldContactInfo, ContactInfo editedContactInfo); + + PendingAccount savePendingAccount(PendingAccount updatedPendingAccount); } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 3254ff1..2d5738f 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -97,17 +97,11 @@ 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()); + logger.info("Existing pending account with the provided id was not found or creation timeout expired.."); + logger.debug("Existing pending account with the provided id {} was not found or creation timeout expired..", request.getAccountId()); return null; } Long timeCreated = Instant.now().toEpochMilli(); - Long checkMinutes = timeCreated - pendingAccount.getCreationTimestamp(); - if (checkMinutes > COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES * 60 * 1000) { - logger.info("Account creation timeout expired."); - pendingAccountRepository.deleteById(UUID.fromString(request.getAccountId())); - return null; - } WriteResult wr = null; try { newAccountInsert(batchOperations, request, pendingAccount, timeCreated); @@ -654,4 +648,12 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } return false; } + + public PendingAccount savePendingAccount(PendingAccount pendingAccount) { + String cql = "INSERT INTO pendingAccount (accountId, profileId, authenticationProvider, authenticationProviderType, creationTimestamp)" + + " VALUES (" + pendingAccount.getAccountId() + ", " + pendingAccount.getProfileId() + ", '" + pendingAccount.getAuthenticationProvider() + "', '" + pendingAccount.getAuthenticationProviderType() + "'," + pendingAccount.getCreationTimestamp() + ") " + + "USING TTL "+COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES *60 +";"; + cassandraTemplate.getCqlOperations().execute(cql); + return pendingAccount; + } } diff --git a/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java b/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java index 4d3c6f1..052aaf5 100644 --- a/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java +++ b/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java @@ -8,7 +8,6 @@ import java.util.UUID; import org.springframework.data.cassandra.repository.CassandraRepository; import org.springframework.stereotype.Repository; -import biz.nynja.account.models.Account; import biz.nynja.account.models.PendingAccount; @Repository @@ -16,4 +15,7 @@ public interface PendingAccountRepository extends CassandraRepository S save(S s); } diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index 77058b7..0618e73 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -382,7 +382,7 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas .setAuthenticationProviderType(foundExistingPendingAccount.getAuthenticationProviderType()); updatedPendingAccount.setCreationTimestamp(Instant.now().toEpochMilli()); - PendingAccount updatePendingAccount = pendingAccountRepository.save(updatedPendingAccount); + PendingAccount updatePendingAccount = accountRepositoryAdditional.savePendingAccount(updatedPendingAccount); CreatePendingAccountResponse response = CreatePendingAccountResponse.newBuilder() .setPendingAccountDetails(updatePendingAccount.toProto()).build(); logger.info("Pending account created."); @@ -405,7 +405,7 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas pendingAccount.setProfileId(UUID.randomUUID()); pendingAccount.setCreationTimestamp(Instant.now().toEpochMilli()); - PendingAccount savedPendingAccount = pendingAccountRepository.save(pendingAccount); + PendingAccount savedPendingAccount = accountRepositoryAdditional.savePendingAccount(pendingAccount); logger.debug("Pending account \"{}\" saved into the DB", savedPendingAccount.toString()); CreatePendingAccountResponse response = CreatePendingAccountResponse.newBuilder() .setPendingAccountDetails(savedPendingAccount.toProto()).build(); diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 7da8739..2528aec 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -461,7 +461,7 @@ public class AccountServiceTests extends GrpcServerTestBase { AuthenticationProvider.createAuthenticationProviderFromStrings(request.getAuthenticationType().name(), request.getAuthenticationProvider()))) .willReturn(existingPendingAccountByAuthenticationProvider); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(existingPendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(existingPendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -500,7 +500,7 @@ public class AccountServiceTests extends GrpcServerTestBase { public void testCreatePendingAccountOK() { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.EMAIL).setAuthenticationProvider(Util.EMAIL).build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -514,7 +514,7 @@ public class AccountServiceTests extends GrpcServerTestBase { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.EMAIL) .setAuthenticationProvider("invalid.E-mail1.@domain_test.com1").build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -527,7 +527,7 @@ public class AccountServiceTests extends GrpcServerTestBase { public void testCreatePendingAccountInvalidPhone() { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.PHONE).setAuthenticationProvider("BG:084365:5555").build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); -- GitLab