From e885fec651976c3ba4a5b60538ea6c9a267d99d8 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 12:57:10 +0530 Subject: [PATCH 01/21] Refactor first name and last name detection of customer logic --- .../android/fluxc/model/customer/WCCustomerMapper.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt index a47f53af1d..100b319c67 100644 --- a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt +++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt @@ -117,6 +117,7 @@ class WCCustomerMapper @Inject constructor() { } } - private fun String?.firstNameFromName() = this?.split(" ")?.firstOrNull() ?: "" - private fun String?.lastNameFromName() = this?.split(" ")?.lastOrNull() ?: "" + private fun String?.firstNameFromName() = this?.substringBefore(" ") ?: "" + + private fun String?.lastNameFromName() = this?.substringAfter(" ", "") ?: "" } From 0266ff201d82619cdbd2525287dbffde61fb2594 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 12:58:32 +0530 Subject: [PATCH 02/21] Add test to verify that proper first name is returned given customer name --- .../model/customer/WCCustomerMapperTest.kt | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 324fde5234..a823419e5d 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -273,4 +273,22 @@ class WCCustomerMapperTest { // THEN assertThat(result.isPayingCustomer).isEqualTo(false) } + + @Test + @Suppress("LongMethod") + fun `given customer name, then first name is properly assigned`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = "firstname with a very long last name") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + with(result) { + assertEquals("firstname", firstName) + } + } } From 8f355b01b07a55cde66410b4ff0ccd3c9b96776c Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 12:58:50 +0530 Subject: [PATCH 03/21] Remove unnecessary annotation --- .../android/fluxc/model/customer/WCCustomerMapperTest.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index a823419e5d..6fdb6444b5 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -275,7 +275,6 @@ class WCCustomerMapperTest { } @Test - @Suppress("LongMethod") fun `given customer name, then first name is properly assigned`() { // given val siteId = 23 From 6e3f63b3490f33c9f5a462d0ed54aee956247a41 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 13:00:20 +0530 Subject: [PATCH 04/21] add test to verify proper last name is detected --- .../model/customer/WCCustomerMapperTest.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 6fdb6444b5..50dc5793d6 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -280,7 +280,7 @@ class WCCustomerMapperTest { val siteId = 23 val site = SiteModel().apply { id = siteId } - val customerDTO = CustomerFromAnalyticsDTO(name = "firstname with a very long last name") + val customerDTO = CustomerFromAnalyticsDTO(name = "firstname lastname") // when val result = mapper.mapToModel(site, customerDTO) @@ -290,4 +290,21 @@ class WCCustomerMapperTest { assertEquals("firstname", firstName) } } + + @Test + fun `given customer name, then last name is properly assigned`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = "firstname lastname") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + with(result) { + assertEquals("lastname", lastName) + } + } } From 2b2a46d6994bd6382540f60098721180de4fa947 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 13:03:19 +0530 Subject: [PATCH 05/21] add test to verify empty string is returned when customer name is null --- .../model/customer/WCCustomerMapperTest.kt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 50dc5793d6..10de7f0d6e 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -286,9 +286,22 @@ class WCCustomerMapperTest { val result = mapper.mapToModel(site, customerDTO) // then - with(result) { - assertEquals("firstname", firstName) - } + assertEquals("firstname", result.firstName) + } + + @Test + fun `given customer name as null, then first name returns empty string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = null) + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("", result.firstName) } @Test From 6bc631427563e38305d7156ef4ea6ca4ef70ee38 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 13:04:45 +0530 Subject: [PATCH 06/21] add test to verify full string is returned when customer name has no space --- .../fluxc/model/customer/WCCustomerMapperTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 10de7f0d6e..a932ae9409 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -304,6 +304,21 @@ class WCCustomerMapperTest { assertEquals("", result.firstName) } + @Test + fun `given customer name with no space, then first name returns full string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = "firstnamelastname") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("firstnamelastname", result.firstName) + } + @Test fun `given customer name, then last name is properly assigned`() { // given From 27f21584f007ccbecc1ba67fc0a9042d9ae56d92 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 13:08:31 +0530 Subject: [PATCH 07/21] add test to verify empty string is returned for last name when customer name is null --- .../model/customer/WCCustomerMapperTest.kt | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index a932ae9409..320bbcaf6a 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -331,8 +331,21 @@ class WCCustomerMapperTest { val result = mapper.mapToModel(site, customerDTO) // then - with(result) { - assertEquals("lastname", lastName) - } + assertEquals("lastname", result.lastName) + } + + @Test + fun `given customer name as null, then last name returns empty string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = null) + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("", result.lastName) } } From bba2b8d7b09470c1207f8ba32313d0270dc3b1e0 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 13:09:32 +0530 Subject: [PATCH 08/21] add test to verify empty string is returned for last name when customer name has no space --- .../fluxc/model/customer/WCCustomerMapperTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 320bbcaf6a..882c3a5634 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -348,4 +348,19 @@ class WCCustomerMapperTest { // then assertEquals("", result.lastName) } + + @Test + fun `given customer name has no space, then last name returns empty string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = "firstnamelastname") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("", result.lastName) + } } From 4ca0bc6dc03a04871fafeaffca2820762280b12e Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 13:13:32 +0530 Subject: [PATCH 09/21] add test to verify proper string is returned for first name when customer name has multiple space --- .../fluxc/model/customer/WCCustomerMapperTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 882c3a5634..c34ea6ffcf 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -363,4 +363,19 @@ class WCCustomerMapperTest { // then assertEquals("", result.lastName) } + + @Test + fun `given customer name has multiple spaces, then first name returns proper string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("firstname", result.firstName) + } } From d76f38189e015b1c1b272bcf292b71ad97840512 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Mon, 25 Nov 2024 13:14:39 +0530 Subject: [PATCH 10/21] add test to verify proper string is returned for last name when customer name has multiple space --- .../fluxc/model/customer/WCCustomerMapperTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index c34ea6ffcf..73d66211b8 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -378,4 +378,19 @@ class WCCustomerMapperTest { // then assertEquals("firstname", result.firstName) } + + @Test + fun `given customer name has multiple spaces, then last name returns proper string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("and a very long last name", result.lastName) + } } From 3b5294ea5210ea7edd5185f67fba409c483cbf89 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 13:32:29 +0530 Subject: [PATCH 11/21] Refactor logic to handle multiple spaces, leading and trailing spaces in customer name --- .../android/fluxc/model/customer/WCCustomerMapper.kt | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt index 100b319c67..de8979dd26 100644 --- a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt +++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt @@ -117,7 +117,10 @@ class WCCustomerMapper @Inject constructor() { } } - private fun String?.firstNameFromName() = this?.substringBefore(" ") ?: "" + private fun String?.firstNameFromName(): String = + this?.trim()?.replace("\\s+".toRegex(), " ")?.substringBefore(" ") ?: "" + + private fun String?.lastNameFromName(): String = + this?.trim()?.replace("\\s+".toRegex(), " ")?.substringAfter(" ", "") ?: "" - private fun String?.lastNameFromName() = this?.substringAfter(" ", "") ?: "" } From 2276918081893ab53ae30179a3f3190f90b52149 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 13:32:48 +0530 Subject: [PATCH 12/21] Add test to verify leading space in customer name will still give us proper first name --- .../fluxc/model/customer/WCCustomerMapperTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 73d66211b8..b975ec9c0d 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -393,4 +393,19 @@ class WCCustomerMapperTest { // then assertEquals("and a very long last name", result.lastName) } + + @Test + fun `given customer name has leading space, then first name returns proper string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("firstname", result.firstName) + } } From 1700a00aea3dc28c089aa96d2d7ec80ea4554a91 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 13:33:48 +0530 Subject: [PATCH 13/21] Add test to verify trailing space in customer name will still give us proper last name --- .../fluxc/model/customer/WCCustomerMapperTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index b975ec9c0d..a7ed117340 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -408,4 +408,19 @@ class WCCustomerMapperTest { // then assertEquals("firstname", result.firstName) } + + @Test + fun `given customer name has trailing space, then last name returns proper string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name ") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("and a very long last name", result.lastName) + } } From c4e267a2c6556714a20a3b6f17a055762a04c985 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 13:34:49 +0530 Subject: [PATCH 14/21] Add test to verify multiple in between space in customer name will still give us proper first name --- .../fluxc/model/customer/WCCustomerMapperTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index a7ed117340..25185d2a7a 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -423,4 +423,19 @@ class WCCustomerMapperTest { // then assertEquals("and a very long last name", result.lastName) } + + @Test + fun `given customer name has multiple in between space, then first name returns proper string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("firstname", result.firstName) + } } From 2455c15f0dc8f3b9c5bfa084d4cd8a21b2d8d31c Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 13:35:45 +0530 Subject: [PATCH 15/21] Add test to verify multiple in between space in customer name will still give us proper last name --- .../fluxc/model/customer/WCCustomerMapperTest.kt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 25185d2a7a..d9c015d1f7 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -438,4 +438,19 @@ class WCCustomerMapperTest { // then assertEquals("firstname", result.firstName) } + + @Test + fun `given customer name has multiple in between space, then last name returns proper string`() { + // given + val siteId = 23 + val site = SiteModel().apply { id = siteId } + + val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name") + + // when + val result = mapper.mapToModel(site, customerDTO) + + // then + assertEquals("and a very long last name", result.lastName) + } } From 8f338d2c450d468df5b1de37069c838d27dd1b23 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 13:37:15 +0530 Subject: [PATCH 16/21] Comment given, when, then in capitals to maintain consistency in file --- .../model/customer/WCCustomerMapperTest.kt | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index d9c015d1f7..16e229d19d 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -276,181 +276,181 @@ class WCCustomerMapperTest { @Test fun `given customer name, then first name is properly assigned`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = "firstname lastname") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("firstname", result.firstName) } @Test fun `given customer name as null, then first name returns empty string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = null) - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("", result.firstName) } @Test fun `given customer name with no space, then first name returns full string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = "firstnamelastname") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("firstnamelastname", result.firstName) } @Test fun `given customer name, then last name is properly assigned`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = "firstname lastname") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("lastname", result.lastName) } @Test fun `given customer name as null, then last name returns empty string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = null) - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("", result.lastName) } @Test fun `given customer name has no space, then last name returns empty string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = "firstnamelastname") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("", result.lastName) } @Test fun `given customer name has multiple spaces, then first name returns proper string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("firstname", result.firstName) } @Test fun `given customer name has multiple spaces, then last name returns proper string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("and a very long last name", result.lastName) } @Test fun `given customer name has leading space, then first name returns proper string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("firstname", result.firstName) } @Test fun `given customer name has trailing space, then last name returns proper string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = "firstname and a very long last name ") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("and a very long last name", result.lastName) } @Test fun `given customer name has multiple in between space, then first name returns proper string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("firstname", result.firstName) } @Test fun `given customer name has multiple in between space, then last name returns proper string`() { - // given + // GIVEN val siteId = 23 val site = SiteModel().apply { id = siteId } val customerDTO = CustomerFromAnalyticsDTO(name = " firstname and a very long last name") - // when + // WHEN val result = mapper.mapToModel(site, customerDTO) - // then + // THEN assertEquals("and a very long last name", result.lastName) } } From e085d0c6f89e1bbf8e250297e90d668d45447a42 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 13:38:38 +0530 Subject: [PATCH 17/21] Add comment --- .../wordpress/android/fluxc/model/customer/WCCustomerMapper.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt index de8979dd26..3c20066e5f 100644 --- a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt +++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt @@ -117,6 +117,7 @@ class WCCustomerMapper @Inject constructor() { } } + // Please refer WCCustomerMapperTest file which serves as documentation of how this function behaves. private fun String?.firstNameFromName(): String = this?.trim()?.replace("\\s+".toRegex(), " ")?.substringBefore(" ") ?: "" From 7b6d579c09ada3aec910fcce61e4a4206287695b Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 13:47:35 +0530 Subject: [PATCH 18/21] Fix checkstyle error --- .../android/fluxc/model/customer/WCCustomerMapper.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt index 3c20066e5f..b6be2e3079 100644 --- a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt +++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt @@ -122,6 +122,8 @@ class WCCustomerMapper @Inject constructor() { this?.trim()?.replace("\\s+".toRegex(), " ")?.substringBefore(" ") ?: "" private fun String?.lastNameFromName(): String = - this?.trim()?.replace("\\s+".toRegex(), " ")?.substringAfter(" ", "") ?: "" + this?.trim()?.replace("\\s+".toRegex(), " ")?.substringAfter( + " ", "" + ) ?: "" } From 651e2b91734a7a7c7c60a3c2785c87da72b6e7f4 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 14:16:37 +0530 Subject: [PATCH 19/21] Fix checkstyle error --- .../android/fluxc/model/customer/WCCustomerMapper.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt index b6be2e3079..3c20066e5f 100644 --- a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt +++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt @@ -122,8 +122,6 @@ class WCCustomerMapper @Inject constructor() { this?.trim()?.replace("\\s+".toRegex(), " ")?.substringBefore(" ") ?: "" private fun String?.lastNameFromName(): String = - this?.trim()?.replace("\\s+".toRegex(), " ")?.substringAfter( - " ", "" - ) ?: "" + this?.trim()?.replace("\\s+".toRegex(), " ")?.substringAfter(" ", "") ?: "" } From a243e08d9b915356bce3b57e105f326fc7a5aa72 Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Tue, 26 Nov 2024 14:56:42 +0530 Subject: [PATCH 20/21] Fix checkstyle error --- .../wordpress/android/fluxc/model/customer/WCCustomerMapper.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt index 3c20066e5f..7199983b3c 100644 --- a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt +++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt @@ -123,5 +123,4 @@ class WCCustomerMapper @Inject constructor() { private fun String?.lastNameFromName(): String = this?.trim()?.replace("\\s+".toRegex(), " ")?.substringAfter(" ", "") ?: "" - } From 2aca0010300896a4a2dc56e4bbc939d20c8f189a Mon Sep 17 00:00:00 2001 From: AnirudhBhat Date: Wed, 27 Nov 2024 08:45:22 +0530 Subject: [PATCH 21/21] Refactor first name and last name extraction logic --- .../android/fluxc/model/customer/WCCustomerMapperTest.kt | 2 +- .../android/fluxc/model/customer/WCCustomerMapper.kt | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt index 16e229d19d..d17e875f10 100644 --- a/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt +++ b/example/src/test/java/org/wordpress/android/fluxc/model/customer/WCCustomerMapperTest.kt @@ -451,6 +451,6 @@ class WCCustomerMapperTest { val result = mapper.mapToModel(site, customerDTO) // THEN - assertEquals("and a very long last name", result.lastName) + assertEquals("and a very long last name", result.lastName) } } diff --git a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt index 7199983b3c..cf618f3197 100644 --- a/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt +++ b/plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/model/customer/WCCustomerMapper.kt @@ -119,8 +119,8 @@ class WCCustomerMapper @Inject constructor() { // Please refer WCCustomerMapperTest file which serves as documentation of how this function behaves. private fun String?.firstNameFromName(): String = - this?.trim()?.replace("\\s+".toRegex(), " ")?.substringBefore(" ") ?: "" + this?.trim()?.substringBefore(' ')?.trim().orEmpty() private fun String?.lastNameFromName(): String = - this?.trim()?.replace("\\s+".toRegex(), " ")?.substringAfter(" ", "") ?: "" + this?.trim()?.substringAfter(' ', "")?.trim().orEmpty() }