Skip to content

Commit 7fb20f0

Browse files
committed
merge
2 parents c8d674d + 6a8c88d commit 7fb20f0

File tree

12 files changed

+277
-67
lines changed

12 files changed

+277
-67
lines changed

.github/workflows/maven-publish.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
3+
4+
# This workflow uses actions that are not certified by GitHub.
5+
# They are provided by a third-party and are governed by
6+
# separate terms of service, privacy policy, and support
7+
# documentation.
8+
9+
name: Publish Docker Packeges
10+
11+
on:
12+
push:
13+
branches: [ "main" ]
14+
15+
jobs:
16+
build:
17+
18+
runs-on: ubuntu-latest
19+
20+
permissions:
21+
contents: read
22+
packages: write
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
- name: Set up JDK 21
27+
uses: actions/setup-java@v3
28+
with:
29+
java-version: '21'
30+
distribution: 'temurin'
31+
cache: maven
32+
- name: Build with Maven
33+
run: mvn -DskipTests -B package --file pom.xml
34+
35+
- uses: actions/upload-artifact@v4
36+
name: Upload JAR artifacts
37+
with:
38+
name: jarfiles
39+
path: '**/target/*.jar'
40+
41+
- name: Log in to the Container registry
42+
uses: docker/login-action@v3
43+
with:
44+
username: ${{ secrets.DOCKERHUB_USERNAME }}
45+
password: ${{ secrets.DOCKERHUB_TOKEN }}
46+
47+
- name: Build and push Jobs Image
48+
run: |
49+
docker build ./services/jobs --tag ahmad45123/workup:service_jobs
50+
docker push ahmad45123/workup:service_jobs
51+
52+
- name: Build and push Payments Image
53+
run: |
54+
docker build ./services/payments --tag ahmad45123/workup:service_payments
55+
docker push ahmad45123/workup:service_payments
56+
57+
- name: Build and push Users Image
58+
run: |
59+
docker build ./services/users --tag ahmad45123/workup:service_users
60+
docker push ahmad45123/workup:service_users
61+
62+
- name: Build and push Contracts Image
63+
run: |
64+
docker build ./services/contracts --tag ahmad45123/workup:service_contracts
65+
docker push ahmad45123/workup:service_contracts

compose.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ services:
1616
- "5672:5672"
1717
- "15672:15672"
1818

19+
service_redis:
20+
image: redis:latest
21+
healthcheck:
22+
test: redis-cli ping
23+
interval: 30s
24+
timeout: 30s
25+
retries: 3
26+
networks:
27+
- microservices
28+
ports:
29+
- "6379:6379"
30+
1931
# ----- JOBS MICROSERVICE -------
2032
service_jobs:
2133
build: ./services/jobs
@@ -35,7 +47,9 @@ services:
3547
payments_db:
3648
condition: service_healthy
3749
service_mq:
38-
condition: service_started
50+
condition: service_healthy
51+
service_redis:
52+
condition: service_healthy
3953
networks:
4054
- microservices
4155
- payments

services/payments/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@
8989
<version>1.19.7</version>
9090
<scope>test</scope>
9191
</dependency>
92+
<dependency>
93+
<groupId>org.testcontainers</groupId>
94+
<artifactId>testcontainers</artifactId>
95+
<version>1.19.7</version>
96+
<scope>test</scope>
97+
</dependency>
98+
<dependency>
99+
<groupId>com.redis.testcontainers</groupId>
100+
<artifactId>testcontainers-redis-junit-jupiter</artifactId>
101+
<version>1.4.6</version>
102+
<scope>test</scope>
103+
</dependency>
92104
</dependencies>
93105

94106
<build>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
import org.springframework.amqp.support.converter.MessageConverter;
77
import org.springframework.boot.SpringApplication;
88
import org.springframework.boot.autoconfigure.SpringBootApplication;
9+
import org.springframework.cache.annotation.EnableCaching;
910
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.ComponentScan;
1012

