Skip to content

Commit

Permalink
Updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya-07 committed Oct 3, 2024
1 parent 08105c0 commit 38fe768
Show file tree
Hide file tree
Showing 2 changed files with 240 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

package com.google.android.fhir.workflow.activity.resource.event

import com.google.android.fhir.workflow.activity.resource.event.EventStatus.COMPLETED
import com.google.android.fhir.workflow.activity.resource.event.EventStatus.ENTEREDINERROR
import com.google.android.fhir.workflow.activity.resource.event.EventStatus.INPROGRESS
import com.google.android.fhir.workflow.activity.resource.event.EventStatus.NOTDONE
import com.google.android.fhir.workflow.activity.resource.event.EventStatus.ONHOLD
import com.google.android.fhir.workflow.activity.resource.event.EventStatus.OTHER
import com.google.android.fhir.workflow.activity.resource.event.EventStatus.PREPARATION
import com.google.android.fhir.workflow.activity.resource.event.EventStatus.STOPPED
import com.google.android.fhir.workflow.activity.resource.event.EventStatus.UNKNOWN

/**
* Since event resources may have different code for same status, each [CPGEventResource] should
Expand All @@ -39,12 +44,12 @@ internal open class EventStatusCodeMapperImpl : EventStatusCodeMapper {
"preparation" -> PREPARATION
"in-progress" -> INPROGRESS
"not-done" -> NOTDONE
"on-hold" -> EventStatus.ONHOLD
"completed" -> EventStatus.COMPLETED
"entered-in-error" -> EventStatus.ENTEREDINERROR
"on-hold" -> ONHOLD
"completed" -> COMPLETED
"entered-in-error" -> ENTEREDINERROR
"stopped" -> STOPPED
"unknown" -> EventStatus.UNKNOWN
else -> EventStatus.OTHER(code)
"unknown" -> UNKNOWN
else -> OTHER(code)
}
}

Expand All @@ -53,12 +58,12 @@ internal open class EventStatusCodeMapperImpl : EventStatusCodeMapper {
PREPARATION -> "preparation"
INPROGRESS -> "in-progress"
NOTDONE -> "not-done"
EventStatus.ONHOLD -> "on-hold"
EventStatus.COMPLETED -> "completed"
EventStatus.ENTEREDINERROR -> "entered-in-error"
ONHOLD -> "on-hold"
COMPLETED -> "completed"
ENTEREDINERROR -> "entered-in-error"
STOPPED -> "stopped"
EventStatus.UNKNOWN -> "unknown"
is EventStatus.OTHER -> status.code
UNKNOWN -> "unknown"
is OTHER -> status.code
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,229 @@ class ActivityFlowTest {

assertThat(flow.getCurrentPhase()).isInstanceOf(OrderPhase::class.java)
}

@Test
fun `initiatePlan should move the flow to plan phase when correct prepared plan is provided`() =
runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

val flow = ActivityFlow.of(repository, cpgCommunicationRequest)

assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)

val preparePlan = flow.preparePlan()
assertThat(preparePlan.isSuccess).isTrue()

val initiatedPlan =
preparePlan.getOrThrow().let {
it.setStatus(Status.ACTIVE)
flow.initiatePlan(it)
}

assertThat(initiatedPlan.isSuccess).isTrue()
assertThat(initiatedPlan.getOrThrow().getPhaseName()).isEqualTo(Phase.PhaseName.PLAN)
}

@Test
fun `initiatePlan should fail when provided plan when corrupted prepared plan is provided`() =
runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

val flow = ActivityFlow.of(repository, cpgCommunicationRequest)

assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)

val preparePlan = flow.preparePlan()
assertThat(preparePlan.isSuccess).isTrue()

val preparedPlanResource = preparePlan.getOrThrow()
preparedPlanResource.let {
it.setStatus(Status.ACTIVE)
it.resource.basedOn.last().apply { this.reference = "" }
}
val initiatedPlan = preparePlan.getOrThrow().let { flow.initiatePlan(it) }

assertThat(initiatedPlan.isFailure).isTrue()
// check that the flow is still in old phase (proposal).
assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)
}

@Test
fun `initiateOrder should move the flow to order phase when correct prepared order is provided`() =
runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

val flow = ActivityFlow.of(repository, cpgCommunicationRequest)

assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)

val prepareOrder = flow.prepareOrder()
assertThat(prepareOrder.isSuccess).isTrue()

val initiatedOrder =
prepareOrder.getOrThrow().let {
it.setStatus(Status.ACTIVE)
flow.initiateOrder(it)
}

assertThat(initiatedOrder.isSuccess).isTrue()
assertThat(initiatedOrder.getOrThrow().getPhaseName()).isEqualTo(Phase.PhaseName.ORDER)
}

@Test
fun `initiateOrder should fail when provided order when corrupted prepared order is provided`() =
runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

val flow = ActivityFlow.of(repository, cpgCommunicationRequest)

assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)

val prepareOrder = flow.prepareOrder()
assertThat(prepareOrder.isSuccess).isTrue()

val preparedPlanResource = prepareOrder.getOrThrow()
preparedPlanResource.let {
it.setStatus(Status.ACTIVE)
it.resource.basedOn.last().apply { this.reference = "" }
}
val initiatedOrder = prepareOrder.getOrThrow().let { flow.initiateOrder(it) }

assertThat(initiatedOrder.isFailure).isTrue()
// check that the flow is still in old phase (proposal).
assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)
}

@Test
fun `initiatePerform should move the flow to perform phase when correct prepared event is provided`() =
runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

val flow = ActivityFlow.of(repository, cpgCommunicationRequest)

assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)

val preparePerform = flow.preparePerform(CPGCommunicationEvent::class.java)
assertThat(preparePerform.isSuccess).isTrue()

val preparedEvent = preparePerform.getOrThrow()
preparedEvent.let { it.setStatus(EventStatus.INPROGRESS) }
val initiatedPerform = preparePerform.getOrThrow().let { flow.initiatePerform(it) }
assertThat(initiatedPerform.isSuccess).isTrue()
assertThat(initiatedPerform.getOrThrow().getPhaseName()).isEqualTo(Phase.PhaseName.PERFORM)
}

@Test
fun `initiatePerform should fail when corrupted prepared event is provided`() =
runBlockingOnWorkerThread {
val cpgCommunicationRequest =
CPGRequestResource.of(
CommunicationRequest().apply {
id = "com-req-01"
status = CommunicationRequest.CommunicationRequestStatus.ACTIVE
subject = Reference("Patient/pat-01")
meta.addProfile(
"http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest",
)

addPayload().apply { content = StringType("Proposal") }
},
)
.apply { setIntent(Intent.PROPOSAL) }
val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine)
repository.create(cpgCommunicationRequest.resource)

val flow = ActivityFlow.of(repository, cpgCommunicationRequest)

assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)

val preparePerform = flow.preparePerform(CPGCommunicationEvent::class.java)
assertThat(preparePerform.isSuccess).isTrue()

val preparedEvent = preparePerform.getOrThrow()
preparedEvent.let {
it.setStatus(EventStatus.INPROGRESS)
it.resource.basedOn.last().apply { this.reference = "" }
}
val initiatedPerform = preparePerform.getOrThrow().let { flow.initiatePerform(it) }

assertThat(initiatedPerform.isFailure).isTrue()
// check that the flow is still in old phase (proposal).
assertThat(flow.getCurrentPhase().getPhaseName()).isEqualTo(Phase.PhaseName.PROPOSAL)
}
}

0 comments on commit 38fe768

Please sign in to comment.