Skip to content

Commit

Permalink
[Contracts] Contracts Service Final PR (#86)
Browse files Browse the repository at this point in the history
* working on added basic boilerplate

* formatting changes

* working on progress milestone command

* working on progress milestone command

* worked on implementing remaining contracts commands, added caching

* format message

* small fix to use send and receive instead of just send

* fixed small bug

* adding testing

* formating

* replaced every sys out with the logger template

* reformatting tests

* more tests for TerminationHandling and MarkMilestoneAsPaid commands

* Evluate Milestone command tests

* added test for progressing milestones

* added checking for valid data in database

* added logging to the contracts microservice

* small bug fix

* formatted code

* pulling origin

* Pulling origin

* working on making logger work :(

* formatting

* bruh

* added the remaining testing of all commands

* logger changes

* more logger changes

* Removing caching from service

* Modified the logic of view pending terminations

* Fixing little bug

* Testing and fixing GetPendingTerminations

* Added printContractCommand

Co-Authored-By: Adam Abouelmagd <Adam2431@users.noreply.github.com>

* Print Contract Tests

Co-Authored-By: Adam Abouelmagd <Adam2431@users.noreply.github.com>

* cache back to service level

* validated contractId in view contract milestones command

* formatting

---------

Co-authored-by: Karim Elmosalamy <karimosalamy@gmail.com>
Co-authored-by: Adam Abouelmagd <Adam2431@users.noreply.github.com>
  • Loading branch information
3 people authored May 19, 2024
1 parent 1119783 commit 4488b96
Show file tree
Hide file tree
Showing 41 changed files with 3,407 additions and 261 deletions.
53 changes: 39 additions & 14 deletions services/contracts/pom.xml
Original file line number Diff line number Diff line change
@@ -1,44 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath /> <!-- lookup parent from repository -->
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.workup</groupId>
<artifactId>contracts</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>contracts</name>
<description>contracts</description>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.workup</groupId>
<artifactId>shared</artifactId>
<version>${project.version}</version>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<artifactId>spring-boot-starter-amqp</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.workup</groupId>
<artifactId>shared</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down Expand Up @@ -74,8 +88,8 @@
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
<artifactId>spring-rabbit</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -84,8 +98,19 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.redis.testcontainers</groupId>
<artifactId>testcontainers-redis-junit-jupiter</artifactId>
<version>1.4.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.workup.contracts;

import static com.workup.contracts.tests.InitiateContractTests.initiateContractTest1;

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.logger.LoggingLevel;
import com.workup.shared.enums.ControllerQueueNames;
import com.workup.shared.enums.ServiceQueueNames;
import com.workup.shared.enums.ThreadPoolSize;
Expand Down Expand Up @@ -33,22 +37,10 @@ public static void main(String[] args) {
@Bean
public ApplicationRunner runner(AmqpTemplate template) {
return args -> {
System.out.println("ApplicationRunner is executing");

// // Use below example function to test sending to the queue.
// initiateContractTest1(template);

// RequestContractTerminationTests requestTerminationCommandTests =
// new RequestContractTerminationTests();
// requestTerminationCommandTests.contractNotFoundTest(template);
// requestTerminationCommandTests.unAuthorizedRequestTest(template);
// requestTerminationCommandTests.requestedBeforeTest(template);
// requestTerminationCommandTests.sucessTest(template);
ContractsLogger.print("ApplicationRunner is executing", LoggingLevel.TRACE);

// HandleContractTerminationTests handleContractTerminationTests =
// new HandleContractTerminationTests();
// handleContractTerminationTests.requestNotFoundTest(template);
// handleContractTerminationTests.successTest(template);
// Use below example function to test sending to the queue.
initiateContractTest1(template);
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.workup.contracts.commands.*;
import com.workup.shared.commands.Command;
import com.workup.shared.commands.CommandRequest;
import com.workup.shared.commands.contracts.requests.*;
import com.workup.shared.commands.contracts.requests.ContractTerminationRequest;
import com.workup.shared.commands.contracts.requests.EvaluateMilestoneRequest;
import com.workup.shared.commands.contracts.requests.GetContractRequest;
Expand All @@ -12,6 +13,7 @@
import com.workup.shared.commands.contracts.requests.MarkPaymentCompletedRequest;
import com.workup.shared.commands.contracts.requests.ProgressMilestoneRequest;
import com.workup.shared.commands.contracts.requests.ViewContractMilestonesRequest;
import com.workup.shared.commands.contracts.responses.*;
import com.workup.shared.commands.contracts.responses.ContractTerminationResponse;
import com.workup.shared.commands.contracts.responses.EvaluateMilestoneResponse;
import com.workup.shared.commands.contracts.responses.GetContractResponse;
Expand Down Expand Up @@ -192,6 +194,21 @@ public CompletableFuture<GetPendingTerminationsResponse> receive(GetPendingTermi
.build());
}
}

@RabbitHandler
@Async
public CompletableFuture<GetMilestoneResponse> receive(GetMilestoneRequest in) throws Exception {
return CompletableFuture.completedFuture(
((GetMilestoneCommand) commandMap.getCommand("GetMilestone")).Run(in));
}

@RabbitHandler
@Async
public CompletableFuture<PrintContractResponse> receive(PrintContractRequest in)
throws Exception {
return CompletableFuture.completedFuture(
((PrintContractCommand) commandMap.getCommand("PrintContract")).Run(in));
}
// NEW_COMMAND_BOILERPLATE

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public void registerCommands() {
commands.put("EvaluateMilestone", EvaluateMilestoneCommand.class);
commands.put("ProgressMilestone", ProgressMilestoneCommand.class);
commands.put("GetPendingTerminations", GetPendingTerminationsCommand.class);
commands.put("GetMilestone", GetMilestoneCommand.class);
commands.put("PrintContract", PrintContractCommand.class);
// NEW_COMMAND_BOILERPLATE
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.workup.contracts.commands;

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.logger.LoggingLevel;
import com.workup.contracts.models.Contract;
import com.workup.contracts.models.ContractMilestone;
import com.workup.shared.commands.contracts.requests.EvaluateMilestoneRequest;
Expand Down Expand Up @@ -42,10 +43,10 @@ public EvaluateMilestoneResponse Run(EvaluateMilestoneRequest request) {

try {
contractMilestoneRepository.save(updatedMilestone);
ContractsLogger.print(" [x] Milestone evaluated " + updatedMilestone);
ContractsLogger.print(" [x] Milestone evaluated " + updatedMilestone, LoggingLevel.TRACE);

if (request.getEvaluatedState() == MilestoneState.ACCEPTED) {
ContractsLogger.print(" [x] Sending payment request ");
ContractsLogger.print(" [x] Sending payment request ", LoggingLevel.TRACE);
// Getting the contract as we need to send the freelancer and client id since they are
// in the payment request parameters.
Optional<Contract> contractOptional =
Expand All @@ -63,7 +64,7 @@ public EvaluateMilestoneResponse Run(EvaluateMilestoneRequest request) {
.withReferenceId(updatedMilestone.getMilestoneId().toString())
.build();
rabbitTemplate.convertSendAndReceive(ServiceQueueNames.PAYMENTS, externalRequest);
ContractsLogger.print(" [x] Payment request sent ");
ContractsLogger.print(" [x] Payment request sent ", LoggingLevel.TRACE);
}

return EvaluateMilestoneResponse.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.workup.contracts.commands;

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.logger.LoggingLevel;
import com.workup.contracts.models.Contract;
import com.workup.shared.commands.contracts.requests.GetContractRequest;
import com.workup.shared.commands.contracts.responses.GetContractResponse;
Expand All @@ -18,7 +19,8 @@ public GetContractResponse Run(GetContractRequest request) {
(GetContractResponse) redisService.getValue(cachingKey, GetContractResponse.class);
if (cachedResponse != null) {
ContractsLogger.print(
"[x] Contract request response fetched from cache: " + cachedResponse.toString());
"[x] Contract request response fetched from cache: " + cachedResponse.toString(),
LoggingLevel.TRACE);

return cachedResponse;
}
Expand Down Expand Up @@ -51,8 +53,10 @@ public GetContractResponse Run(GetContractRequest request) {

redisService.setValue(cachingKey, response);
return response;

} catch (Exception ex) {
ContractsLogger.print("[x] Error occurred while fetching contract: " + ex.getMessage());
ContractsLogger.print(
"[x] Error occurred while fetching contract: " + ex.getMessage(), LoggingLevel.TRACE);
ex.printStackTrace();
return GetContractResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.workup.contracts.commands;

import com.workup.contracts.logger.ContractsLogger;
import com.workup.contracts.logger.LoggingLevel;
import com.workup.contracts.models.ContractMilestone;
import com.workup.shared.commands.contracts.requests.GetMilestoneRequest;
import com.workup.shared.commands.contracts.responses.GetMilestoneResponse;
Expand All @@ -13,43 +14,49 @@ public class GetMilestoneCommand

@Override
public GetMilestoneResponse Run(GetMilestoneRequest request) {
String cachingKey = request.getMilestoneId();

GetMilestoneResponse cachedResponse =
(GetMilestoneResponse) redisService.getValue(cachingKey, GetMilestoneResponse.class);
if (cachedResponse != null) {
ContractsLogger.print(
"[x] Milestone request response fetched from cache: " + cachedResponse.toString());

return cachedResponse;
}

Optional<ContractMilestone> milestoneOptional =
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));

if (milestoneOptional.isEmpty()) {
try {
String cachingKey = request.getMilestoneId();
GetMilestoneResponse cachedResponse =
(GetMilestoneResponse) redisService.getValue(cachingKey, GetMilestoneResponse.class);
if (cachedResponse != null) {
ContractsLogger.print(
"[x] Milestone request response fetched from cache: " + cachedResponse.toString(),
LoggingLevel.TRACE);
return cachedResponse;
}

Optional<ContractMilestone> milestoneOptional =
contractMilestoneRepository.findById(UUID.fromString(request.getMilestoneId()));

if (milestoneOptional.isEmpty()) {
return GetMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Requested milestone does not exist")
.build();
}

ContractMilestone milestone = milestoneOptional.get();

GetMilestoneResponse response =
GetMilestoneResponse.builder()
.withContractId(milestone.getContractId())
.withMilestoneId(milestone.getMilestoneId().toString())
.withAmount(milestone.getAmount())
.withDescription(milestone.getDescription())
.withDueDate(milestone.getDueDate())
.withStatus(milestone.getStatus())
.withAmount(milestone.getAmount())
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();

redisService.setValue(cachingKey, response);
return response;
} catch (Exception e) {
return GetMilestoneResponse.builder()
.withStatusCode(HttpStatusCode.NOT_FOUND)
.withErrorMessage("Requested milestone does not exist")
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage(e.getMessage())
.build();
}

ContractMilestone milestone = milestoneOptional.get();

GetMilestoneResponse response =
GetMilestoneResponse.builder()
.withContractId(milestone.getContractId())
.withMilestoneId(milestone.getMilestoneId().toString())
.withAmount(milestone.getAmount())
.withDescription(milestone.getDescription())
.withDueDate(milestone.getDueDate())
.withStatus(milestone.getStatus())
.withAmount(milestone.getAmount())
.withStatusCode(HttpStatusCode.OK)
.withErrorMessage("")
.build();

redisService.setValue(cachingKey, response);
return response;
}
}
Loading

0 comments on commit 4488b96

Please sign in to comment.