From 48e3bf8a2f3eb4296f8443b414d23002eaa26c90 Mon Sep 17 00:00:00 2001 From: Hung Date: Wed, 12 Apr 2023 11:41:49 +0800 Subject: [PATCH] support STI response --- .../visearch/android/model/ObjectResult.java | 55 ++++ .../visearch/android/model/ProductObject.java | 54 ++++ .../visearch/android/ProductSearchTest.java | 249 +++++++++++++++++- 3 files changed, 356 insertions(+), 2 deletions(-) diff --git a/visearch-android/src/main/java/com/visenze/visearch/android/model/ObjectResult.java b/visearch-android/src/main/java/com/visenze/visearch/android/model/ObjectResult.java index 1d2fec1..1b3b177 100644 --- a/visearch-android/src/main/java/com/visenze/visearch/android/model/ObjectResult.java +++ b/visearch-android/src/main/java/com/visenze/visearch/android/model/ObjectResult.java @@ -11,6 +11,21 @@ public class ObjectResult extends ProductType { @SerializedName("total") private Integer total; + @SerializedName("id") + private String id; + + @SerializedName("category") + private String category; + + @SerializedName("name") + private String name; + + @SerializedName("excluded_im_names") + private List excludedImNames; + + @SerializedName("facets") + private List facets; + public List getResult() { return result; } @@ -26,4 +41,44 @@ public Integer getTotal() { public void setTotal(Integer total) { this.total = total; } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getExcludedImNames() { + return excludedImNames; + } + + public void setExcludedImNames(List excludedImNames) { + this.excludedImNames = excludedImNames; + } + + public List getFacets() { + return facets; + } + + public void setFacets(List facets) { + this.facets = facets; + } } diff --git a/visearch-android/src/main/java/com/visenze/visearch/android/model/ProductObject.java b/visearch-android/src/main/java/com/visenze/visearch/android/model/ProductObject.java index c37c8b1..cb076f4 100644 --- a/visearch-android/src/main/java/com/visenze/visearch/android/model/ProductObject.java +++ b/visearch-android/src/main/java/com/visenze/visearch/android/model/ProductObject.java @@ -11,6 +11,20 @@ public class ProductObject extends ProductType { @SerializedName("total") private Integer total; + @SerializedName("id") + private String id; + + @SerializedName("category") + private String category; + + @SerializedName("name") + private String name; + + @SerializedName("excluded_pids") + private List excludedPids; + + @SerializedName("facets") + private List facets; public List getResult() { return result; @@ -27,4 +41,44 @@ public Integer getTotal() { public void setTotal(Integer total) { this.total = total; } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getExcludedPids() { + return excludedPids; + } + + public void setExcludedPids(List excludedPids) { + this.excludedPids = excludedPids; + } + + public List getFacets() { + return facets; + } + + public void setFacets(List facets) { + this.facets = facets; + } } diff --git a/visearch-android/src/test/java/com/visenze/visearch/android/ProductSearchTest.java b/visearch-android/src/test/java/com/visenze/visearch/android/ProductSearchTest.java index 0870d23..f9bf486 100644 --- a/visearch-android/src/test/java/com/visenze/visearch/android/ProductSearchTest.java +++ b/visearch-android/src/test/java/com/visenze/visearch/android/ProductSearchTest.java @@ -3,18 +3,20 @@ import android.os.Build; +import com.google.common.base.Joiner; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.visenze.visearch.android.model.BestImage; +import com.visenze.visearch.android.model.Box; import com.visenze.visearch.android.model.ErrorData; import com.visenze.visearch.android.model.Experiment; -import com.visenze.visearch.android.model.ImageResult; -import com.visenze.visearch.android.model.ObjectResult; +import com.visenze.visearch.android.model.Facet; import com.visenze.visearch.android.model.ProductObject; import com.visenze.visearch.android.model.ProductResponse; import com.visenze.visearch.android.model.Product; import com.visenze.visearch.android.model.ProductType; import com.visenze.visearch.android.model.SetInfo; +import com.visenze.visearch.android.model.Strategy; import com.visenze.visearch.android.network.ProductSearchService; import com.visenze.visearch.android.network.RetrofitQueryMap; @@ -772,4 +774,247 @@ public void onSearchResult(ProductResponse response, ErrorData error) { }); } + + @Test + public void testStiResponse() { + String json = + "{\n" + + " \"reqid\": \"01806a667776c6f8a31c28105fd99e\",\n" + + " \"status\": \"OK\",\n" + + " \"method\": \"product/recommendations\",\n" + + " \"page\": 1,\n" + + " \"limit\": 5,\n" + + " \"total\": 2,\n" + + + " \"objects\": [\n" + + " {\n" + + " \"id\": \"b0eedf870030ebb7ec637cd2641d0591\",\n" + + " \"category\": \"eyewear\",\n" + + " \"box\": [\n" + + " 54,\n" + + " 1568,\n" + + " 1765,\n" + + " 2189\n" + + " ],\n" + + " \"total\": 745,\n" + + " \"result\": [\n" + + " {\n" + + " \"product_id\": \"4081895\",\n" + + " \"pinned\" : \"true\",\n" + + " \"score\": 1,\n" + + " \"data\": {\n" + + " \"title\": \"Versace VE4281 Women's Square Sunglasses, Black/Brown Gradient\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"product_id\": \"4047149\",\n" + + " \"score\": 0.711370050907135,\n" + + " \"data\": {\n" + + " \"title\": \"Prada PR 02VS Women's Cat's Eye Sunglasses\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"product_id\": \"3839024\",\n" + + " \"score\": 0.692736804485321,\n" + + " \"data\": {\n" + + " \"title\": \"Polo Ralph Lauren RA5160 Women's Cat's Eye Sunglasses, Black/Grey\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"product_id\": \"4299829\",\n" + + " \"score\": 0.6816573143005371,\n" + + " \"data\": {\n" + + " \"title\": \"Ralph RA5257 Women's Square Sunglasses\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"product_id\": \"4894399\",\n" + + " \"score\": 0.6687576174736023,\n" + + " \"data\": {\n" + + " \"title\": \"Michael Kors MK2023 Women's Polarised Round Sunglasses, Tortoise/Brown Gradient\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"excluded_pids\": [\"pid1\",\"pid2\"] , \n" + + " \"facets\": [\n" + + " {\n" + + " \"key\": \"category\",\n" + + " \"items\": [\n" + + " {\n" + + " \"value\": \"Women > Women's Dresses\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + " },\n" + + " {\n" + + " \"id\": \"b0eedf870030ebb7ec637cd2641d0592\",\n" + + " \"category\": \"sunglasses\",\n" + + " \"box\": [\n" + + " 52,\n" + + " 1589,\n" + + " 1753,\n" + + " 2210\n" + + " ],\n" + + " \"total\": 689,\n" + + " \"result\": [\n" + + " {\n" + + " \"product_id\": \"4081895\",\n" + + " \"score\": 1,\n" + + " \"data\": {\n" + + " \"title\": \"Versace VE4281 Women's Square Sunglasses, Black/Brown Gradient\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"product_id\": \"4047149\",\n" + + " \"score\": 0.7817856073379517,\n" + + " \"data\": {\n" + + " \"title\": \"Prada PR 02VS Women's Cat's Eye Sunglasses\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"product_id\": \"3839024\",\n" + + " \"score\": 0.7810016870498657,\n" + + " \"data\": {\n" + + " \"title\": \"Polo Ralph Lauren RA5160 Women's Cat's Eye Sunglasses, Black/Grey\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"product_id\": \"4299829\",\n" + + " \"score\": 0.7412603497505188,\n" + + " \"data\": {\n" + + " \"title\": \"Ralph RA5257 Women's Square Sunglasses\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " },\n" + + " {\n" + + " \"product_id\": \"4894399\",\n" + + " \"score\": 0.7388582825660706,\n" + + " \"data\": {\n" + + " \"title\": \"Michael Kors MK2023 Women's Polarised Round Sunglasses, Tortoise/Brown Gradient\"\n" + + " },\n" + + " \"vs_data\": {\n" + + " \"index_filter.product_tagging\": \"others\",\n" + + " \"detect\": \"eyewear\"\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"facets\": [\n" + + " {\n" + + " \"key\": \"category\",\n" + + " \"items\": [\n" + + " {\n" + + " \"value\": \"Women > Women's Sunglasses\"\n" + + " }\n" + + " ]\n" + + " }\n" + + " ]\n" + + " }\n" + + " ],\n" + + + " \"strategy\": {\n" + + " \"id\": 123,\n" + + " \"name\": \"test STI\",\n" + + " \"algorithm\": \"STI\"\n" + + " }\n" + + "}";; + + ProductResponse response = gson.fromJson(json, ProductResponse.class); + searchService.handleResponse(response, new ProductSearch.ResultListener() { + @Override + public void onSearchResult(ProductResponse response, ErrorData error) { + assertNull(error); + + Strategy strategy = response.getStrategy(); + assertEquals( Integer.valueOf(123), strategy.getId()); + assertEquals("test STI" , strategy.getName()); + assertEquals("STI", strategy.getAlgorithm()); + + assertEquals(1, response.getPage()); + assertEquals(5, response.getLimit()); + assertEquals(2, response.getTotal()); + assertEquals("01806a667776c6f8a31c28105fd99e", response.getReqId()); + + assertEquals(2, response.getObjects().size()); + + ProductObject o1 = response.getObjects().get(0); + assertEquals("b0eedf870030ebb7ec637cd2641d0591", o1.getId()); + + assertEquals("eyewear" , o1.getCategory()); + assertEquals("54,1568,1765,2189", getBoxString(o1.getBox())); + assertEquals(Integer.valueOf(745), o1.getTotal()); + assertEquals(5, o1.getResult().size()); + + Product i1 = o1.getResult().get(0); + + assertEquals("4081895", i1.getProductId()); + assertEquals("Versace VE4281 Women's Square Sunglasses, Black/Brown Gradient", i1.getData().get("title").toString()); + assertEquals("others", i1.getVsData().get("index_filter.product_tagging").toString()); + assertTrue(i1.getPinned()); + + assertEquals("pid1,pid2", Joiner.on(",").join(o1.getExcludedPids())); + + Facet facet1 = o1.getFacets().get(0); + assertEquals("category", facet1.getKey()); + assertEquals(1, facet1.getItems().size()); + assertEquals("Women > Women's Dresses", facet1.getItems().get(0).getValue()); + + + ProductObject o2 = response.getObjects().get(1); + assertEquals("b0eedf870030ebb7ec637cd2641d0592", o2.getId()); + assertEquals("sunglasses" , o2.getCategory()); + assertEquals("52,1589,1753,2210", getBoxString(o2.getBox())); + assertEquals(Integer.valueOf(689), o2.getTotal()); + assertEquals(5, o2.getResult().size()); + + assertEquals(1, o2.getFacets().size()); + + Facet facet = o2.getFacets().get(0); + assertEquals("category", facet.getKey()); + assertEquals(1, facet.getItems().size()); + assertEquals("Women > Women's Sunglasses", facet.getItems().get(0).getValue()); + + } + }); + + } + + private String getBoxString(Box box) { + return box.getX1() + "," + box.getY1() + "," + box.getX2() + "," + box.getY2(); + } }