Take super class methods into account and adding unit test (#1812)

* take super class methods into account as well

* added unit test for pull request 44f9b416ee

* take super class methods into account as well

* added unit test for pull request 44f9b416ee

* added unit test
This commit is contained in:
Zhe Wang 2020-05-01 13:24:28 +02:00 committed by GitHub
parent 439a901e30
commit 38a899bf64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 88 additions and 14 deletions

View File

@ -20,17 +20,19 @@ package ca.uhn.fhir.jaxrs.server.util;
* #L%
*/
import java.lang.reflect.Method;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.lang3.StringUtils;
import ca.uhn.fhir.jaxrs.server.AbstractJaxRsProvider;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
import ca.uhn.fhir.rest.server.exceptions.NotImplementedOperationException;
import ca.uhn.fhir.rest.server.method.*;
import ca.uhn.fhir.rest.server.method.BaseMethodBinding;
import ca.uhn.fhir.rest.server.method.OperationMethodBinding;
import ca.uhn.fhir.rest.server.method.SearchMethodBinding;
import ca.uhn.fhir.util.ReflectionUtil;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Method;
import java.util.LinkedHashSet;
import java.util.concurrent.ConcurrentHashMap;
/**
* Class that contains the method bindings defined by a ResourceProvider
@ -52,7 +54,9 @@ public class JaxRsMethodBindings {
* @param theProviderClass the class definition contaning the operations
*/
public JaxRsMethodBindings(AbstractJaxRsProvider theProvider, Class<? extends AbstractJaxRsProvider> theProviderClass) {
for (final Method m : ReflectionUtil.getDeclaredMethods(theProviderClass)) {
LinkedHashSet<Method> declaredMethodsForCurrentProvider = ReflectionUtil.getDeclaredMethods(theProviderClass);
declaredMethodsForCurrentProvider.addAll(ReflectionUtil.getDeclaredMethods(theProviderClass.getSuperclass()));
for (final Method m : declaredMethodsForCurrentProvider) {
final BaseMethodBinding<?> foundMethodBinding = BaseMethodBinding.bindMethod(m, theProvider.getFhirContext(), theProvider);
if (foundMethodBinding == null) {
continue;

View File

@ -0,0 +1,34 @@
package ca.uhn.fhir.jaxrs.server.test;
import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.jaxrs.server.AbstractJaxRsResourceProvider;
import ca.uhn.fhir.rest.annotation.RequiredParam;
import ca.uhn.fhir.rest.annotation.Search;
import ca.uhn.fhir.rest.param.StringParam;
import org.hl7.fhir.r4.model.Patient;
import java.util.List;
/**
* A dummy patient provider exposing no methods
*/
public abstract class AbstractDummyPatientProvider extends AbstractJaxRsResourceProvider<Patient> {
public AbstractDummyPatientProvider() {
super(FhirContext.forR4());
}
@Override
public abstract String getBaseForServer();
@Search
public List<Patient> search(@RequiredParam(name = Patient.SP_NAME) final StringParam name) {
return null;
}
@Override
public Class<Patient> getResourceType() {
return Patient.class;
}
}

View File

@ -1,15 +1,11 @@
package ca.uhn.fhir.jaxrs.server.test;
import ca.uhn.fhir.jaxrs.server.AbstractJaxRsResourceProvider;
import org.hl7.fhir.r4.model.Patient;
/**
* A dummy patient provider exposing no methods
*/
public class TestJaxRsDummyPatientProviderR4 extends AbstractJaxRsResourceProvider<Patient> {
public class TestJaxRsDummyPatientProviderR4 extends AbstractDummyPatientProvider {
@Override
public Class<Patient> getResourceType() {
return Patient.class;
@Override public String getBaseForServer() {
return "https://fhirserver/fhir/r4";
}
}

View File

@ -0,0 +1,12 @@
package ca.uhn.fhir.jaxrs.server.test;
/**
* A dummy patient provider exposing no methods
*/
public class TestJaxRsDummyPatientProviderR4MimeType extends AbstractDummyPatientProvider {
@Override public String getBaseForServer() {
return "https://fhirserver/fhir";
}
}

View File

@ -0,0 +1,28 @@
package ca.uhn.fhir.jaxrs.server.util;
import ca.uhn.fhir.jaxrs.server.test.AbstractDummyPatientProvider;
import ca.uhn.fhir.jaxrs.server.test.TestJaxRsDummyPatientProviderR4;
import ca.uhn.fhir.jaxrs.server.test.TestJaxRsDummyPatientProviderR4MimeType;
import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import static org.junit.Assert.assertEquals;
@FixMethodOrder(MethodSorters.DEFAULT)
public class JaxRsMethodBindingsMimeTypeTest {
@Before
public void setUp() {
JaxRsMethodBindings.getClassBindings().clear();
}
@Test
public void testFindMethodsFor2ProvidersWithMethods() {
assertEquals(AbstractDummyPatientProvider.class, new TestJaxRsDummyPatientProviderR4().getBindings().getBinding(RestOperationTypeEnum.SEARCH_TYPE, "").getMethod().getDeclaringClass());
assertEquals(AbstractDummyPatientProvider.class, new TestJaxRsDummyPatientProviderR4MimeType().getBindings().getBinding(RestOperationTypeEnum.SEARCH_TYPE, "").getMethod().getDeclaringClass());
}
}