Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.personalization.api.responses.search

import com.google.gson.annotations.SerializedName

data class PopularItem(
@SerializedName("name")
val name: String,
@SerializedName("url")
val url: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ data class SearchBlankResponse(
@SerializedName("products")
val products: List<Product>,
@SerializedName("suggests")
val suggests: List<Suggest>
val suggests: List<Suggest>,
@SerializedName("popular_categories")
val popularCategories: List<PopularItem>?,
@SerializedName("popular_brands")
val popularBrands: List<PopularItem>?,
@SerializedName("popular_links")
val popularLinks: List<PopularItem>?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.personalization.features.search

import com.google.gson.Gson
import com.personalization.api.responses.search.SearchBlankResponse
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test

class SearchBlankResponseParsingTest {

private val gson = Gson()

private val json = """
{
"html": "",
"products": [],
"suggests": [],
"popular_categories": [{"name": "Электроника", "url": "/electronics"}],
"popular_brands": [{"name": "Samsung", "url": "/samsung"}],
"popular_links": [{"name": "Блог", "url": "/blog"}, {"name": "Кейсы", "url": "/cases"}]
}
""".trimIndent()

@Test
fun `popular fields are parsed from JSON`() {
val response = gson.fromJson(json, SearchBlankResponse::class.java)

assertEquals(1, response.popularCategories!!.size)
assertEquals("Электроника", response.popularCategories!![0].name)
assertEquals("/electronics", response.popularCategories!![0].url)

assertEquals(1, response.popularBrands!!.size)
assertEquals("Samsung", response.popularBrands!![0].name)

assertEquals(2, response.popularLinks!!.size)
assertEquals("Блог", response.popularLinks!![0].name)
assertEquals("Кейсы", response.popularLinks!![1].name)
}

@Test
fun `missing popular fields default to empty list`() {
val minimalJson = """{"html": "", "products": [], "suggests": []}"""
val response = gson.fromJson(minimalJson, SearchBlankResponse::class.java)

assertNull(response.popularCategories)
assertNull(response.popularBrands)
assertNull(response.popularLinks)
}
}
Loading