From d9c14074e88a2d2a0d1e73edc2bd38e1c7bc9633 Mon Sep 17 00:00:00 2001 From: Baikov Dmitrii <44803026+D-Baykov@users.noreply.github.com> Date: Sat, 2 Nov 2024 17:41:24 +0300 Subject: [PATCH] EMP-99: Fix duplicate values for limits (#25) --- .../OperationStateHistoryConverterImpl.java | 1 - .../impl/OperationStateHistoryDaoImpl.java | 2 + .../dao/mapper/LimitValueMapper.java | 2 +- .../V1_5__drop_limit_name_in_ops_history.sql | 1 + .../com/empayre/liminator/dao/DaoTests.java | 1 - .../service/LiminatorServiceTest.java | 56 +++++++++++++++++++ 6 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/db/migration/V1_5__drop_limit_name_in_ops_history.sql diff --git a/src/main/java/com/empayre/liminator/converter/impl/OperationStateHistoryConverterImpl.java b/src/main/java/com/empayre/liminator/converter/impl/OperationStateHistoryConverterImpl.java index 91baaa3..a1efd1f 100644 --- a/src/main/java/com/empayre/liminator/converter/impl/OperationStateHistoryConverterImpl.java +++ b/src/main/java/com/empayre/liminator/converter/impl/OperationStateHistoryConverterImpl.java @@ -22,7 +22,6 @@ public class OperationStateHistoryConverterImpl implements OperationStateHistory .map(change -> { OperationStateHistory history = new OperationStateHistory(); history.setOperationId(request.getOperationId()); - history.setLimitName(change.getLimitName()); history.setLimitDataId(limitNamesMap.get(change.getLimitName())); history.setOperationValue(change.getValue()); history.setState(state); diff --git a/src/main/java/com/empayre/liminator/dao/impl/OperationStateHistoryDaoImpl.java b/src/main/java/com/empayre/liminator/dao/impl/OperationStateHistoryDaoImpl.java index c9e0af4..6f2b89b 100644 --- a/src/main/java/com/empayre/liminator/dao/impl/OperationStateHistoryDaoImpl.java +++ b/src/main/java/com/empayre/liminator/dao/impl/OperationStateHistoryDaoImpl.java @@ -124,6 +124,7 @@ public class OperationStateHistoryDaoImpl implements OperationStateHistoryDao { .leftJoin(commitOps) .on( commitOps.OPERATION_ID.eq(holdOps.OPERATION_ID) + .and(commitOps.LIMIT_DATA_ID.eq(holdOps.LIMIT_DATA_ID)) .and(commitOps.STATE.in(OperationState.COMMIT)) .and(operationId == null ? DSL.trueCondition() @@ -132,6 +133,7 @@ public class OperationStateHistoryDaoImpl implements OperationStateHistoryDao { .leftJoin(rollbackOps) .on( rollbackOps.OPERATION_ID.eq(holdOps.OPERATION_ID) + .and(rollbackOps.LIMIT_DATA_ID.eq(holdOps.LIMIT_DATA_ID)) .and(rollbackOps.STATE.in(OperationState.ROLLBACK)) .and(operationId == null ? DSL.trueCondition() diff --git a/src/main/java/com/empayre/liminator/dao/mapper/LimitValueMapper.java b/src/main/java/com/empayre/liminator/dao/mapper/LimitValueMapper.java index 06b00a2..482144c 100644 --- a/src/main/java/com/empayre/liminator/dao/mapper/LimitValueMapper.java +++ b/src/main/java/com/empayre/liminator/dao/mapper/LimitValueMapper.java @@ -15,7 +15,7 @@ public class LimitValueMapper implements RecordMapper { @Override public LimitValue map(Record opsDataRecord) { String limitId = opsDataRecord.get(LIMIT_DATA.LIMIT_ID); - String limitName = opsDataRecord.get(OPERATION_STATE_HISTORY.LIMIT_NAME); + String limitName = opsDataRecord.get(LIMIT_DATA.NAME); Long operationValue = opsDataRecord.get(OPERATION_STATE_HISTORY.OPERATION_VALUE); OperationState state = OperationState.valueOf(opsDataRecord.get("state", String.class)); return new LimitValue(limitId, limitName, state, operationValue); diff --git a/src/main/resources/db/migration/V1_5__drop_limit_name_in_ops_history.sql b/src/main/resources/db/migration/V1_5__drop_limit_name_in_ops_history.sql new file mode 100644 index 0000000..cda7712 --- /dev/null +++ b/src/main/resources/db/migration/V1_5__drop_limit_name_in_ops_history.sql @@ -0,0 +1 @@ +ALTER TABLE lim.operation_state_history drop COLUMN limit_name; diff --git a/src/test/java/com/empayre/liminator/dao/DaoTests.java b/src/test/java/com/empayre/liminator/dao/DaoTests.java index 0b9c84c..384eb64 100644 --- a/src/test/java/com/empayre/liminator/dao/DaoTests.java +++ b/src/test/java/com/empayre/liminator/dao/DaoTests.java @@ -211,7 +211,6 @@ class DaoTests { LocalDateTime createdAt, OperationState state) { OperationStateHistory operation = new OperationStateHistory(); - operation.setLimitName(limitName); operation.setLimitDataId(id); operation.setOperationId(operationId); operation.setState(state); diff --git a/src/test/java/com/empayre/liminator/service/LiminatorServiceTest.java b/src/test/java/com/empayre/liminator/service/LiminatorServiceTest.java index 8d58b60..88948fa 100644 --- a/src/test/java/com/empayre/liminator/service/LiminatorServiceTest.java +++ b/src/test/java/com/empayre/liminator/service/LiminatorServiceTest.java @@ -253,6 +253,62 @@ class LiminatorServiceTest { assertEquals(limitName, limitResponseAfterAllForFourthHold.get(0).getLimitName()); } + @Test + void holdCommitFewValueTest() throws TException { + String limitNameFirst = "TestLimitHold"; + String limitNameSecond = "TestLimitHold2"; + String limitNameThird = "TestLimitHold3"; + String operationId = "OpHold"; + LimitRequest holdRequest = new LimitRequest() + .setOperationId(operationId) + .setLimitChanges(List.of( + new LimitChange(limitNameFirst, 500L), + new LimitChange(limitNameSecond, 500L), + new LimitChange(limitNameThird, 500L)) + ); + + List holdResponse = liminatorService.hold(holdRequest); + + assertEquals(3, holdResponse.size()); + LimitResponse limitResponseFirst = getLimitResponseByLimitName(holdResponse, limitNameFirst); + assertEquals(limitNameFirst, limitResponseFirst.getLimitName()); + assertEquals(500, limitResponseFirst.getTotalValue()); + assertEquals(0, limitResponseFirst.getCommitValue()); + LimitResponse limitResponseSecond = getLimitResponseByLimitName(holdResponse, limitNameSecond); + assertEquals(limitNameSecond, limitResponseSecond.getLimitName()); + assertEquals(500, limitResponseSecond.getTotalValue()); + assertEquals(0, limitResponseSecond.getCommitValue()); + LimitResponse limitResponseThird = getLimitResponseByLimitName(holdResponse, limitNameThird); + assertEquals(limitNameThird, limitResponseThird.getLimitName()); + assertEquals(500, limitResponseThird.getTotalValue()); + assertEquals(0, limitResponseThird.getCommitValue()); + + liminatorService.commit(holdRequest); + List lastLimitsValues = + liminatorService.getLastLimitsValues(List.of(limitNameFirst, limitNameSecond, limitNameThird)); + assertEquals(3, lastLimitsValues.size()); + LimitResponse firstLimitResponseAfterCommit = getLimitResponseByLimitName(lastLimitsValues, limitNameFirst); + assertEquals(limitNameFirst, firstLimitResponseAfterCommit.getLimitName()); + assertEquals(500, firstLimitResponseAfterCommit.getTotalValue()); + assertEquals(500, firstLimitResponseAfterCommit.getCommitValue()); + LimitResponse secondLimitResponseAfterCommit = getLimitResponseByLimitName(lastLimitsValues, limitNameSecond); + assertEquals(limitNameSecond, secondLimitResponseAfterCommit.getLimitName()); + assertEquals(500, secondLimitResponseAfterCommit.getTotalValue()); + assertEquals(500, secondLimitResponseAfterCommit.getCommitValue()); + LimitResponse thirdLimitResponseAfterCommit = getLimitResponseByLimitName(lastLimitsValues, limitNameThird); + assertEquals(limitNameThird, thirdLimitResponseAfterCommit.getLimitName()); + assertEquals(500, thirdLimitResponseAfterCommit.getTotalValue()); + assertEquals(500, thirdLimitResponseAfterCommit.getCommitValue()); + + } + + private LimitResponse getLimitResponseByLimitName(List response, String limitName) { + return response.stream() + .filter(limitResponse -> limitResponse.getLimitName().equals(limitName)) + .findFirst() + .get(); + } + private LimitRequest createRequest(String limitName, String operationId) { return new LimitRequest() .setOperationId(operationId)