Skip to content

Commit

Permalink
Merge pull request #2071 from dhis2/androsdk-1787-2
Browse files Browse the repository at this point in the history
defect: [ANDROSDK-1787-2] DataSetInstanceService: fix isExpired and isClosed logic
  • Loading branch information
vgarciabnz authored Nov 16, 2023
2 parents e2f6cbe + b760c61 commit 6ce7d69
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,48 @@

package org.hisp.dhis.android.testapp.dataset

import com.google.common.truth.Truth
import com.google.common.truth.Truth.assertThat
import org.hisp.dhis.android.core.dataset.DataSetEditableStatus
import org.hisp.dhis.android.core.dataset.DataSetNonEditableReason
import org.hisp.dhis.android.core.period.PeriodType
import org.hisp.dhis.android.core.utils.integration.mock.BaseMockIntegrationTestFullDispatcher
import org.junit.Test
import java.util.Date

class DataSetInstanceServiceMockIntegrationShould :
BaseMockIntegrationTestFullDispatcher() {

@Test
fun do_not_allow_edit_expired_periods() {
val dataSetInstances = d2.dataSetModule().dataSetInstanceService()
val status = d2.dataSetModule().dataSetInstanceService()
.blockingGetEditableStatus(
"lyLU2wR22tC",
"201206",
"DiszpKrYNg8",
"bRowv6yZOF2",
)

Truth.assertThat(dataSetInstances).isInstanceOf(
assertThat(status).isInstanceOf(
DataSetEditableStatus.NonEditable(DataSetNonEditableReason.EXPIRED)::class.java,
)
}

@Test
fun do_not_allow_edit_closed_periods() {
val dataSetInstances = d2.dataSetModule().dataSetInstanceService()
val fivePeriodsInFuture = d2.periodModule().periodHelper().blockingGetPeriodForPeriodTypeAndDate(
PeriodType.Monthly,
Date(),
5,
)
val status = d2.dataSetModule().dataSetInstanceService()
.blockingGetEditableStatus(
"lyLU2wR22tC",
"202311",
fivePeriodsInFuture.periodId()!!,
"DiszpKrYNg8",
"bRowv6yZOF2",
)

Truth.assertThat(dataSetInstances).isInstanceOf(
assertThat(status).isInstanceOf(
DataSetEditableStatus.NonEditable(DataSetNonEditableReason.CLOSED)::class.java,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,26 +139,31 @@ internal class DataSetInstanceServiceImpl(
}

internal fun blockingIsExpired(dataSet: DataSet, period: Period): Boolean {
val expiryDays = dataSet.expiryDays() ?: return false
val generatedPeriod = period.endDate()?.let { endDate ->
periodGenerator.generatePeriod(
periodType = PeriodType.Daily,
date = endDate,
offset = expiryDays - 1,
)
val expiryDays = dataSet.expiryDays()
return if (expiryDays == null || expiryDays <= 0) {
false
} else {
val expiryDate = period.endDate()?.let { endDate ->
periodGenerator.generatePeriod(
periodType = PeriodType.Daily,
date = endDate,
offset = expiryDays - 1,
)
}?.endDate()

expiryDate?.let { Date().after(it) } ?: false
}
return Date().after(generatedPeriod?.endDate())
}

internal fun blockingIsClosed(dataSet: DataSet, period: Period): Boolean {
val periodType = dataSet.periodType() ?: return true
val periodType = dataSet.periodType() ?: return false
val openFuturePeriods = dataSet.openFuturePeriods() ?: 0
val generatedPeriod = periodGenerator.generatePeriod(
val latestFuturePeriod = periodGenerator.generatePeriod(
periodType = periodType,
date = Date(),
offset = openFuturePeriods - 1,
)
return period.endDate()?.before(generatedPeriod?.endDate()) ?: true
return period.endDate()?.after(latestFuturePeriod?.endDate()) ?: false
}

internal fun blockingIsPeriodInOrgUnitRange(period: Period, orgUnitUid: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import kotlinx.datetime.Clock
import org.hisp.dhis.android.core.arch.helpers.AccessHelper
import org.hisp.dhis.android.core.arch.helpers.DateUtils
import org.hisp.dhis.android.core.category.CategoryOption
import org.hisp.dhis.android.core.category.CategoryOptionCollectionRepository
import org.hisp.dhis.android.core.category.CategoryOptionComboService
import org.hisp.dhis.android.core.common.BaseIdentifiableObject
import org.hisp.dhis.android.core.dataset.internal.DataSetInstanceServiceImpl
import org.hisp.dhis.android.core.organisationunit.OrganisationUnitService
import org.hisp.dhis.android.core.period.Period
Expand All @@ -63,10 +63,10 @@ class DataSetInstanceServiceShould {

private val dataSet: DataSet = mock()

private val firstJanuary = BaseIdentifiableObject.DATE_FORMAT.parse("2020-01-01T00:00:00.000")
private val thirdJanuary = BaseIdentifiableObject.DATE_FORMAT.parse("2020-01-03T00:00:00.000")
private val firstFebruary = BaseIdentifiableObject.DATE_FORMAT.parse("2020-02-01T00:00:00.000")
private val thirdFebruary = BaseIdentifiableObject.DATE_FORMAT.parse("2020-02-03T00:00:00.000")
private val firstJanuary = DateUtils.DATE_FORMAT.parse("2020-01-01T00:00:00.000")
private val thirdJanuary = DateUtils.DATE_FORMAT.parse("2020-01-03T00:00:00.000")
private val firstFebruary = DateUtils.DATE_FORMAT.parse("2020-02-01T00:00:00.000")
private val thirdFebruary = DateUtils.DATE_FORMAT.parse("2020-02-03T00:00:00.000")

private val firstPeriod: Period = mock(defaultAnswer = Mockito.RETURNS_DEEP_STUBS)
private val secondPeriod: Period = mock(defaultAnswer = Mockito.RETURNS_DEEP_STUBS)
Expand Down Expand Up @@ -156,7 +156,6 @@ class DataSetInstanceServiceShould {
@Test
fun `Should return false if dataSet is not closed`() {
whenever(dataSet.periodType()) doReturn PeriodType.Monthly
whenever(periodHelper.getPeriodForPeriodId(firstPeriodId).blockingGet()) doReturn secondPeriod
whenever(periodGenerator.generatePeriod(any(), any(), any())) doReturn firstPeriod

assertThat(dataSetInstanceService.blockingIsClosed(dataSet, firstPeriod)).isFalse()
Expand All @@ -165,16 +164,16 @@ class DataSetInstanceServiceShould {
@Test
fun `Should return true if dataSet is closed`() {
whenever(dataSet.periodType()) doReturn PeriodType.Monthly
whenever(periodHelper.getPeriodForPeriodId(firstPeriodId).blockingGet()) doReturn firstPeriod
whenever(periodGenerator.generatePeriod(any(), any(), any())) doReturn secondPeriod
whenever(periodGenerator.generatePeriod(any(), any(), any())) doReturn firstPeriod

assertThat(dataSetInstanceService.blockingIsClosed(dataSet, firstPeriod)).isTrue()
assertThat(dataSetInstanceService.blockingIsClosed(dataSet, secondPeriod)).isTrue()
}

@Test
fun `Should return true if dataSet is expired`() {
whenever(dataSet.periodType()) doReturn PeriodType.Daily
whenever(dataSet.openFuturePeriods()) doReturn 20
whenever(dataSet.expiryDays()) doReturn 5
whenever(periodHelper.getPeriodForPeriodId(any()).blockingGet()) doReturn firstPeriod
whenever(periodGenerator.generatePeriod(any(), any(), any())) doReturn firstPeriod

Expand All @@ -193,4 +192,13 @@ class DataSetInstanceServiceShould {

assertThat(dataSetInstanceService.blockingIsExpired(dataSet, firstPeriod)).isFalse()
}

@Test
fun `Should return false if expiry days is 0 or negative`() {
whenever(dataSet.expiryDays()) doReturn 0
assertThat(dataSetInstanceService.blockingIsExpired(dataSet, firstPeriod)).isFalse()

whenever(dataSet.expiryDays()) doReturn -15
assertThat(dataSetInstanceService.blockingIsExpired(dataSet, firstPeriod)).isFalse()
}
}

0 comments on commit 6ce7d69

Please sign in to comment.