Skip to content

Commit ee29c51

Browse files
quiteeasyesezen
andauthored
[CDX-380] Add support for variationId param in RecommendationsRequest (#183)
* [CDX-380] Add support for variationId param in RecommendationsRequest * add validation for items size * change validation, update test * move expression * lint * Addresss comments --------- Co-authored-by: Enes Kutay SEZEN <eneskutaysezen@gmail.com>
1 parent 138e686 commit ee29c51

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,17 @@ public String recommendationsAsJSON(RecommendationsRequest req, UserInfo userInf
18111811
}
18121812
}
18131813

1814+
if (StringUtils.isNotBlank(req.getVariationId())) {
1815+
if (req.getItemIds() == null || req.getItemIds().size() != 1) {
1816+
throw new IllegalArgumentException(
1817+
"variationId requires exactly one itemId to be specified");
1818+
}
1819+
url =
1820+
url.newBuilder()
1821+
.addQueryParameter("variation_id", req.getVariationId())
1822+
.build();
1823+
}
1824+
18141825
if (StringUtils.isNotBlank(req.getTerm())) {
18151826
url = url.newBuilder().addQueryParameter("term", req.getTerm()).build();
18161827
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public class RecommendationsRequest {
1212
private String term;
1313
private int numResults;
1414
private List<String> itemIds;
15+
private String variationId;
1516
private Map<String, List<String>> facets;
1617
private String section;
1718
private String preFilterExpression;
@@ -32,6 +33,7 @@ public RecommendationsRequest(String podId) throws IllegalArgumentException {
3233
this.podId = podId;
3334
this.numResults = 10;
3435
this.itemIds = null;
36+
this.variationId = null;
3537
this.term = null;
3638
this.section = "Products";
3739
this.variationsMap = null;
@@ -97,6 +99,22 @@ public List<String> getItemIds() {
9799
return itemIds;
98100
}
99101

102+
/**
103+
* @param variationId the variation id to set. Can be used with exactly one item_id specified in
104+
* the request. Applicable for alternative_items, complementary_items, and bundles pod
105+
* types.
106+
*/
107+
public void setVariationId(String variationId) {
108+
this.variationId = variationId;
109+
}
110+
111+
/**
112+
* @return the variation id
113+
*/
114+
public String getVariationId() {
115+
return variationId;
116+
}
117+
100118
/**
101119
* @param section the section to set
102120
*/

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,47 @@ public void getRecommendationsShouldReturnAResultWithSingleItemId() throws Excep
2929
assertTrue("recommendation result id exists", response.getResultId() != null);
3030
}
3131

32+
@Test
33+
public void getRecommendationsShouldReturnAResultWithItemIdAndVariationId() throws Exception {
34+
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);
35+
UserInfo userInfo = new UserInfo(3, "c62a-2a09-faie");
36+
RecommendationsRequest request = new RecommendationsRequest("item_page_1");
37+
request.setItemIds(Arrays.asList("power_drill"));
38+
request.setVariationId("power_drill_variation");
39+
RecommendationsResponse response = constructor.recommendations(request, userInfo);
40+
assertEquals(
41+
"variation_id in request should match the variationId set",
42+
"power_drill_variation",
43+
response.getRequest().get("variation_id"));
44+
assertTrue("recommendation results exist", response.getResponse().getResults().size() >= 0);
45+
assertTrue("recommendation result id exists", response.getResultId() != null);
46+
}
47+
48+
@Test
49+
public void getRecommendationsShouldErrorWithVariationIdAndNoItemIds() throws Exception {
50+
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);
51+
UserInfo userInfo = new UserInfo(3, "c62a-2a09-faie");
52+
RecommendationsRequest request = new RecommendationsRequest("item_page_1");
53+
request.setVariationId("power_drill_variation");
54+
55+
thrown.expect(ConstructorException.class);
56+
thrown.expectMessage("variationId requires exactly one itemId to be specified");
57+
constructor.recommendations(request, userInfo);
58+
}
59+
60+
@Test
61+
public void getRecommendationsShouldErrorWithVariationIdAndMultipleItemIds() throws Exception {
62+
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);
63+
UserInfo userInfo = new UserInfo(3, "c62a-2a09-faie");
64+
RecommendationsRequest request = new RecommendationsRequest("item_page_1");
65+
request.setItemIds(Arrays.asList("power_drill", "drill"));
66+
request.setVariationId("power_drill_variation");
67+
68+
thrown.expect(ConstructorException.class);
69+
thrown.expectMessage("variationId requires exactly one itemId to be specified");
70+
constructor.recommendations(request, userInfo);
71+
}
72+
3273
@Test
3374
public void getRecommendationsShouldReturnAResultWithMultipleItemIds() throws Exception {
3475
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void newShouldReturnDefaultProperties() throws Exception {
3535
assertEquals(request.getPodId(), podId);
3636
assertEquals(request.getNumResults(), 10);
3737
assertNull(request.getItemIds());
38+
assertNull(request.getVariationId());
3839
assertNull(request.getTerm());
3940
assertEquals(request.getSection(), "Products");
4041
assertEquals(request.getFacets().size(), 0);
@@ -53,7 +54,8 @@ public void settersShouldSet() throws Exception {
5354

5455
request.setPodId("zero_results_1");
5556
request.setNumResults(3);
56-
request.setItemIds(Arrays.asList("1", "2", "3"));
57+
request.setItemIds(Arrays.asList("1"));
58+
request.setVariationId("variation-1");
5759
request.setSection("Search Suggestions");
5860
request.setFacets(facets);
5961
request.setTerm(term);
@@ -62,6 +64,8 @@ public void settersShouldSet() throws Exception {
6264

6365
assertEquals(request.getPodId(), "zero_results_1");
6466
assertEquals(request.getNumResults(), 3);
67+
assertEquals(request.getItemIds(), Arrays.asList("1"));
68+
assertEquals(request.getVariationId(), "variation-1");
6569
assertEquals(request.getSection(), "Search Suggestions");
6670
assertEquals(request.getFacets(), facets);
6771
assertEquals(request.getTerm(), term);

0 commit comments

Comments
 (0)