From 09c8308e0e3ce8cb7600777bcf97708bc71d2d3b Mon Sep 17 00:00:00 2001 From: dotasek Date: Thu, 4 Jul 2024 12:13:46 -0400 Subject: [PATCH] Clean up the session cache before adding a new one + fix test runs --- .../cli/services/ValidationService.java | 2 +- ...eTest.java => ValidationServiceTests.java} | 20 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) rename org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/{ValidationServiceTest.java => ValidationServiceTests.java} (95%) diff --git a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java index 6f412397d..44e2544a9 100644 --- a/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java +++ b/org.hl7.fhir.validation/src/main/java/org/hl7/fhir/validation/cli/services/ValidationService.java @@ -489,7 +489,7 @@ public class ValidationService { if (sessionId != null) { System.out.println("No such cached session exists for session id " + sessionId + ", re-instantiating validator."); } - + sessionCache.cleanUp(); ValidationEngine validationEngine = getValidationEngineFromCliContext(cliContext, definitions, tt); sessionId = sessionCache.cacheSession(validationEngine); System.out.println("Cached new session. Cache size = " + sessionCache.getSessionIds().size()); diff --git a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTest.java b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTests.java similarity index 95% rename from org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTest.java rename to org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTests.java index e03665c79..6a39e7320 100644 --- a/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTest.java +++ b/org.hl7.fhir.validation/src/test/java/org/hl7/fhir/validation/cli/services/ValidationServiceTests.java @@ -38,14 +38,13 @@ import org.hl7.fhir.validation.ValidationEngine; import org.hl7.fhir.validation.cli.model.CliContext; import org.hl7.fhir.validation.cli.model.FileInfo; import org.hl7.fhir.validation.cli.model.ValidationRequest; -import org.hl7.fhir.validation.cli.model.ValidationResponse; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.ArgumentMatchers; import org.mockito.Mockito; -class ValidationServiceTest { +class ValidationServiceTests { final String DUMMY_SOURCE = "dummySource"; final String DUMMY_SOURCE1 = "dummySource1"; @@ -60,22 +59,25 @@ class ValidationServiceTest { void validationSessionTest() throws Exception { TestingUtilities.injectCorePackageLoader(); SessionCache sessionCache = Mockito.spy(new PassiveExpiringSessionCache()); - ValidationService myService = new ValidationService(sessionCache); + ValidationService myService = Mockito.spy(new ValidationService(sessionCache)); List filesToValidate = getFilesToValidate(); - ValidationRequest request = new ValidationRequest().setCliContext(new CliContext().setTxServer(FhirSettings.getTxFhirDevelopment()).setTxCache(getTerminologyCacheDirectory("validationService"))).setFilesToValidate(filesToValidate); + ValidationRequest request = new ValidationRequest().setCliContext(new CliContext().setTxServer(FhirSettings.getTxFhirDevelopment()).setTxCache(getTerminologyCacheDirectory("validationService")).setSv("4.0.1")).setFilesToValidate(filesToValidate); // Validation run 1...nothing cached yet myService.validateSources(request); verify(sessionCache, Mockito.times(1)).cacheSession(ArgumentMatchers.any(ValidationEngine.class)); - + verify(sessionCache, Mockito.times(1)).cleanUp(); + verify(myService, Mockito.times(1)).buildValidationEngine(any(), any(), any()); Set sessionIds = sessionCache.getSessionIds(); if (sessionIds.stream().findFirst().isPresent()) { // Verify that after 1 run there is only one entry within the cache Assertions.assertEquals(1, sessionIds.size()); - myService.validateSources(request); - // Verify that the cache has been called on once with the id created in the first run - verify(sessionCache, Mockito.times(1)).fetchSessionValidatorEngine(sessionIds.stream().findFirst().get()); + myService.validateSources(request.setSessionId(sessionIds.stream().findFirst().get())); + // Verify that the cache has been called on twice with the id created in the first run + verify(sessionCache, Mockito.times(2)).fetchSessionValidatorEngine(sessionIds.stream().findFirst().get()); + verify(sessionCache, Mockito.times(1)).cleanUp(); + verify(myService, Mockito.times(1)).buildValidationEngine(any(), any(), any()); } else { // If no sessions exist within the cache after a run, we auto-fail. fail(); @@ -95,7 +97,7 @@ class ValidationServiceTest { { final List filesToValidate = getFilesToValidate(); - final ValidationRequest request = new ValidationRequest().setCliContext(new CliContext()).setFilesToValidate(filesToValidate); + final ValidationRequest request = new ValidationRequest().setCliContext(new CliContext().setSv("4.0.1")).setFilesToValidate(filesToValidate); myService.validateSources(request); verify(myService, Mockito.times(0)).getBaseEngine("myDummyKey");