Skip to content

Commit

Permalink
Merge pull request #14 from SiftScience/atpaino_verification_app_browser
Browse files Browse the repository at this point in the history
(For Arjun) Add support for verification event, app and browser complex fields
  • Loading branch information
atpaino authored Mar 1, 2017
2 parents 1fada40 + 97624a5 commit 5c1bee0
Show file tree
Hide file tree
Showing 9 changed files with 316 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGES.MD
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.4 (2017-02-28)
=================

- Add support for $verification event.
- Add support for $app and $browser complex fields.

1.3.1 (2017-01-26)
==================

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ Java 1.7 or later.
<dependency>
<groupId>com.siftscience</groupId>
<artifactId>sift-java</artifactId>
<version>1.3.1</version>
<version>1.4</version>
</dependency>
```
### Gradle
```
dependencies {
compile 'com.siftscience:sift-java:1.3.1'
compile 'com.siftscience:sift-java:1.4'
}
```
### Other
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'signing'
apply plugin: 'java-library-distribution'

group = 'com.siftscience'
version = '1.3.1'
version = '1.4'
sourceCompatibility = 1.7
targetCompatibility = 1.7

Expand Down
77 changes: 77 additions & 0 deletions src/main/java/com/siftscience/model/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.siftscience.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class App {
@Expose @SerializedName("$os") private String operatingSystem;
@Expose @SerializedName("$os_version") private String operatingSystemVersion;
@Expose @SerializedName("$device_manufacturer") private String deviceManufacturer;
@Expose @SerializedName("$device_model") private String deviceModel;
@Expose @SerializedName("$device_unique_id") private String deviceUniqueId;
@Expose @SerializedName("$app_name") private String appName;
@Expose @SerializedName("$app_version") private String appVersion;

public String getOperatingSystem() {
return operatingSystem;
}

public App setOperatingSystem(String operatingSystem) {
this.operatingSystem = operatingSystem;
return this;
}

public String getOperatingSystemVersion() {
return operatingSystemVersion;
}

public App setOperatingSystemVersion(String operatingSystemVersion) {
this.operatingSystemVersion = operatingSystemVersion;
return this;
}

public String getDeviceManufacturer() {
return deviceManufacturer;
}

public App setDeviceManufacturer(String deviceManufacturer) {
this.deviceManufacturer = deviceManufacturer;
return this;
}

public String getDeviceModel() {
return deviceModel;
}

public App setDeviceModel(String deviceModel) {
this.deviceModel = deviceModel;
return this;
}

public String getDeviceUniqueId() {
return deviceUniqueId;
}

public App setDeviceUniqueId(String deviceUniqueId) {
this.deviceUniqueId = deviceUniqueId;
return this;
}

public String getAppName() {
return appName;
}

public App setAppName(String appName) {
this.appName = appName;
return this;
}

public String getAppVersion() {
return appVersion;
}

public App setAppVersion(String appVersion) {
this.appVersion = appVersion;
return this;
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/siftscience/model/Browser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.siftscience.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Browser {
@Expose @SerializedName("$user_agent") private String userAgent;

public String getUserAgent() {
return userAgent;
}

public Browser setUserAgent(String userAgent) {
this.userAgent = userAgent;
return this;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/siftscience/model/LoginFieldSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public static LoginFieldSet fromJson(String json) {
}

@Expose @SerializedName("$login_status") private String loginStatus;
@Expose @SerializedName("$browser") private Browser browser;
@Expose @SerializedName("$app") private App app;

@Override
public String getEventType() {
Expand All @@ -23,4 +25,22 @@ public LoginFieldSet setLoginStatus(String loginStatus) {
this.loginStatus = loginStatus;
return this;
}

public Browser getBrowser() {
return browser;
}

public LoginFieldSet setBrowser(Browser browser) {
this.browser = browser;
return this;
}

public App getApp() {
return app;
}

public LoginFieldSet setApp(App app) {
this.app = app;
return this;
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/siftscience/model/VerificationFieldSet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.siftscience.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class VerificationFieldSet extends EventsApiRequestFieldSet<VerificationFieldSet> {
public static VerificationFieldSet fromJson(String json) {
return gson.fromJson(json, VerificationFieldSet.class);
}

@Expose @SerializedName("$status") private String status;
@Expose @SerializedName("$verification_type") private String verificationType;
@Expose @SerializedName("$verified_value") private String verifiedValue;

@Override
public String getEventType() {
return "$verification";
}

public String getStatus() {
return status;
}

public VerificationFieldSet setStatus(String status) {
this.status = status;
return this;
}

public String getVerificationType() {
return verificationType;
}

public VerificationFieldSet setVerificationType(String verificationType) {
this.verificationType = verificationType;
return this;
}

public String getVerifiedValue() {
return verifiedValue;
}

public VerificationFieldSet setVerifiedValue(String verifiedValue) {
this.verifiedValue = verifiedValue;
return this;
}
}
76 changes: 73 additions & 3 deletions src/test/java/com/siftscience/LoginEventTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.siftscience;

import com.siftscience.model.App;
import com.siftscience.model.Browser;
import com.siftscience.model.LoginFieldSet;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
Expand All @@ -12,13 +14,79 @@
import static java.net.HttpURLConnection.HTTP_OK;

public class LoginEventTest {

@Test
public void testLoginWithApp() throws Exception {
String operatingSystem = "iOS";
String appName = "Calculator";

String expectedRequestBody = "{\n" +
" \"$type\" : \"$login\",\n" +
" \"$api_key\" : \"your_api_key_here\",\n" +
" \"$user_id\" : \"billy_jones_301\",\n" +
" \"$login_status\" : \"$success\",\n" +
" \"$app\" : {\n" +
" \"$os\" : \"" + operatingSystem + "\",\n" +
" \"$app_name\" : \"" + appName + "\"\n" +
" }\n" +
"}";

// Start a new mock server and enqueue a mock response.
MockWebServer server = new MockWebServer();
MockResponse response = new MockResponse();
response.setResponseCode(HTTP_OK);
response.setBody("{\n" +
" \"status\" : 0,\n" +
" \"error_message\" : \"OK\",\n" +
" \"time\" : 1327604222,\n" +
" \"request\" : \"" + TestUtils.unescapeJson(expectedRequestBody) + "\"\n" +
"}");
server.enqueue(response);
server.start();
HttpUrl baseUrl = server.url("");

// Create a new client and link it to the mock server.
SiftClient client = new SiftClient("your_api_key_here");
client.setBaseUrl(baseUrl);

// Build and execute the request against the mock server.
SiftRequest request = client.buildRequest(new LoginFieldSet()
.setUserId("billy_jones_301")
.setLoginStatus("$success")
.setApp(new App()
.setOperatingSystem(operatingSystem)
.setAppName(appName)));

SiftResponse siftResponse = request.send();

// Verify the request.
RecordedRequest request1 = server.takeRequest();
Assert.assertEquals("POST", request1.getMethod());
Assert.assertEquals("/v204/events", request1.getPath());
JSONAssert.assertEquals(expectedRequestBody, request.getFieldSet().toJson(), true);

// Verify the response.
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
Assert.assertEquals(0, (int) siftResponse.getBody().getStatus());
JSONAssert.assertEquals(response.getBody().readUtf8(),
siftResponse.getBody().toJson(), true);

server.shutdown();

}

@Test
public void testLogin() throws Exception {
public void testLoginWithBrowswer() throws Exception {
String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3)";

String expectedRequestBody = "{\n" +
" \"$type\" : \"$login\",\n" +
" \"$api_key\" : \"your_api_key_here\",\n" +
" \"$user_id\" : \"billy_jones_301\",\n" +
" \"$login_status\" : \"$success\"\n" +
" \"$login_status\" : \"$success\",\n" +
" \"$browser\" : {\n" +
" \"$user_agent\" : \"" + userAgent + "\"\n" +
" }\n" +
"}";

// Start a new mock server and enqueue a mock response.
Expand All @@ -42,7 +110,9 @@ public void testLogin() throws Exception {
// Build and execute the request against the mock server.
SiftRequest request = client.buildRequest(new LoginFieldSet()
.setUserId("billy_jones_301")
.setLoginStatus("$success"));
.setLoginStatus("$success")
.setBrowser(new Browser()
.setUserAgent(userAgent)));

SiftResponse siftResponse = request.send();

Expand Down
74 changes: 74 additions & 0 deletions src/test/java/com/siftscience/VerificationEventTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.siftscience;

import com.siftscience.model.VerificationFieldSet;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Assert;
import org.junit.Test;
import org.skyscreamer.jsonassert.JSONAssert;

import static java.net.HttpURLConnection.HTTP_OK;

public class VerificationEventTest {

@Test
public void testVerification() throws Exception {
String sessionId = "gigtleqddo84l8cm15qe4il";
String verifiedValue = "14155551212";

String expectedRequestBody = "{\n" +
" \"$type\" : \"$verification\",\n" +
" \"$api_key\" : \"your_api_key_here\",\n" +
" \"$user_id\" : \"billy_jones_301\",\n" +
" \"$session_id\" : \"" + sessionId + "\",\n" +
" \"$status\" : \"$pending\",\n" +
" \"$verification_type\" : \"$sms\",\n" +
" \"$verified_value\" : \"" + verifiedValue + "\"\n" +
"}";

// Start a new mock server and enqueue a mock response.
MockWebServer server = new MockWebServer();
MockResponse response = new MockResponse();
response.setResponseCode(HTTP_OK);
response.setBody("{\n" +
" \"status\" : 0,\n" +
" \"error_message\" : \"OK\",\n" +
" \"time\" : 1327604222,\n" +
" \"request\" : \"" + TestUtils.unescapeJson(expectedRequestBody) + "\"\n" +
"}");
server.enqueue(response);
server.start();
HttpUrl baseUrl = server.url("");

// Create a new client and link it to the mock server.
SiftClient client = new SiftClient("your_api_key_here");
client.setBaseUrl(baseUrl);

// Build and execute the request against the mock server.
SiftRequest request = client.buildRequest(new VerificationFieldSet()
.setUserId("billy_jones_301")
.setSessionId(sessionId)
.setStatus("$pending")
.setVerificationType("$sms")
.setVerifiedValue(verifiedValue));

SiftResponse siftResponse = request.send();

// Verify the request.
RecordedRequest request1 = server.takeRequest();
Assert.assertEquals("POST", request1.getMethod());
Assert.assertEquals("/v204/events", request1.getPath());
JSONAssert.assertEquals(expectedRequestBody, request.getFieldSet().toJson(), true);

// Verify the response.
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
Assert.assertEquals(0, (int) siftResponse.getBody().getStatus());
JSONAssert.assertEquals(response.getBody().readUtf8(),
siftResponse.getBody().toJson(), true);

server.shutdown();

}
}

0 comments on commit 5c1bee0

Please sign in to comment.