Skip to content

Commit 5f415af

Browse files
Abdullah204AhmedNasserGAbdulaziz-Hassan
authored
Payments GetWallet and CreateWallet unit testing (#58)
* Added `GetWallet` request and response * Added `CreatePaymentRequest` request/response * Added inheritance from the Command Request and Response class to the * Added request/response * Added `createWallet` and `getWallet` unit testing --------- Co-authored-by: AhmedNasserG <ahmednasser217217@gmail.com> Co-authored-by: Abdulaziz-Hassan <abdulazizhza@gmail.com>
1 parent 1d94842 commit 5f415af

File tree

2 files changed

+103
-20
lines changed

2 files changed

+103
-20
lines changed

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22

33
import com.workup.payments.commands.PaymentCommandMap;
44
import com.workup.payments.commands.paymentrequest.CreatePaymentRequestCommand;
5+
import com.workup.payments.commands.wallet.CreateWalletCommand;
6+
import com.workup.payments.commands.wallet.GetWalletCommand;
57
import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
68
import com.workup.shared.commands.payments.paymentrequest.responses.CreatePaymentRequestResponse;
9+
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
10+
import com.workup.shared.commands.payments.wallet.requests.GetWalletRequest;
11+
import com.workup.shared.commands.payments.wallet.responses.CreateWalletResponse;
12+
import com.workup.shared.commands.payments.wallet.responses.GetWalletResponse;
713
import com.workup.shared.enums.ServiceQueueNames;
814
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
915
import org.springframework.amqp.rabbit.annotation.RabbitListener;
@@ -14,10 +20,21 @@
1420
@RabbitListener(queues = ServiceQueueNames.PAYMENTS)
1521
public class RabbitMQListener {
1622

17-
@Autowired public PaymentCommandMap commandMap;
23+
@Autowired
24+
public PaymentCommandMap commandMap;
1825

1926
@RabbitHandler
2027
public CreatePaymentRequestResponse receive(CreatePaymentRequestRequest in) throws Exception {
2128
return ((CreatePaymentRequestCommand) commandMap.getCommand("CreatePaymentRequest")).Run(in);
2229
}
30+
31+
@RabbitHandler
32+
public CreateWalletResponse receive(CreateWalletRequest in) throws Exception {
33+
return ((CreateWalletCommand) commandMap.getCommand("CreateWallet")).Run(in);
34+
}
35+
36+
@RabbitHandler
37+
public GetWalletResponse receive(GetWalletRequest in) throws Exception {
38+
return ((GetWalletCommand) commandMap.getCommand("GetWallet")).Run(in);
39+
}
2340
}

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

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,32 @@
2424
import org.testcontainers.containers.RabbitMQContainer;
2525
import org.testcontainers.junit.jupiter.Container;
2626
import org.testcontainers.junit.jupiter.Testcontainers;
27+
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
28+
import com.workup.shared.commands.payments.wallet.responses.CreateWalletResponse;
29+
import com.workup.shared.commands.payments.wallet.requests.GetWalletRequest;
30+
import com.workup.shared.commands.payments.wallet.responses.GetWalletResponse;
2731

2832
@SpringBootTest
2933
@Testcontainers
3034
@Import(TestConfigBase.class)
3135
class PaymentsApplicationTests {
3236

3337
@Container
34-
static final PostgreSQLContainer<?> postgreSQLContainer =
35-
new PostgreSQLContainer<>("postgres:latest");
38+
static final PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:latest");
3639

3740
@Container
38-
static final RabbitMQContainer rabbitMQContainer =
39-
new RabbitMQContainer("rabbitmq:3.13-management");
41+
static final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:3.13-management");
4042

41-
@Autowired private AmqpTemplate template;
42-
@Autowired private PaymentRequestRepository paymentRequestRepository;
43-
@Autowired private PaymentTransactionRepository paymentTransactionRepository;
44-
@Autowired private WalletRepository walletRepository;
45-
@Autowired private WalletTransactionRepository walletTransactionRepository;
43+
@Autowired
44+
private AmqpTemplate template;
45+
@Autowired
46+
private PaymentRequestRepository paymentRequestRepository;
47+
@Autowired
48+
private PaymentTransactionRepository paymentTransactionRepository;
49+
@Autowired
50+
private WalletRepository walletRepository;
51+
@Autowired
52+
private WalletTransactionRepository walletTransactionRepository;
4653

4754
@BeforeEach
4855
void clearAll() {
@@ -73,16 +80,14 @@ static void setDatasourceProperties(DynamicPropertyRegistry registry) {
7380

7481
@Test
7582
void testCreatePaymentRequest() {
76-
CreatePaymentRequestRequest createPaymentRequest =
77-
CreatePaymentRequestRequest.builder()
78-
.withAmount(1200)
79-
.withDescription("Payment for services rendered")
80-
.withClientId("3")
81-
.withFreelancerId("4")
82-
.build();
83-
CreatePaymentRequestResponse response =
84-
(CreatePaymentRequestResponse)
85-
template.convertSendAndReceive(ServiceQueueNames.PAYMENTS, createPaymentRequest);
83+
CreatePaymentRequestRequest createPaymentRequest = CreatePaymentRequestRequest.builder()
84+
.withAmount(1200)
85+
.withDescription("Payment for services rendered")
86+
.withClientId("3")
87+
.withFreelancerId("4")
88+
.build();
89+
CreatePaymentRequestResponse response = (CreatePaymentRequestResponse) template
90+
.convertSendAndReceive(ServiceQueueNames.PAYMENTS, createPaymentRequest);
8691

8792
assertNotNull(response);
8893
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());
@@ -98,4 +103,65 @@ void testCreatePaymentRequest() {
98103
},
99104
() -> fail("Payment request not found"));
100105
}
106+
107+
@Test
108+
void testCreateWalletCommand() {
109+
110+
CreateWalletRequest createWalletRequest = CreateWalletRequest.builder()
111+
.withFreelancerId("1")
112+
.build();
113+
CreateWalletResponse response = (CreateWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
114+
createWalletRequest);
115+
assertNotNull(response);
116+
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());
117+
118+
}
119+
120+
@Test
121+
void testCreateDuplicateWalletIsInvalid() {
122+
CreateWalletRequest createWalletRequest = CreateWalletRequest.builder()
123+
.withFreelancerId("1")
124+
.build();
125+
CreateWalletResponse response = (CreateWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
126+
createWalletRequest);
127+
assertNotNull(response);
128+
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());
129+
130+
CreateWalletResponse response2 = (CreateWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
131+
createWalletRequest);
132+
assertNotNull(response2);
133+
assertEquals(HttpStatusCode.BAD_REQUEST, response2.getStatusCode());
134+
}
135+
136+
@Test
137+
void testGetValidWallet() {
138+
CreateWalletRequest createWalletRequest = CreateWalletRequest.builder()
139+
.withFreelancerId("1")
140+
.build();
141+
CreateWalletResponse response = (CreateWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
142+
createWalletRequest);
143+
assertNotNull(response);
144+
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());
145+
146+
GetWalletRequest getWalletRequest = GetWalletRequest.builder()
147+
.withFreelancerId("1")
148+
.build();
149+
GetWalletResponse getWalletResponse = (GetWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
150+
getWalletRequest);
151+
assertNotNull(getWalletResponse);
152+
assertEquals(HttpStatusCode.OK, getWalletResponse.getStatusCode());
153+
assertEquals(0, getWalletResponse.getBalance());
154+
}
155+
156+
@Test
157+
void testGetInvalidWallet() {
158+
GetWalletRequest getWalletRequest = GetWalletRequest.builder()
159+
.withFreelancerId("1")
160+
.build();
161+
GetWalletResponse getWalletResponse = (GetWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
162+
getWalletRequest);
163+
assertNotNull(getWalletResponse);
164+
assertEquals(HttpStatusCode.NOT_FOUND, getWalletResponse.getStatusCode());
165+
}
166+
101167
}

0 commit comments

Comments
 (0)