Skip to content

Commit

Permalink
Add condition to handle case when running RS locally (#1139)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
basiliskus authored Jun 11, 2024
1 parent d1e7b40 commit 3d6b742
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
13 changes: 1 addition & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion e2e-execute.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Map<HttpEndpoint, Function<DomainRequest, DomainResponse>> 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());
Expand All @@ -131,11 +131,12 @@ public Map<HttpEndpoint, Function<DomainRequest, DomainResponse>> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,28 @@ class ApplicationContextTest extends Specification {
}

def "returns an environmental status"() {

when:
def environmentStatus = ApplicationContext.getEnvironment()

then:
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
Expand Down

0 comments on commit 3d6b742

Please sign in to comment.