Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.iemr.common-API</groupId>
<artifactId>common-api</artifactId>
<version>3.8.2</version>
<version>3.12.0</version>
<packaging>war</packaging>

<name>Common-API</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.util.concurrent.CompletableFuture;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -469,15 +470,15 @@
int count = 0;
try {
if (isAdmin != null && isAdmin == true) {
updateCTIPasswordForUserV1(user.getUserID(), loginpass);
updateCTIPasswordInBackground(user.getUserID(), loginpass);
loginpass = securePassword.generateStrongPassword(loginpass);
count = iEMRUserRepositoryCustom.updateSetForgetPassword(user.getUserID(), loginpass);
} else {
String tokenFromRedis = sessionObject
.getSessionObjectForChangePassword((user.getUserID().toString() + user.getUserName()));
if (tokenFromRedis.equalsIgnoreCase(transactionId)) {

updateCTIPasswordForUserV1(user.getUserID(), loginpass);
updateCTIPasswordInBackground(user.getUserID(), loginpass);
loginpass = securePassword.generateStrongPassword(loginpass);
count = iEMRUserRepositoryCustom.updateSetForgetPassword(user.getUserID(), loginpass);
// Deleting transaction Id
Expand Down Expand Up @@ -600,6 +601,15 @@
}
}

/**
* Sends the new password to the CTI server without making the user wait: called from this class, so the
* {@code @Async} of updateCTIPasswordForUserV1 is not applied (no proxy) and an unreachable CTI server held the
* password screen until the network timeout - before the password was even saved.
*/
void updateCTIPasswordInBackground(Long userID, String password) {
CompletableFuture.runAsync(() -> updateCTIPasswordForUserV1(userID, password));

Check failure on line 610 in src/main/java/com/iemr/common/service/users/IEMRAdminUserServiceImpl.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Call async methods via an injected dependency instead of directly via 'this'.

See more on https://sonarcloud.io/project/issues?id=PSMRI_Common-API&issues=AaFco9fKOQ3_qzSlgs0i&open=AaFco9fKOQ3_qzSlgs0i&pullRequest=460
}

@Async
public void updateCTIPasswordForUserV1(Long userID, String password) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,28 @@ void testSetForgetPassword_ValidTransaction() throws Exception {
assertEquals(1, result);
}

@Test
void testSetForgetPassword_DoesNotWaitForCtiServer() throws Exception {
// Given: the CTI server does not answer until released
java.util.concurrent.CountDownLatch ctiReleased = new java.util.concurrent.CountDownLatch(1);
when(iEMRUserRepositoryCustom.findUserByUserID(1L)).thenReturn(testUser);
when(ctiService.addUpdateUserData(anyString(), anyString())).thenAnswer(invocation -> {
ctiReleased.await(10, java.util.concurrent.TimeUnit.SECONDS);
return new OutputResponse();
});
when(securePassword.generateStrongPassword("newpassword")).thenReturn("hashedNewPassword");
when(iEMRUserRepositoryCustom.updateSetForgetPassword(1L, "hashedNewPassword")).thenReturn(1);

// When: the password is saved without waiting for CTI
int result = assertTimeoutPreemptively(java.time.Duration.ofSeconds(2),
() -> userService.setForgetPassword(testUser, "newpassword", "token123", true));

// Then: saved, and CTI is still updated in the background
assertEquals(1, result);
ctiReleased.countDown();
verify(ctiService, timeout(2000)).addUpdateUserData(anyString(), eq("127.0.0.1"));
}

@Test
void testSetForgetPassword_InvalidTransaction() throws Exception {
// Given
Expand Down
Loading