1113
@SpringBootApplication
14+
@ComponentScan(basePackages = "com.workup")
15+
@EnableCaching
1216
public class PaymentsApplication {
1317

1418
public static void main(String[] args) {

services/payments/src/main/java/com/workup/payments/commands/PaymentCommand.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.workup.shared.commands.Command;
88
import com.workup.shared.commands.CommandRequest;
99
import com.workup.shared.commands.CommandResponse;
10+
import com.workup.shared.redis.RedisService;
1011
import lombok.Data;
1112

1213
@Data
@@ -17,4 +18,5 @@ public abstract class PaymentCommand<T extends CommandRequest, Q extends Command
1718
private PaymentTransactionRepository paymentTransactionRepository;
1819
private WalletRepository walletRepository;
1920
private WalletTransactionRepository walletTransactionRepository;
21+
private RedisService redisService;
2022
}

services/payments/src/main/java/com/workup/payments/commands/PaymentCommandMap.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.workup.shared.commands.CommandMap;
1717
import com.workup.shared.commands.CommandRequest;
1818
import com.workup.shared.commands.CommandResponse;
19+
import com.workup.shared.redis.RedisService;
1920
import org.springframework.beans.factory.annotation.Autowired;
2021
import org.springframework.stereotype.Component;
2122

@@ -31,6 +32,8 @@ public class PaymentCommandMap
3132

3233
@Autowired private WalletTransactionRepository walletTransactionRepository;
3334

35+
@Autowired private RedisService redisService;
36+
3437
public void registerCommands() {
3538
/* PaymentRequest commands */
3639
commands.put("CreatePaymentRequest", CreatePaymentRequestCommand.class);
@@ -61,5 +64,7 @@ public void setupCommand(
6164
command.setPaymentTransactionRepository(paymentTransactionRepository);
6265
command.setWalletRepository(walletRepository);
6366
command.setWalletTransactionRepository(walletTransactionRepository);
67+
68+
command.setRedisService(redisService);
6469
}
6570
}

services/payments/src/main/java/com/workup/payments/commands/paymentrequest/GetPaymentRequestCommand.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,26 @@
77
import com.workup.shared.commands.payments.paymentrequest.requests.GetPaymentRequestRequest;
88
import com.workup.shared.commands.payments.paymentrequest.responses.GetPaymentRequestResponse;
99
import com.workup.shared.enums.HttpStatusCode;
10+
import com.workup.shared.redis.RedisService;
1011
import java.util.Optional;
1112

1213
public class GetPaymentRequestCommand
1314
extends PaymentCommand<GetPaymentRequestRequest, GetPaymentRequestResponse> {
1415

1516
@Override
1617
public GetPaymentRequestResponse Run(GetPaymentRequestRequest request) {
18+
RedisService redisService = getRedisService();
19+
20+
GetPaymentRequestResponse cachedResponse =
21+
(GetPaymentRequestResponse)
22+
redisService.getValue(request.getPaymentRequestId(), GetPaymentRequestResponse.class);
23+
if (cachedResponse != null) {
24+
System.out.println(
25+
"[x] Payment request response fetched from cache : " + cachedResponse.getRequest());
26+
27+
return cachedResponse;
28+
}
29+
1730
Optional<PaymentRequest> savedPaymentRequest =
1831
getPaymentRequestRepository().findById(request.getPaymentRequestId());
1932

@@ -30,9 +43,14 @@ public GetPaymentRequestResponse Run(GetPaymentRequestRequest request) {
3043
PaymentRequestDTO paymentRequestDTO =
3144
PaymentRequestMapper.mapToPaymentRequestDTO(savedPaymentRequest.get());
3245

33-
return GetPaymentRequestResponse.builder()
34-
.withStatusCode(HttpStatusCode.OK)
35-
.withRequest(paymentRequestDTO)
36-
.build();
46+
GetPaymentRequestResponse response =
47+
GetPaymentRequestResponse.builder()
48+
.withStatusCode(HttpStatusCode.OK)
49+
.withRequest(paymentRequestDTO)
50+
.build();
51+
52+
redisService.setValue(request.getPaymentRequestId(), response);
53+
54+
return response;
3755
}
3856
}

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

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

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

5+
import com.redis.testcontainers.RedisContainer;
56
import com.workup.payments.models.PaymentRequest;
67
import com.workup.payments.models.PaymentTransaction;
78
import com.workup.payments.models.Wallet;
@@ -13,10 +14,6 @@
1314
import com.workup.shared.commands.payments.dto.PaymentRequestDTO;
1415
import com.workup.shared.commands.payments.paymentrequest.requests.*;
1516
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;
2017
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
2118
import com.workup.shared.commands.payments.wallet.requests.GetWalletRequest;
2219
import com.workup.shared.commands.payments.wallet.responses.CreateWalletResponse;
@@ -34,6 +31,7 @@
3431
import com.workup.shared.enums.payments.PaymentRequestStatus;
3532
import com.workup.shared.enums.payments.PaymentTransactionStatus;
3633
import com.workup.shared.enums.payments.WalletTransactionType;
34+
import com.workup.shared.redis.RedisService;
3735
import java.util.Optional;
3836
import java.util.UUID;
3937
import org.junit.jupiter.api.AfterAll;
@@ -49,6 +47,7 @@
4947
import org.testcontainers.containers.RabbitMQContainer;
5048
import org.testcontainers.junit.jupiter.Container;
5149
import org.testcontainers.junit.jupiter.Testcontainers;
50+
import org.testcontainers.utility.DockerImageName;
5251

5352
@SpringBootTest
5453
@Testcontainers
@@ -63,24 +62,32 @@ class PaymentsApplicationTests {
6362
static final RabbitMQContainer rabbitMQContainer =
6463
new RabbitMQContainer("rabbitmq:3.13-management");
6564

65+
@Container
66+
static final RedisContainer redisContainer =
67+
new RedisContainer(DockerImageName.parse("redis:latest"));
68+
6669
@Autowired private AmqpTemplate template;
6770
@Autowired private PaymentRequestRepository paymentRequestRepository;
6871
@Autowired private PaymentTransactionRepository paymentTransactionRepository;
6972
@Autowired private WalletRepository walletRepository;
7073
@Autowired private WalletTransactionRepository walletTransactionRepository;
74+
@Autowired private RedisService redisService;
7175

7276
@BeforeEach
7377
void clearAll() {
7478
paymentRequestRepository.deleteAll();
7579
paymentTransactionRepository.deleteAll();
7680
walletRepository.deleteAll();
7781
walletTransactionRepository.deleteAll();
82+
83+
redisService.clearCache();
7884
}
7985

8086
@AfterAll
8187
static void stopContainers() {
8288
postgreSQLContainer.stop();
8389
rabbitMQContainer.stop();
90+
redisContainer.stop();
8491
}
8592

8693
@DynamicPropertySource
@@ -94,6 +101,9 @@ static void setDatasourceProperties(DynamicPropertyRegistry registry) {
94101
registry.add("spring.rabbitmq.port", rabbitMQContainer::getFirstMappedPort);
95102
registry.add("spring.rabbitmq.username", rabbitMQContainer::getAdminUsername);
96103
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));
97107
}
98108

99109
@Test
@@ -736,72 +746,49 @@ void testGetInvalidWallet() {
736746
}
737747

738748
@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()
748752
.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")
755756
.build();
756-
paymentTransactionRepository.save(paymentTransaction1);
757-
paymentTransactionRepository.save(paymentTransaction2);
757+
paymentRequestRepository.save(paymentRequest);
758758

759-
GetClientPaymentTransactionsRequest getClientPaymentTransactionsRequest =
760-
GetClientPaymentTransactionsRequest.builder().withClientId("3").build();
759+
GetPaymentRequestRequest getPaymentRequest =
760+
GetPaymentRequestRequest.builder().withPaymentRequestId(paymentRequest.getId()).build();
761761

762-
GetClientPaymentTransactionsResponse response =
763-
(GetClientPaymentTransactionsResponse)
764-
template.convertSendAndReceive(
765-
ServiceQueueNames.PAYMENTS, getClientPaymentTransactionsRequest);
762+
GetPaymentRequestResponse response =
763+
(GetPaymentRequestResponse)
764+
template.convertSendAndReceive(ServiceQueueNames.PAYMENTS, getPaymentRequest);
766765

767766
assertNotNull(response);
768767
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);
793768

794-
GetFreelancerPaymentTransactionsRequest getFreelancerPaymentTransactionsRequest =
795-
GetFreelancerPaymentTransactionsRequest.builder().withFreelancerId("4").build();
769+
GetPaymentRequestResponse cachedResponse =
770+
(GetPaymentRequestResponse)
771+
redisService.getValue(
772+
getPaymentRequest.getPaymentRequestId(), GetPaymentRequestResponse.class);
796773

797-
GetFreelancerPaymentTransactionsResponse response =
798-
(GetFreelancerPaymentTransactionsResponse)
799-
template.convertSendAndReceive(
800-
ServiceQueueNames.PAYMENTS, getFreelancerPaymentTransactionsRequest);
774+
assertNotNull(cachedResponse);
775+
assertEquals(HttpStatusCode.OK, cachedResponse.getStatusCode());
801776

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()));
806793
}
807794
}

0 commit comments

Comments
 (0)