|
| 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 chargeService = new BankAccountService(); |
| 45 | + Assert.assertNull(chargeService.getRequestContext()); |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testBankAccountServiceRequestContext() { |
| 50 | + RequestContext requestContext = new RequestContext(); |
| 51 | + requestContext.setBaseUrl("fakeBaseUrl"); |
| 52 | + BankAccountService chargeService = new BankAccountService(requestContext); |
| 53 | + Assert.assertEquals(requestContext, chargeService.getRequestContext()); |
| 54 | + |
| 55 | + RequestContext secondRequestContext = new RequestContext(); |
| 56 | + secondRequestContext.setBaseUrl("secondBaseUrl"); |
| 57 | + chargeService.setRequestContext(secondRequestContext); |
| 58 | + Assert.assertEquals(secondRequestContext, chargeService.getRequestContext()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testCreateBankAccount() throws BaseException { |
| 63 | + BankAccount expectedBankAccount = new BankAccount.Builder().build(); |
| 64 | + final String chargeString = JsonUtil.serialize(expectedBankAccount); |
| 65 | + final Response mockedResponse = new Response(200, chargeString, "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 chargeService = new BankAccountService(requestContext); |
| 76 | + |
| 77 | + try { |
| 78 | + BankAccount bankAccount = new BankAccount(); |
| 79 | + BankAccount chargeGenerated = chargeService.create(bankAccount, "customerId"); |
| 80 | + Assert.assertEquals(expectedBankAccount, chargeGenerated); |
| 81 | + |
| 82 | + } catch (BaseException e) { |
| 83 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testCreateBankAccountNullBankAccountId() { |
| 89 | + BankAccountService chargeService = new BankAccountService(); |
| 90 | + try { |
| 91 | + chargeService.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 | + chargeService.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 chargeString = JsonUtil.serialize(expectedBankAccount); |
| 113 | + final Response mockedResponse = new Response(200, chargeString, "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 chargeService = new BankAccountService(requestContext); |
| 124 | + |
| 125 | + try { |
| 126 | + Token token = new Token(); |
| 127 | + BankAccount chargeGenerated = chargeService.createFromToken(token, "customerId"); |
| 128 | + Assert.assertEquals(expectedBankAccount, chargeGenerated); |
| 129 | + |
| 130 | + } catch (BaseException e) { |
| 131 | + Assert.fail("Unexpected BaseException thrown " + e.getMessage()); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testCreateFromTokenBankAccountNullBankAccountId() { |
| 137 | + BankAccountService chargeService = new BankAccountService(); |
| 138 | + try { |
| 139 | + chargeService.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 | + chargeService.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 chargeString = JsonUtil.serialize(expectedBankAccount); |
| 160 | + final Response mockedResponse = new Response(200, chargeString, "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 chargeService = new BankAccountService(); |
| 184 | + try { |
| 185 | + chargeService.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 | + chargeService.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 | + chargeService.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 | + chargeService.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 chargeString = JsonUtil.serialize(expectedQueryResponse); |
| 229 | + final Response mockedResponse = new Response(200, chargeString, "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 chargeService = new BankAccountService(); |
| 253 | + try { |
| 254 | + chargeService.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 | + chargeService.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 chargeString = JsonUtil.serialize(expectedQueryResponse); |
| 283 | + final Response mockedResponse = new Response(200, chargeString, "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 chargeService = new BankAccountService(); |
| 307 | + try { |
| 308 | + chargeService.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 | + chargeService.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 chargeString = JsonUtil.serialize(expectedBankAccount); |
| 329 | + final Response mockedResponse = new Response(200, chargeString, "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 chargeService = new BankAccountService(requestContext); |
| 340 | + |
| 341 | + try { |
| 342 | + BankAccount bankAccount = new BankAccount(); |
| 343 | + BankAccount chargeGenerated = chargeService.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 chargeService = new BankAccountService(); |
| 354 | + try { |
| 355 | + chargeService.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 | + chargeService.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 | + chargeService.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 | + chargeService.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