Unknown resource type error message contains plain providers (#3711)

* fix and test

* change stream

* change variable name

* modify test

* changlog

* changlog

Co-authored-by: Justin_Dar <justin.dar@smilecdr.com>
This commit is contained in:
jdar8 2022-06-22 11:26:15 -07:00 committed by GitHub
parent e7ce41e5b8
commit ac006ba7d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 43 additions and 4 deletions

View File

@ -0,0 +1,6 @@
---
type: fix
issue: 3710
jira: SMILE-4230
title: "Changing the `Unknown resource type` error message to include only resource provider classes and
exclude plain providers classes."

View File

@ -1,11 +1,20 @@
package ca.uhn.fhir.jpa.provider.r4;
import ca.uhn.fhir.jpa.api.dao.DaoRegistry;
import ca.uhn.fhir.jpa.provider.BaseJpaProvider;
import ca.uhn.fhir.jpa.provider.BaseJpaSystemProvider;
import ca.uhn.fhir.jpa.provider.TerminologyUploaderProvider;
import ca.uhn.fhir.jpa.provider.ValueSetOperationProvider;
import ca.uhn.fhir.jpa.rp.r4.CodeSystemResourceProvider;
import ca.uhn.fhir.jpa.rp.r4.MeasureResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.hl7.fhir.r4.model.Patient;
import org.hl7.fhir.r4.model.Practitioner;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
@ -13,8 +22,15 @@ import org.springframework.test.context.ContextConfiguration;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;
import static com.healthmarketscience.sqlbuilder.Conditions.not;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.mockito.Mockito.mock;
@SuppressWarnings("Duplicates")
@ContextConfiguration(classes = {ResourceProviderOnlySomeResourcesProvidedR4Test.OnlySomeResourcesProvidedCtxConfig.class})
@ -32,7 +48,20 @@ public class ResourceProviderOnlySomeResourcesProvidedR4Test extends BaseResourc
try {
myClient.create().resource(pract).execute();
} catch (ResourceNotFoundException e) {
assertThat(e.getMessage(), containsString("Unknown resource type 'Practitioner' - Server knows how to handle:"));
String errorMessage = e.getMessage();
assertThat(errorMessage, CoreMatchers.allOf(
containsString("Unknown resource type 'Practitioner' - Server knows how to handle:"),
// Error message should contain all resources providers
containsString("Patient"),
containsString("Practitioner"),
containsString("SearchParameter"),
// Error message should not contain the registered plain providers
Matchers.not(containsString("ValueSet")),
Matchers.not(containsString("CodeSystem")),
Matchers.not(containsString("OperationDefinition"))
));
}
}

View File

@ -108,6 +108,7 @@ import java.util.jar.Manifest;
import java.util.stream.Collectors;
import static ca.uhn.fhir.util.StringUtil.toUtf8String;
import static java.util.stream.Collectors.toList;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
@ -681,7 +682,7 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
.stream()
.filter(t -> t instanceof IServerInterceptor)
.map(t -> (IServerInterceptor) t)
.collect(Collectors.toList());
.collect(toList());
return Collections.unmodifiableList(retVal);
}
@ -1904,7 +1905,10 @@ public class RestfulServer extends HttpServlet implements IRestfulServer<Servlet
}
protected void throwUnknownResourceTypeException(String theResourceName) {
throw new ResourceNotFoundException(Msg.code(302) + "Unknown resource type '" + theResourceName + "' - Server knows how to handle: " + myResourceNameToBinding.keySet());
List<String> knownResourceTypes = myResourceProviders.stream()
.map(t -> t.getResourceType().getSimpleName())
.collect(toList());
throw new ResourceNotFoundException(Msg.code(302) + "Unknown resource type '" + theResourceName + "' - Server knows how to handle: " + knownResourceTypes);
}
/**

View File

@ -79,7 +79,7 @@ public class SearchPreferHandlingInterceptorTest {
try (CloseableHttpResponse result = client.execute(new HttpGet("http://localhost:" + myPort + "/BadResource?foo=bar"))) {
assertEquals(404, result.getStatusLine().getStatusCode());
String response = IOUtils.toString(result.getEntity().getContent(), StandardCharsets.UTF_8);
assertThat(response, containsString("Unknown resource type 'BadResource' - Server knows how to handle: [Patient, OperationDefinition]"));
assertThat(response, containsString("Unknown resource type 'BadResource' - Server knows how to handle: [Patient]"));
}
}
}