From 016f0d733dc6995b841fae759ad0258d324e66ef Mon Sep 17 00:00:00 2001 From: Pavel Popov Date: Fri, 20 Mar 2020 11:10:38 +0300 Subject: [PATCH] BJ-830: Payment system in rates (#103) * BJ-830: Add payment system in rate table * Remove unused imports, fix with excluded fields * Remove unused imports * and so the crazy refactoring process sees the sunlight after some months in the dark! --- pom.xml | 2 +- .../newway/dao/rate/impl/RateDaoImpl.java | 2 +- .../impl/rate/RateCreatedHandler.java | 2 ++ .../V45__1.0.62_add_payment_system_in_rate.sql | 4 ++++ .../rbkmoney/newway/service/ServiceTests.java | 17 +++++++++++++++++ .../newway/utils/RateSinkEventTestUtils.java | 6 ++---- 6 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 src/main/resources/db/migration/V45__1.0.62_add_payment_system_in_rate.sql diff --git a/pom.xml b/pom.xml index 43a6bcf..a3f59c1 100644 --- a/pom.xml +++ b/pom.xml @@ -161,7 +161,7 @@ com.rbkmoney xrates-proto - 1.7-5587fa8 + 1.8-67d5c21 com.rbkmoney diff --git a/src/main/java/com/rbkmoney/newway/dao/rate/impl/RateDaoImpl.java b/src/main/java/com/rbkmoney/newway/dao/rate/impl/RateDaoImpl.java index 3712a91..6634db3 100644 --- a/src/main/java/com/rbkmoney/newway/dao/rate/impl/RateDaoImpl.java +++ b/src/main/java/com/rbkmoney/newway/dao/rate/impl/RateDaoImpl.java @@ -38,7 +38,7 @@ public class RateDaoImpl extends AbstractGenericDao implements RateDao { public Long save(Rate rate) throws DaoException { RateRecord record = getDslContext().newRecord(RATE, rate); Query query = getDslContext().insertInto(RATE).set(record) - .onConflict(RATE.SOURCE_ID, RATE.SEQUENCE_ID, RATE.CHANGE_ID, RATE.SOURCE_SYMBOLIC_CODE, RATE.DESTINATION_SYMBOLIC_CODE) + .onConflict(RATE.SOURCE_ID, RATE.SEQUENCE_ID, RATE.CHANGE_ID, RATE.SOURCE_SYMBOLIC_CODE, RATE.DESTINATION_SYMBOLIC_CODE, RATE.PAYMENT_SYSTEM) .doNothing() .returning(RATE.ID); diff --git a/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/rate/RateCreatedHandler.java b/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/rate/RateCreatedHandler.java index b2c670f..8188e70 100644 --- a/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/rate/RateCreatedHandler.java +++ b/src/main/java/com/rbkmoney/newway/poller/event_stock/impl/rate/RateCreatedHandler.java @@ -14,6 +14,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.List; +import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; @Component @@ -76,6 +77,7 @@ public class RateCreatedHandler extends AbstractRateHandler { rate.setSourceExponent(source.getExponent()); rate.setDestinationSymbolicCode(destination.getSymbolicCode()); rate.setDestinationExponent(destination.getExponent()); + rate.setPaymentSystem(Objects.toString(quote.getPaymentSystem(), null)); // ExchangeRate rate.setExchangeRateRationalP(exchangeRate.getP()); diff --git a/src/main/resources/db/migration/V45__1.0.62_add_payment_system_in_rate.sql b/src/main/resources/db/migration/V45__1.0.62_add_payment_system_in_rate.sql new file mode 100644 index 0000000..2495258 --- /dev/null +++ b/src/main/resources/db/migration/V45__1.0.62_add_payment_system_in_rate.sql @@ -0,0 +1,4 @@ +alter table nw.rate add column payment_system character varying not null default ''; + +drop index idx_uniq; +create unique index rate_ukey on nw.rate(source_id, sequence_id, change_id, source_symbolic_code, destination_symbolic_code, payment_system); \ No newline at end of file diff --git a/src/test/java/com/rbkmoney/newway/service/ServiceTests.java b/src/test/java/com/rbkmoney/newway/service/ServiceTests.java index 478a4fb..21130e7 100644 --- a/src/test/java/com/rbkmoney/newway/service/ServiceTests.java +++ b/src/test/java/com/rbkmoney/newway/service/ServiceTests.java @@ -11,6 +11,7 @@ import org.springframework.jdbc.core.JdbcTemplate; import java.util.List; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; public class ServiceTests extends AbstractAppDaoTests { @@ -43,7 +44,23 @@ public class ServiceTests extends AbstractAppDaoTests { String sourceId = "CBR"; RateSinkEventTestUtils.Dto dto = RateSinkEventTestUtils.create(sourceId); + rateService.handleEvents(dto.getSinkEvent(), dto.getEvent()); + rateService.handleEvents(dto.getSinkEvent(), dto.getEvent()); + List rates = jdbcTemplate.query( + "SELECT * FROM nw.rate AS rate WHERE rate.source_id = ? AND rate.current", + new Object[]{sourceId}, + new BeanPropertyRowMapper(Rate.class) + ); + assertEquals(4, rates.size()); + } + + @Test + public void rateServiceDuplicationWhenPaymentSystemIsNullTest() { + jdbcTemplate.execute("truncate table nw.rate cascade"); + String sourceId = "CBR"; + + RateSinkEventTestUtils.Dto dto = RateSinkEventTestUtils.create(sourceId, "payment_system"); rateService.handleEvents(dto.getSinkEvent(), dto.getEvent()); rateService.handleEvents(dto.getSinkEvent(), dto.getEvent()); diff --git a/src/test/java/com/rbkmoney/newway/utils/RateSinkEventTestUtils.java b/src/test/java/com/rbkmoney/newway/utils/RateSinkEventTestUtils.java index 66b93d4..fb2ae57 100644 --- a/src/test/java/com/rbkmoney/newway/utils/RateSinkEventTestUtils.java +++ b/src/test/java/com/rbkmoney/newway/utils/RateSinkEventTestUtils.java @@ -15,10 +15,8 @@ import static io.github.benas.randombeans.api.EnhancedRandom.randomListOf; public class RateSinkEventTestUtils extends AbstractTestUtils { - public static Dto create(String sourceId) { - List quotes = new ArrayList() {{ - addAll(randomListOf(4, Quote.class)); - }}; + public static Dto create(String sourceId, String... excludedFields) { + List quotes = randomListOf(4, Quote.class, excludedFields); Event event = new Event( Collections.singletonList(