Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 33 additions & 7 deletions src/main/java/dev/vality/anapi/v2/service/DominantService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,9 +27,13 @@ public List<String> getShopIds(String partyId, String realm) {
try {
log.info("Looking for shops, partyId={}, realm={}", partyId, realm);
List<ShopConfigObject> shopsObjects = getShopConfigObjects(partyId);
var shopIds = shopsObjects.stream()
.filter(shopConfigObject -> realmMatches(realm, shopConfigObject))
.map(shopConfigObject -> shopConfigObject.getRef().getId()).toList();
var shopIds = new ArrayList<String>();
var paymentInstitutionRealms = new HashMap<Integer, PaymentInstitutionRealm>();
Comment thread
karle0wne marked this conversation as resolved.
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) {
Expand All @@ -47,10 +54,29 @@ private List<ShopConfigObject> 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<Integer, PaymentInstitutionRealm> paymentInstitutionRealms) throws TException {
var paymentInstitutionRealm = getPaymentInstitutionRealm(
shopConfigObject.getData().getPaymentInstitution(),
paymentInstitutionRealms);
return expectedRealm.equals(paymentInstitutionRealm.name());
}

private PaymentInstitutionRealm getPaymentInstitutionRealm(
PaymentInstitutionRef paymentInstitutionRef,
Map<Integer, PaymentInstitutionRealm> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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))));
}
}
Loading