Skip to content

Commit 5e5ce63

Browse files
authored
Merge pull request #107 from codefupanda/develop
Added unit test cases for bank account and charge services
2 parents 0096d42 + 87b5502 commit 5e5ce63

File tree

2 files changed

+389
-2
lines changed

2 files changed

+389
-2
lines changed

payments-api/src/main/java/com/intuit/payment/services/ChargeService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ public Refund getRefund(String chargeId, String refundId) throws BaseException {
266266
/**
267267
* Method to void a Charge
268268
*
269-
* @param chargeId
270-
* @param refund
269+
* @param chargeRequestId
270+
*
271271
* @return
272272
* @throws BaseException
273273
*/
Lines changed: 387 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,387 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2017 Intuit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*******************************************************************************/
16+
package com.intuit.payment.services;
17+
18+
import com.intuit.payment.config.RequestContext;
19+
import com.intuit.payment.data.BankAccount;
20+
import com.intuit.payment.data.QueryResponse;
21+
import com.intuit.payment.data.Refund;
22+
import com.intuit.payment.data.Token;
23+
import com.intuit.payment.exception.BaseException;
24+
import com.intuit.payment.http.Request;
25+
import com.intuit.payment.http.Response;
26+
import com.intuit.payment.services.base.ServiceBase;
27+
import com.intuit.payment.util.JsonUtil;
28+
import mockit.Expectations;
29+
import mockit.Mocked;
30+
import org.testng.Assert;
31+
import org.testng.annotations.Test;
32+
33+
import java.math.BigDecimal;
34+
import java.util.ArrayList;
35+
import java.util.List;
36+
37+
public class BankAccountServiceTest {
38+
39+
@Mocked
40+
private ServiceBase serviceBase;
41+
42+
@Test
43+
public void testBankAccountServiceCreation() {
44+
BankAccountService bankService = new BankAccountService();
45+
Assert.assertNull(bankService.getRequestContext());
46+
}
47+
48+
@Test
49+
public void testBankAccountServiceRequestContext() {
50+
RequestContext requestContext = new RequestContext();
51+
requestContext.setBaseUrl("fakeBaseUrl");
52+
BankAccountService bankService = new BankAccountService(requestContext);
53+
Assert.assertEquals(requestContext, bankService.getRequestContext());
54+
55+
RequestContext secondRequestContext = new RequestContext();
56+
secondRequestContext.setBaseUrl("secondBaseUrl");
57+
bankService.setRequestContext(secondRequestContext);
58+
Assert.assertEquals(secondRequestContext, bankService.getRequestContext());
59+
}
60+
61+
@Test
62+
public void testCreateBankAccount() throws BaseException {
63+
BankAccount expectedBankAccount = new BankAccount.Builder().build();
64+
final String bankAccountString = JsonUtil.serialize(expectedBankAccount);
65+
final Response mockedResponse = new Response(200, bankAccountString, "fjkdlsfd");
66+
mockedResponse.setResponseObject(expectedBankAccount);
67+
68+
new Expectations() {{
69+
serviceBase.sendRequest((Request) any);
70+
result = mockedResponse;
71+
}};
72+
73+
RequestContext requestContext = new RequestContext();
74+
requestContext.setBaseUrl("fakeBaseUrl");
75+
BankAccountService bankService = new BankAccountService(requestContext);
76+
77+
try {
78+
BankAccount bankAccount = new BankAccount();
79+
BankAccount bankAccountGenerated = bankService.create(bankAccount, "customerId");
80+
Assert.assertEquals(expectedBankAccount, bankAccountGenerated);
81+
82+
} catch (BaseException e) {
83+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
84+
}
85+
}
86+
87+
@Test
88+
public void testCreateBankAccountNullBankAccountId() {
89+
BankAccountService bankService = new BankAccountService();
90+
try {
91+
bankService.create(null, null);
92+
Assert.fail("Expected IllegalArgumentException thrown");
93+
} catch (IllegalArgumentException e) {
94+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
95+
} catch (BaseException e) {
96+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
97+
}
98+
try {
99+
bankService.create(null, "");
100+
Assert.fail("Expected IllegalArgumentException thrown");
101+
} catch (IllegalArgumentException e) {
102+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
103+
} catch (BaseException e) {
104+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
105+
}
106+
}
107+
108+
109+
@Test
110+
public void testCreateFromTokenBankAccount() throws BaseException {
111+
BankAccount expectedBankAccount = new BankAccount.Builder().build();
112+
final String bankAccountGeneratedString = JsonUtil.serialize(expectedBankAccount);
113+
final Response mockedResponse = new Response(200, bankAccountGeneratedString, "fjkdlsfd");
114+
mockedResponse.setResponseObject(expectedBankAccount);
115+
116+
new Expectations() {{
117+
serviceBase.sendRequest((Request) any);
118+
result = mockedResponse;
119+
}};
120+
121+
RequestContext requestContext = new RequestContext();
122+
requestContext.setBaseUrl("fakeBaseUrl");
123+
BankAccountService bankService = new BankAccountService(requestContext);
124+
125+
try {
126+
Token token = new Token();
127+
BankAccount bankAccountGenerated = bankService.createFromToken(token, "customerId");
128+
Assert.assertEquals(expectedBankAccount, bankAccountGenerated);
129+
130+
} catch (BaseException e) {
131+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
132+
}
133+
}
134+
135+
@Test
136+
public void testCreateFromTokenBankAccountNullBankAccountId() {
137+
BankAccountService bankService = new BankAccountService();
138+
try {
139+
bankService.createFromToken(null, null);
140+
Assert.fail("Expected IllegalArgumentException thrown");
141+
} catch (IllegalArgumentException e) {
142+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
143+
} catch (BaseException e) {
144+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
145+
}
146+
try {
147+
bankService.createFromToken(null, "");
148+
Assert.fail("Expected IllegalArgumentException thrown");
149+
} catch (IllegalArgumentException e) {
150+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
151+
} catch (BaseException e) {
152+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
153+
}
154+
}
155+
156+
@Test
157+
public void testRetrieveBankAccount() throws BaseException {
158+
BankAccount expectedBankAccount = new BankAccount.Builder().id("ijids").accountNumber("accountId").build();
159+
final String bankAccountGeneratedString = JsonUtil.serialize(expectedBankAccount);
160+
final Response mockedResponse = new Response(200, bankAccountGeneratedString, "fjkdlsfd");
161+
mockedResponse.setResponseObject(expectedBankAccount);
162+
163+
new Expectations() {{
164+
serviceBase.sendRequest((Request) any);
165+
result = mockedResponse;
166+
}};
167+
168+
RequestContext requestContext = new RequestContext();
169+
requestContext.setBaseUrl("fakeBaseUrl");
170+
BankAccountService bankAccountService = new BankAccountService(requestContext);
171+
172+
try {
173+
BankAccount bankAccount = bankAccountService.getBankAccount("customerId", "accountId");
174+
Assert.assertEquals(expectedBankAccount, bankAccount);
175+
176+
} catch (BaseException e) {
177+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
178+
}
179+
}
180+
181+
@Test
182+
public void testRetrieveBankAccountNullBankAccountId() {
183+
BankAccountService bankService = new BankAccountService();
184+
try {
185+
bankService.getBankAccount(null, null);
186+
Assert.fail("Expected IllegalArgumentException thrown");
187+
} catch (IllegalArgumentException e) {
188+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
189+
} catch (BaseException e) {
190+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
191+
}
192+
try {
193+
bankService.getBankAccount("customerId", null);
194+
Assert.fail("Expected IllegalArgumentException thrown");
195+
} catch (IllegalArgumentException e) {
196+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
197+
} catch (BaseException e) {
198+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
199+
}
200+
try {
201+
bankService.getBankAccount("customerId", "");
202+
Assert.fail("Expected IllegalArgumentException thrown");
203+
} catch (IllegalArgumentException e) {
204+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
205+
} catch (BaseException e) {
206+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
207+
}
208+
try {
209+
bankService.getBankAccount("", "");
210+
Assert.fail("Expected IllegalArgumentException thrown");
211+
} catch (IllegalArgumentException e) {
212+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
213+
} catch (BaseException e) {
214+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
215+
}
216+
}
217+
218+
@Test
219+
public void testRetrieveAllBankAccount() throws BaseException {
220+
List<BankAccount> bankAccounts = new ArrayList<>();
221+
BankAccount bankAccount1 = new BankAccount();
222+
bankAccount1.setAccountNumber("accountNumber1");
223+
bankAccounts.add(bankAccount1);
224+
BankAccount bankAccount2 = new BankAccount();
225+
bankAccount2.setAccountNumber("accountNumber2");
226+
bankAccounts.add(bankAccount2);
227+
QueryResponse expectedQueryResponse = new QueryResponse.Builder().bankAccounts(bankAccounts).build();
228+
final String bankAccountString = JsonUtil.serialize(expectedQueryResponse);
229+
final Response mockedResponse = new Response(200, bankAccountString, "fjkdlsfd");
230+
mockedResponse.setResponseObject(bankAccounts);
231+
232+
new Expectations() {{
233+
serviceBase.sendRequest((Request) any);
234+
result = mockedResponse;
235+
}};
236+
237+
RequestContext requestContext = new RequestContext();
238+
requestContext.setBaseUrl("fakeBaseUrl");
239+
BankAccountService bankAccountService = new BankAccountService(requestContext);
240+
241+
try {
242+
QueryResponse queryResponse = bankAccountService.getAllBankAccounts("customerId");
243+
Assert.assertEquals(expectedQueryResponse.getBankAccounts(), bankAccounts);
244+
245+
} catch (BaseException e) {
246+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
247+
}
248+
}
249+
250+
@Test
251+
public void testRetrieveAllBankAccountNullBankAccountId() {
252+
BankAccountService bankService = new BankAccountService();
253+
try {
254+
bankService.getAllBankAccounts(null);
255+
Assert.fail("Expected IllegalArgumentException thrown");
256+
} catch (IllegalArgumentException e) {
257+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
258+
} catch (BaseException e) {
259+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
260+
}
261+
try {
262+
bankService.getAllBankAccounts("");
263+
Assert.fail("Expected IllegalArgumentException thrown");
264+
} catch (IllegalArgumentException e) {
265+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
266+
} catch (BaseException e) {
267+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
268+
}
269+
}
270+
271+
272+
@Test
273+
public void testRetrieveAllBankAccountWithCount() throws BaseException {
274+
List<BankAccount> bankAccounts = new ArrayList<>();
275+
BankAccount bankAccount1 = new BankAccount();
276+
bankAccount1.setAccountNumber("accountNumber1");
277+
bankAccounts.add(bankAccount1);
278+
BankAccount bankAccount2 = new BankAccount();
279+
bankAccount2.setAccountNumber("accountNumber2");
280+
bankAccounts.add(bankAccount2);
281+
QueryResponse expectedQueryResponse = new QueryResponse.Builder().bankAccounts(bankAccounts).build();
282+
final String bankAccountString = JsonUtil.serialize(expectedQueryResponse);
283+
final Response mockedResponse = new Response(200, bankAccountString, "fjkdlsfd");
284+
mockedResponse.setResponseObject(bankAccounts);
285+
286+
new Expectations() {{
287+
serviceBase.sendRequest((Request) any);
288+
result = mockedResponse;
289+
}};
290+
291+
RequestContext requestContext = new RequestContext();
292+
requestContext.setBaseUrl("fakeBaseUrl");
293+
BankAccountService bankAccountService = new BankAccountService(requestContext);
294+
295+
try {
296+
QueryResponse queryResponse = bankAccountService.getAllBankAccounts("customerId", 2);
297+
Assert.assertEquals(expectedQueryResponse.getBankAccounts(), bankAccounts);
298+
299+
} catch (BaseException e) {
300+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
301+
}
302+
}
303+
304+
@Test
305+
public void testRetrieveAllBankAccountNullBankAccountIdWithCount() {
306+
BankAccountService bankService = new BankAccountService();
307+
try {
308+
bankService.getAllBankAccounts(null, 0);
309+
Assert.fail("Expected IllegalArgumentException thrown");
310+
} catch (IllegalArgumentException e) {
311+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
312+
} catch (BaseException e) {
313+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
314+
}
315+
try {
316+
bankService.getAllBankAccounts("", 0);
317+
Assert.fail("Expected IllegalArgumentException thrown");
318+
} catch (IllegalArgumentException e) {
319+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
320+
} catch (BaseException e) {
321+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
322+
}
323+
}
324+
325+
@Test
326+
public void testDeleteBankAccount() throws BaseException {
327+
BankAccount expectedBankAccount = new BankAccount.Builder().accountNumber("accountId").build();
328+
final String bankAccountString = JsonUtil.serialize(expectedBankAccount);
329+
final Response mockedResponse = new Response(200, bankAccountString, "fjkdlsfd");
330+
mockedResponse.setResponseObject(expectedBankAccount);
331+
332+
new Expectations() {{
333+
serviceBase.sendRequest((Request) any);
334+
result = mockedResponse;
335+
}};
336+
337+
RequestContext requestContext = new RequestContext();
338+
requestContext.setBaseUrl("fakeBaseUrl");
339+
BankAccountService bankService = new BankAccountService(requestContext);
340+
341+
try {
342+
BankAccount bankAccount = new BankAccount();
343+
BankAccount bankAccountGenerated = bankService.delete("customerId", "bankAccountId");
344+
// NOTE: No asserts required as a dummy response is being sent by delete API
345+
} catch (BaseException e) {
346+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
347+
}
348+
}
349+
350+
351+
@Test
352+
public void testDeleteBankAccountNullBankAccountId() {
353+
BankAccountService bankService = new BankAccountService();
354+
try {
355+
bankService.delete(null, null);
356+
Assert.fail("Expected IllegalArgumentException thrown");
357+
} catch (IllegalArgumentException e) {
358+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
359+
} catch (BaseException e) {
360+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
361+
}
362+
try {
363+
bankService.delete("customerId", null);
364+
Assert.fail("Expected IllegalArgumentException thrown");
365+
} catch (IllegalArgumentException e) {
366+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
367+
} catch (BaseException e) {
368+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
369+
}
370+
try {
371+
bankService.delete("customerId", "");
372+
Assert.fail("Expected IllegalArgumentException thrown");
373+
} catch (IllegalArgumentException e) {
374+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
375+
} catch (BaseException e) {
376+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
377+
}
378+
try {
379+
bankService.delete("", "");
380+
Assert.fail("Expected IllegalArgumentException thrown");
381+
} catch (IllegalArgumentException e) {
382+
Assert.assertTrue(true, "Expected IllegalArgumentException was thrown");
383+
} catch (BaseException e) {
384+
Assert.fail("Unexpected BaseException thrown " + e.getMessage());
385+
}
386+
}
387+
}

0 commit comments

Comments
 (0)