Skip to content

Commit

Permalink
Merge branch 'main' into story-672-no_rs_history_api
Browse files Browse the repository at this point in the history
  • Loading branch information
halprin committed Jan 10, 2024
2 parents c5ff444 + a21afb9 commit 2086f46
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class PartnerMetadataOrchestratorTest extends Specification {

def "getMetadata retrieves metadata successfully with the sender already filled"() {
given:
String receivedSubmissionId = "receivedSubmissionId"
def receivedSubmissionId = "receivedSubmissionId"
def metadata = new PartnerMetadata(receivedSubmissionId, "sentSubmissionId", "sender", "receiver", Instant.now(), "hash")

when:
Expand All @@ -221,6 +221,20 @@ class PartnerMetadataOrchestratorTest extends Specification {
0 * mockClient.requestHistoryEndpoint(_, _)
}

def "getMetadata retrieves metadata successfully when receiver is present and sentSubmissionId is missing"() {
given:
def receivedSubmissionId = "receivedSubmissionId"
def metadata = new PartnerMetadata(receivedSubmissionId, null, "sender", "receiver", Instant.now(), "hash")

when:
def result = PartnerMetadataOrchestrator.getInstance().getMetadata(receivedSubmissionId)

then:
result.isPresent()
result.get() == metadata
1 * mockPartnerMetadataStorage.readMetadata(receivedSubmissionId) >> Optional.of(metadata)
}

def "getMetadata gets receiver if missing from metadata"() {
given:
def receivedSubmissionId = "receivedSubmissionId"
Expand Down Expand Up @@ -251,13 +265,13 @@ class PartnerMetadataOrchestratorTest extends Specification {

def "getReceiverName returns correct receiver name from valid JSON response"() {
given:
String validJson = "{\"destinations\": [{\"organization_id\": \"org_id\", \"service\": \"service_name\"}]}"
def validJson = "{\"destinations\": [{\"organization_id\": \"org_id\", \"service\": \"service_name\"}]}"

TestApplicationContext.register(Formatter, Jackson.getInstance())
TestApplicationContext.injectRegisteredImplementations()

when:
String receiverName = PartnerMetadataOrchestrator.getInstance().getReceiverName(validJson)
def receiverName = PartnerMetadataOrchestrator.getInstance().getReceiverName(validJson)

then:
receiverName == "org_id.service_name"
Expand All @@ -269,42 +283,43 @@ class PartnerMetadataOrchestratorTest extends Specification {
TestApplicationContext.injectRegisteredImplementations()

when:
String invalidJson = "invalid JSON"
def invalidJson = "invalid JSON"
PartnerMetadataOrchestrator.getInstance().getReceiverName(invalidJson)

then:
thrown(FormatterProcessingException)

when:
String emptyJson = "{}"
def emptyJson = "{}"
PartnerMetadataOrchestrator.getInstance().getReceiverName(emptyJson)

then:
thrown(FormatterProcessingException)

when:
String jsonWithoutDestinations = "{\"someotherkey\": \"value\"}"
def jsonWithoutDestinations = "{\"someotherkey\": \"value\"}"
PartnerMetadataOrchestrator.getInstance().getReceiverName(jsonWithoutDestinations)

then:
thrown(FormatterProcessingException)

when:
String jsonWithEmptyDestinations = "{\"destinations\": []}"

def jsonWithEmptyDestinations = "{\"destinations\": []}"
def receiverName = PartnerMetadataOrchestrator.getInstance().getReceiverName(jsonWithEmptyDestinations)

then:
receiverName == null

when:
String jsonWithoutOrgId = "{\"destinations\":[{\"service\":\"service\"}]}"
def jsonWithoutOrgId = "{\"destinations\":[{\"service\":\"service\"}]}"
PartnerMetadataOrchestrator.getInstance().getReceiverName(jsonWithoutOrgId)

then:
thrown(FormatterProcessingException)

when:
String jsonWithoutService = "{\"destinations\":[{\"organization_id\":\"org_id\"}]}"
def jsonWithoutService = "{\"destinations\":[{\"organization_id\":\"org_id\"}]}"
PartnerMetadataOrchestrator.getInstance().getReceiverName(jsonWithoutService)

then:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,19 @@ class SendOrderUsecaseTest extends Specification {
1 * mockSender.sendOrder(omlOrder) >> Optional.of("sentId")
1 * mockLogger.logError(_, partnerMetadataException)
}

def "convertAndSend logs event when submissionId is null"() {
given:
def mockOrder = Mock(Order)
TestApplicationContext.injectRegisteredImplementations()

mockSender.sendOrder(_) >> Optional.empty()

when:
SendOrderUseCase.getInstance().convertAndSend(mockOrder, "receivedId")

then:
1 * mockLogger.logWarning(_)
0 * mockOrchestrator.updateMetadataForSentOrder(_ as String, _ as String)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package gov.hhs.cdc.trustedintermediary.external.database

import gov.hhs.cdc.trustedintermediary.context.TestApplicationContext
import gov.hhs.cdc.trustedintermediary.etor.metadata.PartnerMetadata
import gov.hhs.cdc.trustedintermediary.etor.metadata.PartnerMetadataException
import gov.hhs.cdc.trustedintermediary.etor.metadata.PartnerMetadataStorage
import gov.hhs.cdc.trustedintermediary.wrappers.DbDao
import spock.lang.Specification

import java.sql.SQLException
import java.time.Instant

class DatabasePartnerMetadataStorageTest extends Specification {

private def mockDao

def setup() {
TestApplicationContext.reset()
TestApplicationContext.init()

mockDao = Mock(DbDao)

TestApplicationContext.register(DbDao, mockDao)
TestApplicationContext.register(PartnerMetadataStorage, DatabasePartnerMetadataStorage.getInstance())
TestApplicationContext.injectRegisteredImplementations()
}

def "readMetadata happy path works"() {
given:
def receivedSubmissionId = "receivedSubmissionId"
def mockMetadata = new PartnerMetadata(receivedSubmissionId, "sentSubmissionId", "sender", "receiver", Instant.now(), "hash")
def expectedResult = Optional.of(mockMetadata)

mockDao.fetchMetadata(_ as String) >> mockMetadata

when:
def actualResult = DatabasePartnerMetadataStorage.getInstance().readMetadata(receivedSubmissionId)

then:
actualResult == expectedResult
}

def "readMetadata unhappy path works"() {
given:
def receivedSubmissionId = "receivedSubmissionId"
mockDao.fetchMetadata(_ as String) >> { throw new SQLException("Something went wrong!") }

when:
DatabasePartnerMetadataStorage.getInstance().readMetadata(receivedSubmissionId)

then:
thrown(PartnerMetadataException)
}

def "saveMetadata happy path works"() {
given:
def receivedSubmissionId = "receivedSubmissionId"
def mockMetadata = new PartnerMetadata(
receivedSubmissionId,
"sentSubmissionId",
"sender",
"receiver",
Instant.now(),
"hash"
)

when:
DatabasePartnerMetadataStorage.getInstance().saveMetadata(mockMetadata)

then:
1 * mockDao.upsertMetadata(
mockMetadata.receivedSubmissionId(),
mockMetadata.sentSubmissionId(),
mockMetadata.sender(),
mockMetadata.receiver(),
mockMetadata.hash(),
mockMetadata.timeReceived()
)
}

def "saveMetadata unhappy path works"() {
given:
def receivedSubmissionId = "receivedSubmissionId"
def mockMetadata = new PartnerMetadata(
receivedSubmissionId,
"sentSubmissionId",
"sender",
"receiver",
Instant.now(),
"hash"
)

mockDao.upsertMetadata(
mockMetadata.receivedSubmissionId(),
mockMetadata.sentSubmissionId(),
mockMetadata.sender(),
mockMetadata.receiver(),
mockMetadata.hash(),
mockMetadata.timeReceived()
) >> { throw new SQLException("Something went wrong!") }

when:
DatabasePartnerMetadataStorage.getInstance().saveMetadata(mockMetadata)

then:
thrown(PartnerMetadataException)
}
}

0 comments on commit 2086f46

Please sign in to comment.