add inspecting url + fix path for balance url (#17)

* add inspecting url + fix path for balance url

* fixes

* fix name

* fix tests

* fix tests

* fix tests

* fix tests

* fix tests

Co-authored-by: ggmaleva <ggmaleva@yandex.ru>
This commit is contained in:
Gregory 2022-04-04 10:41:25 +03:00 committed by GitHub
parent 0967f2f07d
commit af9696078f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 110 additions and 8 deletions

View File

@ -0,0 +1,6 @@
package dev.vality.scrooge.service;
public interface Inspector<T> {
boolean isSuitable(T object);
}

View File

@ -3,6 +3,7 @@ package dev.vality.scrooge.service.impl;
import dev.vality.account_balance.AccountServiceSrv;
import dev.vality.scrooge.service.ClientBuilder;
import dev.vality.woody.thrift.impl.http.THSpawnClientBuilder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
@ -10,15 +11,18 @@ import org.springframework.stereotype.Component;
import java.net.URI;
@Component
@RequiredArgsConstructor
public class AdapterClientBuilder implements ClientBuilder<AccountServiceSrv.Iface> {
private static final String BALANCE_PATH = "/balance";
@Value("${adapter-client.networkTimeout}")
private int networkTimeout;
@Cacheable(value = "adapters", key = "#url")
@Override
public AccountServiceSrv.Iface build(String url) {
URI adapterUri = URI.create(url);
URI adapterUri = URI.create(url + BALANCE_PATH);
return new THSpawnClientBuilder()
.withNetworkTimeout(networkTimeout)
.withAddress(adapterUri)

View File

@ -0,0 +1,27 @@
package dev.vality.scrooge.service.impl;
import dev.vality.scrooge.service.Inspector;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;
@Component
public class UrlInspector implements Inspector<String> {
@Value("#{'${adapter-client.hosts}'.split(',')}")
private Set<String> hosts;
@Override
public boolean isSuitable(String urlString) {
try {
URL url = new URL(urlString);
String host = url.getHost();
return hosts.contains(host);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Invalid url for adapter " + urlString);
}
}
}

View File

@ -1,12 +1,10 @@
package dev.vality.scrooge.service.impl;
import dev.vality.scrooge.domain.AdapterInfo;
import dev.vality.scrooge.domain.BalanceInfo;
import dev.vality.scrooge.domain.RouteInfo;
import dev.vality.scrooge.domain.WithdrawalTransaction;
import dev.vality.scrooge.service.AccountSurveyService;
import dev.vality.scrooge.service.BalanceService;
import dev.vality.scrooge.service.RouteService;
import dev.vality.scrooge.service.StateService;
import dev.vality.scrooge.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@ -19,11 +17,16 @@ public class WithdrawalBalanceService implements BalanceService<WithdrawalTransa
private final RouteService<WithdrawalTransaction> routeService;
private final AccountSurveyService accountSurveyService;
private final StateService stateService;
private final Inspector<String> urlInspector;
@Override
public void update(WithdrawalTransaction transaction) {
RouteInfo routeInfo = routeService.get(transaction);
BalanceInfo balanceInfo = accountSurveyService.getBalance(routeInfo.getAdapterInfo());
AdapterInfo adapterInfo = routeInfo.getAdapterInfo();
if (!urlInspector.isSuitable(adapterInfo.getUrl())) {
return;
}
BalanceInfo balanceInfo = accountSurveyService.getBalance(adapterInfo);
if (Objects.nonNull(balanceInfo)) {
stateService.update(routeInfo, balanceInfo);
}

View File

@ -43,6 +43,7 @@ party-management:
url: http://hellgate:8022/v1/processing/partymgmt
networkTimeout: 5000
adapter-client:
hosts: adapter-paybox,proxy-mocketbank,adapter-onevision-payout
networkTimeout: 5000
kafka:

View File

@ -170,7 +170,7 @@ public abstract class TestObjectFactory {
.setDescription(randomString())
)
.setProxy(new ProxyDefinition()
.setUrl("http://prt-mngmt:8022/v1")
.setUrl("http://adapter-paybox:8022/v1")
.setOptions(Map.of(randomString(), randomString())));
}

View File

@ -0,0 +1,29 @@
package dev.vality.scrooge.service.impl;
import dev.vality.account_balance.AccountServiceSrv;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {AdapterClientBuilder.class})
@TestPropertySource(properties = {
"adapter-client.networkTimeout=5000"})
class AdapterClientBuilderTest {
@Autowired
private AdapterClientBuilder adapterClientBuilder;
@Test
void build() {
String stringUrl = "http://adapter-paybox:8022/v1";
AccountServiceSrv.Iface adapterClient = adapterClientBuilder.build(stringUrl);
assertNotNull(adapterClient);
}
}

View File

@ -22,6 +22,7 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -31,7 +32,9 @@ import static org.mockito.Mockito.*;
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {WithdrawalBalanceService.class, AccountSurveyServiceImpl.class,
BalanceResponseToBalanceInfoConverter.class, WithdrawalRouteService.class,
ProviderTerminalToRouteInfoConverter.class})
ProviderTerminalToRouteInfoConverter.class, UrlInspector.class})
@TestPropertySource(properties = {
"adapter-client.hosts=adapter-paybox,proxy-mocketbank,adapter-onevision-payout"})
class WithdrawalBalanceServiceTest {
@Autowired
@ -65,6 +68,35 @@ class WithdrawalBalanceServiceTest {
assertEquals("WithdrawalRouteService error call party-management: ", partyManagementException.getMessage());
}
@Test
void updateWithIncorrectUrl() throws TException {
var providerTerminal = TestObjectFactory.testProviderTerminal();
String incorrectUrl = "test";
providerTerminal.getProxy().setUrl(incorrectUrl);
when(partyManagementClient.computeProviderTerminal(any(TerminalRef.class), anyLong(), any(Varset.class)))
.thenReturn(providerTerminal);
var transaction = TestObjectFactory.testWithdrawalTransaction();
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> balanceService.update(transaction));
assertEquals("Invalid url for adapter " + incorrectUrl, exception.getMessage());
}
@Test
void updateWithNotAvailableUrl() throws TException {
var providerTerminal = TestObjectFactory.testProviderTerminal();
providerTerminal.getProxy().setUrl("http://test:8022/v1");
when(partyManagementClient.computeProviderTerminal(any(TerminalRef.class), anyLong(), any(Varset.class)))
.thenReturn(providerTerminal);
var transaction = TestObjectFactory.testWithdrawalTransaction();
balanceService.update(transaction);
verify(clientBuilder, never()).build(anyString());
verify(accountService, never()).getBalance(any(BalanceRequest.class));
}
@Test
void updateWithAdapterException() throws TException {
var providerTerminal = TestObjectFactory.testProviderTerminal();