diff --git a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/AddRelationshipTest.kt b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/AddRelationshipTest.kt new file mode 100644 index 00000000000..dc78c6c3013 --- /dev/null +++ b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/AddRelationshipTest.kt @@ -0,0 +1,82 @@ +package org.dhis2.tracker.relationships.domain + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.test.runTest +import org.dhis2.commons.viewmodel.DispatcherProvider +import org.dhis2.tracker.relationships.data.RelationshipsRepository +import org.dhis2.tracker.relationships.model.RelationshipDirection +import org.hisp.dhis.android.core.relationship.Relationship +import org.junit.Before +import org.junit.Test +import org.mockito.kotlin.doReturn +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever + +class AddRelationshipTest { + + private lateinit var addRelationship: AddRelationship + + private val dispatcherProvider: DispatcherProvider = mock { + on { io() } doReturn Dispatchers.Unconfined + } + private val repository: RelationshipsRepository = mock() + + @Before + fun setup() { + addRelationship = AddRelationship( + dispatcher = dispatcherProvider, + repository = repository + ) + } + + @Test + fun shouldAddRelationship() = runTest { + // Given user tries to add a relationship + val selectedTeiUid = "selectedTeiUid" + val relationshipTypeUid = "relationshipTypeUid" + val direction = RelationshipDirection.TO + + val relationship = Relationship.builder() + .uid("relationshipUid") + .build() + + // When + whenever( + repository.createRelationship(selectedTeiUid, relationshipTypeUid, direction) + ) doReturn relationship + whenever( + repository.addRelationship(relationship) + ) doReturn Result.success("relationshipUid") + + val result = addRelationship(selectedTeiUid, relationshipTypeUid, direction) + + // Then + assert(result.isSuccess) + } + + @Test + fun shouldFailWhenAddRelationship() = runTest { + // Given user tries to add a relationship + val selectedTeiUid = "selectedTeiUid" + val relationshipTypeUid = "relationshipTypeUid" + val direction = RelationshipDirection.TO + + val relationship = Relationship.builder() + .uid("relationshipUid") + .build() + + // When + whenever( + repository.createRelationship(selectedTeiUid, relationshipTypeUid, direction) + ) doReturn relationship + whenever( + repository.addRelationship(relationship) + ) doReturn Result.failure(Exception("Failed to add relationship")) + + val result = addRelationship(selectedTeiUid, relationshipTypeUid, direction) + + // Then there is an error when adding relationship + assert(result.isFailure) + assert(result.exceptionOrNull()?.message == "Failed to add relationship") + } +} \ No newline at end of file diff --git a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt index 57e14b07725..7510fe1584e 100644 --- a/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt +++ b/tracker/src/test/kotlin/org/dhis2/tracker/relationships/domain/GetRelationshipsByTypeTest.kt @@ -28,7 +28,7 @@ import java.util.Date class GetRelationshipsByTypeTest { - lateinit var getRelationshipsByType: GetRelationshipsByType + private lateinit var getRelationshipsByType: GetRelationshipsByType private val relationshipsRepository: RelationshipsRepository = mock() private val dateLabelProvider: DateLabelProvider = mock()