NTP-7179 | Narayan | Added test cases for common utils and extensions (#13593)

This commit is contained in:
Aditya Narayan Malik
2024-11-15 21:54:51 +05:30
committed by GitHub
parent daf3f55a19
commit 0732276ef0
2 changed files with 209 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
/*
*
* * Copyright © 2024 by Navi Technologies Limited
* * All rights reserved. Strictly confidential
*
*/
package com.navi.pay.common.utils
import com.navi.pay.common.utils.NaviPayCommonUtils.getDateTimeObjectFromEpochString
import com.navi.pay.common.utils.NaviPayCommonUtils.getNormalisedPhoneNumber
import com.navi.pay.common.utils.NaviPayCommonUtils.isCurrentDateGreaterOrEqualToOldDateMonthWise
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.junit.Assert.assertEquals
import org.junit.Test
class DateTimeObjectFromEpochStringUnitTest {
@Test
fun testGetDateTimeObjectFromEpochString() {
val dateTimeObject1Jan2023 =
getDateTimeObjectFromEpochString(epochMillis = 1672531200000L) // January 1, 2023
assertEquals(DateTime(2023, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC), dateTimeObject1Jan2023)
val dateTimeObject1Jan1960 =
getDateTimeObjectFromEpochString(epochMillis = -315619200000L) // January 1, 1960
assertEquals(DateTime(1960, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC), dateTimeObject1Jan1960)
val dateTimeObject1Jan1970 =
getDateTimeObjectFromEpochString(epochMillis = 0L) // January 1, 1970
assertEquals(DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC), dateTimeObject1Jan1970)
val dateTimeObject1Jan2100 =
getDateTimeObjectFromEpochString(epochMillis = 4102444800000L) // January 1, 2100
assertEquals(DateTime(2100, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC), dateTimeObject1Jan2100)
val dateTimeObject29Feb2020 =
getDateTimeObjectFromEpochString(epochMillis = 1582934400000L) // February 29, 2020
assertEquals(DateTime(2020, 2, 29, 0, 0, 0, 0, DateTimeZone.UTC), dateTimeObject29Feb2020)
val dateTimeObject15Nov2024 =
getDateTimeObjectFromEpochString(epochMillis = 1731628800000L) // November 15, 2024
assertEquals(DateTime(2024, 11, 15, 0, 0, 0, 0, DateTimeZone.UTC), dateTimeObject15Nov2024)
}
}
class CurrentDateGreaterThanOrEqualToOldDateMonthWiseUnitTest {
@Test
fun testIsCurrentDateGreaterOrEqualToOldDateMonthWise() {
// Same date
assertEquals(
true,
isCurrentDateGreaterOrEqualToOldDateMonthWise(
DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC),
DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC)
)
)
// Current date is later in the same month
assertEquals(
true,
isCurrentDateGreaterOrEqualToOldDateMonthWise(
DateTime(2024, 1, 15, 0, 0, 0, 0, DateTimeZone.UTC),
DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC)
)
)
// Current date is in a later month
assertEquals(
true,
isCurrentDateGreaterOrEqualToOldDateMonthWise(
DateTime(2024, 2, 1, 0, 0, 0, 0, DateTimeZone.UTC),
DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC)
)
)
// Old date is in a later month
assertEquals(
false,
isCurrentDateGreaterOrEqualToOldDateMonthWise(
DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC),
DateTime(2024, 2, 1, 0, 0, 0, 0, DateTimeZone.UTC)
)
)
// Current date is in a different year but earlier
assertEquals(
false,
isCurrentDateGreaterOrEqualToOldDateMonthWise(
DateTime(2023, 12, 31, 0, 0, 0, 0, DateTimeZone.UTC),
DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC)
)
)
// Current date is in a different year and later
assertEquals(
true,
isCurrentDateGreaterOrEqualToOldDateMonthWise(
DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeZone.UTC),
DateTime(2023, 12, 31, 0, 0, 0, 0, DateTimeZone.UTC)
)
)
// Boundary case - end of a month compared to start of the next month
assertEquals(
true,
isCurrentDateGreaterOrEqualToOldDateMonthWise(
DateTime(2024, 2, 1, 0, 0, 0, 0, DateTimeZone.UTC),
DateTime(2024, 1, 31, 0, 0, 0, 0, DateTimeZone.UTC)
)
)
// Exact month difference
assertEquals(
true,
isCurrentDateGreaterOrEqualToOldDateMonthWise(
DateTime(2024, 3, 1, 0, 0, 0, 0, DateTimeZone.UTC),
DateTime(2024, 2, 1, 0, 0, 0, 0, DateTimeZone.UTC)
)
)
}
}
class NormalisedPhoneNumberUnitTest {
@Test
fun phoneNumber_shouldBeNormalizedCorrectly() {
assertEquals("9876543210", getNormalisedPhoneNumber("9876543210"))
assertEquals("9876543210", getNormalisedPhoneNumber("+919876543210"))
assertEquals("9876543210", getNormalisedPhoneNumber("+91 9876543210"))
assertEquals("9876543210", getNormalisedPhoneNumber(" 9876543210"))
assertEquals("9876543210", getNormalisedPhoneNumber("Number : 9876543210"))
assertEquals("", getNormalisedPhoneNumber(null))
}
}

