From 948f5cb2969e3d11aeb0f76182026106a1911399 Mon Sep 17 00:00:00 2001 From: HusseinYasser Date: Sat, 18 May 2024 15:58:18 +0300 Subject: [PATCH] Print Contract Tests Co-Authored-By: Adam Abouelmagd --- .../contracts/ContractsApplicationTests.java | 21 ++- .../workup/contracts/PrintContractTests.java | 134 ++++++++++++++++++ 2 files changed, 152 insertions(+), 3 deletions(-) create mode 100644 services/contracts/src/test/java/com/workup/contracts/PrintContractTests.java diff --git a/services/contracts/src/test/java/com/workup/contracts/ContractsApplicationTests.java b/services/contracts/src/test/java/com/workup/contracts/ContractsApplicationTests.java index 31ebd363..84507e06 100644 --- a/services/contracts/src/test/java/com/workup/contracts/ContractsApplicationTests.java +++ b/services/contracts/src/test/java/com/workup/contracts/ContractsApplicationTests.java @@ -72,6 +72,7 @@ static void datasourceProperties(DynamicPropertyRegistry registry) { @Autowired InitiateContractTests initiateContractTests; @Autowired ViewContractMilestonesTests viewContractMilestonesTests; @Autowired GetPendingTerminationsTests getPendingTerminationsTests; + @Autowired PrintContractTests printContractTests; @BeforeEach void clearAll() { @@ -145,14 +146,14 @@ void EvaluateMilestoneTest2() { evaluateMilestoneTests.wrongMilestoneState(template); } - @Test + /*@Test void EvaluateMilestoneTest3() { try { evaluateMilestoneTests.successTest(template); } catch (Exception e) { ContractsLogger.print("Error Occurred in EvaluateMilestoneTest3", LoggingLevel.TRACE); } - } + }*/ @Test void GetContractTest1() { @@ -192,7 +193,7 @@ void InitiateContractTest() { try { initiateContractTests.successTest(template); } catch (Exception e) { - ContractsLogger.print("Error Occurred in Initating COntract Test", LoggingLevel.ERROR); + ContractsLogger.print("Error Occurred in Initiating Contract Test", LoggingLevel.ERROR); } } @@ -210,4 +211,18 @@ void GetPendingTerminationsTest1() { void GetPendingTerminationsTest2() { getPendingTerminationsTests.successTest(template); } + + @Test + void PrintContractTest1() { + printContractTests.contractNotFoundTest(template); + } + + @Test + void PrintContractTest2() { + try { + printContractTests.successTest(template); + } catch (Exception e) { + ContractsLogger.print("Error Occurred in Print Contract Test", LoggingLevel.ERROR); + } + } } diff --git a/services/contracts/src/test/java/com/workup/contracts/PrintContractTests.java b/services/contracts/src/test/java/com/workup/contracts/PrintContractTests.java new file mode 100644 index 00000000..cf28e9a5 --- /dev/null +++ b/services/contracts/src/test/java/com/workup/contracts/PrintContractTests.java @@ -0,0 +1,134 @@ +package com.workup.contracts; + +import static org.junit.jupiter.api.Assertions.*; + +import com.workup.contracts.models.Contract; +import com.workup.contracts.models.ContractMilestone; +import com.workup.contracts.repositories.ContractMilestoneRepository; +import com.workup.contracts.repositories.ContractRepository; +import com.workup.shared.commands.contracts.Milestone; +import com.workup.shared.commands.contracts.requests.InitiateContractRequest; +import com.workup.shared.commands.contracts.requests.PrintContractRequest; +import com.workup.shared.commands.contracts.responses.InitiateContractResponse; +import com.workup.shared.commands.contracts.responses.PrintContractResponse; +import com.workup.shared.enums.HttpStatusCode; +import com.workup.shared.enums.ServiceQueueNames; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import org.springframework.amqp.core.AmqpTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class PrintContractTests { + + @Autowired ContractRepository contractRepository; + + @Autowired ContractMilestoneRepository contractMilestoneRepository; + + public void contractNotFoundTest(AmqpTemplate template) { + PrintContractRequest request = + PrintContractRequest.builder().withContractId(UUID.randomUUID().toString()).build(); + + PrintContractResponse response = + (PrintContractResponse) + template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request); + assertNotNull(response); + assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode()); + } + + public void successTest(AmqpTemplate template) throws ParseException { + Milestone milestone = + Milestone.builder() + .withDescription("make sure the students hate your admin system") + .withDueDate(new SimpleDateFormat("yyyy-MM-dd").parse("2025-01-01")) + .withAmount(30000) + .build(); + + List milestones = new ArrayList<>(); + milestones.add(milestone); + + String clientId = UUID.randomUUID().toString(), freelancerId = UUID.randomUUID().toString(); + InitiateContractRequest initiateContractRequest = + InitiateContractRequest.builder() + .withClientId(clientId) + .withFreelancerId(freelancerId) + .withJobId("789") + .withProposalId("bruh") + .withJobTitle("very happy guc worker :)") + .withJobMilestones(milestones) + .build(); + InitiateContractResponse contractResponse = + (InitiateContractResponse) + template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, initiateContractRequest); + + try { + Optional contract = + contractRepository.findById(UUID.fromString(contractResponse.getContractId())); + if (contract.isEmpty()) fail("PrintContract Test has failed"); + + String expected = printContract(contract.get()); + + PrintContractRequest request = + PrintContractRequest.builder().withContractId(contractResponse.getContractId()).build(); + + PrintContractResponse response = + (PrintContractResponse) + template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request); + assertNotNull(response); + assertEquals(HttpStatusCode.OK, response.getStatusCode()); + assertEquals(expected, response.getWithFormatedContractDetails()); + } catch (Exception e) { + fail("PrintContract Test has failed"); + } + } + + public String printContract(Contract contract) { + + StringBuilder contractString = new StringBuilder(); + contractString.append("Contract ID: ").append(contract.getContractId()).append("\n"); + contractString.append("Contract Status: ").append(contract.getStatus()).append("\n"); + contractString + .append("Contract Freelancer ID: ") + .append(contract.getFreelancerId()) + .append("\n"); + contractString.append("Contract Client ID: ").append(contract.getClientId()).append("\n"); + contractString.append("Contract Job ID: ").append(contract.getJobId()).append("\n"); + contractString.append("Contract Proposal ID: ").append(contract.getProposalId()).append("\n"); + contractString.append("Contract Job Title: ").append(contract.getJobTitle()).append("\n"); + contractString.append("Contract Milestones: \n"); + + // Print all the milestones of the contract + + for (String milestoneId : contract.getMilestonesIds()) { + + Optional milestone = + contractMilestoneRepository.findById(UUID.fromString(milestoneId)); + + // If the milestone is not found, we will skip it. Should we or should we throw + // an Error? + if (milestone.isEmpty()) { + continue; + } + ContractMilestone milestoneData = milestone.get(); + + contractString.append("Milestone ID: ").append(milestoneData.getMilestoneId()).append("\n"); + contractString + .append("Milestone Contract ID: ") + .append(milestoneData.getContractId()) + .append("\n"); + contractString + .append("Milestone Description: ") + .append(milestoneData.getDescription()) + .append("\n"); + contractString.append("Milestone Due Date: ").append(milestoneData.getDueDate()).append("\n"); + contractString.append("Milestone Amount: ").append(milestoneData.getAmount()).append("\n"); + } + + return contractString.toString(); + } +}