Skip to content

Commit cecdc51

Browse files
authored
Merge pull request #391 from Ecwid/ECWID-136994
ECWID-136994 Add search parameter to the customer group endpoint
2 parents 03e102a + fa1ed65 commit cecdc51

3 files changed

Lines changed: 59 additions & 9 deletions

File tree

src/main/kotlin/com/ecwid/apiclient/v3/dto/customergroup/request/CustomerGroupsSearchRequest.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import com.ecwid.apiclient.v3.responsefields.ResponseFields
77
data class CustomerGroupsSearchRequest(
88
val offset: Int = 0,
99
val limit: Int = 100,
10+
val keyword: String? = null,
11+
val customerGroupIds: List<Int>? = null,
1012
val responseFields: ResponseFields = ResponseFields.All,
1113
) : ApiRequest {
1214
override fun toRequestInfo() = RequestInfo.createGetRequest(
@@ -22,6 +24,12 @@ data class CustomerGroupsSearchRequest(
2224
return mutableMapOf<String, String>().apply {
2325
put("offset", request.offset.toString())
2426
put("limit", request.limit.toString())
27+
if (!request.keyword.isNullOrBlank()) {
28+
put("keyword", request.keyword)
29+
}
30+
if (!request.customerGroupIds.isNullOrEmpty()) {
31+
put("customerGroupIds", request.customerGroupIds.joinToString(","))
32+
}
2533
}.toMap()
2634
}
2735
}

src/test/kotlin/com/ecwid/apiclient/v3/entity/CustomerGroupsTest.kt

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import org.junit.jupiter.api.Assertions.assertTrue
99
import org.junit.jupiter.api.BeforeEach
1010
import org.junit.jupiter.api.Test
1111

12+
const val TEST_SEARCH_PHRASE = "Test"
13+
const val TEST_CUSTOMER_GROUP = "Customer group"
14+
1215
class CustomerGroupsTest : BaseEntityTest() {
1316

1417
@BeforeEach
@@ -61,10 +64,11 @@ class CustomerGroupsTest : BaseEntityTest() {
6164

6265
@Test
6366
fun testSearchPaging() {
64-
// Create three customer groups additionally to always existing “General” group
65-
repeat(3) {
67+
// Create 6 customer groups to test paging
68+
val customerGroups = generateSearchTestCustomerGroups()
69+
repeat(customerGroups.size) {
6670
val customerGroupCreateRequest = CustomerGroupCreateRequest(
67-
newCustomerGroup = generateTestCustomerGroup()
71+
newCustomerGroup = customerGroups[it]
6872
)
6973
val customerGroupCreateResult = apiClient.createCustomerGroup(customerGroupCreateRequest)
7074
assertTrue(customerGroupCreateResult.id > 0)
@@ -73,19 +77,53 @@ class CustomerGroupsTest : BaseEntityTest() {
7377
// Trying to request first page
7478
val customerGroupsSearchRequest1 = CustomerGroupsSearchRequest(offset = 0, limit = 2)
7579
val customerGroupsSearchResult1 = apiClient.searchCustomerGroups(customerGroupsSearchRequest1)
76-
assertEquals(2 + 1, customerGroupsSearchResult1.count) // “General” group exists is on every page
77-
assertEquals(3, customerGroupsSearchResult1.total)
80+
assertEquals(2, customerGroupsSearchResult1.count) // “General” group exists only of first page
81+
assertEquals(7, customerGroupsSearchResult1.total)
7882

7983
// Trying to request second and the last page
80-
val customerGroupsSearchRequest2 = CustomerGroupsSearchRequest(offset = 2, limit = 2)
84+
val customerGroupsSearchRequest2 = CustomerGroupsSearchRequest(offset = 6, limit = 2)
8185
val customerGroupsSearchResult2 = apiClient.searchCustomerGroups(customerGroupsSearchRequest2)
82-
assertEquals(1 + 1, customerGroupsSearchResult2.count) // “General” group exists is on every page
83-
assertEquals(3, customerGroupsSearchResult2.total)
86+
assertEquals(1, customerGroupsSearchResult2.count) // “General” group exists only of first page
87+
assertEquals(7, customerGroupsSearchResult2.total)
88+
89+
// test by keyword "Customer group"
90+
val customerGroupsSearchRequest3 = CustomerGroupsSearchRequest(
91+
keyword = TEST_CUSTOMER_GROUP,
92+
)
93+
val customerGroupsSearchResult3 = apiClient.searchCustomerGroups(customerGroupsSearchRequest3)
94+
assertEquals(true, customerGroupsSearchResult3.items.all { it.name.contains(TEST_CUSTOMER_GROUP) })
95+
96+
// test by keyword "Test"
97+
val customerGroupsSearchRequest4 = CustomerGroupsSearchRequest(
98+
keyword = TEST_SEARCH_PHRASE,
99+
)
100+
val customerGroupsSearchResult4 = apiClient.searchCustomerGroups(customerGroupsSearchRequest4)
101+
assertEquals(true, customerGroupsSearchResult4.items.all { it.name.contains(TEST_SEARCH_PHRASE) })
102+
103+
val testGroupIds = customerGroupsSearchResult4.items.map { it.id }
104+
105+
// test by customerGroupIds
106+
val customerGroupsSearchRequest5 = CustomerGroupsSearchRequest(
107+
customerGroupIds = testGroupIds,
108+
)
109+
val customerGroupsSearchResult5 = apiClient.searchCustomerGroups(customerGroupsSearchRequest5)
110+
assertEquals(testGroupIds.size, customerGroupsSearchResult5.total)
111+
assertEquals(testGroupIds, customerGroupsSearchResult5.items.map { it.id })
112+
84113
}
85114
}
86115

87116
private fun generateTestCustomerGroup(): UpdatedCustomerGroup {
88117
return UpdatedCustomerGroup(
89-
name = "Customer group " + randomAlphanumeric(8)
118+
name = "$TEST_CUSTOMER_GROUP " + randomAlphanumeric(8)
90119
)
91120
}
121+
122+
private fun generateSearchTestCustomerGroups(): List<UpdatedCustomerGroup> {
123+
val result = mutableListOf<UpdatedCustomerGroup>()
124+
repeat(3) {
125+
result.add(UpdatedCustomerGroup("$TEST_CUSTOMER_GROUP $it"))
126+
result.add(UpdatedCustomerGroup("$TEST_SEARCH_PHRASE $it"))
127+
}
128+
return result
129+
}

src/test/kotlin/com/ecwid/apiclient/v3/rule/NullablePropertyRules.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.ecwid.apiclient.v3.dto.batch.result.GetEscapedBatchResult
66
import com.ecwid.apiclient.v3.dto.batch.result.GetTypedBatchResult
77
import com.ecwid.apiclient.v3.dto.cart.result.CartUpdateResult
88
import com.ecwid.apiclient.v3.dto.cart.result.ConvertCartToOrderResult
9+
import com.ecwid.apiclient.v3.dto.customergroup.request.CustomerGroupsSearchRequest
910
import com.ecwid.apiclient.v3.dto.instantsite.redirects.request.InstantSiteRedirectsGetForExactPathRequest
1011
import com.ecwid.apiclient.v3.dto.instantsite.redirects.request.InstantSiteRedirectsSearchRequest
1112
import com.ecwid.apiclient.v3.dto.order.result.DeletedOrder
@@ -132,6 +133,9 @@ val otherNullablePropertyRules: List<NullablePropertyRule<*, *>> = listOf(
132133
AllowNullable(InstantSiteRedirectsGetForExactPathRequest::exactPath),
133134

134135
AllowNullable(UpdatedProductReviewStatus::status),
136+
137+
AllowNullable(CustomerGroupsSearchRequest::keyword),
138+
AllowNullable(CustomerGroupsSearchRequest::customerGroupIds),
135139
)
136140

137141
val nullablePropertyRules: List<NullablePropertyRule<*, *>> = listOf(

0 commit comments

Comments
 (0)