Skip to content

Commit 22c3a16

Browse files
authored
[CDX-319] Update Variation Slicing/Mapping-related fields (#174)
* add new aggregation functions values * serialize variation_slice * add future note
1 parent 296a698 commit 22c3a16

File tree

7 files changed

+295
-0
lines changed

7 files changed

+295
-0
lines changed

constructorio-client/src/main/java/io/constructor/client/VariationsMap.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ public enum AggregationTypes {
5353
max,
5454
@SerializedName("all")
5555
all,
56+
@SerializedName("count")
57+
count,
58+
@SerializedName("field_count")
59+
field_count,
60+
@SerializedName("value_count")
61+
value_count,
5662
}
5763

5864
@SerializedName("dtype")

constructorio-client/src/main/java/io/constructor/client/models/Result.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public class Result {
2222
@SerializedName("variations_map")
2323
private Object variationsMap;
2424

25+
// The value is returned in a list, but practically it would only be a single value list
26+
@SerializedName("variation_slice")
27+
private Map<String, List<String>> variationSlice;
28+
2529
@SerializedName("is_slotted")
2630
private boolean isSlotted;
2731

@@ -80,6 +84,13 @@ public Map<String, String> getStrategy() {
8084
return strategy;
8185
}
8286

87+
/**
88+
* @return the variationSlice
89+
*/
90+
public Map<String, List<String>> getVariationSlice() {
91+
return variationSlice;
92+
}
93+
8394
/**
8495
* @return isSlotted boolean
8596
*/
@@ -115,6 +126,10 @@ public void setStrategy(Map<String, String> strategy) {
115126
this.strategy = strategy;
116127
}
117128

129+
public void setVariationSlice(Map<String, List<String>> variationSlice) {
130+
this.variationSlice = variationSlice;
131+
}
132+
118133
public void setIsSlotted(boolean isSlotted) {
119134
this.isSlotted = isSlotted;
120135
}

constructorio-client/src/test/java/io/constructor/client/BrowseResponseTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.constructor.client;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
46
import static org.junit.Assert.assertTrue;
57

68
import io.constructor.client.models.BrowseResponse;
@@ -48,6 +50,9 @@ public void createBrowseResponseShouldReturnAResult() throws Exception {
4850
"total number of results",
4951
(int) response.getResponse().getTotalNumberOfResults(),
5052
562);
53+
assertNull(
54+
"variation_slice should be null when not present",
55+
response.getResponse().getResults().get(0).getVariationSlice());
5156
assertTrue(
5257
"browse result labels exists",
5358
(Boolean)
@@ -332,4 +337,27 @@ public void createBrowseResponseShouldReturnAResultWithFeatures() throws Excepti
332337
response.getResponse().getFeatures().get(1).getVariant(),
333338
null);
334339
}
340+
341+
@Test
342+
public void createBrowseResponseShouldReturnAResultWithVariationSlice() throws Exception {
343+
String string = Utils.getTestResource("response.browse.variation_slice.json");
344+
BrowseResponse response = ConstructorIO.createBrowseResponse(string);
345+
346+
// Results should have variation_slice with one variation_id
347+
assertNotNull(
348+
"variation_slice should exist",
349+
response.getResponse().getResults().get(0).getVariationSlice());
350+
assertNotNull(
351+
"variation_id key should exist",
352+
response.getResponse().getResults().get(0).getVariationSlice().get("variation_id"));
353+
assertEquals(
354+
"variation_id should match",
355+
response.getResponse()
356+
.getResults()
357+
.get(0)
358+
.getVariationSlice()
359+
.get("variation_id")
360+
.get(0),
361+
"901764002");
362+
}
335363
}

constructorio-client/src/test/java/io/constructor/client/SearchResponseTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.constructor.client;
22

33
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertNull;
46
import static org.junit.Assert.assertTrue;
57

