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
Expand Up @@ -53,6 +53,12 @@ public enum AggregationTypes {
max,
@SerializedName("all")
all,
@SerializedName("count")
count,
@SerializedName("field_count")
field_count,
@SerializedName("value_count")
value_count,
}

@SerializedName("dtype")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public class Result {
@SerializedName("variations_map")
private Object variationsMap;

// The value is returned in a list, but practically it would only be a single value list
@SerializedName("variation_slice")
private Map<String, List<String>> variationSlice;

@SerializedName("is_slotted")
private boolean isSlotted;

Expand Down Expand Up @@ -80,6 +84,13 @@ public Map<String, String> getStrategy() {
return strategy;
}

/**
* @return the variationSlice
*/
public Map<String, List<String>> getVariationSlice() {
return variationSlice;
}

/**
* @return isSlotted boolean
*/
Expand Down Expand Up @@ -115,6 +126,10 @@ public void setStrategy(Map<String, String> strategy) {
this.strategy = strategy;
}

public void setVariationSlice(Map<String, List<String>> variationSlice) {
this.variationSlice = variationSlice;
}

public void setIsSlotted(boolean isSlotted) {
this.isSlotted = isSlotted;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.constructor.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import io.constructor.client.models.BrowseResponse;
Expand Down Expand Up @@ -48,6 +50,9 @@ public void createBrowseResponseShouldReturnAResult() throws Exception {
"total number of results",
(int) response.getResponse().getTotalNumberOfResults(),
562);
assertNull(
"variation_slice should be null when not present",
response.getResponse().getResults().get(0).getVariationSlice());
assertTrue(
"browse result labels exists",
(Boolean)
Expand Down Expand Up @@ -332,4 +337,27 @@ public void createBrowseResponseShouldReturnAResultWithFeatures() throws Excepti
response.getResponse().getFeatures().get(1).getVariant(),
null);
}

@Test
public void createBrowseResponseShouldReturnAResultWithVariationSlice() throws Exception {
String string = Utils.getTestResource("response.browse.variation_slice.json");
BrowseResponse response = ConstructorIO.createBrowseResponse(string);

// Results should have variation_slice with one variation_id
assertNotNull(
"variation_slice should exist",
response.getResponse().getResults().get(0).getVariationSlice());
assertNotNull(
"variation_id key should exist",
response.getResponse().getResults().get(0).getVariationSlice().get("variation_id"));
assertEquals(
"variation_id should match",
response.getResponse()
.getResults()
.get(0)
.getVariationSlice()
.get("variation_id")
.get(0),
"901764002");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.constructor.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import io.constructor.client.models.SearchResponse;
Expand Down Expand Up @@ -40,6 +42,9 @@ public void createSearchResponseShouldReturnAResult() throws Exception {
"total number of results",
(int) response.getResponse().getTotalNumberOfResults(),
301);
assertNull(
"variation_slice should be null when not present",
response.getResponse().getResults().get(0).getVariationSlice());
assertTrue("search result id exists", response.getResultId() != null);
assertTrue("request exists", response.getRequest() != null);
assertEquals("request query exists", response.getRequest().get("term"), "peanut");
Expand Down Expand Up @@ -308,4 +313,27 @@ public void createSearchResponseShouldReturnAResultWithFeatures() throws Excepti
response.getResponse().getFeatures().get(2).getVariant(),
null);
}

