Skip to content
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

FM2-651: Allow DAOs access to all API load-able by the OpenMRS Class loader. #554

Merged
merged 3 commits into from
Dec 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}
}
}
Loading