From 3d6b742456b089d37ef672fade6fd24ffd8a34f4 Mon Sep 17 00:00:00 2001 From: Basilio Bogado <541149+basiliskus@users.noreply.github.com> Date: Tue, 11 Jun 2024 07:19:33 -0700 Subject: [PATCH] Add condition to handle case when running RS locally (#1139) * Added condition to handle case when running RS locally * Simplified condition and inverted if statement * Removed unnecessary setup step and added step to generate .env file * Testing this change makes the e2e tests fail * Reverted change for testing * Flipped function return boolean for clarity * Added test coverage * Added test coverage * Removed test by mistake * Added by mistake --- README.md | 13 +--------- e2e-execute.sh | 2 +- .../etor/EtorDomainRegistration.java | 9 +++---- .../etor/EtorDomainRegistrationTest.groovy | 24 +++++++++++++++++++ .../context/ApplicationContext.java | 5 ++++ .../context/ApplicationContextTest.groovy | 16 ++++++++++++- 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index fa14542bd..3530ee603 100644 --- a/README.md +++ b/README.md @@ -293,18 +293,7 @@ For database documentation [go here](/docs/database.md) #### CDC-TI Setup 1. Checkout `main` branch for `CDCgov/trusted-intermediary` -2. Edit the `app/src/main/java/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistration.java` file and replace: - ```Java - if (ApplicationContext.getEnvironment().equalsIgnoreCase("local")) { - ApplicationContext.register(RSEndpointClient.class, MockRSEndpointClient.getInstance()); - } else { - ApplicationContext.register(RSEndpointClient.class, ReportStreamEndpointClient.getInstance()); - } - ``` - with: - ```Java - ApplicationContext.register(RSEndpointClient.class, ReportStreamEndpointClient.getInstance()); - ``` +2. Run `./generate_env.sh` to generate `.env` file with required environment variables 3. Run TI with `./gradlew clean app:run` #### ReportStream Setup diff --git a/e2e-execute.sh b/e2e-execute.sh index 9ed650b8c..fcfa9c1de 100755 --- a/e2e-execute.sh +++ b/e2e-execute.sh @@ -15,7 +15,7 @@ start_api() { JAR_NAME="app-all.jar" echo 'Starting API' - java -jar "${DIR}"/"${SUB_DIR}"/"${JAR_NAME}" > /dev/null & + REPORT_STREAM_URL_PREFIX= java -jar "${DIR}"/"${SUB_DIR}"/"${JAR_NAME}" > /dev/null & export API_PID="${!}" echo "API starting at PID ${API_PID}" diff --git a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistration.java b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistration.java index ce0f14a62..b888ab4dd 100644 --- a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistration.java +++ b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistration.java @@ -119,7 +119,7 @@ public Map> domainRegistra ApplicationContext.register(SendMessageHelper.class, SendMessageHelper.getInstance()); - if (ApplicationContext.getProperty("DB_URL") != null) { + if (ApplicationContext.isPropertyPresent("DB_URL")) { ApplicationContext.register(DbDao.class, PostgresDao.getInstance()); ApplicationContext.register( PartnerMetadataStorage.class, DatabasePartnerMetadataStorage.getInstance()); @@ -131,11 +131,12 @@ public Map> domainRegistra ApplicationContext.register( MessageLinkStorage.class, FileMessageLinkStorage.getInstance()); } - if (ApplicationContext.getEnvironment().equalsIgnoreCase("local")) { - ApplicationContext.register(RSEndpointClient.class, MockRSEndpointClient.getInstance()); - } else { + + if (ApplicationContext.isPropertyPresent("REPORT_STREAM_URL_PREFIX")) { ApplicationContext.register( RSEndpointClient.class, ReportStreamEndpointClient.getInstance()); + } else { + ApplicationContext.register(RSEndpointClient.class, MockRSEndpointClient.getInstance()); } return endpoints; diff --git a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistrationTest.groovy b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistrationTest.groovy index a36120936..7e669534f 100644 --- a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistrationTest.groovy +++ b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/etor/EtorDomainRegistrationTest.groovy @@ -2,6 +2,7 @@ package gov.hhs.cdc.trustedintermediary.etor import gov.hhs.cdc.trustedintermediary.OrderMock import gov.hhs.cdc.trustedintermediary.ResultMock +import gov.hhs.cdc.trustedintermediary.context.ApplicationContext import gov.hhs.cdc.trustedintermediary.context.TestApplicationContext import gov.hhs.cdc.trustedintermediary.domainconnector.DomainRequest import gov.hhs.cdc.trustedintermediary.domainconnector.DomainResponse @@ -22,6 +23,8 @@ import gov.hhs.cdc.trustedintermediary.etor.orders.SendOrderUseCase import gov.hhs.cdc.trustedintermediary.etor.results.ResultController import gov.hhs.cdc.trustedintermediary.etor.results.ResultResponse import gov.hhs.cdc.trustedintermediary.etor.results.SendResultUseCase +import gov.hhs.cdc.trustedintermediary.external.localfile.MockRSEndpointClient +import gov.hhs.cdc.trustedintermediary.external.reportstream.ReportStreamEndpointClient import gov.hhs.cdc.trustedintermediary.wrappers.FhirParseException import gov.hhs.cdc.trustedintermediary.wrappers.HapiFhir import gov.hhs.cdc.trustedintermediary.wrappers.Logger @@ -75,6 +78,27 @@ class EtorDomainRegistrationTest extends Specification { endpoints.get(consolidatedOrdersEndpoint) != null } + def "RSEndpointClient uses the right implementation depending if REPORT_STREAM_URL_PREFIX is present or not"() { + given: + def domainRegistration = new EtorDomainRegistration() + + when: + TestApplicationContext.addEnvironmentVariable("REPORT_STREAM_URL_PREFIX", "") + domainRegistration.domainRegistration() + def mockImplementation = ApplicationContext.getImplementation(RSEndpointClient.class) + + then: + MockRSEndpointClient.isInstance(mockImplementation) + + when: + TestApplicationContext.addEnvironmentVariable("REPORT_STREAM_URL_PREFIX", "something") + domainRegistration.domainRegistration() + def implementation = ApplicationContext.getImplementation(RSEndpointClient.class) + + then: + ReportStreamEndpointClient.isInstance(implementation) + } + def "has an OpenAPI specification"() { given: def domainRegistration = new EtorDomainRegistration() diff --git a/shared/src/main/java/gov/hhs/cdc/trustedintermediary/context/ApplicationContext.java b/shared/src/main/java/gov/hhs/cdc/trustedintermediary/context/ApplicationContext.java index 3f4504c9f..3ce4da00a 100644 --- a/shared/src/main/java/gov/hhs/cdc/trustedintermediary/context/ApplicationContext.java +++ b/shared/src/main/java/gov/hhs/cdc/trustedintermediary/context/ApplicationContext.java @@ -163,6 +163,11 @@ public static String getProperty(String key, String defaultValue) { return DotEnv.get(key, defaultValue); } + public static boolean isPropertyPresent(String key) { + String value = getProperty(key); + return value != null && !value.isBlank(); + } + public static String getEnvironment() { return getProperty("ENV", "local"); } diff --git a/shared/src/test/groovy/gov/hhs/cdc/trustedintermediary/context/ApplicationContextTest.groovy b/shared/src/test/groovy/gov/hhs/cdc/trustedintermediary/context/ApplicationContextTest.groovy index 48d0e59cc..aaceb6ec0 100644 --- a/shared/src/test/groovy/gov/hhs/cdc/trustedintermediary/context/ApplicationContextTest.groovy +++ b/shared/src/test/groovy/gov/hhs/cdc/trustedintermediary/context/ApplicationContextTest.groovy @@ -35,7 +35,6 @@ class ApplicationContextTest extends Specification { } def "returns an environmental status"() { - when: def environmentStatus = ApplicationContext.getEnvironment() @@ -43,6 +42,21 @@ class ApplicationContextTest extends Specification { environmentStatus == "local" } + def "isPropertyNullOrBlank returns true when property is null or empty"() { + when: + def isPresentWhenNull = ApplicationContext.isPropertyPresent("nonExistentProperty") + + then: + !isPresentWhenNull + + when: + TestApplicationContext.addEnvironmentVariable("emptyProperty", "") + def isPresentWhenEmpty = ApplicationContext.isPropertyPresent("emptyProperty") + + then: + !isPresentWhenEmpty + } + class InjectionDeclaringClass { @Inject private String aField