diff --git a/src/main/java/dev/vality/anapi/v2/service/DominantService.java b/src/main/java/dev/vality/anapi/v2/service/DominantService.java index 9e9053fc..701ee544 100644 --- a/src/main/java/dev/vality/anapi/v2/service/DominantService.java +++ b/src/main/java/dev/vality/anapi/v2/service/DominantService.java @@ -11,7 +11,10 @@ import org.apache.thrift.TException; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Slf4j @Service @@ -24,9 +27,13 @@ public List getShopIds(String partyId, String realm) { try { log.info("Looking for shops, partyId={}, realm={}", partyId, realm); List shopsObjects = getShopConfigObjects(partyId); - var shopIds = shopsObjects.stream() - .filter(shopConfigObject -> realmMatches(realm, shopConfigObject)) - .map(shopConfigObject -> shopConfigObject.getRef().getId()).toList(); + var shopIds = new ArrayList(); + var paymentInstitutionRealms = new HashMap(); + for (var shopConfigObject : shopsObjects) { + if (realmMatches(realm, shopConfigObject, paymentInstitutionRealms)) { + shopIds.add(shopConfigObject.getRef().getId()); + } + } log.info("Found {} shops, partyId={}, realm={}", shopIds.size(), partyId, realm); return shopIds; } catch (TException e) { @@ -47,10 +54,29 @@ private List getShopConfigObjects(String partyId) throws TExce return shopConfigObjects; } - private boolean realmMatches(String expectedRealm, ShopConfigObject shopConfigObject) { - int realmId = shopConfigObject.getData().getPaymentInstitution().getId(); - var paymentInstitutionRealm = - PaymentInstitutionRealm.findByValue(realmId); + private boolean realmMatches( + String expectedRealm, + ShopConfigObject shopConfigObject, + Map paymentInstitutionRealms) throws TException { + var paymentInstitutionRealm = getPaymentInstitutionRealm( + shopConfigObject.getData().getPaymentInstitution(), + paymentInstitutionRealms); return expectedRealm.equals(paymentInstitutionRealm.name()); } + + private PaymentInstitutionRealm getPaymentInstitutionRealm( + PaymentInstitutionRef paymentInstitutionRef, + Map paymentInstitutionRealms) throws TException { + var paymentInstitutionId = paymentInstitutionRef.getId(); + if (paymentInstitutionRealms.containsKey(paymentInstitutionId)) { + return paymentInstitutionRealms.get(paymentInstitutionId); + } + var paymentInstitutionReference = Reference.payment_institution(paymentInstitutionRef); + var versionedObject = dominantClient.checkoutObject( + VersionReference.head(new Head()), + paymentInstitutionReference); + var realm = versionedObject.getObject().getPaymentInstitution().getData().getRealm(); + paymentInstitutionRealms.put(paymentInstitutionId, realm); + return realm; + } } diff --git a/src/test/java/dev/vality/anapi/v2/service/DominantServiceTest.java b/src/test/java/dev/vality/anapi/v2/service/DominantServiceTest.java new file mode 100644 index 00000000..21266a84 --- /dev/null +++ b/src/test/java/dev/vality/anapi/v2/service/DominantServiceTest.java @@ -0,0 +1,63 @@ +package dev.vality.anapi.v2.service; + +import dev.vality.damsel.domain.*; +import dev.vality.damsel.domain_config_v2.RepositoryClientSrv; +import dev.vality.damsel.domain_config_v2.VersionedObject; +import dev.vality.damsel.domain_config_v2.VersionedObjectWithReferences; +import org.apache.thrift.TException; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +class DominantServiceTest { + + @Test + void getShopIdsMatchesPaymentInstitutionDataRealm() throws TException { + var dominantClient = mock(RepositoryClientSrv.Iface.class); + var service = new DominantService(dominantClient); + + when(dominantClient.checkoutObjectWithReferences(any(), any())) + .thenReturn(partyWithReferences(shop("shop-1", 1), shop("shop-2", 1), wallet())); + when(dominantClient.checkoutObject(any(), any())) + .thenReturn(paymentInstitution(1, PaymentInstitutionRealm.test)); + + assertEquals(Set.of("shop-1", "shop-2"), + Set.copyOf(service.getShopIds("party-1", PaymentInstitutionRealm.test.name()))); + assertEquals(List.of(), service.getShopIds("party-1", PaymentInstitutionRealm.live.name())); + verify(dominantClient, times(2)).checkoutObject(any(), any()); + } + + private static VersionedObjectWithReferences partyWithReferences(DomainObject... references) { + var referencedBy = Set.of(references).stream() + .map(domainObject -> new VersionedObject().setObject(domainObject)) + .collect(Collectors.toSet()); + return new VersionedObjectWithReferences() + .setReferencedBy(referencedBy); + } + + private static DomainObject shop(String shopId, int paymentInstitutionId) { + return DomainObject.shop_config(new ShopConfigObject() + .setRef(new ShopConfigRef(shopId)) + .setData(new ShopConfig() + .setPaymentInstitution(new PaymentInstitutionRef(paymentInstitutionId)))); + } + + private static DomainObject wallet() { + return DomainObject.wallet_config(new WalletConfigObject() + .setRef(new WalletConfigRef("wallet-1")) + .setData(new WalletConfig())); + } + + private static VersionedObject paymentInstitution(int id, PaymentInstitutionRealm realm) { + return new VersionedObject() + .setObject(DomainObject.payment_institution(new PaymentInstitutionObject() + .setRef(new PaymentInstitutionRef(id)) + .setData(new PaymentInstitution().setRealm(realm)))); + } +}