Skip to content

Refactor MessageService to Use Spring Dependency Injection #4983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
31 changes: 15 additions & 16 deletions api/src/main/java/org/openmrs/api/context/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -565,23 +565,22 @@ public static ProgramWorkflowService getProgramWorkflowService() {
*
* @return message service
*/
public static MessageService getMessageService() {
MessageService ms = getServiceContext().getMessageService();
try {
// Message service dependencies
if (ms.getMessagePreparator() == null) {
ms.setMessagePreparator(getMessagePreparator());
}

if (ms.getMessageSender() == null) {
ms.setMessageSender(getMessageSender());
}

}
catch (Exception e) {
log.error("Unable to create message service due", e);
public static MessageService getMessageService() throws APIException {
return getServiceContext().getMessageService();
}

/**
* Convenience method to configure the MessageService via dependency injection.
*
* @param sender the MessageSender to set
* @param preparator the MessagePreparator to set
*/
public static void configureMessageService(MessageSender sender, MessagePreparator preparator) {
MessageService messageService = getServiceContext().getMessageService();
if (messageService != null) {
messageService.setMessageSender(sender);
messageService.setMessagePreparator(preparator);
}
return ms;
}

/**
Expand Down
18 changes: 11 additions & 7 deletions api/src/test/java/org/openmrs/api/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
package org.openmrs.api;

import static org.mockito.Mockito.*;
import org.mockito.Mockito;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
Expand Down Expand Up @@ -1564,15 +1567,16 @@ public void getUserByUsernameOrEmail_shouldFailIfEmailIsNull() {
assertThat(exception.getMessage(), is(messages.getMessage("error.usernameOrEmail.notNullOrBlank")));
}


@Test
public void setUserActivationKey_shouldCreateUserActivationKey() throws Exception {
User createdUser = createTestUser();
Context.getAdministrationService().setGlobalProperty(OpenmrsConstants.GP_PASSWORD_RESET_URL,
"http://localhost:8080/openmrs/admin/users/changePassword.form/{activationKey}");
assertNull(dao.getLoginCredential(createdUser).getActivationKey());
assertThrows(MessageException.class, () -> userService.setUserActivationKey(createdUser));
assertNotNull(dao.getLoginCredential(createdUser).getActivationKey());
User createdUser = createTestUser();
Context.getAdministrationService().setGlobalProperty(OpenmrsConstants.GP_PASSWORD_RESET_URL,
"http://localhost:8080/openmrs/admin/users/changePassword.form/{activationKey}");
assertNull(dao.getLoginCredential(createdUser).getActivationKey());
userService.setUserActivationKey(createdUser);
LoginCredential credentials = dao.getLoginCredential(createdUser);
assertNotNull(credentials.getActivationKey());
assertTrue(credentials.getActivationKey().matches("^[a-f0-9]+:\\d+$")); // Verify format
}

@Test
Expand Down
23 changes: 23 additions & 0 deletions api/src/test/java/org/openmrs/api/context/ContextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

import java.util.List;
import java.util.Locale;
Expand All @@ -34,6 +35,10 @@
import org.openmrs.api.UserService;
import org.openmrs.api.handler.EncounterVisitHandler;
import org.openmrs.api.handler.ExistingOrNewVisitAssignmentHandler;
import org.openmrs.notification.MessagePreparator;
import org.openmrs.notification.MessageSender;
import org.openmrs.notification.MessageService;
import org.openmrs.notification.impl.MessageServiceImpl;
import org.openmrs.test.jupiter.BaseContextSensitiveTest;
import org.openmrs.util.LocaleUtility;
import org.openmrs.util.OpenmrsConstants;
Expand Down Expand Up @@ -353,4 +358,22 @@ public void clearEntireCache_shouldClearEntireCache() {
assertFalse(sf.getCache().containsEntity(PERSON_NAME_CLASS, PERSON_NAME_ID_8));
assertFalse(sf.getCache().containsEntity(Patient.class, PERSON_NAME_ID_2));
}
@Test
public void configureMessageService_shouldSetMessageSenderAndPreparator() {
// Given
MessageService messageService = new MessageServiceImpl();
MessageSender mockSender = mock(MessageSender.class);
MessagePreparator mockPreparator = mock(MessagePreparator.class);

// Inject test instance
Context.getServiceContext().setMessageService(messageService);

// When
Context.configureMessageService(mockSender, mockPreparator);

// Then
assertEquals(mockSender, messageService.getMessageSender());
assertEquals(mockPreparator, messageService.getMessagePreparator());
}

}
Loading