2
2
3
3
import static org .junit .jupiter .api .Assertions .*;
4
4
5
+ import com .redis .testcontainers .RedisContainer ;
5
6
import com .workup .payments .models .PaymentRequest ;
6
7
import com .workup .payments .models .PaymentTransaction ;
7
8
import com .workup .payments .models .Wallet ;
13
14
import com .workup .shared .commands .payments .dto .PaymentRequestDTO ;
14
15
import com .workup .shared .commands .payments .paymentrequest .requests .*;
15
16
import com .workup .shared .commands .payments .paymentrequest .responses .*;
16
- import com .workup .shared .commands .payments .paymenttransaction .requests .GetClientPaymentTransactionsRequest ;
17
- import com .workup .shared .commands .payments .paymenttransaction .requests .GetFreelancerPaymentTransactionsRequest ;
18
- import com .workup .shared .commands .payments .paymenttransaction .responses .GetClientPaymentTransactionsResponse ;
19
- import com .workup .shared .commands .payments .paymenttransaction .responses .GetFreelancerPaymentTransactionsResponse ;
20
17
import com .workup .shared .commands .payments .wallet .requests .CreateWalletRequest ;
21
18
import com .workup .shared .commands .payments .wallet .requests .GetWalletRequest ;
22
19
import com .workup .shared .commands .payments .wallet .responses .CreateWalletResponse ;
34
31
import com .workup .shared .enums .payments .PaymentRequestStatus ;
35
32
import com .workup .shared .enums .payments .PaymentTransactionStatus ;
36
33
import com .workup .shared .enums .payments .WalletTransactionType ;
34
+ import com .workup .shared .redis .RedisService ;
37
35
import java .util .Optional ;
38
36
import java .util .UUID ;
39
37
import org .junit .jupiter .api .AfterAll ;
49
47
import org .testcontainers .containers .RabbitMQContainer ;
50
48
import org .testcontainers .junit .jupiter .Container ;
51
49
import org .testcontainers .junit .jupiter .Testcontainers ;
50
+ import org .testcontainers .utility .DockerImageName ;
52
51
53
52
@ SpringBootTest
54
53
@ Testcontainers
@@ -63,24 +62,32 @@ class PaymentsApplicationTests {
63
62
static final RabbitMQContainer rabbitMQContainer =
64
63
new RabbitMQContainer ("rabbitmq:3.13-management" );
65
64
65
+ @ Container
66
+ static final RedisContainer redisContainer =
67
+ new RedisContainer (DockerImageName .parse ("redis:latest" ));
68
+
66
69
@ Autowired private AmqpTemplate template ;
67
70
@ Autowired private PaymentRequestRepository paymentRequestRepository ;
68
71
@ Autowired private PaymentTransactionRepository paymentTransactionRepository ;
69
72
@ Autowired private WalletRepository walletRepository ;
70
73
@ Autowired private WalletTransactionRepository walletTransactionRepository ;
74
+ @ Autowired private RedisService redisService ;
71
75
72
76
@ BeforeEach
73
77
void clearAll () {
74
78
paymentRequestRepository .deleteAll ();
75
79
paymentTransactionRepository .deleteAll ();
76
80
walletRepository .deleteAll ();
77
81
walletTransactionRepository .deleteAll ();
82
+
83
+ redisService .clearCache ();
78
84
}
79
85
80
86
@ AfterAll
81
87
static void stopContainers () {
82
88
postgreSQLContainer .stop ();
83
89
rabbitMQContainer .stop ();
90
+ redisContainer .stop ();
84
91
}
85
92
86
93
@ DynamicPropertySource
@@ -94,6 +101,9 @@ static void setDatasourceProperties(DynamicPropertyRegistry registry) {
94
101
registry .add ("spring.rabbitmq.port" , rabbitMQContainer ::getFirstMappedPort );
95
102
registry .add ("spring.rabbitmq.username" , rabbitMQContainer ::getAdminUsername );
96
103
registry .add ("spring.rabbitmq.password" , rabbitMQContainer ::getAdminPassword );
104
+
105
+ registry .add ("spring.redis.host" , redisContainer ::getHost );
106
+ registry .add ("spring.redis.port" , () -> redisContainer .getMappedPort (6379 ));
97
107
}
98
108
99
109
@ Test
@@ -736,72 +746,49 @@ void testGetInvalidWallet() {
736
746
}
737
747
738
748
@ Test
739
- void testGetClientPaymentTransactions () {
740
- PaymentRequest paymentRequest1 =
741
- PaymentRequest .builder ().withAmount (1200 ).withClientId ("3" ).withFreelancerId ("4" ).build ();
742
- PaymentRequest paymentRequest2 =
743
- PaymentRequest .builder ().withAmount (3600 ).withClientId ("3" ).withFreelancerId ("1" ).build ();
744
- paymentRequestRepository .save (paymentRequest1 );
745
- paymentRequestRepository .save (paymentRequest2 );
746
- PaymentTransaction paymentTransaction1 =
747
- PaymentTransaction .builder ()
749
+ void testGetPaymentRequestResponseFromCache () {
750
+ PaymentRequest paymentRequest =
751
+ PaymentRequest .builder ()
748
752
.withAmount (1200 )
749
- .withPaymentRequestId (paymentRequest1 .getId ())
750
- .build ();
751
- PaymentTransaction paymentTransaction2 =
752
- PaymentTransaction .builder ()
753
- .withAmount (3600 )
754
- .withPaymentRequestId (paymentRequest2 .getId ())
753
+ .withDescription ("Payment for services rendered" )
754
+ .withClientId ("3" )
755
+ .withFreelancerId ("4" )
755
756
.build ();
756
- paymentTransactionRepository .save (paymentTransaction1 );
757
- paymentTransactionRepository .save (paymentTransaction2 );
757
+ paymentRequestRepository .save (paymentRequest );
758
758
759
- GetClientPaymentTransactionsRequest getClientPaymentTransactionsRequest =
760
- GetClientPaymentTransactionsRequest .builder ().withClientId ( "3" ).build ();
759
+ GetPaymentRequestRequest getPaymentRequest =
760
+ GetPaymentRequestRequest .builder ().withPaymentRequestId ( paymentRequest . getId () ).build ();
761
761
762
- GetClientPaymentTransactionsResponse response =
763
- (GetClientPaymentTransactionsResponse )
764
- template .convertSendAndReceive (
765
- ServiceQueueNames .PAYMENTS , getClientPaymentTransactionsRequest );
762
+ GetPaymentRequestResponse response =
763
+ (GetPaymentRequestResponse )
764
+ template .convertSendAndReceive (ServiceQueueNames .PAYMENTS , getPaymentRequest );
766
765
767
766
assertNotNull (response );
768
767
assertEquals (HttpStatusCode .OK , response .getStatusCode ());
769
- assertNotNull (response .getTransactions ());
770
- assertEquals (2 , response .getTransactions ().size ());
771
- }
772
-
773
- @ Test
774
- void testGetFreelancerPaymentTransactions () {
775
- PaymentRequest paymentRequest1 =
776
- PaymentRequest .builder ().withAmount (1200 ).withClientId ("3" ).withFreelancerId ("4" ).build ();
777
- PaymentRequest paymentRequest2 =
778
- PaymentRequest .builder ().withAmount (3600 ).withClientId ("10" ).withFreelancerId ("4" ).build ();
779
- paymentRequestRepository .save (paymentRequest1 );
780
- paymentRequestRepository .save (paymentRequest2 );
781
- PaymentTransaction paymentTransaction1 =
782
- PaymentTransaction .builder ()
783
- .withAmount (1200 )
784
- .withPaymentRequestId (paymentRequest1 .getId ())
785
- .build ();
786
- PaymentTransaction paymentTransaction2 =
787
- PaymentTransaction .builder ()
788
- .withAmount (3600 )
789
- .withPaymentRequestId (paymentRequest2 .getId ())
790
- .build ();
791
- paymentTransactionRepository .save (paymentTransaction1 );
792
- paymentTransactionRepository .save (paymentTransaction2 );
793
768
794
- GetFreelancerPaymentTransactionsRequest getFreelancerPaymentTransactionsRequest =
795
- GetFreelancerPaymentTransactionsRequest .builder ().withFreelancerId ("4" ).build ();
769
+ GetPaymentRequestResponse cachedResponse =
770
+ (GetPaymentRequestResponse )
771
+ redisService .getValue (
772
+ getPaymentRequest .getPaymentRequestId (), GetPaymentRequestResponse .class );
796
773
797
- GetFreelancerPaymentTransactionsResponse response =
798
- (GetFreelancerPaymentTransactionsResponse )
799
- template .convertSendAndReceive (
800
- ServiceQueueNames .PAYMENTS , getFreelancerPaymentTransactionsRequest );
774
+ assertNotNull (cachedResponse );
775
+ assertEquals (HttpStatusCode .OK , cachedResponse .getStatusCode ());
801
776
802
- assertNotNull (response );
803
- assertEquals (HttpStatusCode .OK , response .getStatusCode ());
804
- assertNotNull (response .getTransactions ());
805
- assertEquals (2 , response .getTransactions ().size ());
777
+ assertAll (
778
+ () -> assertEquals (response .getRequest ().getId (), cachedResponse .getRequest ().getId ()),
779
+ () ->
780
+ assertEquals (
781
+ response .getRequest ().getAmount (), cachedResponse .getRequest ().getAmount ()),
782
+ () ->
783
+ assertEquals (
784
+ response .getRequest ().getDescription (),
785
+ cachedResponse .getRequest ().getDescription ()),
786
+ () ->
787
+ assertEquals (
788
+ response .getRequest ().getClientId (), cachedResponse .getRequest ().getClientId ()),
789
+ () ->
790
+ assertEquals (
791
+ response .getRequest ().getFreelancerId (),
792
+ cachedResponse .getRequest ().getFreelancerId ()));
806
793
}
807
794
}
0 commit comments