#59 - Even more sorting out profile vs id problem

This commit is contained in:
b.debeaubien 2014-12-16 13:48:59 -05:00
parent 8865a77227
commit 4599ec1544
9 changed files with 62 additions and 26 deletions

View File

@ -19,6 +19,8 @@ import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.rest.server.provider.ServerConformanceProvider; import ca.uhn.fhir.rest.server.provider.ServerConformanceProvider;
import ca.uhn.fhir.util.ExtensionConstants; import ca.uhn.fhir.util.ExtensionConstants;
import javax.servlet.http.HttpServletRequest;
public class JpaConformanceProvider extends ServerConformanceProvider { public class JpaConformanceProvider extends ServerConformanceProvider {
private String myImplementationDescription; private String myImplementationDescription;
@ -34,14 +36,14 @@ public class JpaConformanceProvider extends ServerConformanceProvider {
} }
@Override @Override
public Conformance getServerConformance() { public Conformance getServerConformance(HttpServletRequest theRequest) {
Conformance retVal = myCachedValue; Conformance retVal = myCachedValue;
Map<String, Long> counts = mySystemDao.getResourceCounts(); Map<String, Long> counts = mySystemDao.getResourceCounts();
FhirContext ctx = myRestfulServer.getFhirContext(); FhirContext ctx = myRestfulServer.getFhirContext();
retVal = super.getServerConformance(); retVal = super.getServerConformance(theRequest);
for (Rest nextRest : retVal.getRest()) { for (Rest nextRest : retVal.getRest()) {
for (RestResource nextResource : nextRest.getResource()) { for (RestResource nextResource : nextRest.getResource()) {

View File

@ -65,17 +65,17 @@ public class ServerProfileProvider implements IResourceProvider {
@Search() @Search()
public List<Profile> getAllProfiles(HttpServletRequest theRequest) { public List<Profile> getAllProfiles(HttpServletRequest theRequest) {
final String serverBase = getServerBase(theRequest);
List<RuntimeResourceDefinition> defs = new ArrayList<RuntimeResourceDefinition>(myContext.getResourceDefinitions()); List<RuntimeResourceDefinition> defs = new ArrayList<RuntimeResourceDefinition>(myContext.getResourceDefinitions());
Collections.sort(defs, new Comparator<RuntimeResourceDefinition>() { Collections.sort(defs, new Comparator<RuntimeResourceDefinition>() {
@Override @Override
public int compare(RuntimeResourceDefinition theO1, RuntimeResourceDefinition theO2) { public int compare(RuntimeResourceDefinition theO1, RuntimeResourceDefinition theO2) {
int cmp = theO1.getName().compareTo(theO2.getName()); int cmp = theO1.getName().compareTo(theO2.getName());
if (cmp==0) { if (cmp==0) {
cmp=theO1.getResourceProfile().compareTo(theO2.getResourceProfile()); cmp=theO1.getResourceProfile(serverBase).compareTo(theO2.getResourceProfile(serverBase));
} }
return cmp; return cmp;
}}); }});
String serverBase = getServerBase(theRequest);
ArrayList<Profile> retVal = new ArrayList<Profile>(); ArrayList<Profile> retVal = new ArrayList<Profile>();
for (RuntimeResourceDefinition next : defs) { for (RuntimeResourceDefinition next : defs) {
retVal.add((Profile) next.toProfile(serverBase)); retVal.add((Profile) next.toProfile(serverBase));

View File

@ -59,6 +59,8 @@ import ca.uhn.fhir.rest.server.ResourceBinding;
import ca.uhn.fhir.rest.server.RestfulServer; import ca.uhn.fhir.rest.server.RestfulServer;
import ca.uhn.fhir.util.ExtensionConstants; import ca.uhn.fhir.util.ExtensionConstants;
import javax.servlet.http.HttpServletRequest;
/** /**
* Server FHIR Provider which serves the conformance statement for a RESTful server implementation * Server FHIR Provider which serves the conformance statement for a RESTful server implementation
* *
@ -95,7 +97,7 @@ public class ServerConformanceProvider {
* See the class documentation for an important note if you are extending this class * See the class documentation for an important note if you are extending this class
*/ */
@Metadata @Metadata
public Conformance getServerConformance() { public Conformance getServerConformance(HttpServletRequest theRequest) {
if (myConformance != null && myCache) { if (myConformance != null && myCache) {
return myConformance; return myConformance;
} }
@ -134,7 +136,7 @@ public class ServerConformanceProvider {
String resourceName = next.getResourceName(); String resourceName = next.getResourceName();
RuntimeResourceDefinition def = myRestfulServer.getFhirContext().getResourceDefinition(resourceName); RuntimeResourceDefinition def = myRestfulServer.getFhirContext().getResourceDefinition(resourceName);
resource.getType().setValue(def.getName()); resource.getType().setValue(def.getName());
resource.getProfile().setReference(new IdDt(def.getResourceProfile())); resource.getProfile().setReference(new IdDt(def.getResourceProfile(myRestfulServer.getServerBaseForRequest(theRequest))));
TreeSet<String> includes = new TreeSet<String>(); TreeSet<String> includes = new TreeSet<String>();

View File

@ -66,17 +66,17 @@ public class ServerProfileProvider implements IResourceProvider {
@Search() @Search()
public List<Profile> getAllProfiles(HttpServletRequest theRequest) { public List<Profile> getAllProfiles(HttpServletRequest theRequest) {
final String serverBase = getServerBase(theRequest);
List<RuntimeResourceDefinition> defs = new ArrayList<RuntimeResourceDefinition>(myContext.getResourceDefinitions()); List<RuntimeResourceDefinition> defs = new ArrayList<RuntimeResourceDefinition>(myContext.getResourceDefinitions());
Collections.sort(defs, new Comparator<RuntimeResourceDefinition>() { Collections.sort(defs, new Comparator<RuntimeResourceDefinition>() {
@Override @Override
public int compare(RuntimeResourceDefinition theO1, RuntimeResourceDefinition theO2) { public int compare(RuntimeResourceDefinition theO1, RuntimeResourceDefinition theO2) {
int cmp = theO1.getName().compareTo(theO2.getName()); int cmp = theO1.getName().compareTo(theO2.getName());
if (cmp==0) { if (cmp==0) {
cmp=theO1.getResourceProfile().compareTo(theO2.getResourceProfile()); cmp=theO1.getResourceProfile(serverBase).compareTo(theO2.getResourceProfile(serverBase));
} }
return cmp; return cmp;
}}); }});
String serverBase = getServerBase(theRequest);
ArrayList<Profile> retVal = new ArrayList<Profile>(); ArrayList<Profile> retVal = new ArrayList<Profile>();
for (RuntimeResourceDefinition next : defs) { for (RuntimeResourceDefinition next : defs) {
retVal.add((Profile) next.toProfile(serverBase)); retVal.add((Profile) next.toProfile(serverBase));

View File

@ -25,13 +25,13 @@ public class DuplicateExtensionTest extends TestCase {
public void testScannerShouldAddProvidedResources() { public void testScannerShouldAddProvidedResources() {
FhirContext ctx = new FhirContext(); FhirContext ctx = new FhirContext();
RuntimeResourceDefinition patientDef = ctx.getResourceDefinition(CustomPatient.class); RuntimeResourceDefinition patientDef = ctx.getResourceDefinition(CustomPatient.class);
Profile profile = (Profile) patientDef.toProfile(); Profile profile = (Profile) patientDef.toProfile("http://foo.org/fhir");
String res = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(profile); String res = ctx.newJsonParser().setPrettyPrint(true).encodeResourceToString(profile);
ourLog.info(res); ourLog.info(res);
RuntimeResourceDefinition observationDef = ctx.getResourceDefinition(CustomObservation.class); RuntimeResourceDefinition observationDef = ctx.getResourceDefinition(CustomObservation.class);
profile = (Profile) observationDef.toProfile(); profile = (Profile) observationDef.toProfile("http://foo.org/fhir");
} }
@ResourceDef(name = "Observation", id = "CustomObservation") @ResourceDef(name = "Observation", id = "CustomObservation")

View File

@ -80,7 +80,7 @@ public class ExtensionTest {
{ {
FhirContext ctx2 = new FhirContext(); FhirContext ctx2 = new FhirContext();
RuntimeResourceDefinition def = ctx2.getResourceDefinition(patient); RuntimeResourceDefinition def = ctx2.getResourceDefinition(patient);
System.out.println(ctx2.newXmlParser().setPrettyPrint(true).encodeResourceToString(def.toProfile())); System.out.println(ctx2.newXmlParser().setPrettyPrint(true).encodeResourceToString(def.toProfile("http://foo.org/fhir")));
} }
} }

View File

@ -23,7 +23,7 @@ public class RuntimeResourceDefinitionTest {
FhirContext ctx = new FhirContext(Patient.class, Profile.class); FhirContext ctx = new FhirContext(Patient.class, Profile.class);
RuntimeResourceDefinition def = ctx.getResourceDefinition(Patient.class); RuntimeResourceDefinition def = ctx.getResourceDefinition(Patient.class);
Profile profile = (Profile) def.toProfile(); Profile profile = (Profile) def.toProfile("http://foo.org/fhir");
ourLog.info(ctx.newXmlParser().encodeResourceToString(profile)); ourLog.info(ctx.newXmlParser().encodeResourceToString(profile));
@ -42,7 +42,7 @@ public class RuntimeResourceDefinitionTest {
FhirContext ctx = new FhirContext(ValueSet.class, Profile.class); FhirContext ctx = new FhirContext(ValueSet.class, Profile.class);
RuntimeResourceDefinition def = ctx.getResourceDefinition(ValueSet.class); RuntimeResourceDefinition def = ctx.getResourceDefinition(ValueSet.class);
Profile profile = (Profile) def.toProfile(); Profile profile = (Profile) def.toProfile("http://foo.org/fhir");
String encoded = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(profile); String encoded = ctx.newXmlParser().setPrettyPrint(true).encodeResourceToString(profile);
ourLog.info(encoded); ourLog.info(encoded);
@ -57,7 +57,7 @@ public class RuntimeResourceDefinitionTest {
FhirContext ctx = new FhirContext(ResourceWithExtensionsA.class, Profile.class); FhirContext ctx = new FhirContext(ResourceWithExtensionsA.class, Profile.class);
RuntimeResourceDefinition def = ctx.getResourceDefinition(ResourceWithExtensionsA.class); RuntimeResourceDefinition def = ctx.getResourceDefinition(ResourceWithExtensionsA.class);
Profile profile = (Profile) def.toProfile(); Profile profile = (Profile) def.toProfile("http://foo.org/fhir");
ourLog.info(ctx.newXmlParser().encodeResourceToString(profile)); ourLog.info(ctx.newXmlParser().encodeResourceToString(profile));
@ -98,7 +98,7 @@ public class RuntimeResourceDefinitionTest {
FhirContext ctx = new FhirContext(CustomObservation.class); FhirContext ctx = new FhirContext(CustomObservation.class);
RuntimeResourceDefinition def = ctx.getResourceDefinition(CustomObservation.class); RuntimeResourceDefinition def = ctx.getResourceDefinition(CustomObservation.class);
Profile profile = (Profile) def.toProfile(); Profile profile = (Profile) def.toProfile("http://foo.org/fhir");
assertEquals("customobservation", profile.getId().toString()); assertEquals("customobservation", profile.getId().toString());
} }

View File

@ -3,6 +3,8 @@ package ca.uhn.fhir.rest.server;
import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.not;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -72,6 +74,8 @@ import ca.uhn.fhir.rest.server.exceptions.UnprocessableEntityException;
import ca.uhn.fhir.rest.server.provider.ServerProfileProvider; import ca.uhn.fhir.rest.server.provider.ServerProfileProvider;
import ca.uhn.fhir.util.PortUtil; import ca.uhn.fhir.util.PortUtil;
import javax.servlet.http.HttpServletRequest;
/** /**
* Created by dsotnikov on 2/25/2014. * Created by dsotnikov on 2/25/2014.
*/ */
@ -997,10 +1001,19 @@ public class RestfulServerMethodTest {
public void testServerProfileProviderFindsProfiles() { public void testServerProfileProviderFindsProfiles() {
ServerProfileProvider profileProvider = (ServerProfileProvider)ourRestfulServer.getServerProfilesProvider(); ServerProfileProvider profileProvider = (ServerProfileProvider)ourRestfulServer.getServerProfilesProvider();
IdDt id = new IdDt("Profile", "observation"); IdDt id = new IdDt("Profile", "observation");
Profile profile = profileProvider.getProfileById(null, id); Profile profile = profileProvider.getProfileById(createHttpServletRequest(), id);
assertNotNull(profile); assertNotNull(profile);
} }
private HttpServletRequest createHttpServletRequest() {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/FhirStorm/fhir/Patient/_search");
when(req.getServletPath()).thenReturn("/fhir");
when(req.getRequestURL()).thenReturn(new StringBuffer().append("http://fhirstorm.dyndns.org:8080/FhirStorm/fhir/Patient/_search"));
when(req.getContextPath()).thenReturn("/FhirStorm");
return req;
}
@AfterClass @AfterClass
public static void afterClass() throws Exception { public static void afterClass() throws Exception {
ourServer.stop(); ourServer.stop();

View File

@ -4,6 +4,8 @@ import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -33,6 +35,9 @@ import ca.uhn.fhir.rest.param.DateRangeParam;
import ca.uhn.fhir.rest.param.TokenOrListParam; import ca.uhn.fhir.rest.param.TokenOrListParam;
import ca.uhn.fhir.rest.server.provider.ServerConformanceProvider; import ca.uhn.fhir.rest.server.provider.ServerConformanceProvider;
import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
public class ServerConformanceProviderTest { public class ServerConformanceProviderTest {
private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ServerConformanceProviderTest.class); private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ServerConformanceProviderTest.class);
@ -47,7 +52,7 @@ public class ServerConformanceProviderTest {
ServerConformanceProvider sc = new ServerConformanceProvider(rs); ServerConformanceProvider sc = new ServerConformanceProvider(rs);
rs.setServerConformanceProvider(sc); rs.setServerConformanceProvider(sc);
rs.init(null); rs.init(createServletConfig());
boolean found=false; boolean found=false;
Collection<ResourceBinding> resourceBindings = rs.getResourceBindings(); Collection<ResourceBinding> resourceBindings = rs.getResourceBindings();
@ -61,7 +66,7 @@ public class ServerConformanceProviderTest {
} }
} }
assertTrue(found); assertTrue(found);
Conformance conformance = sc.getServerConformance(); Conformance conformance = sc.getServerConformance(createHttpServletRequest());
String conf = myCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(conformance); String conf = myCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(conformance);
ourLog.info(conf); ourLog.info(conf);
@ -81,9 +86,9 @@ public class ServerConformanceProviderTest {
ServerConformanceProvider sc = new ServerConformanceProvider(rs); ServerConformanceProvider sc = new ServerConformanceProvider(rs);
rs.setServerConformanceProvider(sc); rs.setServerConformanceProvider(sc);
rs.init(null); rs.init(createServletConfig());
Conformance conformance = sc.getServerConformance(); Conformance conformance = sc.getServerConformance(createHttpServletRequest());
myCtx.newValidator().validate(conformance); myCtx.newValidator().validate(conformance);
} }
@ -99,7 +104,7 @@ public class ServerConformanceProviderTest {
ServerConformanceProvider sc = new ServerConformanceProvider(rs); ServerConformanceProvider sc = new ServerConformanceProvider(rs);
rs.setServerConformanceProvider(sc); rs.setServerConformanceProvider(sc);
rs.init(null); rs.init(createServletConfig());
boolean found=false; boolean found=false;
Collection<ResourceBinding> resourceBindings = rs.getResourceBindings(); Collection<ResourceBinding> resourceBindings = rs.getResourceBindings();
@ -113,7 +118,7 @@ public class ServerConformanceProviderTest {
} }
} }
assertTrue(found); assertTrue(found);
Conformance conformance = sc.getServerConformance(); Conformance conformance = sc.getServerConformance(createHttpServletRequest());
String conf = new FhirContext().newXmlParser().setPrettyPrint(true).encodeResourceToString(conformance); String conf = new FhirContext().newXmlParser().setPrettyPrint(true).encodeResourceToString(conformance);
ourLog.info(conf); ourLog.info(conf);
@ -131,9 +136,9 @@ public class ServerConformanceProviderTest {
ServerConformanceProvider sc = new ServerConformanceProvider(rs); ServerConformanceProvider sc = new ServerConformanceProvider(rs);
rs.setServerConformanceProvider(sc); rs.setServerConformanceProvider(sc);
rs.init(null); rs.init(createServletConfig());
Conformance conformance = sc.getServerConformance(); Conformance conformance = sc.getServerConformance(createHttpServletRequest());
String conf = new FhirContext().newXmlParser().setPrettyPrint(true).encodeResourceToString(conformance); String conf = new FhirContext().newXmlParser().setPrettyPrint(true).encodeResourceToString(conformance);
ourLog.info(conf); ourLog.info(conf);
@ -148,6 +153,20 @@ public class ServerConformanceProviderTest {
assertEquals("DiagnosticReport.result", res.getSearchIncludeFirstRep().getValue()); assertEquals("DiagnosticReport.result", res.getSearchIncludeFirstRep().getValue());
} }
private HttpServletRequest createHttpServletRequest() {
HttpServletRequest req = mock(HttpServletRequest.class);
when(req.getRequestURI()).thenReturn("/FhirStorm/fhir/Patient/_search");
when(req.getServletPath()).thenReturn("/fhir");
when(req.getRequestURL()).thenReturn(new StringBuffer().append("http://fhirstorm.dyndns.org:8080/FhirStorm/fhir/Patient/_search"));
when(req.getContextPath()).thenReturn("/FhirStorm");
return req;
}
private ServletConfig createServletConfig() {
ServletConfig sc = mock(ServletConfig.class);
when (sc.getServletContext()).thenReturn(null);
return sc;
}
/** /**
* Created by dsotnikov on 2/25/2014. * Created by dsotnikov on 2/25/2014.