@Test
public void createSearchResponseShouldReturnAResultWithVariationSlice() throws Exception {
String string = Utils.getTestResource("response.search.variation_slice.json");
SearchResponse response = ConstructorIO.createSearchResponse(string);

// Results should have variation_slice with one variation_id
assertNotNull(
"variation_slice should exist",
response.getResponse().getResults().get(0).getVariationSlice());
assertNotNull(
"variation_id key should exist",
response.getResponse().getResults().get(0).getVariationSlice().get("variation_id"));
assertEquals(
"variation_id should match",
response.getResponse()
.getResults()
.get(0)
.getVariationSlice()
.get("variation_id")
.get(0),
"801764002");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package io.constructor.client;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.junit.Test;

public class VariationsMapTest {

@Test
public void newShouldReturnVariationsMap() throws Exception {
VariationsMap variationsMap = new VariationsMap();
assertNotNull(variationsMap);
assertEquals(variationsMap.getdType(), VariationsMap.Dtypes.array);
}

@Test
public void addGroupByRuleShouldAddRule() throws Exception {
VariationsMap variationsMap = new VariationsMap();
variationsMap.addGroupByRule("variation", "data.variation_id");
assertEquals(variationsMap.getGroupBy().size(), 1);
assertEquals(variationsMap.getGroupBy().get(0).name, "variation");
assertEquals(variationsMap.getGroupBy().get(0).field, "data.variation_id");
}

@Test
public void addValueRuleShouldAddRule() throws Exception {
VariationsMap variationsMap = new VariationsMap();
variationsMap.addValueRule(
"size", VariationsMap.AggregationTypes.first, "data.facets.size");
assertEquals(variationsMap.getValues().size(), 1);
assertEquals(
variationsMap.getValues().get("size").aggregation,
VariationsMap.AggregationTypes.first);
assertEquals(variationsMap.getValues().get("size").field, "data.facets.size");
}

@Test
public void allAggregationTypesShouldSerializeCorrectly() throws Exception {
VariationsMap variationsMap = new VariationsMap();
variationsMap.addValueRule(
"size", VariationsMap.AggregationTypes.first, "data.facets.size");
variationsMap.addValueRule(
"min_price", VariationsMap.AggregationTypes.min, "data.facets.price");
variationsMap.addValueRule(
"max_price", VariationsMap.AggregationTypes.max, "data.facets.price");
variationsMap.addValueRule("tags", VariationsMap.AggregationTypes.all, "data.facets.tags");
variationsMap.addValueRule(
"variation_count", VariationsMap.AggregationTypes.count, "data.variation_id");
variationsMap.addValueRule(
"color_count", VariationsMap.AggregationTypes.field_count, "data.facets.color");
variationsMap.addValueRule(
"blue_count", VariationsMap.AggregationTypes.value_count, "data.facets.color");

Gson gson = new Gson();
String json = gson.toJson(variationsMap);
JsonObject jsonObj = gson.fromJson(json, JsonObject.class);

// Verify all aggregation types are serialized correctly in JSON string
assertTrue(json.contains("\"aggregation\":\"first\""));
assertTrue(json.contains("\"aggregation\":\"min\""));
assertTrue(json.contains("\"aggregation\":\"max\""));
assertTrue(json.contains("\"aggregation\":\"all\""));
assertTrue(json.contains("\"aggregation\":\"count\""));
assertTrue(json.contains("\"aggregation\":\"field_count\""));
assertTrue(json.contains("\"aggregation\":\"value_count\""));

// Verify each aggregation type is correctly deserialized
assertEquals(
jsonObj.getAsJsonObject("values")
.getAsJsonObject("size")
.get("aggregation")
.getAsString(),
"first");
assertEquals(
jsonObj.getAsJsonObject("values")
.getAsJsonObject("min_price")
.get("aggregation")
.getAsString(),
"min");
assertEquals(
jsonObj.getAsJsonObject("values")
.getAsJsonObject("max_price")
.get("aggregation")
.getAsString(),
"max");
assertEquals(
jsonObj.getAsJsonObject("values")
.getAsJsonObject("tags")
.get("aggregation")
.getAsString(),
"all");
assertEquals(
jsonObj.getAsJsonObject("values")
.getAsJsonObject("variation_count")
.get("aggregation")
.getAsString(),
"count");
assertEquals(
jsonObj.getAsJsonObject("values")
.getAsJsonObject("color_count")
.get("aggregation")
.getAsString(),
"field_count");
assertEquals(
jsonObj.getAsJsonObject("values")
.getAsJsonObject("blue_count")
.get("aggregation")
.getAsString(),
"value_count");

assertEquals(variationsMap.getValues().size(), 7);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"response": {
"facets": [],
"groups": [],
"results": [{
"matched_terms": [],
"data": {
"id": "20001",
"url": "https://test.com/p/20001",
"facets": [{
"name": "Color",
"values": ["blue"]
}],
"image_url": "https://test.com/p/20001",
"description": "browse item with variation_slice",
"deactivated": false
},
"value": "BrowseItem1",
"is_slotted": false,
"labels": {},
"variation_slice": {
"variation_id": ["901764002"]
}
}, {
"matched_terms": [],
"data": {
"id": "20002",
"url": "https://test.com/p/20002",
"facets": [{
"name": "Color",
"values": ["blue"]
}],
"image_url": "https://test.com/p/20002",
"description": "browse item with variation_slice",
"deactivated": false
},
"value": "BrowseItem2",
"is_slotted": false,
"labels": {},
"variation_slice": {
"variation_id": ["904422022"]
}
}],
"total_num_results": 2,
"result_id": "test-browse-result-id"
},
"result_id": "test-browse-result-id",
"request": {
"browse_filter_name": "Color",
"browse_filter_value": "Blue"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"response": {
"facets": [],
"groups": [],
"results": [{
"matched_terms": ["item"],
"data": {
"id": "10001",
"url": "https://test.com/p/10001",
"facets": [{
"name": "Price",
"values": [30]
}],
"image_url": "https://test.com/p/10001",
"description": "item with variation_slice",
"deactivated": false
},
"value": "Item1",
"is_slotted": false,
"labels": {},
"variation_slice": {
"variation_id": ["801764002"]
}
}, {
"matched_terms": ["item"],
"data": {
"id": "10002",
"url": "https://test.com/p/10002",
"facets": [{
"name": "Price",
"values": [20]
}],
"image_url": "https://test.com/p/10002",
"description": "item with variation_slice",
"deactivated": false
},
"value": "Item2",
"is_slotted": false,
"labels": {},
"variation_slice": {
"variation_id": ["804422022"]
}
}],
"total_num_results": 2,
"result_id": "test-result-id"
},
"result_id": "test-result-id",
"request": {}
}