From 3eb1c79cec3704a5b821377c4df3f8e9f703c8a3 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Fri, 15 Aug 2025 12:37:42 +0200 Subject: feat: check consent for DNPM 2.1 requests (#126) Co-authored-by: Jakub Lidke --- .../processor/consent/GicsConsentServiceTest.java | 192 ++++++++++++++------- 1 file changed, 125 insertions(+), 67 deletions(-) (limited to 'src/test/java/dev/dnpm/etl/processor') diff --git a/src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java b/src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java index d26eca2..6fa8f08 100644 --- a/src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java +++ b/src/test/java/dev/dnpm/etl/processor/consent/GicsConsentServiceTest.java @@ -1,85 +1,112 @@ package dev.dnpm.etl.processor.consent; -import static org.assertj.core.api.Assertions.assertThat; -import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; -import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; import com.fasterxml.jackson.databind.ObjectMapper; import dev.dnpm.etl.processor.config.AppConfiguration; import dev.dnpm.etl.processor.config.AppFhirConfig; import dev.dnpm.etl.processor.config.GIcsConfigProperties; -import java.time.Instant; -import java.util.Date; -import org.hl7.fhir.r4.model.BooleanType; -import org.hl7.fhir.r4.model.Identifier; -import org.hl7.fhir.r4.model.OperationOutcome; +import org.hl7.fhir.r4.model.*; import org.hl7.fhir.r4.model.OperationOutcome.IssueSeverity; import org.hl7.fhir.r4.model.OperationOutcome.IssueType; import org.hl7.fhir.r4.model.OperationOutcome.OperationOutcomeIssueComponent; -import org.hl7.fhir.r4.model.Parameters; import org.hl7.fhir.r4.model.Parameters.ParametersParameterComponent; -import org.hl7.fhir.r4.model.StringType; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.client.RestClientTest; import org.springframework.http.MediaType; +import org.springframework.retry.support.RetryTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.web.client.RestTemplate; -@ContextConfiguration(classes = {AppConfiguration.class, ObjectMapper.class}) -@TestPropertySource(properties = {"app.consent.gics.enabled=true", - "app.consent.gics.uri=http://localhost:8090/ttp-fhir/fhir/gics"}) -@RestClientTest -public class GicsConsentServiceTest { +import java.time.Instant; +import java.util.Date; - public static final String GICS_BASE_URI = "http://localhost:8090/ttp-fhir/fhir/gics"; - @Autowired - MockRestServiceServer mockRestServiceServer; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError; +import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess; - @Autowired - GicsConsentService gicsConsentService; +@ContextConfiguration(classes = {AppConfiguration.class, ObjectMapper.class}) +@TestPropertySource(properties = { + "app.consent.service=gics", + "app.consent.gics.uri=http://localhost:8090/ttp-fhir/fhir/gics" +}) +@RestClientTest +class GicsConsentServiceTest { - @Autowired - AppConfiguration appConfiguration; + static final String GICS_BASE_URI = "http://localhost:8090/ttp-fhir/fhir/gics"; - @Autowired + MockRestServiceServer mockRestServiceServer; AppFhirConfig appFhirConfig; - - @Autowired GIcsConfigProperties gIcsConfigProperties; + GicsConsentService gicsConsentService; + @BeforeEach - public void setUp() { - mockRestServiceServer = MockRestServiceServer.createServer(appConfiguration.restTemplate()); + void setUp( + @Autowired AppFhirConfig appFhirConfig, + @Autowired GIcsConfigProperties gIcsConfigProperties + ) { + this.appFhirConfig = appFhirConfig; + this.gIcsConfigProperties = gIcsConfigProperties; + + var restTemplate = new RestTemplate(); + + this.mockRestServiceServer = MockRestServiceServer.createServer(restTemplate); + this.gicsConsentService = new GicsConsentService( + this.gIcsConfigProperties, + RetryTemplate.builder().maxAttempts(1).build(), + restTemplate, + this.appFhirConfig + ); } @Test - void getTtpBroadConsentStatus() { - final Parameters responseConsented = new Parameters().addParameter( - new ParametersParameterComponent().setName("consented") - .setValue(new BooleanType().setValue(true))); - - mockRestServiceServer.expect(requestTo( - "http://localhost:8090/ttp-fhir/fhir/gics" + GicsConsentService.IS_CONSENTED_ENDPOINT)) - .andRespond(withSuccess(appFhirConfig.fhirContext().newJsonParser() - .encodeResourceToString(responseConsented), MediaType.APPLICATION_JSON)); + void shouldReturnTtpBroadConsentStatus() { + final Parameters consentedResponse = new Parameters() + .addParameter( + new ParametersParameterComponent() + .setName("consented") + .setValue(new BooleanType().setValue(true)) + ); + + mockRestServiceServer + .expect( + requestTo( + "http://localhost:8090/ttp-fhir/fhir/gics" + GicsConsentService.IS_CONSENTED_ENDPOINT) + ) + .andRespond( + withSuccess( + appFhirConfig.fhirContext().newJsonParser().encodeResourceToString(consentedResponse), + MediaType.APPLICATION_JSON + ) + ); var consentStatus = gicsConsentService.getTtpBroadConsentStatus("123456"); assertThat(consentStatus).isEqualTo(TtpConsentStatus.BROAD_CONSENT_GIVEN); } @Test - void consentRevoced() { - final Parameters responseRevoced = new Parameters().addParameter( - new ParametersParameterComponent().setName("consented") - .setValue(new BooleanType().setValue(false))); - - mockRestServiceServer.expect(requestTo( - "http://localhost:8090/ttp-fhir/fhir/gics" + GicsConsentService.IS_CONSENTED_ENDPOINT)) - .andRespond(withSuccess( - appFhirConfig.fhirContext().newJsonParser().encodeResourceToString(responseRevoced), - MediaType.APPLICATION_JSON)); + void shouldReturnRevokedConsent() { + final Parameters revokedResponse = new Parameters() + .addParameter( + new ParametersParameterComponent() + .setName("consented") + .setValue(new BooleanType().setValue(false)) + ); + + mockRestServiceServer + .expect( + requestTo( + "http://localhost:8090/ttp-fhir/fhir/gics" + GicsConsentService.IS_CONSENTED_ENDPOINT) + ) + .andRespond( + withSuccess( + appFhirConfig.fhirContext().newJsonParser().encodeResourceToString(revokedResponse), + MediaType.APPLICATION_JSON) + ); var consentStatus = gicsConsentService.getTtpBroadConsentStatus("123456"); assertThat(consentStatus).isEqualTo(TtpConsentStatus.BROAD_CONSENT_MISSING_OR_REJECTED); @@ -87,15 +114,39 @@ public class GicsConsentServiceTest { @Test - void gicsParameterInvalid() { - final OperationOutcome responseErrorOutcome = new OperationOutcome().addIssue( - new OperationOutcomeIssueComponent().setSeverity(IssueSeverity.ERROR) - .setCode(IssueType.PROCESSING).setDiagnostics("Invalid policy parameter...")); + void shouldReturnInvalidParameterResponse() { + final OperationOutcome responseWithErrorOutcome = new OperationOutcome() + .addIssue( + new OperationOutcomeIssueComponent() + .setSeverity(IssueSeverity.ERROR) + .setCode(IssueType.PROCESSING) + .setDiagnostics("Invalid policy parameter...") + ); + + mockRestServiceServer + .expect( + requestTo(GICS_BASE_URI + GicsConsentService.IS_CONSENTED_ENDPOINT) + ) + .andRespond( + withSuccess( + appFhirConfig.fhirContext().newJsonParser().encodeResourceToString(responseWithErrorOutcome), + MediaType.APPLICATION_JSON + ) + ); - mockRestServiceServer.expect( - requestTo(GICS_BASE_URI + GicsConsentService.IS_CONSENTED_ENDPOINT)).andRespond( - withSuccess(appFhirConfig.fhirContext().newJsonParser() - .encodeResourceToString(responseErrorOutcome), MediaType.APPLICATION_JSON)); + var consentStatus = gicsConsentService.getTtpBroadConsentStatus("123456"); + assertThat(consentStatus).isEqualTo(TtpConsentStatus.FAILED_TO_ASK); + } + + @Test + void shouldReturnRequestError() { + mockRestServiceServer + .expect( + requestTo(GICS_BASE_URI + GicsConsentService.IS_CONSENTED_ENDPOINT) + ) + .andRespond( + withServerError() + ); var consentStatus = gicsConsentService.getTtpBroadConsentStatus("123456"); assertThat(consentStatus).isEqualTo(TtpConsentStatus.FAILED_TO_ASK); @@ -103,20 +154,27 @@ public class GicsConsentServiceTest { @Test void buildRequestParameterCurrentPolicyStatesForPersonTest() { - String pid = "12345678"; - var result = GicsConsentService.buildRequestParameterCurrentPolicyStatesForPerson( - gIcsConfigProperties, pid, Date.from(Instant.now()), - gIcsConfigProperties.getGenomDeConsentDomainName()); - - assertThat(result.getParameter().size()).as("should contain 3 parameter resources") - .isEqualTo(3); - - assertThat(((StringType) result.getParameter("domain").getValue()).getValue()).isEqualTo( - gIcsConfigProperties.getGenomDeConsentDomainName()); - assertThat( - ((Identifier) result.getParameter("personIdentifier").getValue()).getValue()).isEqualTo( - pid); + var result = gicsConsentService + .buildRequestParameterCurrentPolicyStatesForPerson( + pid, + Date.from(Instant.now()), + ConsentDomain.MODELLVORHABEN_64E + ); + + assertThat(result.getParameter()) + .as("should contain 3 parameter resources") + .hasSize(3); + + assertThat(((StringType) result.getParameter("domain").getValue()).getValue()) + .isEqualTo( + gIcsConfigProperties.getGenomDeConsentDomainName() + ); + + assertThat(((Identifier) result.getParameter("personIdentifier").getValue()).getValue()) + .isEqualTo( + pid + ); } -- cgit v1.2.3