diff --git a/README.md b/README.md index e52f6d2..32c03ad 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The client is available in the [Maven Central Repository](https://mvnrepository. com.recombee api-client - 1.4.0 + 1.5.0 ``` diff --git a/pom.xml b/pom.xml index 7bc3835..f624887 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.recombee api-client - 1.4.0 + 1.5.0 Recombee API Client A client library for easy use of the Recombee recommendation API http://recombee.com diff --git a/src/main/java/com/recombee/api_client/RecombeeClient.java b/src/main/java/com/recombee/api_client/RecombeeClient.java index 1e0c4dd..c85a445 100644 --- a/src/main/java/com/recombee/api_client/RecombeeClient.java +++ b/src/main/java/com/recombee/api_client/RecombeeClient.java @@ -59,6 +59,8 @@ import com.recombee.api_client.api_requests.ListUserCartAdditions; import com.recombee.api_client.api_requests.ListItemBookmarks; import com.recombee.api_client.api_requests.ListUserBookmarks; +import com.recombee.api_client.api_requests.ListItemViewPortions; +import com.recombee.api_client.api_requests.ListUserViewPortions; import com.recombee.api_client.api_requests.UserBasedRecommendation; import com.recombee.api_client.api_requests.ItemBasedRecommendation; @@ -77,7 +79,7 @@ public class RecombeeClient { final int BATCH_MAX_SIZE = 10000; //Maximal number of requests within one batch request - final String USER_AGENT = "recombee-java-api-client/1.4.0"; + final String USER_AGENT = "recombee-java-api-client/1.5.0"; public RecombeeClient(String databaseId, String token) { this.databaseId = databaseId; @@ -278,6 +280,26 @@ public Bookmark[] send(ListUserBookmarks request) throws ApiException { return null; } + public ViewPortion[] send(ListItemViewPortions request) throws ApiException { + String responseStr = sendRequest(request); + try { + return this.mapper.readValue(responseStr, ViewPortion[].class); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public ViewPortion[] send(ListUserViewPortions request) throws ApiException { + String responseStr = sendRequest(request); + try { + return this.mapper.readValue(responseStr, ViewPortion[].class); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + /* End of the generated code */ public BatchResponse[] send(Batch batchRequest) throws ApiException { @@ -502,6 +524,22 @@ else if (request instanceof ListUserBookmarks) for(int j=0;j> array = (ArrayList>) parsedResponse; + ViewPortion[] ar = new ViewPortion[array.size()]; + for(int j=0;j> array = (ArrayList>) parsedResponse; + ViewPortion[] ar = new ViewPortion[array.size()]; + for(int j=0;j getQueryParameters() { + HashMap params = new HashMap(); + params.put("userId", this.userId.toString()); + params.put("itemId", this.itemId.toString()); + if (this.sessionId!=null) { + params.put("sessionId", this.sessionId.toString()); + } + return params; + } + + /** + * Get body parameters + * @return Values of body parameters (name of parameter: value of the parameter) + */ + @Override + public Map getBodyParameters() { + HashMap params = new HashMap(); + return params; + } + +} diff --git a/src/main/java/com/recombee/api_client/api_requests/ListItemViewPortions.java b/src/main/java/com/recombee/api_client/api_requests/ListItemViewPortions.java new file mode 100644 index 0000000..f038dec --- /dev/null +++ b/src/main/java/com/recombee/api_client/api_requests/ListItemViewPortions.java @@ -0,0 +1,74 @@ +package com.recombee.api_client.api_requests; + +/* + This file is auto-generated, do not edit +*/ + +import java.util.Date; +import java.util.Map; +import java.util.HashMap; + +import com.recombee.api_client.util.HTTPMethod; + +/** + * The view portions feature is currently experimental. + * List all the view portions of an item ever submitted by different users. + */ +public class ListItemViewPortions extends Request { + + /** + * ID of the item of which the view portions are to be listed. + */ + protected String itemId; + + /** + * Construct the request + * @param itemId ID of the item of which the view portions are to be listed. + */ + public ListItemViewPortions (String itemId) { + this.itemId = itemId; + this.timeout = 100000; + } + + + public String getItemId() { + return this.itemId; + } + + /** + * @return Used HTTP method + */ + @Override + public HTTPMethod getHTTPMethod() { + return HTTPMethod.GET; + } + + /** + * @return URI to the endpoint including path parameters + */ + @Override + public String getPath() { + return String.format("/items/%s/viewportions/", this.itemId); + } + + /** + * Get query parameters + * @return Values of query parameters (name of parameter: value of the parameter) + */ + @Override + public Map getQueryParameters() { + HashMap params = new HashMap(); + return params; + } + + /** + * Get body parameters + * @return Values of body parameters (name of parameter: value of the parameter) + */ + @Override + public Map getBodyParameters() { + HashMap params = new HashMap(); + return params; + } + +} diff --git a/src/main/java/com/recombee/api_client/api_requests/ListUserViewPortions.java b/src/main/java/com/recombee/api_client/api_requests/ListUserViewPortions.java new file mode 100644 index 0000000..1136e80 --- /dev/null +++ b/src/main/java/com/recombee/api_client/api_requests/ListUserViewPortions.java @@ -0,0 +1,74 @@ +package com.recombee.api_client.api_requests; + +/* + This file is auto-generated, do not edit +*/ + +import java.util.Date; +import java.util.Map; +import java.util.HashMap; + +import com.recombee.api_client.util.HTTPMethod; + +/** + * The view portions feature is currently experimental. + * List all the view portions ever submitted by a given user. + */ +public class ListUserViewPortions extends Request { + + /** + * ID of the user whose view portions are to be listed. + */ + protected String userId; + + /** + * Construct the request + * @param userId ID of the user whose view portions are to be listed. + */ + public ListUserViewPortions (String userId) { + this.userId = userId; + this.timeout = 100000; + } + + + public String getUserId() { + return this.userId; + } + + /** + * @return Used HTTP method + */ + @Override + public HTTPMethod getHTTPMethod() { + return HTTPMethod.GET; + } + + /** + * @return URI to the endpoint including path parameters + */ + @Override + public String getPath() { + return String.format("/users/%s/viewportions/", this.userId); + } + + /** + * Get query parameters + * @return Values of query parameters (name of parameter: value of the parameter) + */ + @Override + public Map getQueryParameters() { + HashMap params = new HashMap(); + return params; + } + + /** + * Get body parameters + * @return Values of body parameters (name of parameter: value of the parameter) + */ + @Override + public Map getBodyParameters() { + HashMap params = new HashMap(); + return params; + } + +} diff --git a/src/main/java/com/recombee/api_client/api_requests/SetViewPortion.java b/src/main/java/com/recombee/api_client/api_requests/SetViewPortion.java new file mode 100644 index 0000000..80bf6ba --- /dev/null +++ b/src/main/java/com/recombee/api_client/api_requests/SetViewPortion.java @@ -0,0 +1,155 @@ +package com.recombee.api_client.api_requests; + +/* + This file is auto-generated, do not edit +*/ + +import java.util.Date; +import java.util.Map; +import java.util.HashMap; + +import com.recombee.api_client.util.HTTPMethod; + +/** + * The view portions feature is currently experimental. + * Sets viewed portion of an item (for example a video or article) by a user (at a session). + * If you send new request with the same (`userId`, `itemId`, `sessionId`), the portion gets updated. + */ +public class SetViewPortion extends Request { + + /** + * User who viewed a portion of the item + */ + protected String userId; + /** + * Viewed item + */ + protected String itemId; + /** + * Id of session in which the user viewed the item + */ + protected String sessionId; + /** + * UTC timestamp of the rating as ISO8601-1 pattern or UTC epoch time. The default value is the current time. + */ + protected Date timestamp; + /** + * Viewed portion of the item (number between 0.0 (viewed nothing) and 1.0 (viewed full item) ). + */ + protected Double portion; + /** + * Sets whether the given user/item should be created if not present in the database. + */ + protected Boolean cascadeCreate; + + /** + * Construct the request + * @param userId User who viewed a portion of the item + * @param itemId Viewed item + * @param portion Viewed portion of the item (number between 0.0 (viewed nothing) and 1.0 (viewed full item) ). + */ + public SetViewPortion (String userId,String itemId,double portion) { + this.userId = userId; + this.itemId = itemId; + this.portion = portion; + this.timeout = 1000; + } + + /** + * @param sessionId Id of session in which the user viewed the item + */ + public SetViewPortion setSessionId(String sessionId) { + this.sessionId = sessionId; + return this; + } + + /** + * @param timestamp UTC timestamp of the rating as ISO8601-1 pattern or UTC epoch time. The default value is the current time. + */ + public SetViewPortion setTimestamp(Date timestamp) { + this.timestamp = timestamp; + return this; + } + + /** + * @param cascadeCreate Sets whether the given user/item should be created if not present in the database. + */ + public SetViewPortion setCascadeCreate(boolean cascadeCreate) { + this.cascadeCreate = cascadeCreate; + return this; + } + + public String getUserId() { + return this.userId; + } + + public String getItemId() { + return this.itemId; + } + + public String getSessionId() { + return this.sessionId; + } + + public Date getTimestamp() { + return this.timestamp; + } + + public double getPortion() { + return this.portion; + } + + public boolean getCascadeCreate() { + if (this.cascadeCreate==null) return false; + return this.cascadeCreate; + } + + /** + * @return Used HTTP method + */ + @Override + public HTTPMethod getHTTPMethod() { + return HTTPMethod.POST; + } + + /** + * @return URI to the endpoint including path parameters + */ + @Override + public String getPath() { + return "/viewportions/"; + } + + /** + * Get query parameters + * @return Values of query parameters (name of parameter: value of the parameter) + */ + @Override + public Map getQueryParameters() { + HashMap params = new HashMap(); + return params; + } + + /** + * Get body parameters + * @return Values of body parameters (name of parameter: value of the parameter) + */ + @Override + public Map getBodyParameters() { + HashMap params = new HashMap(); + params.put("userId", this.userId); + params.put("itemId", this.itemId); + params.put("portion", this.portion); + if (this.sessionId!=null) { + params.put("sessionId", this.sessionId); + } + if (this.timestamp!=null) { + params.put("timestamp", this.timestamp.getTime()/1000.0); + } + if (this.cascadeCreate!=null) { + params.put("cascadeCreate", this.cascadeCreate); + } + return params; + } + +} diff --git a/src/main/java/com/recombee/api_client/bindings/ViewPortion.java b/src/main/java/com/recombee/api_client/bindings/ViewPortion.java new file mode 100644 index 0000000..315f45f --- /dev/null +++ b/src/main/java/com/recombee/api_client/bindings/ViewPortion.java @@ -0,0 +1,105 @@ +package com.recombee.api_client.bindings; + +/* + This file is auto-generated, do not edit +*/ + +import org.apache.commons.lang.builder.EqualsBuilder; +import org.apache.commons.lang.builder.HashCodeBuilder; +import java.util.Map; +import java.util.Date; + +public class ViewPortion extends RecombeeBinding { + /** + * User who viewed a portion of the item + */ + protected String userId; + /** + * Viewed item + */ + protected String itemId; + /** + * Id of session in which the user viewed the item + */ + protected String sessionId; + /** + * UTC timestamp of the rating as ISO8601-1 pattern or UTC epoch time. The default value is the current time. + */ + protected Date timestamp; + /** + * Viewed portion of the item (number between 0.0 (viewed nothing) and 1.0 (viewed full item) ). + */ + protected Double portion; + + public ViewPortion () {} + + public ViewPortion (String userId,String itemId,String sessionId,Date timestamp,double portion) { + this.userId = userId; + this.itemId = itemId; + this.sessionId = sessionId; + this.timestamp = timestamp; + this.portion = portion; + } + + public ViewPortion (Map jsonObject) { + this.userId = (String) jsonObject.get("userId"); + this.itemId = (String) jsonObject.get("itemId"); + this.sessionId = (String) jsonObject.get("sessionId"); + this.portion = (Double) jsonObject.get("portion"); + Double epoch = 1000*(Double)jsonObject.get("timestamp"); + this.timestamp = new Date(epoch.longValue()); + } + + public void setTimestamp(double epoch) + { + this.timestamp = new Date( (long) (1000 * epoch)); + } + + public String getUserId() { + return this.userId; + } + + public String getItemId() { + return this.itemId; + } + + public String getSessionId() { + return this.sessionId; + } + + public Date getTimestamp() { + return this.timestamp; + } + + public double getPortion() { + return this.portion; + } + + @Override + public int hashCode() { + return new HashCodeBuilder(17, 31). + append(this.userId). + append(this.itemId). + append(this.sessionId). + append(this.timestamp). + append(this.portion). + toHashCode(); + } + + @Override + public boolean equals(Object obj) { + if (!(obj instanceof ViewPortion)) + return false; + if (obj == this) + return true; + + ViewPortion rhs = (ViewPortion) obj; + return new EqualsBuilder(). + append(this.userId, rhs.userId). + append(this.itemId, rhs.itemId). + append(this.sessionId, rhs.sessionId). + append(this.timestamp, rhs.timestamp). + append(this.portion, rhs.portion). + isEquals(); + } +} diff --git a/src/test/java/com/recombee/api_client/DeleteViewPortionBatchTest.java b/src/test/java/com/recombee/api_client/DeleteViewPortionBatchTest.java new file mode 100644 index 0000000..f03141e --- /dev/null +++ b/src/test/java/com/recombee/api_client/DeleteViewPortionBatchTest.java @@ -0,0 +1,33 @@ +package com.recombee.api_client; +/* + This file is auto-generated, do not edit +*/ + + +import com.recombee.api_client.api_requests.*; +import com.recombee.api_client.bindings.*; +import com.recombee.api_client.exceptions.ApiException; +import com.recombee.api_client.exceptions.ResponseException; + +import java.util.HashMap; +import java.util.Date; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class DeleteViewPortionBatchTest extends InteractionsTestCase { + + @Test + public void testDeleteViewPortion() throws ApiException { + Request[] requests = new Request[] { + new DeleteViewPortion("user","item"), + new DeleteViewPortion("user","item") + }; + + BatchResponse[] responses = this.client.send(new Batch(requests)); + assertEquals(200,responses[0].getStatusCode()); + assertEquals(404,responses[1].getStatusCode()); + } +} diff --git a/src/test/java/com/recombee/api_client/DeleteViewPortionTest.java b/src/test/java/com/recombee/api_client/DeleteViewPortionTest.java new file mode 100644 index 0000000..ae10f31 --- /dev/null +++ b/src/test/java/com/recombee/api_client/DeleteViewPortionTest.java @@ -0,0 +1,38 @@ +package com.recombee.api_client; +/* + This file is auto-generated, do not edit +*/ + + +import com.recombee.api_client.api_requests.*; +import com.recombee.api_client.bindings.*; +import com.recombee.api_client.exceptions.ApiException; +import com.recombee.api_client.exceptions.ResponseException; + +import java.util.HashMap; +import java.util.Date; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class DeleteViewPortionTest extends InteractionsTestCase { + + @Test + public void testDeleteViewPortion() throws ApiException { + DeleteViewPortion req; + Request req2; + String resp; + // it 'does not fail with existing entity id' + req = new DeleteViewPortion("user","item"); + resp = this.client.send(req); + req = new DeleteViewPortion("user","item"); + try { + this.client.send(req); + fail("No exception thrown"); + } catch (ResponseException ex) { + assertEquals(404,ex.getStatusCode()); + } + } +} diff --git a/src/test/java/com/recombee/api_client/InteractionsTestCase.java b/src/test/java/com/recombee/api_client/InteractionsTestCase.java index 6f770fe..9bcee6d 100644 --- a/src/test/java/com/recombee/api_client/InteractionsTestCase.java +++ b/src/test/java/com/recombee/api_client/InteractionsTestCase.java @@ -24,7 +24,8 @@ public void setUp() throws ApiException { new AddPurchase("user", "item").setTimestamp(new Date(0)), new AddRating("user", "item", 1).setTimestamp(new Date(0)), new AddCartAddition("user", "item").setTimestamp(new Date(0)), - new AddBookmark("user", "item").setTimestamp(new Date(0)) + new AddBookmark("user", "item").setTimestamp(new Date(0)), + new SetViewPortion("user", "item", 1).setTimestamp(new Date(0)) }; this.client.send(new Batch(requests)); diff --git a/src/test/java/com/recombee/api_client/ListItemViewPortionsBatchTest.java b/src/test/java/com/recombee/api_client/ListItemViewPortionsBatchTest.java new file mode 100644 index 0000000..c4e4f98 --- /dev/null +++ b/src/test/java/com/recombee/api_client/ListItemViewPortionsBatchTest.java @@ -0,0 +1,34 @@ +package com.recombee.api_client; +/* + This file is auto-generated, do not edit +*/ + + +import com.recombee.api_client.api_requests.*; +import com.recombee.api_client.bindings.*; +import com.recombee.api_client.exceptions.ApiException; +import com.recombee.api_client.exceptions.ResponseException; + +import java.util.HashMap; +import java.util.Date; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class ListItemViewPortionsBatchTest extends InteractionsTestCase { + + @Test + public void testListItemViewPortions() throws ApiException { + Request[] requests = new Request[] { + new ListItemViewPortions("item") + }; + + BatchResponse[] responses = this.client.send(new Batch(requests)); + assertEquals(200,responses[0].getStatusCode()); + assertEquals(1, ((ViewPortion []) responses[0].getResponse()).length); + assertEquals ("item",((ViewPortion []) responses[0].getResponse())[0].getItemId()); + assertEquals ("user",((ViewPortion []) responses[0].getResponse())[0].getUserId()); + } +} diff --git a/src/test/java/com/recombee/api_client/ListItemViewPortionsTest.java b/src/test/java/com/recombee/api_client/ListItemViewPortionsTest.java new file mode 100644 index 0000000..79352f3 --- /dev/null +++ b/src/test/java/com/recombee/api_client/ListItemViewPortionsTest.java @@ -0,0 +1,34 @@ +package com.recombee.api_client; +/* + This file is auto-generated, do not edit +*/ + + +import com.recombee.api_client.api_requests.*; +import com.recombee.api_client.bindings.*; +import com.recombee.api_client.exceptions.ApiException; +import com.recombee.api_client.exceptions.ResponseException; + +import java.util.HashMap; +import java.util.Date; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class ListItemViewPortionsTest extends InteractionsTestCase { + + @Test + public void testListItemViewPortions() throws ApiException { + ListItemViewPortions req; + Request req2; + ViewPortion [] resp; + // it 'lists interactions' + req = new ListItemViewPortions("item"); + resp = this.client.send(req); + assertEquals(1, resp.length); + assertEquals ("item",resp[0].getItemId()); + assertEquals ("user",resp[0].getUserId()); + } +} diff --git a/src/test/java/com/recombee/api_client/ListUserViewPortionsBatchTest.java b/src/test/java/com/recombee/api_client/ListUserViewPortionsBatchTest.java new file mode 100644 index 0000000..640bbfa --- /dev/null +++ b/src/test/java/com/recombee/api_client/ListUserViewPortionsBatchTest.java @@ -0,0 +1,34 @@ +package com.recombee.api_client; +/* + This file is auto-generated, do not edit +*/ + + +import com.recombee.api_client.api_requests.*; +import com.recombee.api_client.bindings.*; +import com.recombee.api_client.exceptions.ApiException; +import com.recombee.api_client.exceptions.ResponseException; + +import java.util.HashMap; +import java.util.Date; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class ListUserViewPortionsBatchTest extends InteractionsTestCase { + + @Test + public void testListUserViewPortions() throws ApiException { + Request[] requests = new Request[] { + new ListUserViewPortions("user") + }; + + BatchResponse[] responses = this.client.send(new Batch(requests)); + assertEquals(200,responses[0].getStatusCode()); + assertEquals(1, ((ViewPortion []) responses[0].getResponse()).length); + assertEquals ("item",((ViewPortion []) responses[0].getResponse())[0].getItemId()); + assertEquals ("user",((ViewPortion []) responses[0].getResponse())[0].getUserId()); + } +} diff --git a/src/test/java/com/recombee/api_client/ListUserViewPortionsTest.java b/src/test/java/com/recombee/api_client/ListUserViewPortionsTest.java new file mode 100644 index 0000000..10c0c75 --- /dev/null +++ b/src/test/java/com/recombee/api_client/ListUserViewPortionsTest.java @@ -0,0 +1,34 @@ +package com.recombee.api_client; +/* + This file is auto-generated, do not edit +*/ + + +import com.recombee.api_client.api_requests.*; +import com.recombee.api_client.bindings.*; +import com.recombee.api_client.exceptions.ApiException; +import com.recombee.api_client.exceptions.ResponseException; + +import java.util.HashMap; +import java.util.Date; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class ListUserViewPortionsTest extends InteractionsTestCase { + + @Test + public void testListUserViewPortions() throws ApiException { + ListUserViewPortions req; + Request req2; + ViewPortion [] resp; + // it 'lists user interactions' + req = new ListUserViewPortions("user"); + resp = this.client.send(req); + assertEquals(1, resp.length); + assertEquals ("item",resp[0].getItemId()); + assertEquals ("user",resp[0].getUserId()); + } +} diff --git a/src/test/java/com/recombee/api_client/SetViewPortionBatchTest.java b/src/test/java/com/recombee/api_client/SetViewPortionBatchTest.java new file mode 100644 index 0000000..ad78361 --- /dev/null +++ b/src/test/java/com/recombee/api_client/SetViewPortionBatchTest.java @@ -0,0 +1,43 @@ +package com.recombee.api_client; +/* + This file is auto-generated, do not edit +*/ + + +import com.recombee.api_client.api_requests.*; +import com.recombee.api_client.bindings.*; +import com.recombee.api_client.exceptions.ApiException; +import com.recombee.api_client.exceptions.ResponseException; + +import java.util.HashMap; +import java.util.Date; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class SetViewPortionBatchTest extends RecombeeTestCase { + + @Test + public void testSetViewPortion() throws ApiException { + Request[] requests = new Request[] { + new SetViewPortion("u_id","i_id",1).setCascadeCreate(true), + new SetViewPortion("entity_id","entity_id",0), + new SetViewPortion("entity_id","nonex_id",1), + new SetViewPortion("nonex_id","entity_id",0.5), + new SetViewPortion("entity_id","entity_id",0).setTimestamp(new Date(-15)), + new SetViewPortion("entity_id","entity_id",-2), + new SetViewPortion("entity_id","entity_id",0.7).setSessionId("a****") + }; + + BatchResponse[] responses = this.client.send(new Batch(requests)); + assertEquals(200,responses[0].getStatusCode()); + assertEquals(200,responses[1].getStatusCode()); + assertEquals(404,responses[2].getStatusCode()); + assertEquals(404,responses[3].getStatusCode()); + assertEquals(400,responses[4].getStatusCode()); + assertEquals(400,responses[5].getStatusCode()); + assertEquals(400,responses[6].getStatusCode()); + } +} diff --git a/src/test/java/com/recombee/api_client/SetViewPortionTest.java b/src/test/java/com/recombee/api_client/SetViewPortionTest.java new file mode 100644 index 0000000..c8bdd4f --- /dev/null +++ b/src/test/java/com/recombee/api_client/SetViewPortionTest.java @@ -0,0 +1,74 @@ +package com.recombee.api_client; +/* + This file is auto-generated, do not edit +*/ + + +import com.recombee.api_client.api_requests.*; +import com.recombee.api_client.bindings.*; +import com.recombee.api_client.exceptions.ApiException; +import com.recombee.api_client.exceptions.ResponseException; + +import java.util.HashMap; +import java.util.Date; + +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class SetViewPortionTest extends RecombeeTestCase { + + @Test + public void testSetViewPortion() throws ApiException { + SetViewPortion req; + Request req2; + String resp; + // it 'does not fail with cascadeCreate' + req = new SetViewPortion("u_id","i_id",1).setCascadeCreate(true); + resp = this.client.send(req); + // it 'does not fail with existing item and user' + req = new SetViewPortion("entity_id","entity_id",0); + resp = this.client.send(req); + // it 'fails with nonexisting item id' + req = new SetViewPortion("entity_id","nonex_id",1); + try { + this.client.send(req); + fail("No exception thrown"); + } catch (ResponseException ex) { + assertEquals(404,ex.getStatusCode()); + } + // it 'fails with nonexisting user id' + req = new SetViewPortion("nonex_id","entity_id",0.5); + try { + this.client.send(req); + fail("No exception thrown"); + } catch (ResponseException ex) { + assertEquals(404,ex.getStatusCode()); + } + // it 'fails with invalid time' + req = new SetViewPortion("entity_id","entity_id",0).setTimestamp(new Date(-15)); + try { + this.client.send(req); + fail("No exception thrown"); + } catch (ResponseException ex) { + assertEquals(400,ex.getStatusCode()); + } + // it 'fails with invalid portion' + req = new SetViewPortion("entity_id","entity_id",-2); + try { + this.client.send(req); + fail("No exception thrown"); + } catch (ResponseException ex) { + assertEquals(400,ex.getStatusCode()); + } + // it 'fails with invalid sessionId' + req = new SetViewPortion("entity_id","entity_id",0.7).setSessionId("a****"); + try { + this.client.send(req); + fail("No exception thrown"); + } catch (ResponseException ex) { + assertEquals(400,ex.getStatusCode()); + } + } +}