diff --git a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamOrderSender.java b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamOrderSender.java index 55bfd69b5..da91c64de 100644 --- a/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamOrderSender.java +++ b/etor/src/main/java/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamOrderSender.java @@ -47,10 +47,6 @@ public class ReportStreamOrderSender implements OrderSender { private String rsTokenCache; - protected synchronized String getRsTokenCache() { - return this.rsTokenCache; - } - protected synchronized void setRsTokenCache(String token) { this.rsTokenCache = token; } diff --git a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamOrderSenderTest.groovy b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamOrderSenderTest.groovy index 01b614f9f..b6b77f44c 100644 --- a/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamOrderSenderTest.groovy +++ b/etor/src/test/groovy/gov/hhs/cdc/trustedintermediary/external/reportstream/ReportStreamOrderSenderTest.groovy @@ -379,62 +379,52 @@ class ReportStreamOrderSenderTest extends Specification { def mockAuthEngine = Mock(AuthEngine) def mockFormatter = Mock(Formatter) def mockCache = Mock(Cache) + + mockCache.get(_ as String) >> "shouldn't be returned" + + //mock the auth engine so that the JWT looks like it is invalid + mockAuthEngine.getExpirationDate(_) >> LocalDateTime.now().plus(10, ChronoUnit.SECONDS) + + def freshTokenFromRs = "new token" + mockFormatter.convertJsonToObject(_, _ as TypeReference) >> Map.of("access_token", freshTokenFromRs) + TestApplicationContext.register(Formatter, mockFormatter) TestApplicationContext.register(AuthEngine, mockAuthEngine) TestApplicationContext.register(HttpClient, mockClient) TestApplicationContext.register(Cache, mockCache) - mockCache.get(_ as String) >> "shouldn't be returned" TestApplicationContext.register(Secrets, Mock(Secrets)) - TestApplicationContext.injectRegisteredImplementations() - - //mock the auth engine so that the JWT looks like it is invalid - mockAuthEngine.getExpirationDate(_) >> LocalDateTime.now().plus(10, ChronoUnit.SECONDS) - def tokenFromRS = "new token" - mockFormatter.convertJsonToObject(_, _ as TypeReference) >> Map.of("access_token", tokenFromRS) + TestApplicationContext.injectRegisteredImplementations() when: def token = ReportStreamOrderSender.getInstance().getRsToken() then: 1 * mockClient.post(_, _, _) - token == tokenFromRS + token == freshTokenFromRs } - def "getRsToken when cache token is valid"() { + def "getRsToken when cache token is valid, return that cached token"() { given: - def orderSender = ReportStreamOrderSender.getInstance() - orderSender.setRsTokenCache("valid Token") - TestApplicationContext.register(OrderSender, orderSender) + def mockAuthEngine = Mock(AuthEngine) + def mockCache = Mock(Cache) - def mockFormatter = Mock(Formatter) - mockFormatter.convertJsonToObject(_ as String, _ as TypeReference) >> Map.of("access_token", "fake token") - TestApplicationContext.register(Formatter, mockFormatter) + def cachedRsToken = "DogCow goes Moof!" + mockCache.get(_ as String) >> cachedRsToken - def mockLogFormatter = Mock(Formatter) - mockLogFormatter.convertJsonToObject(_ as String, _ as TypeReference) >> null - TestApplicationContext.register(Formatter, mockLogFormatter) + //mock the auth engine so that the JWT looks valid + mockAuthEngine.getExpirationDate(_) >> LocalDateTime.now().plus(60, ChronoUnit.SECONDS) - def mockAuthEngine = Mock(AuthEngine) - mockAuthEngine.generateSenderToken(_ as String, _ as String, _ as String, _ as String, 300) >> "fake token" - mockAuthEngine.getExpirationDate(_ as String) >> LocalDateTime.now().plus(25, ChronoUnit.SECONDS) TestApplicationContext.register(AuthEngine, mockAuthEngine) - - def mockClient = Mock(HttpClient) - mockClient.post(_ as String, _ as Map, _ as String) >> """{"foo":"foo value", "access_token":fake token, "boo":"boo value"}""" - TestApplicationContext.register(HttpClient, mockClient) - - def mockSecrets = Mock(Secrets) - mockSecrets.getKey(_ as String) >> "fakePrivateKey" - TestApplicationContext.register(Secrets, mockSecrets) + TestApplicationContext.register(Cache, mockCache) TestApplicationContext.injectRegisteredImplementations() when: - def token = orderSender.getRsToken() + def token = ReportStreamOrderSender.getInstance().getRsToken() then: - token == orderSender.getRsTokenCache() + token == cachedRsToken } def "logRsSubmissionId logs submissionId if convertJsonToObject is successful"() {