Skip to content

Commit 07c0301

Browse files
committed
add validation for items size
1 parent 7c488f3 commit 07c0301

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

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

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

18141814
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+
}
18151819
url =
18161820
url.newBuilder()
18171821
.addQueryParameter("variation_id", req.getVariationId())

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,35 @@ public void getRecommendationsShouldReturnAResultWithItemIdAndVariationId() thro
3737
request.setItemIds(Arrays.asList("power_drill"));
3838
request.setVariationId("power_drill_variation");
3939
RecommendationsResponse response = constructor.recommendations(request, userInfo);
40-
assertTrue("recommendation results exist", response.getResponse().getResults().size() >= 0);
40+
assertTrue("recommendation results exist", response.getResponse().getResults().size() > 0);
4141
assertTrue("recommendation result id exists", response.getResultId() != null);
4242
}
4343

44+
@Test
45+
public void getRecommendationsShouldErrorWithVariationIdAndNoItemIds() throws Exception {
46+
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);
47+
UserInfo userInfo = new UserInfo(3, "c62a-2a09-faie");
48+
RecommendationsRequest request = new RecommendationsRequest("item_page_1");
49+
request.setVariationId("power_drill_variation");
50+
51+
thrown.expect(ConstructorException.class);
52+
thrown.expectMessage("variationId requires exactly one itemId to be specified");
53+
constructor.recommendations(request, userInfo);
54+
}
55+
56+
@Test
57+
public void getRecommendationsShouldErrorWithVariationIdAndMultipleItemIds() throws Exception {
58+
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);
59+
UserInfo userInfo = new UserInfo(3, "c62a-2a09-faie");
60+
RecommendationsRequest request = new RecommendationsRequest("item_page_1");
61+
request.setItemIds(Arrays.asList("power_drill", "drill"));
62+
request.setVariationId("power_drill_variation");
63+
64+
thrown.expect(ConstructorException.class);
65+
thrown.expectMessage("variationId requires exactly one itemId to be specified");
66+
constructor.recommendations(request, userInfo);
67+
}
68+
4469
@Test
4570
public void getRecommendationsShouldReturnAResultWithMultipleItemIds() throws Exception {
4671
ConstructorIO constructor = new ConstructorIO("", apiKey, true, null);

0 commit comments

Comments
 (0)