Skip to content

Commit

Permalink
TRUNK-6174: ModuleService should not block context refresh when calle…
Browse files Browse the repository at this point in the history
…d incorrectly via Spring (#4368)
  • Loading branch information
k4pran authored and ibacher committed Aug 18, 2023
1 parent 44081a4 commit 20ec033
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 2 deletions.
6 changes: 4 additions & 2 deletions api/src/main/java/org/openmrs/api/context/ServiceContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,9 @@ public void setModuleService(List<Object> params) {
Object classInstance = params.get(1);

if (classString == null || classInstance == null) {
throw new APIException("service.unable.find", (Object[]) null);
throw new APIException(
String.format("Unable to find service as unexpected null value found for class [%s] or instance [%s]",
classString, classInstance));
}

Class cls = null;
Expand Down Expand Up @@ -775,7 +777,7 @@ public void setModuleService(List<Object> params) {
}
}
catch (ClassNotFoundException e) {
throw new APIException("service.unable.set", new Object[] { classString }, e);
throw new APIException("Unable to find service as class not found: " + classString, e);
}

// add this module service to the normal list of services
Expand Down
99 changes: 99 additions & 0 deletions api/src/test/java/org/openmrs/api/context/ServiceContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.api.context;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.openMocks;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openmrs.api.APIException;
import org.openmrs.test.jupiter.BaseContextSensitiveTest;
import org.openmrs.util.DatabaseUpdateException;
import org.openmrs.util.InputRequiredException;

public class ServiceContextTest extends BaseContextSensitiveTest {

private ServiceContext serviceContext;

private ServiceContext spiedServiceContext;

private boolean isUseSystemClassLoader;

@BeforeEach
public void setUp() throws InputRequiredException, DatabaseUpdateException {
openMocks(this);
serviceContext = Context.getServiceContext();
spiedServiceContext = spy(serviceContext);
isUseSystemClassLoader = serviceContext.isUseSystemClassLoader();
}

@AfterEach
public void tearDown() {
serviceContext.setUseSystemClassLoader(isUseSystemClassLoader);
}

@Test
public void getModuleOpenmrsServices_shouldRaiseApiExceptionWithNonExistentClass() {
List<Object> params = new ArrayList<>();
params.add("org.openmrs.module.webservices.rest.nonexistent.InvalidClass");
params.add(Object.class);

spiedServiceContext.setUseSystemClassLoader(false);
APIException thrownException = assertThrows(APIException.class, () -> spiedServiceContext.setModuleService(params));

assertNotNull(thrownException.getMessage());
assertNotNull(thrownException.getCause());

verify(spiedServiceContext, never()).getMessageService();
verify(spiedServiceContext, never()).getMessageSourceService();
}

@Test
public void getModuleOpenmrsServices_shouldRaiseApiExceptionWithNullClass() {
List<Object> params = new ArrayList<>();
params.add(null);
params.add(Object.class);

spiedServiceContext.setUseSystemClassLoader(false);
APIException thrownException = assertThrows(APIException.class, () -> spiedServiceContext.setModuleService(params));

assertNotNull(thrownException.getMessage());
assertNull(thrownException.getCause());

verify(spiedServiceContext, never()).getMessageService();
verify(spiedServiceContext, never()).getMessageSourceService();
}

@Test
public void getModuleOpenmrsServices_shouldRaiseApiExceptionWithNullClassInstance() {
List<Object> params = new ArrayList<>();
params.add("org.openmrs.module.webservices.rest.nonexistent.InvalidClass");
params.add(null);

spiedServiceContext.setUseSystemClassLoader(false);
APIException thrownException = assertThrows(APIException.class, () -> spiedServiceContext.setModuleService(params));

assertNotNull(thrownException.getMessage());
assertNull(thrownException.getCause());

verify(spiedServiceContext, never()).getMessageService();
verify(spiedServiceContext, never()).getMessageSourceService();
}
}

0 comments on commit 20ec033

Please sign in to comment.