|
62 | 62 | import static io.vertx.core.Future.failedFuture; |
63 | 63 | import static io.vertx.core.Future.succeededFuture; |
64 | 64 | import static org.hamcrest.Matchers.nullValue; |
| 65 | +import static org.mockito.ArgumentMatchers.isNull; |
| 66 | +import org.folio.models.EncumbranceConversionHolder; |
| 67 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
65 | 68 |
|
66 | 69 | @ExtendWith(VertxExtension.class) |
67 | 70 | public class FinanceHoldersBuilderTest { |
@@ -425,4 +428,168 @@ void testGetLedgerIdsShouldHandleMixedNullAndValidFundIds() { |
425 | 428 | assertThat(holderWithFund.getLedgerId(), is(validLedgerId)); |
426 | 429 | assertThat(holderWithNullFund.getLedgerId(), nullValue()); |
427 | 430 | } |
| 431 | + |
| 432 | + @Test |
| 433 | + void testGetExchangeRatesPerCurrencyHolderShouldFilterOutHoldersWithoutPoLines(VertxTestContext vertxTestContext) { |
| 434 | + // TestMate-889f7f4ac3125f03846c04247965fca4 |
| 435 | + // Given |
| 436 | + String fyCurrency = "EUR"; |
| 437 | + String poLineCurrency = "USD"; |
| 438 | + PoLine poLine = new PoLine().withCost(new Cost().withCurrency(poLineCurrency)); |
| 439 | + EncumbranceRelationsHolder validHolder = new EncumbranceRelationsHolder() |
| 440 | + .withPoLine(poLine) |
| 441 | + .withCurrency(fyCurrency); |
| 442 | + EncumbranceRelationsHolder invalidHolder = new EncumbranceRelationsHolder() |
| 443 | + .withPoLine(null) |
| 444 | + .withCurrency(fyCurrency); |
| 445 | + List<EncumbranceRelationsHolder> holders = List.of(validHolder, invalidHolder); |
| 446 | + ExchangeRate exchangeRate = mock(ExchangeRate.class); |
| 447 | + when(exchangeRate.getExchangeRate()).thenReturn(1.0); |
| 448 | + when(exchangeRate.getOperationMode()).thenReturn(ExchangeRate.OperationMode.MULTIPLY); |
| 449 | + when(cacheableExchangeRateService.getExchangeRate(eq(poLineCurrency), eq(fyCurrency), isNull(), eq(requestContext))) |
| 450 | + .thenReturn(succeededFuture(exchangeRate)); |
| 451 | + // When |
| 452 | + Future<List<EncumbranceConversionHolder>> future = financeHoldersBuilder.getExchangeRatesPerCurrencyHolder(holders, requestContext); |
| 453 | + // Then |
| 454 | + vertxTestContext.assertComplete(future) |
| 455 | + .onComplete(result -> { |
| 456 | + assertTrue(result.succeeded()); |
| 457 | + List<EncumbranceConversionHolder> conversionHolders = result.result(); |
| 458 | + assertThat(conversionHolders, hasSize(1)); |
| 459 | + EncumbranceConversionHolder conversionHolder = conversionHolders.get(0); |
| 460 | + assertThat(conversionHolder.getEncumbranceRelationsHolders(), hasSize(1)); |
| 461 | + assertThat(conversionHolder.getEncumbranceRelationsHolders(), contains(validHolder)); |
| 462 | + verify(cacheableExchangeRateService, times(1)) |
| 463 | + .getExchangeRate(eq(poLineCurrency), eq(fyCurrency), any(), eq(requestContext)); |
| 464 | + vertxTestContext.completeNow(); |
| 465 | + }); |
| 466 | + } |
| 467 | + |
| 468 | + @Test |
| 469 | + void testGetExchangeRatesPerCurrencyHolderShouldGroupHoldersByCurrencyAndRetrieveRates(VertxTestContext vertxTestContext) { |
| 470 | + // TestMate-d4e825ce318718d4d1f7fdb9ca9d51ae |
| 471 | + // Given |
| 472 | + String fyCurrency = "RUB"; |
| 473 | + String usd = "USD"; |
| 474 | + String gbp = "GBP"; |
| 475 | + holder1.withCurrency(fyCurrency); |
| 476 | + holder2.withCurrency(fyCurrency); |
| 477 | + holder3.withCurrency(fyCurrency); |
| 478 | + holder3.getPoLine().getCost().setCurrency(gbp); |
| 479 | + List<EncumbranceRelationsHolder> holders = List.of(holder1, holder2, holder3); |
| 480 | + ExchangeRate usdRate = mock(ExchangeRate.class); |
| 481 | + when(usdRate.getExchangeRate()).thenReturn(75.0); |
| 482 | + when(usdRate.getOperationMode()).thenReturn(ExchangeRate.OperationMode.MULTIPLY); |
| 483 | + ExchangeRate gbpRate = mock(ExchangeRate.class); |
| 484 | + when(gbpRate.getExchangeRate()).thenReturn(95.0); |
| 485 | + when(gbpRate.getOperationMode()).thenReturn(ExchangeRate.OperationMode.MULTIPLY); |
| 486 | + when(cacheableExchangeRateService.getExchangeRate(eq(usd), eq(fyCurrency), any(), eq(requestContext))) |
| 487 | + .thenReturn(succeededFuture(usdRate)); |
| 488 | + when(cacheableExchangeRateService.getExchangeRate(eq(gbp), eq(fyCurrency), any(), eq(requestContext))) |
| 489 | + .thenReturn(succeededFuture(gbpRate)); |
| 490 | + when(requestContext.getContext()) |
| 491 | + .thenReturn(Vertx.vertx().getOrCreateContext()); |
| 492 | + // When |
| 493 | + Future<List<EncumbranceConversionHolder>> future = financeHoldersBuilder.getExchangeRatesPerCurrencyHolder(holders, requestContext); |
| 494 | + // Then |
| 495 | + vertxTestContext.assertComplete(future) |
| 496 | + .onComplete(result -> { |
| 497 | + List<EncumbranceConversionHolder> conversionHolders = result.result(); |
| 498 | + assertThat(conversionHolders, hasSize(2)); |
| 499 | + EncumbranceConversionHolder usdConversion = conversionHolders.stream() |
| 500 | + .filter(h -> h.getEncumbranceRelationsHolders().stream().anyMatch(erh -> erh.getPoLine().getCost().getCurrency().equals(usd))) |
| 501 | + .findFirst() |
| 502 | + .orElseThrow(); |
| 503 | + assertThat(usdConversion.getEncumbranceRelationsHolders(), hasSize(2)); |
| 504 | + assertThat(usdConversion.getEncumbranceRelationsHolders(), containsInAnyOrder(holder1, holder2)); |
| 505 | + EncumbranceConversionHolder gbpConversion = conversionHolders.stream() |
| 506 | + .filter(h -> h.getEncumbranceRelationsHolders().stream().anyMatch(erh -> erh.getPoLine().getCost().getCurrency().equals(gbp))) |
| 507 | + .findFirst() |
| 508 | + .orElseThrow(); |
| 509 | + assertThat(gbpConversion.getEncumbranceRelationsHolders(), hasSize(1)); |
| 510 | + assertEquals(holder3, gbpConversion.getEncumbranceRelationsHolders().get(0)); |
| 511 | + verify(cacheableExchangeRateService, times(1)).getExchangeRate(eq(usd), eq(fyCurrency), any(), eq(requestContext)); |
| 512 | + verify(cacheableExchangeRateService, times(1)).getExchangeRate(eq(gbp), eq(fyCurrency), any(), eq(requestContext)); |
| 513 | + vertxTestContext.completeNow(); |
| 514 | + }); |
| 515 | + } |
| 516 | + |
| 517 | + @Test |
| 518 | + void testGetExchangeRatesPerCurrencyHolderShouldUseFirstAvailableFixedExchangeRateInGroup(VertxTestContext vertxTestContext) { |
| 519 | + // TestMate-06227643b9a22e84d54690ebdcd6c34e |
| 520 | + // Given |
| 521 | + String poLineCurrency = "USD"; |
| 522 | + String fyCurrency = "EUR"; |
| 523 | + Double firstManualRate = 1.5; |
| 524 | + Double ignoredManualRate = 1.8; |
| 525 | + PoLine line1 = new PoLine().withId("00000000-0000-0000-0000-000000000001") |
| 526 | + .withCost(new Cost().withCurrency(poLineCurrency).withExchangeRate(null)) |
| 527 | + .withFundDistribution(List.of(new FundDistribution().withFundId(UUID.randomUUID().toString()))); |
| 528 | + PoLine line2 = new PoLine().withId("00000000-0000-0000-0000-000000000002") |
| 529 | + .withCost(new Cost().withCurrency(poLineCurrency).withExchangeRate(firstManualRate)) |
| 530 | + .withFundDistribution(List.of(new FundDistribution().withFundId(UUID.randomUUID().toString()))); |
| 531 | + PoLine line3 = new PoLine().withId("00000000-0000-0000-0000-000000000003") |
| 532 | + .withCost(new Cost().withCurrency(poLineCurrency).withExchangeRate(ignoredManualRate)) |
| 533 | + .withFundDistribution(List.of(new FundDistribution().withFundId(UUID.randomUUID().toString()))); |
| 534 | + EncumbranceRelationsHolder holderA = new EncumbranceRelationsHolder() |
| 535 | + .withPoLine(line1) |
| 536 | + .withCurrency(fyCurrency); |
| 537 | + EncumbranceRelationsHolder holderB = new EncumbranceRelationsHolder() |
| 538 | + .withPoLine(line2) |
| 539 | + .withCurrency(fyCurrency); |
| 540 | + EncumbranceRelationsHolder holderC = new EncumbranceRelationsHolder() |
| 541 | + .withPoLine(line3) |
| 542 | + .withCurrency(fyCurrency); |
| 543 | + List<EncumbranceRelationsHolder> holders = List.of(holderA, holderB, holderC); |
| 544 | + ExchangeRate exchangeRate = mock(ExchangeRate.class); |
| 545 | + when(exchangeRate.getExchangeRate()).thenReturn(firstManualRate); |
| 546 | + when(exchangeRate.getOperationMode()).thenReturn(ExchangeRate.OperationMode.MULTIPLY); |
| 547 | + when(cacheableExchangeRateService.getExchangeRate(eq(poLineCurrency), eq(fyCurrency), eq(firstManualRate), eq(requestContext))) |
| 548 | + .thenReturn(succeededFuture(exchangeRate)); |
| 549 | + when(requestContext.getContext()) |
| 550 | + .thenReturn(Vertx.vertx().getOrCreateContext()); |
| 551 | + // When |
| 552 | + Future<List<EncumbranceConversionHolder>> future = financeHoldersBuilder.getExchangeRatesPerCurrencyHolder(holders, requestContext); |
| 553 | + // Then |
| 554 | + vertxTestContext.assertComplete(future) |
| 555 | + .onComplete(result -> { |
| 556 | + assertTrue(result.succeeded()); |
| 557 | + List<EncumbranceConversionHolder> conversionHolders = result.result(); |
| 558 | + |
| 559 | + assertThat(conversionHolders, hasSize(1)); |
| 560 | + EncumbranceConversionHolder conversionHolder = conversionHolders.get(0); |
| 561 | + |
| 562 | + assertThat(conversionHolder.getEncumbranceRelationsHolders(), hasSize(3)); |
| 563 | + assertThat(conversionHolder.getEncumbranceRelationsHolders(), containsInAnyOrder(holderA, holderB, holderC)); |
| 564 | + |
| 565 | + verify(cacheableExchangeRateService, times(1)) |
| 566 | + .getExchangeRate(eq(poLineCurrency), eq(fyCurrency), eq(firstManualRate), eq(requestContext)); |
| 567 | + |
| 568 | + vertxTestContext.completeNow(); |
| 569 | + }); |
| 570 | + } |
| 571 | + |
| 572 | + @Test |
| 573 | + void testGetExchangeRatesPerCurrencyHolderShouldPropagateFailureFromExchangeRateService(VertxTestContext vertxTestContext) { |
| 574 | + // TestMate-7de4e182d028e1b0a04a3c79fb5a6fb2 |
| 575 | + // Given |
| 576 | + String fyCurrency = "RUB"; |
| 577 | + String poLineCurrency = holder1.getPoLine().getCost().getCurrency(); |
| 578 | + holder1.withCurrency(fyCurrency); |
| 579 | + HttpException expectedException = new HttpException(500, "API Error"); |
| 580 | + when(cacheableExchangeRateService.getExchangeRate(eq(poLineCurrency), eq(fyCurrency), any(), eq(requestContext))) |
| 581 | + .thenReturn(failedFuture(expectedException)); |
| 582 | + // When |
| 583 | + Future<List<EncumbranceConversionHolder>> future = financeHoldersBuilder.getExchangeRatesPerCurrencyHolder(List.of(holder1), requestContext); |
| 584 | + // Then |
| 585 | + vertxTestContext.assertFailure(future) |
| 586 | + .onComplete(result -> { |
| 587 | + HttpException actualException = (HttpException) result.cause(); |
| 588 | + assertEquals(expectedException.getCode(), actualException.getCode()); |
| 589 | + assertEquals("API Error", actualException.getError().getMessage()); |
| 590 | + verify(cacheableExchangeRateService, times(1)) |
| 591 | + .getExchangeRate(eq(poLineCurrency), eq(fyCurrency), any(), eq(requestContext)); |
| 592 | + vertxTestContext.completeNow(); |
| 593 | + }); |
| 594 | + } |
428 | 595 | } |
0 commit comments