Skip to content

Commit

Permalink
Merge pull request #197 from ADORSYS-GIS/183-prevent-duplicate-accounts
Browse files Browse the repository at this point in the history
183 prevent duplicate accounts
  • Loading branch information
Arielpetit authored Feb 25, 2025
2 parents 768689a + 6581cff commit 9c9ba4a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 112 deletions.
11 changes: 1 addition & 10 deletions obs/obs-service-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,7 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>3.4.2</version>
</dependency>




Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -26,7 +25,6 @@ public class ObsServiceImpl implements RegistrationServiceApi {

// Injecting RedisTemplate
@Autowired
private RedisTemplate<String, String> redisTemplate;

public ObsServiceImpl(JwtCertValidator jwtCertValidator, BankAccountTransactionService bankAccountTransactionService, BankAccountService bankAccountService, BankAccountCertificateCreationService bankAccountCertificateCreationService) {
this.jwtCertValidator = jwtCertValidator;
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -30,53 +29,36 @@ class ObsServiceImplTest {
@Mock
private BankAccountService bankAccountService;

@Mock
private RedisTemplate<String, String> 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
Expand All @@ -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<String, String> valueOps = mock(ValueOperations.class);
when(redisTemplate.opsForValue()).thenReturn(valueOps);

// Mock BankAccountCertificateCreationService's registerNewBankAccount method
String mockResult = "Header\nSubheader\nAccount ID: 12345";
when(bankAccountCertificateCreationService.registerNewBankAccount(
Expand All @@ -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<String, String> 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<BankAccountBO> captor = ArgumentCaptor.forClass(BankAccountBO.class);
// Call the method
obsService.registerAccount(registrationRequest, phoneNumberCertificateJwt);

// Capture the BankAccountBO argument
ArgumentCaptor<BankAccountBO> 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);
}
}
7 changes: 1 addition & 6 deletions online-banking-app/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9c9ba4a

Please sign in to comment.