Skip to content

Commit

Permalink
Add testing setup (#55)
Browse files Browse the repository at this point in the history
* Add testing setup

* Delete old testing setup
  • Loading branch information
Abdulaziz-Hassan authored Apr 29, 2024
1 parent 53fce39 commit 1d94842
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 27 deletions.
32 changes: 24 additions & 8 deletions services/payments/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,30 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-testcontainers</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.19.6</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>rabbitmq</artifactId>
<version>1.19.7</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -85,14 +109,6 @@
<groupId>com.cosium.code</groupId>
<artifactId>git-code-format-maven-plugin</artifactId>
<version>5.3</version>
<executions>
<execution>
<id>validate-code-format</id>
<goals>
<goal>validate-code-format</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.cosium.code</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package com.workup.payments;

import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
import com.workup.shared.enums.ServiceQueueNames;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
Expand All @@ -18,20 +15,6 @@ public static void main(String[] args) {
SpringApplication.run(PaymentsApplication.class, args);
}

@Bean
public ApplicationRunner runner(AmqpTemplate template) {
return args -> {
CreatePaymentRequestRequest createPaymentRequest =
CreatePaymentRequestRequest.builder()
.withAmount(1200)
.withDescription("Payment for services rendered")
.withClientId("3")
.withFreelancerId("4")
.build();
template.convertSendAndReceive(ServiceQueueNames.PAYMENTS, createPaymentRequest);
};
}

@Bean
public Queue myQueue() {
return new Queue(ServiceQueueNames.PAYMENTS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import javax.validation.constraints.DecimalMin;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;

@Entity
@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
public class PaymentRequest {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import javax.validation.constraints.DecimalMin;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;

@Entity
@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
public class PaymentTransaction {

@Id
Expand Down Expand Up @@ -41,4 +43,19 @@ public class PaymentTransaction {
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated_at", nullable = false)
private Date updatedAt;

public PaymentTransaction(
String id,
String paymentRequestId,
double amount,
PaymentTransactionStatus status,
Date createdAt,
Date updatedAt) {
this.id = id;
this.paymentRequestId = paymentRequestId;
this.amount = amount;
this.status = PaymentTransactionStatus.PENDING;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

@Entity
@Data
@Builder(setterPrefix = "with")
@AllArgsConstructor
@NoArgsConstructor
public class Wallet {

@Id private String freelancerId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;

@Entity
@Data
@Builder(setterPrefix = "with")
@AllArgsConstructor
@NoArgsConstructor
public class WalletTransaction {

@Id
Expand Down
7 changes: 6 additions & 1 deletion services/payments/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true
server.error.include-message=always
server.error.include-message=always

spring.rabbitmq.host=service_mq
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
Original file line number Diff line number Diff line change
@@ -1,11 +1,101 @@
package com.workup.payments;

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

import com.workup.payments.repositories.PaymentRequestRepository;
import com.workup.payments.repositories.PaymentTransactionRepository;
import com.workup.payments.repositories.WalletRepository;
import com.workup.payments.repositories.WalletTransactionRepository;
import com.workup.shared.commands.payments.paymentrequest.requests.CreatePaymentRequestRequest;
import com.workup.shared.commands.payments.paymentrequest.responses.CreatePaymentRequestResponse;
import com.workup.shared.enums.HttpStatusCode;
import com.workup.shared.enums.ServiceQueueNames;
import java.util.UUID;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.RabbitMQContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

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

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

@Container
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;

@BeforeEach
void clearAll() {
paymentRequestRepository.deleteAll();
paymentTransactionRepository.deleteAll();
walletRepository.deleteAll();
walletTransactionRepository.deleteAll();
}

@AfterAll
static void stopContainers() {
postgreSQLContainer.stop();
rabbitMQContainer.stop();
}

@DynamicPropertySource
static void setDatasourceProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.jpa.hibernate.ddl-auto", () -> "create-drop");

registry.add("spring.rabbitmq.host", rabbitMQContainer::getHost);
registry.add("spring.rabbitmq.port", rabbitMQContainer::getFirstMappedPort);
registry.add("spring.rabbitmq.username", rabbitMQContainer::getAdminUsername);
registry.add("spring.rabbitmq.password", rabbitMQContainer::getAdminPassword);
}

@Test
void contextLoads() {}
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);

assertNotNull(response);
assertEquals(HttpStatusCode.CREATED, response.getStatusCode());

paymentRequestRepository
.findById(String.valueOf(UUID.fromString(response.getPaymentRequestId())))
.ifPresentOrElse(
paymentRequest -> {
assertEquals(1200, paymentRequest.getAmount());
assertEquals("Payment for services rendered", paymentRequest.getDescription());
assertEquals("3", paymentRequest.getClientId());
assertEquals("4", paymentRequest.getFreelancerId());
},
() -> fail("Payment request not found"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.workup.payments;

import com.workup.shared.enums.ServiceQueueNames;
import org.springframework.amqp.core.Queue;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;

@TestConfiguration
public class TestConfigBase {

@Bean
public Queue paymentsQueueMock() {
return new Queue(ServiceQueueNames.PAYMENTS);
}
}

0 comments on commit 1d94842

Please sign in to comment.