diff --git a/obs/obs-service-impl/pom.xml b/obs/obs-service-impl/pom.xml index 7ac380e..0e4e8d6 100644 --- a/obs/obs-service-impl/pom.xml +++ b/obs/obs-service-impl/pom.xml @@ -98,16 +98,7 @@ junit test - - org.springframework.boot - spring-boot-starter-data-redis - - - - org.springframework.data - spring-data-redis - 3.4.2 - + diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/config/RedisConfig.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/config/RedisConfig.java deleted file mode 100644 index a95b92e..0000000 --- a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/config/RedisConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.adorsys.webank.obs.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.serializer.StringRedisSerializer; - -@Configuration -public class RedisConfig { - - @Bean - public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) { - RedisTemplate template = new RedisTemplate<>(); - template.setConnectionFactory(redisConnectionFactory); - template.setKeySerializer(new StringRedisSerializer()); - template.setValueSerializer(new StringRedisSerializer()); - return template; - } -} diff --git a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java index f65c8b3..7c0028f 100644 --- a/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java +++ b/obs/obs-service-impl/src/main/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImpl.java @@ -8,7 +8,6 @@ import de.adorsys.webank.bank.api.service.util.*; import org.slf4j.*; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.*; import java.math.*; @@ -26,7 +25,6 @@ public class ObsServiceImpl implements RegistrationServiceApi { // Injecting RedisTemplate @Autowired - private RedisTemplate redisTemplate; public ObsServiceImpl(JwtCertValidator jwtCertValidator, BankAccountTransactionService bankAccountTransactionService, BankAccountService bankAccountService, BankAccountCertificateCreationService bankAccountCertificateCreationService) { this.jwtCertValidator = jwtCertValidator; @@ -40,11 +38,6 @@ public String registerAccount(RegistrationRequest registrationRequest, String ph try { String phoneNumber = registrationRequest.getPhoneNumber(); - // Check if the phone number is already in the cache - if (redisTemplate.hasKey(phoneNumber)) { - log.warn("Phone number {} is already registered in Redis.", phoneNumber); - return "Phone number is already registered."; - } log.info("Checking JWT certificate for phone number: {}", phoneNumber); // Validate the JWT token passed from the frontend @@ -95,9 +88,6 @@ public String registerAccount(RegistrationRequest registrationRequest, String ph String deposit = makeTrans(accountId); log.info("Created account with id: {} and deposit amount: {}", accountId, deposit); - // Store the phone number in Redis to prevent re-registration - redisTemplate.opsForValue().set(phoneNumber, "registered"); - log.info("Phone number {} added to Redis as registered.", phoneNumber); return "Bank account successfully created. Details: " + createdAccountResult; } catch (Exception e) { diff --git a/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImplTest.java b/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImplTest.java index 4286aa1..9a70746 100644 --- a/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImplTest.java +++ b/obs/obs-service-impl/src/test/java/com/adorsys/webank/obs/serviceimpl/ObsServiceImplTest.java @@ -1,21 +1,20 @@ package com.adorsys.webank.obs.serviceimpl; + import com.adorsys.webank.obs.dto.*; import com.adorsys.webank.obs.security.*; + import de.adorsys.webank.bank.api.domain.*; import de.adorsys.webank.bank.api.service.*; + import de.adorsys.webank.bank.api.service.util.*; import org.junit.jupiter.api.*; import org.mockito.*; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ValueOperations; - -import java.lang.reflect.Field; - import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; + class ObsServiceImplTest { @Mock @@ -30,53 +29,36 @@ class ObsServiceImplTest { @Mock private BankAccountService bankAccountService; - @Mock - private RedisTemplate redisTemplate; - @InjectMocks private ObsServiceImpl obsService; + @BeforeEach - void setUp() throws NoSuchFieldException, IllegalAccessException { + void setUp() { MockitoAnnotations.openMocks(this); - - // Manually inject RedisTemplate mock using reflection - Field redisTemplateField = ObsServiceImpl.class.getDeclaredField("redisTemplate"); - redisTemplateField.setAccessible(true); - redisTemplateField.set(obsService, redisTemplate); } @Test void testRegisterAccount_invalidJwt() { + // Prepare test data RegistrationRequest registrationRequest = new RegistrationRequest(); registrationRequest.setPhoneNumber("1234567890"); registrationRequest.setPublicKey("publicKey123"); - String jwt = "invalidJwt"; - - when(redisTemplate.hasKey("1234567890")).thenReturn(false); - when(jwtCertValidator.validateJWT(jwt)).thenReturn(false); - - String result = obsService.registerAccount(registrationRequest, jwt); - assertEquals("Invalid certificate or JWT. Account creation failed", result); - verify(redisTemplate).hasKey("1234567890"); - verify(jwtCertValidator).validateJWT(jwt); - verifyNoInteractions(bankAccountCertificateCreationService); - } + String phoneNumberCertificateJwt = "invalidJwt"; - @Test - void testRegisterAccount_phoneAlreadyRegistered() { - RegistrationRequest request = new RegistrationRequest(); - request.setPhoneNumber("1234567890"); - String jwt = "anyJwt"; + // Mock JwtCertValidator's validateJWT method to return false + when(jwtCertValidator.validateJWT(phoneNumberCertificateJwt)).thenReturn(false); - when(redisTemplate.hasKey("1234567890")).thenReturn(true); + // Call the method to test + String result = obsService.registerAccount(registrationRequest, phoneNumberCertificateJwt); - String result = obsService.registerAccount(request, jwt); + // Verify the result + assertEquals("Invalid certificate or JWT. Account creation failed", result); - assertEquals("Phone number is already registered.", result); - verify(redisTemplate).hasKey("1234567890"); - verifyNoInteractions(jwtCertValidator, bankAccountCertificateCreationService); + // Verify the interaction with the mock + verify(jwtCertValidator, times(1)).validateJWT(phoneNumberCertificateJwt); + verify(bankAccountCertificateCreationService, times(0)).registerNewBankAccount(any(), any(), any(), anyString(), anyString()); } @Test @@ -91,10 +73,6 @@ void testRegisterAccount_success() { // Mock JwtCertValidator's validateJWT method to return true when(jwtCertValidator.validateJWT(phoneNumberCertificateJwt)).thenReturn(true); - // Mock RedisTemplate's opsForValue() to return a valid mock - ValueOperations valueOps = mock(ValueOperations.class); - when(redisTemplate.opsForValue()).thenReturn(valueOps); - // Mock BankAccountCertificateCreationService's registerNewBankAccount method String mockResult = "Header\nSubheader\nAccount ID: 12345"; when(bankAccountCertificateCreationService.registerNewBankAccount( @@ -105,65 +83,77 @@ void testRegisterAccount_success() { // Call the method to test String result = obsService.registerAccount(registrationRequest, phoneNumberCertificateJwt); - // Adjust the expected message to match the multi-line actual result - String expectedMessage = "Bank account successfully created. Details: Header\nSubheader\nAccount ID: 12345"; - // Verify the result - assertEquals(expectedMessage, result); + assertEquals("Bank account successfully created. Details: " + mockResult, result); // Verify the interactions with the mocks verify(jwtCertValidator, times(1)).validateJWT(phoneNumberCertificateJwt); verify(bankAccountCertificateCreationService, times(1)).registerNewBankAccount(any(), any(), any(), anyString(), anyString()); } - - @Test void testRegisterAccount_success_verifyBankAccountBO() { - RegistrationRequest request = new RegistrationRequest(); - request.setPhoneNumber("1234567890"); - request.setPublicKey("publicKey123"); - String jwt = "validJwt"; - ValueOperations valueOps = mock(ValueOperations.class); + // Prepare test data + RegistrationRequest registrationRequest = new RegistrationRequest(); + registrationRequest.setPhoneNumber("1234567890"); + registrationRequest.setPublicKey("publicKey123"); - when(redisTemplate.hasKey("1234567890")).thenReturn(false); - when(jwtCertValidator.validateJWT(jwt)).thenReturn(true); - when(redisTemplate.opsForValue()).thenReturn(valueOps); - when(bankAccountService.getAccountById(any())).thenReturn(new BankAccountBO()); + String phoneNumberCertificateJwt = "validJwt"; - obsService.registerAccount(request, jwt); + // Mock JwtCertValidator's validateJWT method + when(jwtCertValidator.validateJWT(phoneNumberCertificateJwt)).thenReturn(true); + + // Mock BankAccountCertificateCreationService + String mockResult = "Header\nSubheader\nAccount ID: 12345"; + when(bankAccountCertificateCreationService.registerNewBankAccount( + anyString(), anyString(), any(BankAccountBO.class), anyString(), anyString() + )).thenReturn(mockResult); - ArgumentCaptor captor = ArgumentCaptor.forClass(BankAccountBO.class); + // Call the method + obsService.registerAccount(registrationRequest, phoneNumberCertificateJwt); + + // Capture the BankAccountBO argument + ArgumentCaptor bankAccountCaptor = ArgumentCaptor.forClass(BankAccountBO.class); verify(bankAccountCertificateCreationService).registerNewBankAccount( - eq("1234567890"), eq("publicKey123"), captor.capture(), any(), any()); - - BankAccountBO account = captor.getValue(); - assertEquals("1234567890", account.getMsisdn()); - assertEquals("XAF", account.getCurrency().getCurrencyCode()); - assertEquals("Standard", account.getProduct()); - assertEquals("72070032", account.getBic()); - assertEquals("OBS", account.getBranch()); + eq("1234567890"), eq("publicKey123"), bankAccountCaptor.capture(), anyString(), anyString()); + + // Assert BankAccountBO properties + BankAccountBO capturedBankAccount = bankAccountCaptor.getValue(); + assertNotNull(capturedBankAccount); + assertEquals("1234567890", capturedBankAccount.getMsisdn()); + assertEquals("XAF", capturedBankAccount.getCurrency().getCurrencyCode()); + assertEquals("Standard", capturedBankAccount.getProduct()); + assertEquals("72070032", capturedBankAccount.getBic()); + assertEquals("OBS", capturedBankAccount.getBranch()); } @Test void testMakeTrans_success() { String accountId = "12345"; + + // Mock BankAccountService when(bankAccountService.getAccountById(accountId)).thenReturn(new BankAccountBO()); + // Call the method String result = obsService.makeTrans(accountId); + // Verify deposits were made + verify(bankAccountTransactionService, times(5)) + .depositCash(eq(accountId), any(), anyString()); + assertEquals("5 transactions completed successfully for account " + accountId, result); - verify(bankAccountTransactionService, times(5)).depositCash(eq(accountId), any(), any()); } @Test void testMakeTrans_accountNotFound() { String accountId = "nonExistent"; + + // Mock BankAccountService to return null when(bankAccountService.getAccountById(accountId)).thenReturn(null); + // Call the method String result = obsService.makeTrans(accountId); assertEquals("Bank account not found for ID: " + accountId, result); - verifyNoInteractions(bankAccountTransactionService); } } \ No newline at end of file diff --git a/online-banking-app/src/main/resources/application.properties b/online-banking-app/src/main/resources/application.properties index bfa438f..c688aed 100644 --- a/online-banking-app/src/main/resources/application.properties +++ b/online-banking-app/src/main/resources/application.properties @@ -10,9 +10,4 @@ server.port=8081 spring.jackson.serialization.write-dates-as-timestamps=false -# Redis connection properties -spring.redis.host=${SPRING_REDIS_HOST:localhost} -spring.redis.port=${SPRING_REDIS_PORT:6379} -spring.redis.password=${SPRING_REDIS_PASSWORD:your_password} -spring.redis.timeout=${SPRING_REDIS_TIMEOUT:2000} -spring.redis.database=0 +