From bf04e3290eb57035b99ca8a5ba46ebdd9c3c1a19 Mon Sep 17 00:00:00 2001 From: Ronnak Saxena Date: Thu, 7 Sep 2023 12:18:30 -0700 Subject: [PATCH] added unit test Signed-off-by: Ronnak Saxena --- .../rollup/util/RollupUtils.kt | 6 +- .../rollup/util/RollupUtilsTests.kt | 60 ++++++++++++++++++- 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtils.kt b/src/main/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtils.kt index 615af1a74..86e7342be 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtils.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtils.kt @@ -467,7 +467,7 @@ fun parseRollup(response: GetResponse, xContentRegistry: NamedXContentRegistry = return xcp.parseWithType(response.id, response.seqNo, response.primaryTerm, Rollup.Companion::parse) } -// Changes aggregations in search source builder to new original aggregation (Change query too?) +// Returns a SearchSourceBuilder with different aggregations but the rest of the properties are the same @Suppress("ComplexMethod") fun SearchSourceBuilder.changeAggregations(aggregationBuilderCollection: Collection): SearchSourceBuilder { val ssb = SearchSourceBuilder() @@ -501,12 +501,14 @@ fun SearchSourceBuilder.changeAggregations(aggregationBuilderCollection: Collect if (this.collapse() != null) ssb.collapse(this.collapse()) return ssb } - +@Suppress("MagicNumber") fun convertDateStringToEpochMillis(dateString: String): Long { val parts = dateString.split(" ") + require(parts.size == 2) { "Date in was not correct format" } val dateParts = parts[0].split("-") val timeParts = parts[1].split(":") + require((dateParts.size == 3 && timeParts.size == 3)) { "Date in was not correct format" } val year = dateParts[0].toInt() val month = dateParts[1].toInt() val day = dateParts[2].toInt() diff --git a/src/test/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtilsTests.kt b/src/test/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtilsTests.kt index face563b2..fdb5cece6 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtilsTests.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/rollup/util/RollupUtilsTests.kt @@ -31,8 +31,13 @@ import org.opensearch.indexmanagement.rollup.randomValueCount import org.opensearch.indexmanagement.transform.randomAggregationBuilder import org.opensearch.search.aggregations.metrics.AvgAggregationBuilder import org.opensearch.search.aggregations.metrics.ValueCountAggregationBuilder +import org.opensearch.search.builder.SearchSourceBuilder import org.opensearch.test.OpenSearchTestCase import org.opensearch.test.rest.OpenSearchRestTestCase +import java.time.ZoneId +import java.time.ZonedDateTime +import kotlin.test.assertFails +import kotlin.test.assertFailsWith class RollupUtilsTests : OpenSearchTestCase() { @@ -231,6 +236,59 @@ class RollupUtilsTests : OpenSearchTestCase() { } } fun `test changeAggregations`() { - /* add later */ + val ssb = SearchSourceBuilder() + val oldAggBuilder = randomAggregationBuilder() + ssb.aggregation(oldAggBuilder) + var newAgg = randomAggregationBuilder() + while (newAgg == oldAggBuilder) newAgg = randomAggregationBuilder() + val newSsb = ssb.changeAggregations(listOf(newAgg)) + assertNotEquals("Did not change search source builders aggregations :(", newSsb, ssb) + } + fun `test convertDateStringToEpochMillis`() { + // Check correct time format + val dateString = "2023-07-18 12:30:00" + val expectedMillis = 1689683400000L + val actualMillis = convertDateStringToEpochMillis(dateString) + assertEquals(expectedMillis, actualMillis) + + // Testing an invalid date format throws error + assertFails { convertDateStringToEpochMillis("invalid format") } + } + fun `test convertFixedIntervalStringToMs`() { + // Test ms + val msString = "5ms" + val expectedMs = 5L + assertEquals("ms conversion is wrong", convertFixedIntervalStringToMs(msString), expectedMs) + // Test s + val sString = "5s" + val expectedS = 5000L + assertEquals("ms conversion is wrong", convertFixedIntervalStringToMs(sString), expectedS) + // Test m + val mString = "3m" + val expectedM = 180000L + assertEquals("m conversion is wrong", convertFixedIntervalStringToMs(mString), expectedM) + // Test h + val hString = "2h" + val expectedH = 7200000L + assertEquals("h conversion is wrong", convertFixedIntervalStringToMs(hString), expectedH) + // Test d + val dString = "1d" + val expectedD = 86400000L + assertEquals("d conversion is wrong", convertFixedIntervalStringToMs(dString), expectedD) + // Test w + val wString = "1w" + val expectedW = 604800000L + assertEquals("w conversion is wrong", convertFixedIntervalStringToMs(wString), expectedW) + // Test error + val invalid = ";)" + assertFailsWith { + convertFixedIntervalStringToMs(invalid) + } + } + fun `test zonedDateTimeToMillis`() { + val zonedDateTime = ZonedDateTime.of(2023, 7, 18, 12, 30, 0, 0, ZoneId.of("UTC")) + val expectedMillis = 1689683400000L // ms since epoch of the zonedDateTime + val actualMillis = zonedDateTimeToMillis(zonedDateTime) + assertEquals(expectedMillis, actualMillis) } }