-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathUpdateAccountEventTest.java
More file actions
154 lines (141 loc) · 7.23 KB
/
UpdateAccountEventTest.java
File metadata and controls
154 lines (141 loc) · 7.23 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.siftscience;
import static java.net.HttpURLConnection.HTTP_OK;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.siftscience.model.PaymentMethod;
import com.siftscience.model.Promotion;
import com.siftscience.model.UpdateAccountFieldSet;
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.skyscreamer.jsonassert.JSONAssert;
public class UpdateAccountEventTest {
@Test
public void testUpdateAccount() throws Exception {
String expectedRequestBody = "\n" +
"{\n" +
" \"$type\" : \"$update_account\",\n" +
" \"$api_key\" : \"YOUR_API_KEY\",\n" +
" \"$user_id\" : \"billy_jones_301\",\n" +
" \"$changed_password\" : true,\n" +
" \"$user_email\" : \"bill@gmail.com\",\n" +
" \"$name\" : \"Bill Jones\",\n" +
" \"$phone\" : \"1-415-555-6040\",\n" +
" \"$referrer_user_id\" : \"janejane102\",\n" +
" \"$payment_methods\" : [\n" +
" {\n" +
" \"$payment_type\" : \"$credit_card\",\n" +
" \"$card_bin\" : \"542486\",\n" +
" \"$card_last4\" : \"4444\"\n" +
" }\n" +
" ],\n" +
" \"$billing_address\" : {\n" +
" \"$name\" : \"Bill Jones\",\n" +
" \"$phone\" : \"1-415-555-6041\",\n" +
" \"$address_1\" : \"2100 Main Street\",\n" +
" \"$address_2\" : \"Apt 3B\",\n" +
" \"$city\" : \"New London\",\n" +
" \"$region\" : \"New Hampshire\",\n" +
" \"$country\" : \"US\",\n" +
" \"$zipcode\" : \"03257\"\n" +
" },\n" +
" \"$shipping_address\" : {\n" +
" \"$name\" : \"Bill Jones\",\n" +
" \"$phone\" : \"1-415-555-6041\",\n" +
" \"$address_1\" : \"2100 Main Street\",\n" +
" \"$address_2\" : \"Apt 3B\",\n" +
" \"$city\" : \"New London\",\n" +
" \"$region\" : \"New Hampshire\",\n" +
" \"$country\" : \"US\",\n" +
" \"$zipcode\" : \"03257\"\n" +
" },\n" +
" \"$promotions\" : [\n" +
" {\n" +
" \"$promotion_id\" : \"FriendReferral\",\n" +
" \"$status\" : \"$success\",\n" +
" \"$referrer_user_id\" : \"janejane102\",\n" +
" \"$credit_point\" : {\n" +
" \"$amount\" : 100,\n" +
" \"$credit_point_type\" : \"account karma\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"$social_sign_on_type\" : \"$twitter\",\n" +
" \"email_confirmed_status\" : \"$success\",\n" +
" \"phone_confirmed_status\" : \"$success\",\n" +
" \"$account_types\" : [\"merchant\", \"premium\"],\n" +
" \"$merchant_profile\" : {\n" +
" \"$merchant_id\" : \"12345\",\n" +
" \"$merchant_category_code\" : \"9876\",\n" +
" \"$merchant_name\" : \"ABC Merchant\",\n" +
" \"$merchant_address\" : {\n" +
" \"$address_1\" : \"2100 Main Street\",\n" +
" \"$address_2\" : \"Apt 3B\",\n" +
" \"$city\" : \"New London\",\n" +
" \"$country\" : \"US\",\n" +
" \"$name\" : \"Bill Jones\",\n" +
" \"$phone\" : \"1-415-555-6040\",\n" +
" \"$region\" : \"New Hampshire\",\n" +
" \"$zipcode\" : \"03257\"\n" +
" }\n" +
" }\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();
// 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());
// Payment methods.
List<PaymentMethod> paymentMethodList = new ArrayList<>();
paymentMethodList.add(TestUtils.samplePaymentMethod2());
// Promotions.
List<Promotion> promotionList = new ArrayList<>();
promotionList.add(TestUtils.samplePromotion2());
// Build and execute the request against the mock server.
SiftRequest request = client.buildRequest(
new UpdateAccountFieldSet()
.setUserId("billy_jones_301")
.setUserEmail("bill@gmail.com")
.setChangedPassword(true)
.setName("Bill Jones")
.setPhone("1-415-555-6040")
.setReferrerUserId("janejane102")
.setPaymentMethods(paymentMethodList)
.setBillingAddress(TestUtils.sampleAddress2())
.setShippingAddress(TestUtils.sampleAddress2())
.setPromotions(promotionList)
.setSocialSignOnType("$twitter")
.setCustomField("email_confirmed_status", "$success")
.setCustomField("phone_confirmed_status", "$success")
.setAccountTypes(Arrays.asList("merchant", "premium"))
.setMerchantProfile(TestUtils.sampleMerchantProfile()));
SiftResponse 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.assertEquals(0, (int) siftResponse.getBody().getStatus());
JSONAssert.assertEquals(response.getBody().readUtf8(),
siftResponse.getBody().toJson(), true);
server.shutdown();
}
}