/
TestDefaultAccountUserApi.java
238 lines (196 loc) · 13 KB
/
TestDefaultAccountUserApi.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* Copyright 2010-2013 Ning, Inc.
* Copyright 2014-2015 Groupon, Inc
* Copyright 2014-2015 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.killbill.billing.account.api.user;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Callable;
import org.killbill.billing.ErrorCode;
import org.killbill.billing.account.AccountTestSuiteWithEmbeddedDB;
import org.killbill.billing.account.api.Account;
import org.killbill.billing.account.api.AccountApiException;
import org.killbill.billing.account.api.AccountData;
import org.killbill.billing.account.api.DefaultAccount;
import org.killbill.billing.account.api.DefaultMutableAccountData;
import org.killbill.billing.account.api.MutableAccountData;
import org.killbill.billing.account.dao.AccountModelDao;
import org.killbill.billing.catalog.api.Currency;
import org.killbill.billing.events.AccountCreationInternalEvent;
import org.killbill.billing.util.entity.Pagination;
import org.testng.Assert;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.eventbus.Subscribe;
import static com.jayway.awaitility.Awaitility.await;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.killbill.billing.account.AccountTestUtils.createAccountData;
import static org.killbill.billing.account.AccountTestUtils.createTestAccount;
import static org.testng.Assert.assertEquals;
public class TestDefaultAccountUserApi extends AccountTestSuiteWithEmbeddedDB {
@Test(groups = "slow", description = "Test Account search")
public void testSearch() throws Exception {
final MutableAccountData mutableAccountData1 = createAccountData();
mutableAccountData1.setEmail("john@acme.com");
mutableAccountData1.setCompanyName("Acme, Inc.");
final AccountModelDao account1ModelDao = new AccountModelDao(UUID.randomUUID(), mutableAccountData1);
final AccountData accountData1 = new DefaultAccount(account1ModelDao);
accountUserApi.createAccount(accountData1, callContext);
final MutableAccountData mutableAccountData2 = createAccountData();
mutableAccountData2.setEmail("bob@gmail.com");
mutableAccountData2.setCompanyName("Acme, Inc.");
final AccountModelDao account2ModelDao = new AccountModelDao(UUID.randomUUID(), mutableAccountData2);
final AccountData accountData2 = new DefaultAccount(account2ModelDao);
accountUserApi.createAccount(accountData2, callContext);
final Pagination<Account> search1 = accountUserApi.searchAccounts("Inc.", 0L, 5L, callContext);
Assert.assertEquals(search1.getCurrentOffset(), (Long) 0L);
Assert.assertNull(search1.getNextOffset());
Assert.assertEquals(search1.getMaxNbRecords(), (Long) 2L);
Assert.assertEquals(search1.getTotalNbRecords(), (Long) 2L);
Assert.assertEquals(ImmutableList.<Account>copyOf(search1.iterator()).size(), 2);
final Pagination<Account> search2 = accountUserApi.searchAccounts("Inc.", 0L, 1L, callContext);
Assert.assertEquals(search2.getCurrentOffset(), (Long) 0L);
Assert.assertEquals(search2.getNextOffset(), (Long) 1L);
Assert.assertEquals(search2.getMaxNbRecords(), (Long) 2L);
Assert.assertEquals(search2.getTotalNbRecords(), (Long) 2L);
Assert.assertEquals(ImmutableList.<Account>copyOf(search2.iterator()).size(), 1);
final Pagination<Account> search3 = accountUserApi.searchAccounts("acme.com", 0L, 5L, callContext);
Assert.assertEquals(search3.getCurrentOffset(), (Long) 0L);
Assert.assertNull(search3.getNextOffset());
Assert.assertEquals(search3.getMaxNbRecords(), (Long) 2L);
Assert.assertEquals(search3.getTotalNbRecords(), (Long) 1L);
Assert.assertEquals(ImmutableList.<Account>copyOf(search3.iterator()).size(), 1);
// Exact search will fail
final Pagination<Account> search4 = accountUserApi.searchAccounts("acme.com", -1L, 1L, callContext);
Assert.assertEquals(search4.getCurrentOffset(), (Long) 0L);
Assert.assertNull(search4.getNextOffset());
// Not computed
Assert.assertNull(search4.getMaxNbRecords());
Assert.assertEquals(search4.getTotalNbRecords(), (Long) 0L);
Assert.assertEquals(ImmutableList.<Account>copyOf(search4.iterator()).size(), 0);
final Pagination<Account> search5 = accountUserApi.searchAccounts("john@acme.com", -1L, 1L, callContext);
Assert.assertEquals(search5.getCurrentOffset(), (Long) 0L);
Assert.assertNull(search5.getNextOffset());
// Not computed
Assert.assertNull(search5.getMaxNbRecords());
Assert.assertEquals(search5.getTotalNbRecords(), (Long) 1L);
Assert.assertEquals(ImmutableList.<Account>copyOf(search5.iterator()).size(), 1);
}
@Test(groups = "slow", description = "Test Account creation generates an event")
public void testBusEvents() throws Exception {
final AccountEventHandler eventHandler = new AccountEventHandler();
bus.register(eventHandler);
final AccountModelDao accountModelDao = createTestAccount();
final AccountData defaultAccount = new DefaultAccount(accountModelDao);
final Account account = createAccount(defaultAccount);
await().atMost(10, SECONDS).until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return eventHandler.getAccountCreationInternalEvents().size() == 1;
}
});
final AccountCreationInternalEvent accountCreationInternalEvent = eventHandler.getAccountCreationInternalEvents().get(0);
Assert.assertEquals(accountCreationInternalEvent.getId(), account.getId());
// account_record_id is most likely 1, although, depending on the DB, we cannot be sure
Assert.assertNotNull(accountCreationInternalEvent.getSearchKey1());
Assert.assertEquals(accountCreationInternalEvent.getSearchKey2(), internalCallContext.getTenantRecordId());
}
@Test(groups = "slow", description = "Test Account update with null values")
public void testShouldBeAbleToPassNullForSomeFieldsToAvoidUpdate() throws Exception {
final Account account = createAccount(new DefaultAccount(createTestAccount()));
// Update the address and leave other fields null
final MutableAccountData mutableAccountData = new DefaultMutableAccountData(null, null, null, 0, null, null, false, 0, null,
null, null, null, null, null, null,
null, null, null, null, null, false, false);
final String newAddress1 = UUID.randomUUID().toString();
mutableAccountData.setAddress1(newAddress1);
accountUserApi.updateAccount(account.getId(), mutableAccountData, callContext);
final Account retrievedAccount = accountUserApi.getAccountById(account.getId(), callContext);
Assert.assertEquals(retrievedAccount.getAddress1(), newAddress1);
Assert.assertEquals(retrievedAccount.getAddress2(), account.getAddress2());
Assert.assertEquals(retrievedAccount.getCurrency(), account.getCurrency());
Assert.assertEquals(retrievedAccount.getExternalKey(), account.getExternalKey());
Assert.assertEquals(retrievedAccount.getBillCycleDayLocal(), account.getBillCycleDayLocal());
}
@Test(groups = "slow", expectedExceptions = IllegalArgumentException.class, description = "Test updating Account BCD does throws an exception")
public void testShouldntBeAbleToUpdateBillCycleDay() throws Exception {
final Account account = createAccount(new DefaultAccount(createTestAccount()));
final MutableAccountData otherAccount = new DefaultAccount(account.getId(), account).toMutableAccountData();
otherAccount.setBillCycleDayLocal(account.getBillCycleDayLocal() + 2);
accountUserApi.updateAccount(new DefaultAccount(account.getId(), otherAccount), callContext);
}
@Test(groups = "slow", expectedExceptions = IllegalArgumentException.class, description = "Test updating Account currency throws an exception")
public void testShouldntBeAbleToUpdateCurrency() throws Exception {
final Account account = createAccount(new DefaultAccount(createTestAccount()));
final MutableAccountData otherAccount = new DefaultAccount(account.getId(), account).toMutableAccountData();
otherAccount.setCurrency(Currency.GBP);
accountUserApi.updateAccount(new DefaultAccount(account.getId(), otherAccount), callContext);
}
@Test(groups = "slow", expectedExceptions = IllegalArgumentException.class, description = "Test updating Account externalKey throws an exception")
public void testShouldntBeAbleToUpdateExternalKey() throws Exception {
final Account account = createAccount(new DefaultAccount(createTestAccount()));
final MutableAccountData otherAccount = new DefaultAccount(account.getId(), account).toMutableAccountData();
otherAccount.setExternalKey(UUID.randomUUID().toString());
accountUserApi.updateAccount(new DefaultAccount(account.getId(), otherAccount), callContext);
}
private static final class AccountEventHandler {
private final List<AccountCreationInternalEvent> accountCreationInternalEvents = new LinkedList<AccountCreationInternalEvent>();
@Subscribe
public void handleAccountCreationInternalEvent(final AccountCreationInternalEvent creationInternalEvent) {
this.accountCreationInternalEvents.add(creationInternalEvent);
}
public List<AccountCreationInternalEvent> getAccountCreationInternalEvents() {
return accountCreationInternalEvents;
}
}
@Test(groups = "slow", description = "Test Account create Parent and Child")
public void testCreateParentAndChildAccounts() throws Exception {
final Account parentAccount = accountUserApi.createAccount(new DefaultAccount(createTestAccount()), callContext);
final AccountModelDao childAccountModel = createTestAccount();
childAccountModel.setParentAccountId(parentAccount.getId());
childAccountModel.setIsPaymentDelegatedToParent(true);
final AccountData childAccountData = new DefaultAccount(childAccountModel);
final Account childAccount = accountUserApi.createAccount(childAccountData, callContext);
final Account retrievedChildAccount = accountUserApi.getAccountById(childAccount.getId(), callContext);
Assert.assertNull(parentAccount.getParentAccountId());
Assert.assertNotNull(retrievedChildAccount.getParentAccountId());
Assert.assertEquals(retrievedChildAccount.getId(), childAccount.getId());
Assert.assertEquals(retrievedChildAccount.getParentAccountId(), parentAccount.getId());
Assert.assertEquals(retrievedChildAccount.isPaymentDelegatedToParent(), childAccount.isPaymentDelegatedToParent());
}
@Test(groups = "slow", description = "Test Account create Child with a non existing Parent",
expectedExceptions = AccountApiException.class, expectedExceptionsMessageRegExp = "Account does not exist for id .*")
public void testCreateChildAccountWithInvalidParent() throws Exception {
final AccountModelDao childAccountModel = createTestAccount();
childAccountModel.setParentAccountId(UUID.randomUUID());
final AccountData childAccountData = new DefaultAccount(childAccountModel);
final Account childAccount = accountUserApi.createAccount(childAccountData, callContext);
}
@Test(groups = "slow", description = "Test Account creation with External Key over limit")
public void testCreateAccountWithExternalKeyOverLimit() throws Exception {
AccountModelDao accountModelDao = createTestAccount();
// Set an externalKey of 256 characters (over limit)
accountModelDao.setExternalKey("Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis,.");
final AccountData accountData = new DefaultAccount(accountModelDao);
try {
accountUserApi.createAccount(accountData, callContext);
Assert.fail();
} catch (final AccountApiException e) {
assertEquals(e.getCode(), ErrorCode.EXTERNAL_KEY_LIMIT_EXCEEDED.getCode());
}
}
}