|
| 1 | +package com.ecwid.apiclient.v3.entity |
| 2 | + |
| 3 | +import com.ecwid.apiclient.v3.dto.brand.request.BrandsSearchRequest |
| 4 | +import com.ecwid.apiclient.v3.dto.product.request.ProductCreateRequest |
| 5 | +import com.ecwid.apiclient.v3.dto.product.request.ProductsSearchRequest.ByFilters |
| 6 | +import com.ecwid.apiclient.v3.dto.product.request.UpdatedProduct |
| 7 | +import com.ecwid.apiclient.v3.dto.product.request.UpdatedProduct.* |
| 8 | +import com.ecwid.apiclient.v3.util.randomAlphanumeric |
| 9 | +import com.ecwid.apiclient.v3.util.randomPrice |
| 10 | +import org.junit.jupiter.api.Assertions.assertTrue |
| 11 | +import org.junit.jupiter.api.BeforeEach |
| 12 | +import kotlin.test.Test |
| 13 | +import kotlin.test.assertEquals |
| 14 | + |
| 15 | +class BrandsTest : BaseEntityTest() { |
| 16 | + |
| 17 | + @BeforeEach |
| 18 | + override fun beforeEach() { |
| 19 | + super.beforeEach() |
| 20 | + |
| 21 | + initStoreProfile() |
| 22 | + removeAllProducts() |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + fun getBrands() { |
| 27 | + val brandedProductsCreateResult = createProductsWithBrands() |
| 28 | + |
| 29 | + // Waiting till product became available for searching |
| 30 | + brandedProductsCreateResult.skus.forEach { sku -> |
| 31 | + waitForIndexedProducts( |
| 32 | + productsSearchRequest = ByFilters(sku = sku), |
| 33 | + desiredProductCount = 1 |
| 34 | + ) |
| 35 | + } |
| 36 | + |
| 37 | + val result = apiClient.searchBrands(BrandsSearchRequest.ByFilters()) |
| 38 | + assertEquals( |
| 39 | + brandedProductsCreateResult.brandNames, |
| 40 | + result.items.map { it.name } |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + private fun createProductsWithBrands(productsCount: Int = 5): BrandedProductsCreateResult { |
| 45 | + val brands = mutableListOf<String>() |
| 46 | + val skus = mutableListOf<String>() |
| 47 | + |
| 48 | + for (i in 1..productsCount) { |
| 49 | + val randomBrand = randomAlphanumeric(8) |
| 50 | + val randomSku = randomAlphanumeric(10) |
| 51 | + val price = randomPrice() |
| 52 | + |
| 53 | + val productCreateRequest = ProductCreateRequest( |
| 54 | + newProduct = UpdatedProduct( |
| 55 | + name = "Product $i ${randomAlphanumeric(8)}", |
| 56 | + sku = randomSku, |
| 57 | + price = price, |
| 58 | + compareToPrice = 2 * price, |
| 59 | + enabled = true, |
| 60 | + quantity = 10, |
| 61 | + attributes = listOf( |
| 62 | + AttributeValue.createBrandAttributeValue(randomBrand), |
| 63 | + ), |
| 64 | + options = listOf( |
| 65 | + ProductOption.createSelectOption( |
| 66 | + name = "Color", |
| 67 | + choices = listOf( |
| 68 | + ProductOptionChoice("Black"), |
| 69 | + ProductOptionChoice("White"), |
| 70 | + ProductOptionChoice("Yellow"), |
| 71 | + ProductOptionChoice("Red") |
| 72 | + ), |
| 73 | + defaultChoice = 0, |
| 74 | + required = true |
| 75 | + ) |
| 76 | + ) |
| 77 | + ) |
| 78 | + ) |
| 79 | + val productCreateResult = apiClient.createProduct(productCreateRequest) |
| 80 | + assertTrue(productCreateResult.id > 0) |
| 81 | + |
| 82 | + skus.add(randomSku) |
| 83 | + brands.add(randomBrand) |
| 84 | + } |
| 85 | + |
| 86 | + return BrandedProductsCreateResult( |
| 87 | + brandNames = brands, |
| 88 | + skus = skus, |
| 89 | + ) |
| 90 | + } |
| 91 | + |
| 92 | + data class BrandedProductsCreateResult( |
| 93 | + val brandNames: List<String>, |
| 94 | + val skus: List<String>, |
| 95 | + ) |
| 96 | +} |
0 commit comments