Skip to content

Commit

Permalink
Added createWallet and getWallet unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdullah204 committed May 1, 2024
1 parent 468f7b5 commit 73b74b5
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import com.workup.payments.commands.PaymentCommandMap;
import com.workup.payments.commands.paymentrequest.CreatePaymentRequestCommand;
import com.workup.payments.commands.wallet.CreateWalletCommand;
import com.workup.payments.commands.wallet.GetWalletCommand;
import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
import com.workup.shared.commands.payments.paymentrequest.responses.CreatePaymentRequestResponse;
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
import com.workup.shared.commands.payments.wallet.requests.GetWalletRequest;
import com.workup.shared.commands.payments.wallet.responses.CreateWalletResponse;
import com.workup.shared.commands.payments.wallet.responses.GetWalletResponse;
import com.workup.shared.enums.ServiceQueueNames;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
Expand All @@ -14,10 +20,21 @@
@RabbitListener(queues = ServiceQueueNames.PAYMENTS)
public class RabbitMQListener {

@Autowired public PaymentCommandMap commandMap;
@Autowired
public PaymentCommandMap commandMap;

@RabbitHandler
public CreatePaymentRequestResponse receive(CreatePaymentRequestRequest in) throws Exception {
return ((CreatePaymentRequestCommand) commandMap.getCommand("CreatePaymentRequest")).Run(in);
}

@RabbitHandler
public CreateWalletResponse receive(CreateWalletRequest in) throws Exception {
return ((CreateWalletCommand) commandMap.getCommand("CreateWallet")).Run(in);
}

@RabbitHandler
public GetWalletResponse receive(GetWalletRequest in) throws Exception {
return ((GetWalletCommand) commandMap.getCommand("GetWallet")).Run(in);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,32 @@
import org.testcontainers.containers.RabbitMQContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
import com.workup.shared.commands.payments.wallet.responses.CreateWalletResponse;
import com.workup.shared.commands.payments.wallet.requests.GetWalletRequest;
import com.workup.shared.commands.payments.wallet.responses.GetWalletResponse;

@SpringBootTest
@Testcontainers
@Import(TestConfigBase.class)
class PaymentsApplicationTests {

@Container
static final PostgreSQLContainer<?> postgreSQLContainer =
new PostgreSQLContainer<>("postgres:latest");
static final PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:latest");

@Container
static final RabbitMQContainer rabbitMQContainer =
new RabbitMQContainer("rabbitmq:3.13-management");
static final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:3.13-management");

@Autowired private AmqpTemplate template;
@Autowired private PaymentRequestRepository paymentRequestRepository;
@Autowired private PaymentTransactionRepository paymentTransactionRepository;
@Autowired private WalletRepository walletRepository;
@Autowired private WalletTransactionRepository walletTransactionRepository;
@Autowired
private AmqpTemplate template;
@Autowired
private PaymentRequestRepository paymentRequestRepository;
@Autowired
private PaymentTransactionRepository paymentTransactionRepository;
@Autowired
private WalletRepository walletRepository;
@Autowired
private WalletTransactionRepository walletTransactionRepository;

@BeforeEach
void clearAll() {
Expand Down Expand Up @@ -73,16 +80,14 @@ static void setDatasourceProperties(DynamicPropertyRegistry registry) {

@Test
void testCreatePaymentRequest() {
CreatePaymentRequestRequest createPaymentRequest =
CreatePaymentRequestRequest.builder()
.withAmount(1200)
.withDescription("Payment for services rendered")
.withClientId("3")
.withFreelancerId("4")
.build();
CreatePaymentRequestResponse response =
(CreatePaymentRequestResponse)
template.convertSendAndReceive(ServiceQueueNames.PAYMENTS, createPaymentRequest);
CreatePaymentRequestRequest createPaymentRequest = CreatePaymentRequestRequest.builder()
.withAmount(1200)
.withDescription("Payment for services rendered")
.withClientId("3")
.withFreelancerId("4")
.build();
CreatePaymentRequestResponse response = (CreatePaymentRequestResponse) template
.convertSendAndReceive(ServiceQueueNames.PAYMENTS, createPaymentRequest);

assertNotNull(response);
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());
Expand All @@ -98,4 +103,65 @@ void testCreatePaymentRequest() {
},
() -> fail("Payment request not found"));
}

@Test
void testCreateWalletCommand() {

CreateWalletRequest createWalletRequest = CreateWalletRequest.builder()
.withFreelancerId("1")
.build();
CreateWalletResponse response = (CreateWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
createWalletRequest);
assertNotNull(response);
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());

}

@Test
void testCreateDuplicateWalletIsInvalid() {
CreateWalletRequest createWalletRequest = CreateWalletRequest.builder()
.withFreelancerId("1")
.build();
CreateWalletResponse response = (CreateWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
createWalletRequest);
assertNotNull(response);
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());

CreateWalletResponse response2 = (CreateWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
createWalletRequest);
assertNotNull(response2);
assertEquals(HttpStatusCode.BAD_REQUEST, response2.getStatusCode());
}

@Test
void testGetValidWallet() {
CreateWalletRequest createWalletRequest = CreateWalletRequest.builder()
.withFreelancerId("1")
.build();
CreateWalletResponse response = (CreateWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
createWalletRequest);
assertNotNull(response);
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());

GetWalletRequest getWalletRequest = GetWalletRequest.builder()
.withFreelancerId("1")
.build();
GetWalletResponse getWalletResponse = (GetWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
getWalletRequest);
assertNotNull(getWalletResponse);
assertEquals(HttpStatusCode.OK, getWalletResponse.getStatusCode());
assertEquals(0, getWalletResponse.getBalance());
}

@Test
void testGetInvalidWallet() {
GetWalletRequest getWalletRequest = GetWalletRequest.builder()
.withFreelancerId("1")
.build();
GetWalletResponse getWalletResponse = (GetWalletResponse) template.convertSendAndReceive(ServiceQueueNames.PAYMENTS,
getWalletRequest);
assertNotNull(getWalletResponse);
assertEquals(HttpStatusCode.NOT_FOUND, getWalletResponse.getStatusCode());
}

}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 73b74b5

Please sign in to comment.