68
import io.constructor.client.models.SearchResponse;
@@ -40,6 +42,9 @@ public void createSearchResponseShouldReturnAResult() throws Exception {
4042
"total number of results",
4143
(int) response.getResponse().getTotalNumberOfResults(),
4244
301);
45+
assertNull(
46+
"variation_slice should be null when not present",
47+
response.getResponse().getResults().get(0).getVariationSlice());
4348
assertTrue("search result id exists", response.getResultId() != null);
4449
assertTrue("request exists", response.getRequest() != null);
4550
assertEquals("request query exists", response.getRequest().get("term"), "peanut");
@@ -308,4 +313,27 @@ public void createSearchResponseShouldReturnAResultWithFeatures() throws Excepti
308313
response.getResponse().getFeatures().get(2).getVariant(),
309314
null);
310315
}
316+
317+
@Test
318+
public void createSearchResponseShouldReturnAResultWithVariationSlice() throws Exception {
319+
String string = Utils.getTestResource("response.search.variation_slice.json");
320+
SearchResponse response = ConstructorIO.createSearchResponse(string);
321+
322+
// Results should have variation_slice with one variation_id
323+
assertNotNull(
324+
"variation_slice should exist",
325+
response.getResponse().getResults().get(0).getVariationSlice());
326+
assertNotNull(
327+
"variation_id key should exist",
328+
response.getResponse().getResults().get(0).getVariationSlice().get("variation_id"));
329+
assertEquals(
330+
"variation_id should match",
331+
response.getResponse()
332+
.getResults()
333+
.get(0)
334+
.getVariationSlice()
335+
.get("variation_id")
336+
.get(0),
337+
"801764002");
338+
}
311339
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package io.constructor.client;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import com.google.gson.Gson;
8+
import com.google.gson.JsonObject;
9+
import org.junit.Test;
10+
11+
public class VariationsMapTest {
12+
13+
@Test
14+
public void newShouldReturnVariationsMap() throws Exception {
15+
VariationsMap variationsMap = new VariationsMap();
16+
assertNotNull(variationsMap);
17+
assertEquals(variationsMap.getdType(), VariationsMap.Dtypes.array);
18+
}
19+
20+
@Test
21+
public void addGroupByRuleShouldAddRule() throws Exception {
22+
VariationsMap variationsMap = new VariationsMap();
23+
variationsMap.addGroupByRule("variation", "data.variation_id");
24+
assertEquals(variationsMap.getGroupBy().size(), 1);
25+
assertEquals(variationsMap.getGroupBy().get(0).name, "variation");
26+
assertEquals(variationsMap.getGroupBy().get(0).field, "data.variation_id");
27+
}
28+
29+
@Test
30+
public void addValueRuleShouldAddRule() throws Exception {
31+
VariationsMap variationsMap = new VariationsMap();
32+
variationsMap.addValueRule(
33+
"size", VariationsMap.AggregationTypes.first, "data.facets.size");
34+
assertEquals(variationsMap.getValues().size(), 1);
35+
assertEquals(
36+
variationsMap.getValues().get("size").aggregation,
37+
VariationsMap.AggregationTypes.first);
38+
assertEquals(variationsMap.getValues().get("size").field, "data.facets.size");
39+
}
40+
41+
@Test
42+
public void allAggregationTypesShouldSerializeCorrectly() throws Exception {
43+
VariationsMap variationsMap = new VariationsMap();
44+
variationsMap.addValueRule(
45+
"size", VariationsMap.AggregationTypes.first, "data.facets.size");
46+
variationsMap.addValueRule(
47+
"min_price", VariationsMap.AggregationTypes.min, "data.facets.price");
48+
variationsMap.addValueRule(
49+
"max_price", VariationsMap.AggregationTypes.max, "data.facets.price");
50+
variationsMap.addValueRule("tags", VariationsMap.AggregationTypes.all, "data.facets.tags");
51+
variationsMap.addValueRule(
52+
"variation_count", VariationsMap.AggregationTypes.count, "data.variation_id");
53+
variationsMap.addValueRule(
54+
"color_count", VariationsMap.AggregationTypes.field_count, "data.facets.color");
55+
variationsMap.addValueRule(
56+
"blue_count", VariationsMap.AggregationTypes.value_count, "data.facets.color");
57+
58+
Gson gson = new Gson();
59+
String json = gson.toJson(variationsMap);
60+
JsonObject jsonObj = gson.fromJson(json, JsonObject.class);
61+
62+
// Verify all aggregation types are serialized correctly in JSON string
63+
assertTrue(json.contains("\"aggregation\":\"first\""));
64+
assertTrue(json.contains("\"aggregation\":\"min\""));
65+
assertTrue(json.contains("\"aggregation\":\"max\""));
66+
assertTrue(json.contains("\"aggregation\":\"all\""));
67+
assertTrue(json.contains("\"aggregation\":\"count\""));
68+
assertTrue(json.contains("\"aggregation\":\"field_count\""));
69+
assertTrue(json.contains("\"aggregation\":\"value_count\""));
70+
71+
// Verify each aggregation type is correctly deserialized
72+
assertEquals(
73+
jsonObj.getAsJsonObject("values")
74+
.getAsJsonObject("size")
75+
.get("aggregation")
76+
.getAsString(),
77+
"first");
78+
assertEquals(
79+
jsonObj.getAsJsonObject("values")
80+
.getAsJsonObject("min_price")
81+
.get("aggregation")
82+
.getAsString(),
83+
"min");
84+
assertEquals(
85+
jsonObj.getAsJsonObject("values")
86+
.getAsJsonObject("max_price")
87+
.get("aggregation")
88+
.getAsString(),
89+
"max");
90+
assertEquals(
91+
jsonObj.getAsJsonObject("values")
92+
.getAsJsonObject("tags")
93+
.get("aggregation")
94+
.getAsString(),
95+
"all");
96+
assertEquals(
97+
jsonObj.getAsJsonObject("values")
98+
.getAsJsonObject("variation_count")
99+
.get("aggregation")
100+
.getAsString(),
101+
"count");
102+
assertEquals(
103+
jsonObj.getAsJsonObject("values")
104+
.getAsJsonObject("color_count")
105+
.get("aggregation")
106+
.getAsString(),
107+
"field_count");
108+
assertEquals(
109+
jsonObj.getAsJsonObject("values")
110+
.getAsJsonObject("blue_count")
111+
.get("aggregation")
112+
.getAsString(),
113+
"value_count");
114+
115+
assertEquals(variationsMap.getValues().size(), 7);
116+
}
117+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"response": {
3+
"facets": [],
4+
"groups": [],
5+
"results": [{
6+
"matched_terms": [],
7+
"data": {
8+
"id": "20001",
9+
"url": "https://test.com/p/20001",
10+
"facets": [{
11+
"name": "Color",
12+
"values": ["blue"]
13+
}],
14+
"image_url": "https://test.com/p/20001",
15+
"description": "browse item with variation_slice",
16+
"deactivated": false
17+
},
18+
"value": "BrowseItem1",
19+
"is_slotted": false,
20+
"labels": {},
21+
"variation_slice": {
22+
"variation_id": ["901764002"]
23+
}
24+
}, {
25+
"matched_terms": [],
26+
"data": {
27+
"id": "20002",
28+
"url": "https://test.com/p/20002",
29+
"facets": [{
30+
"name": "Color",
31+
"values": ["blue"]
32+
}],
33+
"image_url": "https://test.com/p/20002",
34+
"description": "browse item with variation_slice",
35+
"deactivated": false
36+
},
37+
"value": "BrowseItem2",
38+
"is_slotted": false,
39+
"labels": {},
40+
"variation_slice": {
41+
"variation_id": ["904422022"]
42+
}
43+
}],
44+
"total_num_results": 2,
45+
"result_id": "test-browse-result-id"
46+
},
47+
"result_id": "test-browse-result-id",
48+
"request": {
49+
"browse_filter_name": "Color",
50+
"browse_filter_value": "Blue"
51+
}
52+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"response": {
3+
"facets": [],
4+
"groups": [],
5+
"results": [{
6+
"matched_terms": ["item"],
7+
"data": {
8+
"id": "10001",
9+
"url": "https://test.com/p/10001",
10+
"facets": [{
11+
"name": "Price",
12+
"values": [30]
13+
}],
14+
"image_url": "https://test.com/p/10001",
15+
"description": "item with variation_slice",
16+
"deactivated": false
17+
},
18+
"value": "Item1",
19+
"is_slotted": false,
20+
"labels": {},
21+
"variation_slice": {
22+
"variation_id": ["801764002"]
23+
}
24+
}, {
25+
"matched_terms": ["item"],
26+
"data": {
27+
"id": "10002",
28+
"url": "https://test.com/p/10002",
29+
"facets": [{
30+
"name": "Price",
31+
"values": [20]
32+
}],
33+
"image_url": "https://test.com/p/10002",
34+
"description": "item with variation_slice",
35+
"deactivated": false
36+
},
37+
"value": "Item2",
38+
"is_slotted": false,
39+
"labels": {},
40+
"variation_slice": {
41+
"variation_id": ["804422022"]
42+
}
43+
}],
44+
"total_num_results": 2,
45+
"result_id": "test-result-id"
46+
},
47+
"result_id": "test-result-id",
48+
"request": {}
49+
}

0 commit comments

Comments
 (0)