-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFlagContentEventTest.java
More file actions
104 lines (90 loc) · 4.15 KB
/
FlagContentEventTest.java
File metadata and controls
104 lines (90 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package com.siftscience;
import static java.net.HttpURLConnection.HTTP_OK;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.siftscience.model.EventResponseBody;
import com.siftscience.model.FlagContentFieldSet;
import okhttp3.OkHttpClient;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.skyscreamer.jsonassert.JSONAssert;
@RunWith(Parameterized.class)
public class FlagContentEventTest {
@Parameterized.Parameters
public static Collection<Object[]> data() {
FlagContentFieldSet.FlagContentReason[] values = FlagContentFieldSet.FlagContentReason.values();
List<Object[]> data = new ArrayList<>(values.length);
for (FlagContentFieldSet.FlagContentReason reason : values) {
data.add(new Object[] { reason });
}
return data;
}
private final FlagContentFieldSet.FlagContentReason reason;
public FlagContentEventTest(FlagContentFieldSet.FlagContentReason reason) {
this.reason = reason;
}
@Test
public void testFlagContent() throws Exception {
String expectedRequestBody = "{\n" +
" \"$type\" : \"$flag_content\", \n" +
" \"$api_key\" : \"YOUR_API_KEY\",\n" +
" \"$brand_name\" : \"sift\",\n" +
" \"$user_id\" : \"billy_jones_301\",\n" +
" \"$content_id\" : \"9671500641\",\n" +
"\n" +
" \"$flagged_by\" : \"jamieli89\",\n" +
" \"$reason\" : \"" + this.reason.value + "\",\n" +
" \"$site_country\" : \"US\",\n" +
" \"$site_domain\" : \"sift.com\",\n" +
" \"$user_email\" : \"billy_jones_301@email.com\",\n" +
" \"$verification_phone_number\" : \"+12345678901\"\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();
// Create a new client and link it to the mock server.
SiftClient client = new SiftClient("YOUR_API_KEY", "YOUR_ACCOUNT_ID",
new OkHttpClient.Builder()
.addInterceptor(OkHttpUtils.urlRewritingInterceptor(server))
.build());
// Build and execute the request against the mock server.
SiftRequest<EventResponse> request = client.buildRequest(new FlagContentFieldSet()
.setUserId("billy_jones_301")
.setContentId("9671500641")
.setFlaggedBy("jamieli89")
.setReason(this.reason)
.setBrandName("sift")
.setSiteCountry("US")
.setSiteDomain("sift.com")
.setUserEmail("billy_jones_301@email.com")
.setVerificationPhoneNumber("+12345678901"));
SiftResponse<EventResponseBody> siftResponse = request.send();
// Verify the request.
RecordedRequest request1 = server.takeRequest();
Assert.assertEquals("POST", request1.getMethod());
Assert.assertEquals("/v205/events", request1.getPath());
JSONAssert.assertEquals(expectedRequestBody, request.getFieldSet().toJson(), true);
// Verify the response.
Assert.assertEquals(HTTP_OK, siftResponse.getHttpStatusCode());
Assert.assertNotNull(siftResponse.getBody());
Assert.assertEquals(0, (int) siftResponse.getBody().getStatus());
JSONAssert.assertEquals(response.getBody().readUtf8(),
siftResponse.getBody().toJson(), true);
server.shutdown();
}
}