diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-autoconfigure/src/main/java/ca/uhn/fhir/spring/boot/autoconfigure/FhirAutoConfiguration.java b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-autoconfigure/src/main/java/ca/uhn/fhir/spring/boot/autoconfigure/FhirAutoConfiguration.java index a9cdbacdb67d..f6950fc4df71 100644 --- a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-autoconfigure/src/main/java/ca/uhn/fhir/spring/boot/autoconfigure/FhirAutoConfiguration.java +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-autoconfigure/src/main/java/ca/uhn/fhir/spring/boot/autoconfigure/FhirAutoConfiguration.java @@ -20,7 +20,6 @@ package ca.uhn.fhir.spring.boot.autoconfigure; import ca.uhn.fhir.context.FhirContext; -import ca.uhn.fhir.jaxrs.server.AbstractJaxRsProvider; import ca.uhn.fhir.jpa.api.config.JpaStorageSettings; import ca.uhn.fhir.jpa.config.HapiJpaConfig; import ca.uhn.fhir.jpa.config.JpaDstu2Config; @@ -103,7 +102,7 @@ public FhirContext fhirContext() { } @Configuration - @ConditionalOnClass(AbstractJaxRsProvider.class) + @ConditionalOnClass(RestfulServer.class) @EnableConfigurationProperties(FhirProperties.class) @ConfigurationProperties("hapi.fhir.rest") @SuppressWarnings("serial") diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-client-apache/src/main/java/sample/fhir/client/SampleApacheRestfulClientApplication.java b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-client-apache/src/main/java/sample/fhir/client/SampleApacheRestfulClientApplication.java index 66b85ae7c16e..f0578548c131 100644 --- a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-client-apache/src/main/java/sample/fhir/client/SampleApacheRestfulClientApplication.java +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-client-apache/src/main/java/sample/fhir/client/SampleApacheRestfulClientApplication.java @@ -21,13 +21,17 @@ import ca.uhn.fhir.rest.client.api.IGenericClient; import ca.uhn.fhir.rest.client.interceptor.LoggingInterceptor; +import ca.uhn.fhir.spring.boot.autoconfigure.FhirAutoConfiguration; + import org.hl7.fhir.dstu3.model.CapabilityStatement; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; @SpringBootApplication +@ImportAutoConfiguration(FhirAutoConfiguration.class) public class SampleApacheRestfulClientApplication { public static void main(String[] args) { diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-client-apache/src/test/java/sample/fhir/client/AutoconfigurationTest.java b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-client-apache/src/test/java/sample/fhir/client/AutoconfigurationTest.java new file mode 100644 index 000000000000..34e3efb84c3b --- /dev/null +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-client-apache/src/test/java/sample/fhir/client/AutoconfigurationTest.java @@ -0,0 +1,61 @@ +package sample.fhir.client; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.ApplicationContext; + +import ca.uhn.fhir.rest.client.api.IGenericClient; +import ca.uhn.fhir.spring.boot.autoconfigure.FhirProperties; + +@SpringBootTest +public class AutoconfigurationTest { + + @Autowired + private FhirProperties properties; + + @Autowired + private ApplicationContext applicationContext; + + @MockBean + private CommandLineRunner runner; + + @Test + public void testContextNotNull() { + assertThat(applicationContext).isNotNull(); + + } + + @Test + public void testClientBeanCreated() { + // Test that a client bean is created + String[] beanNames = applicationContext.getBeanNamesForType(IGenericClient.class); + assertThat(beanNames).isNotEmpty(); + } + + @Test + public void testServerBeanNotCreated() { + // Test that no bean has a URL mapping that includes the FHIR server path set in + // the application configuration + String[] beanNames = applicationContext.getBeanNamesForType(ServletRegistrationBean.class); + String expectedPath = properties.getServer().getPath(); + long count = 0; + for (String beanName : beanNames) { + ServletRegistrationBean bean = applicationContext.getBean(beanName, ServletRegistrationBean.class); + for (String mapping : bean.getUrlMappings()) { + if (mapping.contains(expectedPath)) { + count++; + } + } + } + // The server should not be created because the hapi-fhir-server dependency is + // not added + assertThat(count).isEqualTo(0); + } + +} diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/pom.xml b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/pom.xml new file mode 100644 index 000000000000..53f5be13da47 --- /dev/null +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/pom.xml @@ -0,0 +1,94 @@ + + 4.0.0 + + + ca.uhn.hapi.fhir + hapi-fhir-spring-boot-samples + 8.5.1-SNAPSHOT + + + + hapi-fhir-spring-boot-sample-server-plain + HAPI-FHIR Spring Boot Sample Server + + jar + + + + + org.springframework.boot + spring-boot-starter-web + + + ca.uhn.hapi.fhir + hapi-fhir-spring-boot-starter + ${project.version} + + + ca.uhn.hapi.fhir + hapi-fhir-structures-r4 + ${project.version} + + + ca.uhn.hapi.fhir + hapi-fhir-server + ${project.version} + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + + + org.slf4j + log4j-over-slf4j + + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring_boot_version} + pom + import + true + + + + + + + + + org.basepom.maven + duplicate-finder-maven-plugin + + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/java/sample/fhir/server/plain/SamplePlainRestfulServerApplication.java b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/java/sample/fhir/server/plain/SamplePlainRestfulServerApplication.java new file mode 100644 index 000000000000..9dd5e71dc588 --- /dev/null +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/java/sample/fhir/server/plain/SamplePlainRestfulServerApplication.java @@ -0,0 +1,36 @@ +/*- + * #%L + * hapi-fhir-spring-boot-sample-server-jersey + * %% + * Copyright (C) 2014 - 2017 University Health Network + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package sample.fhir.server.plain; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import ca.uhn.fhir.spring.boot.autoconfigure.FhirAutoConfiguration; + +@SpringBootApplication +@ImportAutoConfiguration(FhirAutoConfiguration.class) +public class SamplePlainRestfulServerApplication { + + public static void main(String[] args) { + SpringApplication.run(SamplePlainRestfulServerApplication.class, args); + } + +} diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/java/sample/fhir/server/plain/provider/PatientResourceProvider.java b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/java/sample/fhir/server/plain/provider/PatientResourceProvider.java new file mode 100644 index 000000000000..1b86ae3a4e50 --- /dev/null +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/java/sample/fhir/server/plain/provider/PatientResourceProvider.java @@ -0,0 +1,66 @@ +/*- + * #%L + * hapi-fhir-spring-boot-sample-server-jersey + * %% + * Copyright (C) 2014 - 2017 University Health Network + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package sample.fhir.server.plain.provider; + +import java.util.HashMap; + +import org.hl7.fhir.r4.model.HumanName; +import org.hl7.fhir.r4.model.IdType; +import org.hl7.fhir.r4.model.Patient; +import org.springframework.stereotype.Component; + +import ca.uhn.fhir.i18n.Msg; +import ca.uhn.fhir.rest.annotation.IdParam; +import ca.uhn.fhir.rest.annotation.Read; +import ca.uhn.fhir.rest.server.IResourceProvider; +import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException; + +@Component +public class PatientResourceProvider implements IResourceProvider { + + private static final HashMap patients = new HashMap<>(); + + static { + patients.put(String.valueOf(1L), createPatient("1", "Doe", "Jane")); + patients.put(String.valueOf(2L), createPatient("2", "Doe", "John")); + } + + @Override + public Class getResourceType() { + return Patient.class; + } + + @Read + public Patient find(@IdParam final IdType theId) { + if (patients.containsKey(theId.getIdPart())) { + return patients.get(theId.getIdPart()); + } else { + throw new ResourceNotFoundException(Msg.code(2005) + theId); + } + } + + private static Patient createPatient(final String id, final String family, final String given) { + final Patient patient = new Patient(); + patient.getName().add(new HumanName().setFamily(family).addGiven(given)); + patient.setId(id); + return patient; + } + +} diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/resources/application.yml b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/resources/application.yml new file mode 100644 index 000000000000..332500d1091b --- /dev/null +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/main/resources/application.yml @@ -0,0 +1,18 @@ +hapi: + fhir: + version: R4 + server: + path: /fhir/* + rest: + server-name: hapi-fhir-spring-boot-sample-server-plain + server-version: 1.0.0 + implementation-description: Spring Boot Plain Server Sample + default-response-encoding: json + e-tag-support: enabled + default-pretty-print: true + validation: + enabled: true + request-only: true +management: + security: + enabled: false diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/test/java/sample/fhir/server/plain/SamplePlainRestfulServerApplicationTest.java b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/test/java/sample/fhir/server/plain/SamplePlainRestfulServerApplicationTest.java new file mode 100644 index 000000000000..dffbd75e7d27 --- /dev/null +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/test/java/sample/fhir/server/plain/SamplePlainRestfulServerApplicationTest.java @@ -0,0 +1,40 @@ +package sample.fhir.server.plain; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.ApplicationContext; + +import ca.uhn.fhir.spring.boot.autoconfigure.FhirProperties; + +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +public class SamplePlainRestfulServerApplicationTest { + + @Autowired + private FhirProperties properties; + + @Autowired + private ApplicationContext applicationContext; + + @Test + public void testBean() { + // Test if exactly one bean has a URL mapping that includes the FHIR server path set in the application configuration + String[] beanNames = applicationContext.getBeanNamesForType(ServletRegistrationBean.class); + String expectedPath = properties.getServer().getPath(); + long count = 0; + for (String beanName : beanNames) { + ServletRegistrationBean bean = applicationContext.getBean(beanName, ServletRegistrationBean.class); + for (String mapping : bean.getUrlMappings()) { + if (mapping.contains(expectedPath)) { + count++; + } + } + } + assertThat(count).isEqualTo(1); + } + +} diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/test/resources/logback-test.xml b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/test/resources/logback-test.xml new file mode 100644 index 000000000000..dc857b128142 --- /dev/null +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/hapi-fhir-spring-boot-sample-server-plain/src/test/resources/logback-test.xml @@ -0,0 +1,14 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} [%file:%line] - %msg%n + + + + + + + + + diff --git a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/pom.xml b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/pom.xml index d1de5ae7ef4d..a474c367e7fa 100644 --- a/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/pom.xml +++ b/hapi-fhir-spring-boot/hapi-fhir-spring-boot-samples/pom.xml @@ -17,6 +17,7 @@ hapi-fhir-spring-boot-sample-client-apache hapi-fhir-spring-boot-sample-client-okhttp hapi-fhir-spring-boot-sample-server-jersey + hapi-fhir-spring-boot-sample-server-plain