From bae5bd857fe86e159191f06cbe5a77e4cc2b812d Mon Sep 17 00:00:00 2001 From: Rohit Kandimalla Date: Fri, 15 Mar 2024 11:34:34 -0400 Subject: [PATCH] MAT-6736 Added Caching at Service level for Manifest list --- .../TerminologyServiceApplication.java | 2 + .../controller/VsacController.java | 4 +- .../service/FhirTerminologyService.java | 2 + .../service/FhirTerminologyServiceTest.java | 160 +++++++++--------- .../FhirTerminologyServiceWebClientTest.java | 10 +- 5 files changed, 94 insertions(+), 84 deletions(-) diff --git a/src/main/java/gov/cms/madie/terminology/TerminologyServiceApplication.java b/src/main/java/gov/cms/madie/terminology/TerminologyServiceApplication.java index 0ed76d1..1bba35c 100644 --- a/src/main/java/gov/cms/madie/terminology/TerminologyServiceApplication.java +++ b/src/main/java/gov/cms/madie/terminology/TerminologyServiceApplication.java @@ -2,11 +2,13 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @SpringBootApplication +@EnableCaching public class TerminologyServiceApplication { public static void main(String[] args) { diff --git a/src/main/java/gov/cms/madie/terminology/controller/VsacController.java b/src/main/java/gov/cms/madie/terminology/controller/VsacController.java index 50ac8e2..7851856 100644 --- a/src/main/java/gov/cms/madie/terminology/controller/VsacController.java +++ b/src/main/java/gov/cms/madie/terminology/controller/VsacController.java @@ -149,7 +149,9 @@ public ResponseEntity> getManifests(Principal principal) if (umlsUser.isPresent() && !StringUtils.isBlank(umlsUser.get().getApiKey())) { return ResponseEntity.ok().body(fhirTerminologyService.getManifests(umlsUser.get())); } - log.error("Unable to Retrieve List of available manifests, UMLS Authentication Key Not found for user : [{}}]", username); + log.error( + "Unable to Retrieve List of available manifests, UMLS Authentication Key Not found for user : [{}}]", + username); throw new VsacUnauthorizedException("Please login to UMLS before proceeding"); } } diff --git a/src/main/java/gov/cms/madie/terminology/service/FhirTerminologyService.java b/src/main/java/gov/cms/madie/terminology/service/FhirTerminologyService.java index 9c159a1..4641215 100644 --- a/src/main/java/gov/cms/madie/terminology/service/FhirTerminologyService.java +++ b/src/main/java/gov/cms/madie/terminology/service/FhirTerminologyService.java @@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.hl7.fhir.r4.model.Bundle; +import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import java.util.ArrayList; @@ -20,6 +21,7 @@ public class FhirTerminologyService { FhirContext ctx = FhirContext.forR4(); private final FhirTerminologyServiceWebClient fhirTerminologyServiceWebClient; + @Cacheable("manifest-list") public List getManifests(UmlsUser umlsUser) { IParser parser = ctx.newJsonParser(); String responseString = fhirTerminologyServiceWebClient.getManifestBundle(umlsUser.getApiKey()); diff --git a/src/test/java/gov/cms/madie/terminology/service/FhirTerminologyServiceTest.java b/src/test/java/gov/cms/madie/terminology/service/FhirTerminologyServiceTest.java index cfe01c1..959d3ec 100644 --- a/src/test/java/gov/cms/madie/terminology/service/FhirTerminologyServiceTest.java +++ b/src/test/java/gov/cms/madie/terminology/service/FhirTerminologyServiceTest.java @@ -15,86 +15,90 @@ @ExtendWith(MockitoExtension.class) class FhirTerminologyServiceTest { - @Mock - FhirTerminologyServiceWebClient fhirTerminologyServiceWebClient; + @Mock FhirTerminologyServiceWebClient fhirTerminologyServiceWebClient; - @InjectMocks FhirTerminologyService fhirTerminologyService; - private UmlsUser umlsUser; - private static final String TEST_HARP_ID = "te$tHarpId"; - private static final String TEST_API_KEY = "te$tKey"; + @InjectMocks FhirTerminologyService fhirTerminologyService; + private UmlsUser umlsUser; + private static final String TEST_HARP_ID = "te$tHarpId"; + private static final String TEST_API_KEY = "te$tKey"; - private final String responseFromServer = """ - { - "resourceType": "Bundle", - "id": "library-search", - "meta": - { - "lastUpdated": "2024-03-14T14:04:52.456-04:00" - }, - "type": "searchset", - "total": 25, - "link": - [ - { - "relation": "self", - "url": "https://uat-cts.nlm.nih.gov/fhir/Library" - } - ], - "entry": - [ - { - "fullUrl": "http://cts.nlm.nih.gov/fhir/Library/ecqm-update-4q2017-eh", - "resource": + private final String responseFromServer = + """ { - "resourceType": "Library", - "id": "ecqm-update-4q2017-eh", - "meta": - { - "profile": - [ - "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/publishable-library-cqfm", - "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/quality-program-cqfm" - ] - }, - "url": "http://cts.nlm.nih.gov/fhir/Library/ecqm-update-4q2017-eh", - "version": "2017-09-15", - "status": "active" - } - }, - { - "fullUrl": "http://cts.nlm.nih.gov/fhir/Library/mu2-update-2012-10-25", - "resource": - { - "resourceType": "Library", - "id": "mu2-update-2012-10-25", - "meta": - { - "profile": - [ - "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/publishable-library-cqfm", - "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/quality-program-cqfm" - ] - }, - "url": "http://cts.nlm.nih.gov/fhir/Library/mu2-update-2012-10-25", - "version": "2012-10-25", - "status": "active" - } - } - ] - }"""; + "resourceType": "Bundle", + "id": "library-search", + "meta": + { + "lastUpdated": "2024-03-14T14:04:52.456-04:00" + }, + "type": "searchset", + "total": 25, + "link": + [ + { + "relation": "self", + "url": "https://uat-cts.nlm.nih.gov/fhir/Library" + } + ], + "entry": + [ + { + "fullUrl": "http://cts.nlm.nih.gov/fhir/Library/ecqm-update-4q2017-eh", + "resource": + { + "resourceType": "Library", + "id": "ecqm-update-4q2017-eh", + "meta": + { + "profile": + [ + "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/publishable-library-cqfm", + "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/quality-program-cqfm" + ] + }, + "url": "http://cts.nlm.nih.gov/fhir/Library/ecqm-update-4q2017-eh", + "version": "2017-09-15", + "status": "active" + } + }, + { + "fullUrl": "http://cts.nlm.nih.gov/fhir/Library/mu2-update-2012-10-25", + "resource": + { + "resourceType": "Library", + "id": "mu2-update-2012-10-25", + "meta": + { + "profile": + [ + "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/publishable-library-cqfm", + "http://hl7.org/fhir/us/cqfmeasures/StructureDefinition/quality-program-cqfm" + ] + }, + "url": "http://cts.nlm.nih.gov/fhir/Library/mu2-update-2012-10-25", + "version": "2012-10-25", + "status": "active" + } + } + ] + }"""; + + @BeforeEach + public void setUp() { + umlsUser = UmlsUser.builder().apiKey(TEST_API_KEY).harpId(TEST_HARP_ID).build(); + } - @BeforeEach - public void setUp() { - umlsUser = UmlsUser.builder().apiKey(TEST_API_KEY).harpId(TEST_HARP_ID).build(); - } - @Test - void getManifests() { - when(fhirTerminologyServiceWebClient.getManifestBundle(anyString())).thenReturn(responseFromServer); - var result = fhirTerminologyService.getManifests(umlsUser); - assertEquals(2, result.size()); - assertEquals("ecqm-update-4q2017-eh", result.get(0).getId()); - assertEquals("http://cts.nlm.nih.gov/fhir/Library/ecqm-update-4q2017-eh", result.get(0).getFullUrl()); - assertEquals("mu2-update-2012-10-25", result.get(1).getId()); - assertEquals("http://cts.nlm.nih.gov/fhir/Library/mu2-update-2012-10-25", result.get(1).getFullUrl()); - } + @Test + void getManifests() { + when(fhirTerminologyServiceWebClient.getManifestBundle(anyString())) + .thenReturn(responseFromServer); + var result = fhirTerminologyService.getManifests(umlsUser); + assertEquals(2, result.size()); + assertEquals("ecqm-update-4q2017-eh", result.get(0).getId()); + assertEquals( + "http://cts.nlm.nih.gov/fhir/Library/ecqm-update-4q2017-eh", result.get(0).getFullUrl()); + assertEquals("mu2-update-2012-10-25", result.get(1).getId()); + assertEquals( + "http://cts.nlm.nih.gov/fhir/Library/mu2-update-2012-10-25", result.get(1).getFullUrl()); + } } diff --git a/src/test/java/gov/cms/madie/terminology/webclient/FhirTerminologyServiceWebClientTest.java b/src/test/java/gov/cms/madie/terminology/webclient/FhirTerminologyServiceWebClientTest.java index 99979bb..b2aaa32 100644 --- a/src/test/java/gov/cms/madie/terminology/webclient/FhirTerminologyServiceWebClientTest.java +++ b/src/test/java/gov/cms/madie/terminology/webclient/FhirTerminologyServiceWebClientTest.java @@ -47,7 +47,7 @@ static void tearDown() throws IOException { void getManifestBundleSuccessfully() { mockBackEnd.enqueue( new MockResponse() - .setResponseCode(200) + .setResponseCode(200) .setBody(MOCK_RESPONSE_STRING) .addHeader("Content-Type", "application/fhir+json")); String actualResponse = fhirTerminologyServiceWebClient.getManifestBundle(MOCK_API_KEY); @@ -57,9 +57,9 @@ void getManifestBundleSuccessfully() { @Test void getManifestBundle_ReturnsException() { - mockBackEnd.enqueue( - new MockResponse() - .setResponseCode(HttpStatus.UNAUTHORIZED.value())); - assertThrows(WebClientResponseException.class, () -> fhirTerminologyServiceWebClient.getManifestBundle(MOCK_API_KEY)); + mockBackEnd.enqueue(new MockResponse().setResponseCode(HttpStatus.UNAUTHORIZED.value())); + assertThrows( + WebClientResponseException.class, + () -> fhirTerminologyServiceWebClient.getManifestBundle(MOCK_API_KEY)); } }