Skip to content

Commit 9146b6c

Browse files
authored
Add payments transactions tests (#70)
* Add payments transactions tests * delete redis tests
1 parent 6a8c88d commit 9146b6c

File tree

6 files changed

+33
-125
lines changed

6 files changed

+33
-125
lines changed

services/payments/src/main/java/com/workup/payments/RabbitMQListener.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
import com.workup.payments.commands.PaymentCommandMap;
44
import com.workup.payments.commands.paymentrequest.*;
5+
import com.workup.payments.commands.paymenttransaction.GetClientPaymentTransactionsCommand;
6+
import com.workup.payments.commands.paymenttransaction.GetFreelancerPaymentTransactionsCommand;
57
import com.workup.payments.commands.wallet.CreateWalletCommand;
68
import com.workup.payments.commands.wallet.GetWalletCommand;
79
import com.workup.payments.commands.wallettransaction.CreateWalletTransactionCommand;
810
import com.workup.payments.commands.wallettransaction.GetWalletTransactionCommand;
911
import com.workup.payments.commands.wallettransaction.GetWalletTransactionsCommand;
1012
import com.workup.payments.commands.wallettransaction.WithdrawFromWalletCommand;
1113
import com.workup.shared.commands.payments.paymentrequest.requests.*;
12-
import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
1314
import com.workup.shared.commands.payments.paymentrequest.responses.*;
14-
import com.workup.shared.commands.payments.paymentrequest.responses.CreatePaymentRequestResponse;
15+
import com.workup.shared.commands.payments.paymenttransaction.requests.GetClientPaymentTransactionsRequest;
16+
import com.workup.shared.commands.payments.paymenttransaction.requests.GetFreelancerPaymentTransactionsRequest;
17+
import com.workup.shared.commands.payments.paymenttransaction.responses.GetClientPaymentTransactionsResponse;
18+
import com.workup.shared.commands.payments.paymenttransaction.responses.GetFreelancerPaymentTransactionsResponse;
1519
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
1620
import com.workup.shared.commands.payments.wallet.requests.GetWalletRequest;
1721
import com.workup.shared.commands.payments.wallet.responses.CreateWalletResponse;
@@ -97,4 +101,20 @@ public CreateWalletResponse receive(CreateWalletRequest in) throws Exception {
97101
public GetWalletResponse receive(GetWalletRequest in) throws Exception {
98102
return ((GetWalletCommand) commandMap.getCommand("GetWallet")).Run(in);
99103
}
104+
105+
@RabbitHandler
106+
public GetClientPaymentTransactionsResponse receive(GetClientPaymentTransactionsRequest in)
107+
throws Exception {
108+
return ((GetClientPaymentTransactionsCommand)
109+
commandMap.getCommand("GetClientPaymentTransactions"))
110+
.Run(in);
111+
}
112+
113+
@RabbitHandler
114+
public GetFreelancerPaymentTransactionsResponse receive(
115+
GetFreelancerPaymentTransactionsRequest in) throws Exception {
116+
return ((GetFreelancerPaymentTransactionsCommand)
117+
commandMap.getCommand("GetFreelancerPaymentTransactions"))
118+
.Run(in);
119+
}
100120
}

services/payments/src/main/java/com/workup/payments/commands/wallettransaction/WithdrawFromWalletCommand.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class WithdrawFromWalletCommand
1818
public WithdrawFromWalletResponse Run(WithdrawFromWalletRequest request) {
1919
String freelancerId = request.getFreelancerId();
2020
double amount = request.getAmount();
21-
String paymentTransactionId = request.getPaymentTransactionId();
2221
String description = request.getDescription();
2322

2423
Optional<Wallet> wallet = getWalletRepository().findById(freelancerId);
@@ -50,7 +49,6 @@ public WithdrawFromWalletResponse Run(WithdrawFromWalletRequest request) {
5049
WalletTransaction.builder()
5150
.withWalletId(freelancerId)
5251
.withAmount(amount)
53-
.withPaymentTransactionId(paymentTransactionId)
5452
.withDescription(description)
5553
.withTransactionType(WalletTransactionType.DEBIT)
5654
.build();

services/payments/src/main/java/com/workup/payments/repositories/PaymentTransactionRepository.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
public interface PaymentTransactionRepository extends JpaRepository<PaymentTransaction, String> {
99
@Query(
1010
value =
11-
"SELECT * FROM PaymentTransaction pt WHERE pt.payment_request_id IN (SELECT pr.id FROM PaymentRequest pr WHERE pr.freelancer_id = ?1)",
11+
"SELECT * FROM payment_transaction pt WHERE pt.payment_request_id IN (SELECT pr.id FROM payment_request pr WHERE pr.freelancer_id = ?1)",
1212
nativeQuery = true)
1313
List<PaymentTransaction> findAllByFreelancerId(String freelancerId);
1414

1515
@Query(
1616
value =
17-
"SELECT * FROM PaymentTransaction pt WHERE pt.payment_request_id IN (SELECT pr.id FROM PaymentRequest pr WHERE pr.client_id = ?1)",
17+
"SELECT * FROM payment_transaction pt WHERE pt.payment_request_id IN (SELECT pr.id FROM payment_request pr WHERE pr.client_id = ?1)",
1818
nativeQuery = true)
1919
List<PaymentTransaction> findAllByClientId(String clientId);
2020
}

services/payments/src/test/java/com/workup/payments/PaymentsApplicationTests.java

Lines changed: 7 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.junit.jupiter.api.Assertions.*;
44

5-
import com.redis.testcontainers.RedisContainer;
65
import com.workup.payments.models.PaymentRequest;
76
import com.workup.payments.models.PaymentTransaction;
87
import com.workup.payments.models.Wallet;
@@ -12,10 +11,14 @@
1211
import com.workup.payments.repositories.WalletRepository;
1312
import com.workup.payments.repositories.WalletTransactionRepository;
1413
import com.workup.shared.commands.payments.dto.PaymentRequestDTO;
15-
import com.workup.shared.commands.payments.paymentrequest.requests.*;
1614
import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
17-
import com.workup.shared.commands.payments.paymentrequest.responses.*;
15+
import com.workup.shared.commands.payments.paymentrequest.requests.GetClientPaymentRequestsRequest;
16+
import com.workup.shared.commands.payments.paymentrequest.requests.GetFreelancerPaymentRequestsRequest;
17+
import com.workup.shared.commands.payments.paymentrequest.requests.PayPaymentRequestRequest;
1818
import com.workup.shared.commands.payments.paymentrequest.responses.CreatePaymentRequestResponse;
19+
import com.workup.shared.commands.payments.paymentrequest.responses.GetClientPaymentRequestsResponse;
20+
import com.workup.shared.commands.payments.paymentrequest.responses.GetFreelancerPaymentRequestsResponse;
21+
import com.workup.shared.commands.payments.paymentrequest.responses.PayPaymentRequestResponse;
1922
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
2023
import com.workup.shared.commands.payments.wallet.requests.GetWalletRequest;
2124
import com.workup.shared.commands.payments.wallet.responses.CreateWalletResponse;
@@ -33,7 +36,6 @@
3336
import com.workup.shared.enums.payments.PaymentRequestStatus;
3437
import com.workup.shared.enums.payments.PaymentTransactionStatus;
3538
import com.workup.shared.enums.payments.WalletTransactionType;
36-
import com.workup.shared.redis.RedisService;
3739
import java.util.Optional;
3840
import java.util.UUID;
3941
import org.junit.jupiter.api.AfterAll;
@@ -49,7 +51,6 @@
4951
import org.testcontainers.containers.RabbitMQContainer;
5052
import org.testcontainers.junit.jupiter.Container;
5153
import org.testcontainers.junit.jupiter.Testcontainers;
52-
import org.testcontainers.utility.DockerImageName;
5354

5455
@SpringBootTest
5556
@Testcontainers
@@ -64,32 +65,24 @@ class PaymentsApplicationTests {
6465
static final RabbitMQContainer rabbitMQContainer =
6566
new RabbitMQContainer("rabbitmq:3.13-management");
6667

67-
@Container
68-
static final RedisContainer redisContainer =
69-
new RedisContainer(DockerImageName.parse("redis:latest")).withExposedPorts(6379);
70-
7168
@Autowired private AmqpTemplate template;
7269
@Autowired private PaymentRequestRepository paymentRequestRepository;
7370
@Autowired private PaymentTransactionRepository paymentTransactionRepository;
7471
@Autowired private WalletRepository walletRepository;
7572
@Autowired private WalletTransactionRepository walletTransactionRepository;
76-
@Autowired private RedisService redisService;
7773

7874
@BeforeEach
7975
void clearAll() {
8076
paymentRequestRepository.deleteAll();
8177
paymentTransactionRepository.deleteAll();
8278
walletRepository.deleteAll();
8379
walletTransactionRepository.deleteAll();
84-
85-
redisService.clearCache();
8680
}
8781

8882
@AfterAll
8983
static void stopContainers() {
9084
postgreSQLContainer.stop();
9185
rabbitMQContainer.stop();
92-
redisContainer.stop();
9386
}
9487

9588
@DynamicPropertySource
@@ -103,9 +96,6 @@ static void setDatasourceProperties(DynamicPropertyRegistry registry) {
10396
registry.add("spring.rabbitmq.port", rabbitMQContainer::getFirstMappedPort);
10497
registry.add("spring.rabbitmq.username", rabbitMQContainer::getAdminUsername);
10598
registry.add("spring.rabbitmq.password", rabbitMQContainer::getAdminPassword);
106-
107-
registry.add("spring.redis.host", redisContainer::getHost);
108-
registry.add("spring.redis.port", () -> redisContainer.getMappedPort(6379));
10999
}
110100

111101
@Test
@@ -330,7 +320,6 @@ void testWithdrawFromWalletRequest() {
330320
WithdrawFromWalletRequest.builder()
331321
.withAmount(withdrawAmount)
332322
.withFreelancerId("1")
333-
.withPaymentTransactionId("1")
334323
.build();
335324

336325
WithdrawFromWalletResponse response =
@@ -356,11 +345,7 @@ void testNotFoundWithdrawFromWalletRequest() {
356345
Wallet savedWallet = walletRepository.save(wallet);
357346

358347
WithdrawFromWalletRequest withdrawFromWalletRequest =
359-
WithdrawFromWalletRequest.builder()
360-
.withAmount(200)
361-
.withFreelancerId("2")
362-
.withPaymentTransactionId("1")
363-
.build();
348+
WithdrawFromWalletRequest.builder().withAmount(200).withFreelancerId("2").build();
364349

365350
WithdrawFromWalletResponse response =
366351
(WithdrawFromWalletResponse)
@@ -382,7 +367,6 @@ void testInvalidBiggerAmountWithdrawFromWalletRequest() {
382367
WithdrawFromWalletRequest.builder()
383368
.withAmount(withdrawAmount)
384369
.withFreelancerId("1")
385-
.withPaymentTransactionId("1")
386370
.build();
387371

388372
WithdrawFromWalletResponse response =
@@ -413,7 +397,6 @@ void testInvalidNegativeAmountWithdrawFromWalletRequest() {
413397
WithdrawFromWalletRequest.builder()
414398
.withAmount(withdrawAmount)
415399
.withFreelancerId("1")
416-
.withPaymentTransactionId("1")
417400
.build();
418401

419402
WithdrawFromWalletResponse response =
@@ -532,53 +515,6 @@ void testGetFreelancerPaymentRequests() {
532515
() -> assertEquals(paymentRequest2.getFreelancerId(), requestDTO2.getFreelancerId()));
533516
}
534517

535-
@Test
536-
void testGetPaymentRequest() {
537-
PaymentRequest paymentRequest =
538-
PaymentRequest.builder()
539-
.withAmount(1200)
540-
.withDescription("Payment for services rendered")
541-
.withClientId("3")
542-
.withFreelancerId("4")
543-
.build();
544-
paymentRequestRepository.save(paymentRequest);
545-
546-
GetPaymentRequestRequest getPaymentRequest =
547-
GetPaymentRequestRequest.builder().withPaymentRequestId(paymentRequest.getId()).build();
548-
549-
GetPaymentRequestResponse response =
550-
(GetPaymentRequestResponse)
551-
template.convertSendAndReceive(ServiceQueueNames.PAYMENTS, getPaymentRequest);
552-
553-
assertNotNull(response);
554-
assertEquals(HttpStatusCode.OK, response.getStatusCode());
555-
556-
PaymentRequestDTO requestDTO = response.getRequest();
557-
558-
assertNotNull(requestDTO);
559-
560-
assertAll(
561-
() -> assertEquals(paymentRequest.getId(), requestDTO.getId()),
562-
() -> assertEquals(paymentRequest.getAmount(), requestDTO.getAmount()),
563-
() -> assertEquals(paymentRequest.getDescription(), requestDTO.getDescription()),
564-
() -> assertEquals(paymentRequest.getClientId(), requestDTO.getClientId()),
565-
() -> assertEquals(paymentRequest.getFreelancerId(), requestDTO.getFreelancerId()));
566-
}
567-
568-
@Test
569-
void testGetPaymentRequestNotFound() {
570-
GetPaymentRequestRequest getPaymentRequest =
571-
GetPaymentRequestRequest.builder().withPaymentRequestId("123").build();
572-
573-
GetPaymentRequestResponse response =
574-
(GetPaymentRequestResponse)
575-
template.convertSendAndReceive(ServiceQueueNames.PAYMENTS, getPaymentRequest);
576-
577-
assertNotNull(response);
578-
assertEquals(HttpStatusCode.NOT_FOUND, response.getStatusCode());
579-
assertNull(response.getRequest());
580-
}
581-
582518
@Test
583519
void testPayPaymentRequest() {
584520
PaymentRequest paymentRequest =
@@ -753,51 +689,4 @@ void testGetInvalidWallet() {
753689
assertNotNull(getWalletResponse);
754690
assertEquals(HttpStatusCode.NOT_FOUND, getWalletResponse.getStatusCode());
755691
}
756-
757-
@Test
758-
void testGetPaymentRequestResponseFromCache() {
759-
PaymentRequest paymentRequest =
760-
PaymentRequest.builder()
761-
.withAmount(1200)
762-
.withDescription("Payment for services rendered")
763-
.withClientId("3")
764-
.withFreelancerId("4")
765-
.build();
766-
paymentRequestRepository.save(paymentRequest);
767-
768-
GetPaymentRequestRequest getPaymentRequest =
769-
GetPaymentRequestRequest.builder().withPaymentRequestId(paymentRequest.getId()).build();
770-
771-
GetPaymentRequestResponse response =
772-
(GetPaymentRequestResponse)
773-
template.convertSendAndReceive(ServiceQueueNames.PAYMENTS, getPaymentRequest);
774-
775-
assertNotNull(response);
776-
assertEquals(HttpStatusCode.OK, response.getStatusCode());
777-
778-
GetPaymentRequestResponse cachedResponse =
779-
(GetPaymentRequestResponse)
780-
redisService.getValue(
781-
getPaymentRequest.getPaymentRequestId(), GetPaymentRequestResponse.class);
782-
783-
assertNotNull(cachedResponse);
784-
assertEquals(HttpStatusCode.OK, cachedResponse.getStatusCode());
785-
786-
assertAll(
787-
() -> assertEquals(response.getRequest().getId(), cachedResponse.getRequest().getId()),
788-
() ->
789-
assertEquals(
790-
response.getRequest().getAmount(), cachedResponse.getRequest().getAmount()),
791-
() ->
792-
assertEquals(
793-
response.getRequest().getDescription(),
794-
cachedResponse.getRequest().getDescription()),
795-
() ->
796-
assertEquals(
797-
response.getRequest().getClientId(), cachedResponse.getRequest().getClientId()),
798-
() ->
799-
assertEquals(
800-
response.getRequest().getFreelancerId(),
801-
cachedResponse.getRequest().getFreelancerId()));
802-
}
803692
}

shared/src/main/java/com/workup/shared/commands/payments/dto/PaymentTransactionDTO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.workup.shared.enums.payments.PaymentTransactionStatus;
44
import java.util.Date;
5+
import lombok.Data;
56
import lombok.experimental.SuperBuilder;
67
import lombok.extern.jackson.Jacksonized;
78

89
@SuperBuilder(setterPrefix = "with")
910
@Jacksonized
11+
@Data
1012
public class PaymentTransactionDTO {
1113

1214
private String id;

shared/src/main/java/com/workup/shared/commands/payments/wallettransaction/requests/WithdrawFromWalletRequest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ public class WithdrawFromWalletRequest extends CommandRequest {
1212

1313
private final String freelancerId;
1414
private final double amount;
15-
private final String paymentTransactionId;
1615
private final String description;
1716
}

0 commit comments

Comments
 (0)