From 46e3b96d7f8c5edfed585d22fcf2604112cc9c60 Mon Sep 17 00:00:00 2001 From: Ruhanga <41738040+Ruhanga@users.noreply.github.com> Date: Fri, 13 Dec 2024 19:34:37 +0300 Subject: [PATCH] FM2-651: Allow DAOs access to all API load-able by the OpenMRS Class loader. (#554) --- .../fhir2/web/servlet/FhirRestServlet.java | 12 +++ .../web/servlet/FhirRestServletTest.java | 85 +++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java diff --git a/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirRestServlet.java b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirRestServlet.java index ebc68316e..ff729113b 100644 --- a/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirRestServlet.java +++ b/omod/src/main/java/org/openmrs/module/fhir2/web/servlet/FhirRestServlet.java @@ -11,6 +11,11 @@ import static org.openmrs.module.fhir2.FhirConstants.FHIR2_MODULE_ID; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import java.io.IOException; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Arrays; @@ -46,6 +51,7 @@ import org.openmrs.module.fhir2.web.util.NarrativeUtils; import org.openmrs.module.fhir2.web.util.SummaryInterceptor; import org.openmrs.module.fhir2.web.util.SupportMergePatchInterceptor; +import org.openmrs.util.OpenmrsClassLoader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; import org.springframework.beans.factory.annotation.Qualifier; @@ -181,6 +187,12 @@ protected String getRequestPath(String requestFullPath, String servletContextPat .substring(escapedLength(servletContextPath) + escapedLength(servletPath) + escapedLength("/fhir2Servlet")); } + @Override + protected void service(HttpServletRequest theReq, HttpServletResponse theResp) throws ServletException, IOException { + Thread.currentThread().setContextClassLoader(OpenmrsClassLoader.getInstance()); + super.service(theReq, theResp); + } + @Override @Autowired @Qualifier("fhirR4") diff --git a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java b/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java new file mode 100644 index 000000000..5d7c01bd1 --- /dev/null +++ b/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java @@ -0,0 +1,85 @@ +/* + * 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.module.fhir2.web.servlet; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.io.PrintWriter; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openmrs.util.OpenmrsClassLoader; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class FhirRestServletTest { + + @Mock + private HttpServletRequest mockRequest; + + @Mock + private HttpServletResponse mockResponse; + + @Mock + private ServletConfig mockServletConfig; + + @Mock + private PrintWriter mockWriter; + + private TestableFhirRestServlet servlet; + + @Before + public void setUp() throws ServletException, IOException { + MockitoAnnotations.initMocks(this); + + servlet = new TestableFhirRestServlet(); + + when(mockServletConfig.getServletContext()).thenReturn(mock(javax.servlet.ServletContext.class)); + when(mockResponse.getWriter()).thenReturn(mockWriter); + + servlet.init(mockServletConfig); + } + + @Test + public void testServiceSetsContextClassLoader() throws ServletException, IOException { + // setup + when(mockRequest.getMethod()).thenReturn("GET"); + when(mockRequest.getRequestURI()).thenReturn("/fhir"); + + ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader(); + + assertNotEquals("ContextClassLoader should not be OpenmrsClassLoader initially", + OpenmrsClassLoader.getInstance().getClass().getName(), initialClassLoader.getClass().getName()); + + // replay + servlet.service(mockRequest, mockResponse); + + // Verify + ClassLoader newClassLoader = Thread.currentThread().getContextClassLoader(); + + assertEquals("ContextClassLoader should be OpenmrsClassLoader after service method", + OpenmrsClassLoader.getInstance().getClass().getName(), newClassLoader.getClass().getName()); + } + + class TestableFhirRestServlet extends FhirRestServlet { + @Override + public void initialize() { + } + } +} \ No newline at end of file