View File

@@ -350,3 +350,78 @@ class PhoneNumberWithoutCountryCodeUtilUnitTest {
assertEquals("", " ".phoneNumberWithoutCountryCode())
}
}
class DoubleRoundToUnitTest {
@Test
fun decimalPlaces_shouldBeRoundedCorrectly() {
assertEquals(1.0, 1.0.roundTo(2), 0.0)
assertEquals(1.0, 1.49.roundTo(0), 0.0)
assertEquals(2.0, 1.5.roundTo(0), 0.0)
assertEquals(-1.23, (-1.234).roundTo(2), 0.0)
assertEquals(-1.24, (-1.236).roundTo(2), 0.0)
assertEquals(12345.68, 12345.6789.roundTo(2), 0.0)
assertEquals(0.0, 0.0004.roundTo(3), 0.0)
assertEquals(0.001, 0.0014.roundTo(3), 0.0)
assertEquals(1.24, 1.235.roundTo(2), 0.0)
assertEquals(-1.24, (-1.235).roundTo(2), 0.0)
assertEquals(5.0, 5.0.roundTo(3), 0.0)
assertEquals(123457.0, 123456.789.roundTo(0), 0.0)
assertEquals(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY.roundTo(2), 0.0)
assertEquals(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY.roundTo(2), 0.0)
assertEquals(Double.NaN, Double.NaN.roundTo(2), 0.0)
}
}
class UpiIdValidationUnitTest {
@Test
fun checkUpiIdInput_isValidatedCorrectly() {
assertEquals(true, "1234567890@upi".isValidUpiId())
assertEquals(
true,
"-1234567890@UPI".isValidUpiId()
) // Can start with special char and can be case insensitive
assertEquals(true, "rohitsharma@upi.".isValidUpiId()) // Can contain . at the end
assertEquals(true, "narendra@modi@up@i.".isValidUpiId()) // Can contain multiple @
assertEquals(true, "#delhi&@paytm".isValidUpiId()) // Can contain special characters
assertEquals(true, "virat.kohli@upi".isValidUpiId()) // Can contain . in middle
assertEquals(false, "@naviaxis".isValidUpiId()) // Should not start with @
assertEquals(false, "msdhoni@".isValidUpiId()) // Should not end with @
assertEquals(false, "1234567890".isValidUpiId()) // Should contain @
assertEquals(false, "".isValidUpiId()) // Should not be empty
assertEquals(false, " ".isValidUpiId()) // Should not contain only whitespace
}
}
class UpiNumberValidationUnitTest {
@Test
fun checkUpiNumberInput_isValidatedCorrectly() {
assertEquals(true, "12345678".isValidUpiNumber()) // Could be 8 digits
assertEquals(true, "123456789".isValidUpiNumber()) // Could be 9 digits
assertEquals(true, "1234567890".isValidUpiNumber()) // Could be 10 digits
assertEquals(false, "1234567".isValidUpiNumber()) // Should not be less than 8 digits
assertEquals(false, "12345678901".isValidUpiNumber()) // Should not be more than 10 digits
assertEquals(false, "1234567a".isValidUpiNumber()) // Should not contain alphabets
assertEquals(false, "".isValidUpiNumber()) // Should not be empty
assertEquals(false, "abcdefghij".isValidUpiNumber()) // Should not contain only alphabets
assertEquals(false, "123 56789".isValidUpiNumber()) // Should not contain whitespace
}
}
class SearchQueryValidationUnitTest {
@Test
fun checkSearchQueryInput_isValidatedCorrectly() {
assertEquals(true, "123".isValidSearchQuery())
assertEquals(true, "+123".isValidSearchQuery())
assertEquals(true, "0".isValidSearchQuery())
assertEquals(true, "+0".isValidSearchQuery())
assertEquals(true, "9876543210".isValidSearchQuery())
assertEquals(false, "12 34".isValidSearchQuery())
assertEquals(false, "123+".isValidSearchQuery())
assertEquals(false, "123.45".isValidSearchQuery())
assertEquals(false, "+123.45".isValidSearchQuery())
assertEquals(false, "-123".isValidSearchQuery())
assertEquals(false, "+".isValidSearchQuery())
assertEquals(false, "".isValidSearchQuery())
assertEquals(false, " ".isValidSearchQuery())
}
}