use of com.ibm.watson.discovery.v1.model.CreateEventOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryServiceTest method createEventIsSuccessful.
/**
* Creates the event is successful.
*
* @throws InterruptedException the interrupted exception
*/
@Test
public void createEventIsSuccessful() throws InterruptedException {
server.enqueue(jsonResponse(createEventResp));
Long displayRank = 1L;
String sessionToken = "mock_session_token";
EventData eventData = new EventData.Builder().environmentId(environmentId).collectionId(collectionId).documentId(documentId).displayRank(displayRank).sessionToken(sessionToken).clientTimestamp(date).build();
CreateEventOptions createEventOptions = new CreateEventOptions.Builder().type(CreateEventOptions.Type.CLICK).data(eventData).build();
CreateEventResponse response = discoveryService.createEvent(createEventOptions).execute().getResult();
RecordedRequest request = server.takeRequest();
assertEquals(CREATE_EVENT_PATH, request.getPath());
assertEquals(POST, request.getMethod());
assertEquals(CreateEventOptions.Type.CLICK, response.getType());
assertEquals(environmentId, response.getData().environmentId());
assertEquals(collectionId, response.getData().collectionId());
assertEquals(documentId, response.getData().documentId());
assertNotNull(response.getData().clientTimestamp());
assertEquals(displayRank, response.getData().displayRank());
assertEquals(queryId, response.getData().queryId());
assertEquals(sessionToken, response.getData().sessionToken());
}
use of com.ibm.watson.discovery.v1.model.CreateEventOptions in project java-sdk by watson-developer-cloud.
the class DiscoveryTest method testCreateEventWOptions.
// Test the createEvent operation with a valid options model parameter
@Test
public void testCreateEventWOptions() throws Throwable {
// Register a mock response
String mockResponseBody = "{\"type\": \"click\", \"data\": {\"environment_id\": \"environmentId\", \"session_token\": \"sessionToken\", \"client_timestamp\": \"2019-01-01T12:00:00.000Z\", \"display_rank\": 11, \"collection_id\": \"collectionId\", \"document_id\": \"documentId\", \"query_id\": \"queryId\"}}";
String createEventPath = "/v1/events";
server.enqueue(new MockResponse().setHeader("Content-type", "application/json").setResponseCode(201).setBody(mockResponseBody));
// Construct an instance of the EventData model
EventData eventDataModel = new EventData.Builder().environmentId("testString").sessionToken("testString").clientTimestamp(DateUtils.parseAsDateTime("2019-01-01T12:00:00.000Z")).displayRank(Long.valueOf("26")).collectionId("testString").documentId("testString").build();
// Construct an instance of the CreateEventOptions model
CreateEventOptions createEventOptionsModel = new CreateEventOptions.Builder().type("click").data(eventDataModel).build();
// Invoke createEvent() with a valid options model and verify the result
Response<CreateEventResponse> response = discoveryService.createEvent(createEventOptionsModel).execute();
assertNotNull(response);
CreateEventResponse responseObj = response.getResult();
assertNotNull(responseObj);
// Verify the contents of the request sent to the mock server
RecordedRequest request = server.takeRequest();
assertNotNull(request);
assertEquals(request.getMethod(), "POST");
// Verify request path
String parsedPath = TestUtilities.parseReqPath(request);
assertEquals(parsedPath, createEventPath);
// Verify query params
Map<String, String> query = TestUtilities.parseQueryString(request);
assertNotNull(query);
assertEquals(query.get("version"), "testString");
}
use of com.ibm.watson.discovery.v1.model.CreateEventOptions in project java-sdk by watson-developer-cloud.
the class Discovery method createEvent.
/**
* Create event.
*
* <p>The **Events** API can be used to create log entries that are associated with specific
* queries. For example, you can record which documents in the results set were "clicked" by a
* user and when that click occurred.
*
* @param createEventOptions the {@link CreateEventOptions} containing the options for the call
* @return a {@link ServiceCall} with a result of type {@link CreateEventResponse}
*/
public ServiceCall<CreateEventResponse> createEvent(CreateEventOptions createEventOptions) {
com.ibm.cloud.sdk.core.util.Validator.notNull(createEventOptions, "createEventOptions cannot be null");
RequestBuilder builder = RequestBuilder.post(RequestBuilder.resolveRequestUrl(getServiceUrl(), "/v1/events"));
Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("discovery", "v1", "createEvent");
for (Entry<String, String> header : sdkHeaders.entrySet()) {
builder.header(header.getKey(), header.getValue());
}
builder.header("Accept", "application/json");
builder.query("version", String.valueOf(this.version));
final JsonObject contentJson = new JsonObject();
contentJson.addProperty("type", createEventOptions.type());
contentJson.add("data", com.ibm.cloud.sdk.core.util.GsonSingleton.getGson().toJsonTree(createEventOptions.data()));
builder.bodyJson(contentJson);
ResponseConverter<CreateEventResponse> responseConverter = ResponseConverterUtils.getValue(new com.google.gson.reflect.TypeToken<CreateEventResponse>() {
}.getType());
return createServiceCall(builder.build(), responseConverter);
}
Aggregations