Skip to content

Commit

Permalink
Add unit test for batch order response's deserializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
hafizrahman committed Dec 12, 2024
1 parent 7b8793e commit 0a0d3eb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.wordpress.android.fluxc.wc.order

import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import org.junit.Test
import org.wordpress.android.fluxc.UnitTestUtils
import org.wordpress.android.fluxc.model.order.ShippingLine
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.BatchOrderApiResponse
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue
Expand Down Expand Up @@ -143,4 +145,31 @@ class OrderEntityTest {
assertEquals("Flat Rate Shipping", shippingLinesList[0].methodTitle)
assertEquals("Local Pickup Shipping", shippingLinesList[1].methodTitle)
}

@Test
fun testDeserializeBatchOrderResponse() {
val testGson = GsonBuilder()
.registerTypeAdapter(
BatchOrderApiResponse.OrderResponse::class.java,
BatchOrderApiResponse.OrderResponseDeserializer())
.create()

val batchOrderJson = UnitTestUtils.getStringFromResourceFile(
this.javaClass, "wc/orders-batch.json"
)

val response = testGson.fromJson(batchOrderJson, BatchOrderApiResponse::class.java)
val orders = response.update

assertEquals(2, orders.size)

val firstOrder = orders[0] as BatchOrderApiResponse.OrderResponse.Success
assertEquals(1032L, firstOrder.order.id)
assertEquals("224.00", firstOrder.order.total)

val secondOrder = orders[1] as BatchOrderApiResponse.OrderResponse.Error
assertEquals(525L, secondOrder.id)
assertEquals("woocommerce_rest_shop_order_invalid_id", secondOrder.error.code)
assertEquals(400, secondOrder.error.data.status)
}
}
2 changes: 1 addition & 1 deletion example/src/test/resources/wc/orders-batch.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
"id": "525",
"error": {
"code": "woocommerce_rest_shop_order_invalid_id",
"message": "Id tidak valid.",
"message": "Invalid ID.",
"data": {
"status": 400
}
Expand Down

0 comments on commit 0a0d3eb

Please sign in to comment.