Skip to content

Commit

Permalink
Merge branch 'trunk' into issue/6992-add-support-order-needs_payment-…
Browse files Browse the repository at this point in the history
…needs_processing
  • Loading branch information
toupper committed Oct 2, 2023
2 parents da44423 + 88de533 commit 6d25ed9
Show file tree
Hide file tree
Showing 32 changed files with 1,884 additions and 182 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ allprojects {

ext {
minSdkVersion = 24
compileSdkVersion = 33
compileSdkVersion = 34
targetSdkVersion = 33
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,6 @@ class MockedStack_WCProductsTest : MockedStack_Base() {
assertEquals(25, payload.reviews.size)
assertNull(payload.filterProductIds)
assertNull(payload.filterByStatus)
assertFalse(payload.loadedMore)
assertTrue(payload.canLoadMore)

// Save product reviews to the database
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@ class WooProductsFragment : StoreSelectingFragment() {
FetchProductReviewsPayload(
site,
productIds = listOf(remoteProductId)
)
),
deletePreviouslyCachedReviews = false
)
prependToLog("Fetched ${result.rowsAffected} product reviews")
}
Expand All @@ -321,7 +322,10 @@ class WooProductsFragment : StoreSelectingFragment() {
coroutineScope.launch {
prependToLog("Submitting request to fetch product reviews for site ${site.id}")
val payload = FetchProductReviewsPayload(site)
val result = wcProductStore.fetchProductReviews(payload)
val result = wcProductStore.fetchProductReviews(
payload,
deletePreviouslyCachedReviews = false
)
prependToLog("Fetched ${result.rowsAffected} product reviews")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class SiteRestClientTest {
}

@Test
fun `returns fetched sites`() = test {
fun `returns fetched sites using filter`() = test {
val response = SiteWPComRestResponse()
response.ID = siteId
val name = "Updated name"
Expand All @@ -123,15 +123,18 @@ class SiteRestClientTest {
sitesResponse.sites = listOf(response)

initSitesResponse(data = sitesResponse)
initSitesFeaturesResponse(data = SitesFeaturesRestResponse(emptyMap()))

val responseModel = restClient.fetchSites(listOf(WPCOM), false)
assertThat(responseModel.sites).hasSize(1)
assertThat(responseModel.sites[0].name).isEqualTo(name)
assertThat(responseModel.sites[0].siteId).isEqualTo(siteId)

assertThat(urlCaptor.lastValue)
assertThat(urlCaptor.firstValue)
.isEqualTo("https://public-api.wordpress.com/rest/v1.2/me/sites/")
assertThat(paramsCaptor.lastValue).isEqualTo(
assertThat(urlCaptor.lastValue)
.isEqualTo("https://public-api.wordpress.com/rest/v1.1/me/sites/features/")
assertThat(paramsCaptor.firstValue).isEqualTo(
mapOf(
"filters" to "wpcom",
"fields" to "ID,URL,name,description,jetpack,jetpack_connection," +
Expand All @@ -141,6 +144,35 @@ class SiteRestClientTest {
)
}

@Test
fun `returns fetched sites when not filtering`() = test {
val response = SiteWPComRestResponse()
response.ID = siteId
val name = "Updated name"
response.name = name
response.URL = "site.com"

val sitesResponse = SitesResponse()
sitesResponse.sites = listOf(response)

initSitesResponse(data = sitesResponse)

val responseModel = restClient.fetchSites(emptyList(), false)
assertThat(responseModel.sites).hasSize(1)
assertThat(responseModel.sites[0].name).isEqualTo(name)
assertThat(responseModel.sites[0].siteId).isEqualTo(siteId)

assertThat(urlCaptor.firstValue)
.isEqualTo("https://public-api.wordpress.com/rest/v1.1/me/sites/")
assertThat(paramsCaptor.firstValue).isEqualTo(
mapOf(
"fields" to "ID,URL,name,description,jetpack,jetpack_connection," +
"visible,is_private,options,plan,capabilities,quota,icon,meta,zendesk_site_meta," +
"organization_id,was_ecommerce_trial"
)
)
}

@Test
fun `fetched sites can filter JP connected package sites`() = test {
val response = SiteWPComRestResponse()
Expand All @@ -155,6 +187,7 @@ class SiteRestClientTest {
sitesResponse.sites = listOf(response)

initSitesResponse(data = sitesResponse)
initSitesFeaturesResponse(data = SitesFeaturesRestResponse(features = emptyMap()))

val responseModel = restClient.fetchSites(listOf(WPCOM), true)

Expand Down Expand Up @@ -521,6 +554,13 @@ class SiteRestClientTest {
return initGetResponse(PostFormatsResponse::class.java, data ?: mock(), error)
}

private suspend fun initSitesFeaturesResponse(
data: SitesFeaturesRestResponse? = null,
error: WPComGsonNetworkError? = null
): Response<SitesFeaturesRestResponse> {
return initGetResponse(SitesFeaturesRestResponse::class.java, data ?: mock(), error)
}

private suspend fun <T> initGetResponse(
clazz: Class<T>,
data: T,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType.INVALID_RE
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload
import org.wordpress.android.fluxc.network.rest.wpcom.wc.customer.CustomerRestClient
import org.wordpress.android.fluxc.network.rest.wpcom.wc.customer.dto.CustomerDTO
import org.wordpress.android.fluxc.network.rest.wpcom.wc.customer.dto.CustomerFromAnalyticsDTO
import org.wordpress.android.fluxc.persistence.CustomerSqlUtils
import org.wordpress.android.fluxc.persistence.WellSqlConfig
import org.wordpress.android.fluxc.test
Expand Down Expand Up @@ -395,95 +394,6 @@ class WCCustomerStoreTest {
assertEquals(customerModel, result.model)
}

@Test
fun `given page 1, when fetchCustomersFromAnalytics, then result deleted and stored`() =
test {
// given
val siteModelId = 1
val siteModel = SiteModel().apply { id = siteModelId }
val customerOne: CustomerFromAnalyticsDTO = mock()
val customerTwo: CustomerFromAnalyticsDTO = mock()
val response = arrayOf(customerOne, customerTwo)
whenever(
restClient.fetchCustomersFromAnalytics(
siteModel,
page = 1,
pageSize = 25
)
).thenReturn(WooPayload(response))
val modelOne = WCCustomerModel().apply {
remoteCustomerId = 1L
localSiteId = siteModelId
}
val modelTwo = WCCustomerModel().apply {
remoteCustomerId = 2L
localSiteId = siteModelId
}
whenever(mapper.mapToModel(siteModel, customerOne)).thenReturn(modelOne)
whenever(mapper.mapToModel(siteModel, customerTwo)).thenReturn(modelTwo)

// when
val result = store.fetchCustomersFromAnalytics(siteModel, 1)

// then
assertThat(result.isError).isFalse
assertThat(result.model).isEqualTo(listOf(modelOne, modelTwo))
assertThat(CustomerSqlUtils.getCustomersForSite(siteModel)).isEqualTo(
listOf(modelOne, modelTwo)
)
}

@Test
fun `given page 1 then page 2, when fetchCustomersFromAnalytics, then both result stored`() =
test {
// given
val siteModelId = 1
val siteModel = SiteModel().apply { id = siteModelId }
val customerOne: CustomerFromAnalyticsDTO = mock()
val customerTwo: CustomerFromAnalyticsDTO = mock()
val response = arrayOf(customerOne, customerTwo)
whenever(
restClient.fetchCustomersFromAnalytics(
siteModel,
page = 1,
pageSize = 25
)
).thenReturn(WooPayload(response))
whenever(
restClient.fetchCustomersFromAnalytics(
siteModel,
page = 2,
pageSize = 25
)
).thenReturn(WooPayload(response))
val modelOne = WCCustomerModel().apply {
remoteCustomerId = 1L
localSiteId = siteModelId
}
val modelTwo = WCCustomerModel().apply {
remoteCustomerId = 2L
localSiteId = siteModelId
}
whenever(mapper.mapToModel(siteModel, customerOne)).thenReturn(modelOne)
whenever(mapper.mapToModel(siteModel, customerTwo)).thenReturn(modelTwo)

// when
val result = store.fetchCustomersFromAnalytics(siteModel, 1)
val result2 = store.fetchCustomersFromAnalytics(siteModel, 2)

// then
assertThat(result.isError).isFalse
assertThat(result.model).isEqualTo(listOf(modelOne, modelTwo))
assertThat(CustomerSqlUtils.getCustomersForSite(siteModel)).isEqualTo(
listOf(modelOne, modelTwo)
)
assertThat(result2.isError).isFalse
assertThat(result2.model).isEqualTo(listOf(modelOne, modelTwo))
assertThat(CustomerSqlUtils.getCustomersForSite(siteModel)).isEqualTo(
listOf(modelOne, modelTwo)
)
}

@Test
fun `given error, when fetchCustomersFromAnalytics, then nothing is stored and error`() =
test {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.wordpress.android.fluxc.wc.taxes

import org.wordpress.android.fluxc.JsonLoaderUtils.jsonFileAs
import org.wordpress.android.fluxc.model.taxes.WCTaxClassModel
import org.wordpress.android.fluxc.network.rest.wpcom.wc.taxes.TaxRateDto
import org.wordpress.android.fluxc.network.rest.wpcom.wc.taxes.WCTaxRestClient.TaxClassApiResponse

object TaxTestUtils {
Expand Down Expand Up @@ -31,4 +33,9 @@ object TaxTestUtils {
return listOf(TaxClassApiResponse("example1", "example1"),
TaxClassApiResponse("example2", "example2"))
}

fun generateSampleTaxRateApiResponse() =
"wc/tax-rate-response.json"
.jsonFileAs(Array<TaxRateDto>::class.java)
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.wordpress.android.fluxc.wc.taxes

import com.yarolegovich.wellsql.WellSql
import kotlinx.coroutines.runBlocking
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
Expand All @@ -22,6 +23,7 @@ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult
import org.wordpress.android.fluxc.network.rest.wpcom.wc.taxes.WCTaxRestClient
import org.wordpress.android.fluxc.persistence.SiteSqlUtils
import org.wordpress.android.fluxc.persistence.WellSqlConfig
import org.wordpress.android.fluxc.persistence.dao.TaxRateDao
import org.wordpress.android.fluxc.store.WCTaxStore
import org.wordpress.android.fluxc.test
import org.wordpress.android.fluxc.tools.initCoroutineEngine
Expand All @@ -34,6 +36,7 @@ class WCTaxStoreTest {
private val errorSite = SiteModel().apply { id = 123 }
private val mapper = WCTaxClassMapper()
private lateinit var store: WCTaxStore
private val taxRateDao = mock<TaxRateDao>()

private val sampleTaxClassList = TaxTestUtils.generateSampleTaxClassApiResponse()
private val error = WooError(INVALID_RESPONSE, NETWORK_ERROR, "Invalid site ID")
Expand All @@ -52,7 +55,8 @@ class WCTaxStoreTest {
store = WCTaxStore(
restClient,
initCoroutineEngine(),
mapper
mapper,
taxRateDao
)

// Insert the site into the db so it's available later when fetching tax classes
Expand Down Expand Up @@ -86,6 +90,28 @@ class WCTaxStoreTest {
assertThat(invalidRequestResult.size).isEqualTo(0)
}

@Test
fun`when fetch tax rate fails, then error is returned` () {
runBlocking {
val error = WooError(INVALID_RESPONSE, NETWORK_ERROR, "Invalid site ID")
whenever(restClient.fetchTaxRateList(site, 1, 100)).thenReturn(WooPayload(error))
val result = store.fetchTaxRateList(site, 1, 100)
assertThat(result.error).isEqualTo(error)
}
}

@Test
fun `when fetch tax rate succeeds, then success returns` () {
runBlocking {
val taxRateApiResponse = TaxTestUtils.generateSampleTaxRateApiResponse()
whenever(restClient.fetchTaxRateList(site, 1, 100)).thenReturn(WooPayload(taxRateApiResponse))
val result = store.fetchTaxRateList(site, 1, 100)
assertThat(this).isNotNull
assertThat(result.isError).isFalse
assertThat(result).isEqualTo(WooResult(false))
}
}

private suspend fun fetchTaxClassListForSite(): WooResult<List<WCTaxClassModel>> {
val fetchTaxClassListPayload = WooPayload(sampleTaxClassList.toTypedArray())
whenever(restClient.fetchTaxClassList(site)).thenReturn(fetchTaxClassListPayload)
Expand Down
33 changes: 33 additions & 0 deletions example/src/test/resources/wc/tax-rate-response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[{
"id":1,
"country":"US",
"state":"GA",
"postcode":"31707",
"city":"ALBANY",
"rate":"10.0000",
"name":"Superlong taaaaaaaax nameeeeee",
"priority":1,
"compound":false,
"shipping":true,
"order":0,
"class":"standard",
"postcodes":[
"31707"
],
"cities":[
"ALBANY"
],
"_links":{
"self":[
{
"href":"https://testwoomobile.wpcomstaging.com/wp-json/wc/v3/taxes/1"
}
],
"collection":[
{
"href":"https://testwoomobile.wpcomstaging.com/wp-json/wc/v3/taxes"
}
]
}
}
]
1 change: 1 addition & 0 deletions fluxc-processor/src/main/resources/wp-com-endpoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
/me/domain-contact-information/
/me/settings/
/me/sites/
/me/sites/features
/me/send-verification-email/
/me/social-login/connect/
/me/username/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SiteWPAPIRestClient @Inject constructor(
}

wpApiRestUrl = discoveredWpApiUrl
this.url = response?.url ?: cleanedUrl.replaceBefore("://", urlScheme)
this.url = cleanedUrl.replaceBefore("://", urlScheme)
this.username = payload.username
this.password = payload.password
}
Expand Down
Loading

0 comments on commit 6d25ed9

Please sign in to comment.