diff --git a/src/main/kotlin/com/ecwid/apiclient/v3/dto/customergroup/request/CustomerGroupsSearchRequest.kt b/src/main/kotlin/com/ecwid/apiclient/v3/dto/customergroup/request/CustomerGroupsSearchRequest.kt index 61d54bcd0..e968cf3f5 100644 --- a/src/main/kotlin/com/ecwid/apiclient/v3/dto/customergroup/request/CustomerGroupsSearchRequest.kt +++ b/src/main/kotlin/com/ecwid/apiclient/v3/dto/customergroup/request/CustomerGroupsSearchRequest.kt @@ -7,6 +7,8 @@ import com.ecwid.apiclient.v3.responsefields.ResponseFields data class CustomerGroupsSearchRequest( val offset: Int = 0, val limit: Int = 100, + val keyword: String? = null, + val customerGroupIds: List? = null, val responseFields: ResponseFields = ResponseFields.All, ) : ApiRequest { override fun toRequestInfo() = RequestInfo.createGetRequest( @@ -22,6 +24,12 @@ data class CustomerGroupsSearchRequest( return mutableMapOf().apply { put("offset", request.offset.toString()) put("limit", request.limit.toString()) + if (!request.keyword.isNullOrBlank()) { + put("keyword", request.keyword) + } + if (!request.customerGroupIds.isNullOrEmpty()) { + put("customerGroupIds", request.customerGroupIds.joinToString(",")) + } }.toMap() } } diff --git a/src/test/kotlin/com/ecwid/apiclient/v3/entity/CustomerGroupsTest.kt b/src/test/kotlin/com/ecwid/apiclient/v3/entity/CustomerGroupsTest.kt index f19426e18..a4145afaa 100644 --- a/src/test/kotlin/com/ecwid/apiclient/v3/entity/CustomerGroupsTest.kt +++ b/src/test/kotlin/com/ecwid/apiclient/v3/entity/CustomerGroupsTest.kt @@ -9,6 +9,9 @@ import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test +const val TEST_SEARCH_PHRASE = "Test" +const val TEST_CUSTOMER_GROUP = "Customer group" + class CustomerGroupsTest : BaseEntityTest() { @BeforeEach @@ -61,10 +64,11 @@ class CustomerGroupsTest : BaseEntityTest() { @Test fun testSearchPaging() { - // Create three customer groups additionally to always existing “General” group - repeat(3) { + // Create 6 customer groups to test paging + val customerGroups = generateSearchTestCustomerGroups() + repeat(customerGroups.size) { val customerGroupCreateRequest = CustomerGroupCreateRequest( - newCustomerGroup = generateTestCustomerGroup() + newCustomerGroup = customerGroups[it] ) val customerGroupCreateResult = apiClient.createCustomerGroup(customerGroupCreateRequest) assertTrue(customerGroupCreateResult.id > 0) @@ -73,19 +77,53 @@ class CustomerGroupsTest : BaseEntityTest() { // Trying to request first page val customerGroupsSearchRequest1 = CustomerGroupsSearchRequest(offset = 0, limit = 2) val customerGroupsSearchResult1 = apiClient.searchCustomerGroups(customerGroupsSearchRequest1) - assertEquals(2 + 1, customerGroupsSearchResult1.count) // “General” group exists is on every page - assertEquals(3, customerGroupsSearchResult1.total) + assertEquals(2, customerGroupsSearchResult1.count) // “General” group exists only of first page + assertEquals(7, customerGroupsSearchResult1.total) // Trying to request second and the last page - val customerGroupsSearchRequest2 = CustomerGroupsSearchRequest(offset = 2, limit = 2) + val customerGroupsSearchRequest2 = CustomerGroupsSearchRequest(offset = 6, limit = 2) val customerGroupsSearchResult2 = apiClient.searchCustomerGroups(customerGroupsSearchRequest2) - assertEquals(1 + 1, customerGroupsSearchResult2.count) // “General” group exists is on every page - assertEquals(3, customerGroupsSearchResult2.total) + assertEquals(1, customerGroupsSearchResult2.count) // “General” group exists only of first page + assertEquals(7, customerGroupsSearchResult2.total) + + // test by keyword "Customer group" + val customerGroupsSearchRequest3 = CustomerGroupsSearchRequest( + keyword = TEST_CUSTOMER_GROUP, + ) + val customerGroupsSearchResult3 = apiClient.searchCustomerGroups(customerGroupsSearchRequest3) + assertEquals(true, customerGroupsSearchResult3.items.all { it.name.contains(TEST_CUSTOMER_GROUP) }) + + // test by keyword "Test" + val customerGroupsSearchRequest4 = CustomerGroupsSearchRequest( + keyword = TEST_SEARCH_PHRASE, + ) + val customerGroupsSearchResult4 = apiClient.searchCustomerGroups(customerGroupsSearchRequest4) + assertEquals(true, customerGroupsSearchResult4.items.all { it.name.contains(TEST_SEARCH_PHRASE) }) + + val testGroupIds = customerGroupsSearchResult4.items.map { it.id } + + // test by customerGroupIds + val customerGroupsSearchRequest5 = CustomerGroupsSearchRequest( + customerGroupIds = testGroupIds, + ) + val customerGroupsSearchResult5 = apiClient.searchCustomerGroups(customerGroupsSearchRequest5) + assertEquals(testGroupIds.size, customerGroupsSearchResult5.total) + assertEquals(testGroupIds, customerGroupsSearchResult5.items.map { it.id }) + } } private fun generateTestCustomerGroup(): UpdatedCustomerGroup { return UpdatedCustomerGroup( - name = "Customer group " + randomAlphanumeric(8) + name = "$TEST_CUSTOMER_GROUP " + randomAlphanumeric(8) ) } + +private fun generateSearchTestCustomerGroups(): List { + val result = mutableListOf() + repeat(3) { + result.add(UpdatedCustomerGroup("$TEST_CUSTOMER_GROUP $it")) + result.add(UpdatedCustomerGroup("$TEST_SEARCH_PHRASE $it")) + } + return result +} diff --git a/src/test/kotlin/com/ecwid/apiclient/v3/rule/NullablePropertyRules.kt b/src/test/kotlin/com/ecwid/apiclient/v3/rule/NullablePropertyRules.kt index a67c40c35..2b2d1ad38 100644 --- a/src/test/kotlin/com/ecwid/apiclient/v3/rule/NullablePropertyRules.kt +++ b/src/test/kotlin/com/ecwid/apiclient/v3/rule/NullablePropertyRules.kt @@ -6,6 +6,7 @@ import com.ecwid.apiclient.v3.dto.batch.result.GetEscapedBatchResult import com.ecwid.apiclient.v3.dto.batch.result.GetTypedBatchResult import com.ecwid.apiclient.v3.dto.cart.result.CartUpdateResult import com.ecwid.apiclient.v3.dto.cart.result.ConvertCartToOrderResult +import com.ecwid.apiclient.v3.dto.customergroup.request.CustomerGroupsSearchRequest import com.ecwid.apiclient.v3.dto.instantsite.redirects.request.InstantSiteRedirectsGetForExactPathRequest import com.ecwid.apiclient.v3.dto.instantsite.redirects.request.InstantSiteRedirectsSearchRequest import com.ecwid.apiclient.v3.dto.order.result.DeletedOrder @@ -132,6 +133,9 @@ val otherNullablePropertyRules: List> = listOf( AllowNullable(InstantSiteRedirectsGetForExactPathRequest::exactPath), AllowNullable(UpdatedProductReviewStatus::status), + + AllowNullable(CustomerGroupsSearchRequest::keyword), + AllowNullable(CustomerGroupsSearchRequest::customerGroupIds), ) val nullablePropertyRules: List> = listOf(