From 4ff78ef0661f3f85144232f0dab6f9d6d0017f2c Mon Sep 17 00:00:00 2001 From: HusseinYasser Date: Fri, 17 May 2024 02:44:56 +0300 Subject: [PATCH] Testing and fixing GetPendingTerminations --- .../GetPendingTerminationsCommand.java | 22 ++-- .../contracts/ContractsApplicationTests.java | 11 ++ .../GetPendingTerminationsTests.java | 115 ++++++++++++++++++ 3 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 services/contracts/src/test/java/com/workup/contracts/GetPendingTerminationsTests.java diff --git a/services/contracts/src/main/java/com/workup/contracts/commands/GetPendingTerminationsCommand.java b/services/contracts/src/main/java/com/workup/contracts/commands/GetPendingTerminationsCommand.java index 525481b1..f8254d21 100644 --- a/services/contracts/src/main/java/com/workup/contracts/commands/GetPendingTerminationsCommand.java +++ b/services/contracts/src/main/java/com/workup/contracts/commands/GetPendingTerminationsCommand.java @@ -2,12 +2,15 @@ import com.workup.contracts.logger.ContractsLogger; import com.workup.contracts.logger.LoggingLevel; +import com.workup.contracts.models.Contract; import com.workup.shared.commands.contracts.TerminationRequest; import com.workup.shared.commands.contracts.requests.GetPendingTerminationsRequest; import com.workup.shared.commands.contracts.responses.GetPendingTerminationsResponse; import com.workup.shared.enums.HttpStatusCode; import com.workup.shared.enums.contracts.TerminationRequestStatus; import java.util.List; +import java.util.Optional; +import java.util.UUID; public class GetPendingTerminationsCommand extends ContractCommand { @@ -15,6 +18,14 @@ public class GetPendingTerminationsCommand @Override public GetPendingTerminationsResponse Run(GetPendingTerminationsRequest request) { + Optional contract = + contractRepository.findById(UUID.fromString(request.getContractId())); + if (contract.isEmpty()) + return GetPendingTerminationsResponse.builder() + .withStatusCode(HttpStatusCode.BAD_REQUEST) + .withErrorMessage("Invalid Contract Id") + .build(); + try { @SuppressWarnings("unchecked") List terminationsList = @@ -30,14 +41,9 @@ public GetPendingTerminationsResponse Run(GetPendingTerminationsRequest request) .withRequestId(String.valueOf(req.getRequestId())) .withContractId(req.getContractId()) .withReason(req.getReason()) - .build()); - - if (terminationsList.isEmpty()) { - return GetPendingTerminationsResponse.builder() - .withStatusCode(HttpStatusCode.NOT_FOUND) - .withErrorMessage("No pending terminations exist") - .build(); - } + .withStatus(req.getStatus()) + .build()) + .toList(); return GetPendingTerminationsResponse.builder() .withTerminationRequests(terminationsList) 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 a0f2c25b..31ebd363 100644 --- a/services/contracts/src/test/java/com/workup/contracts/ContractsApplicationTests.java +++ b/services/contracts/src/test/java/com/workup/contracts/ContractsApplicationTests.java @@ -71,6 +71,7 @@ static void datasourceProperties(DynamicPropertyRegistry registry) { @Autowired GetMilestoneTests getMilestoneTests; @Autowired InitiateContractTests initiateContractTests; @Autowired ViewContractMilestonesTests viewContractMilestonesTests; + @Autowired GetPendingTerminationsTests getPendingTerminationsTests; @BeforeEach void clearAll() { @@ -199,4 +200,14 @@ void InitiateContractTest() { void ViewContractMilestonesTest() { viewContractMilestonesTests.successTest(template); } + + @Test + void GetPendingTerminationsTest1() { + getPendingTerminationsTests.invalidContract(template); + } + + @Test + void GetPendingTerminationsTest2() { + getPendingTerminationsTests.successTest(template); + } } diff --git a/services/contracts/src/test/java/com/workup/contracts/GetPendingTerminationsTests.java b/services/contracts/src/test/java/com/workup/contracts/GetPendingTerminationsTests.java new file mode 100644 index 00000000..959da42c --- /dev/null +++ b/services/contracts/src/test/java/com/workup/contracts/GetPendingTerminationsTests.java @@ -0,0 +1,115 @@ +package com.workup.contracts; + +import static org.junit.jupiter.api.Assertions.*; + +import com.workup.contracts.models.TerminationRequest; +import com.workup.contracts.repositories.TerminationRequestRepository; +import com.workup.shared.commands.contracts.requests.GetPendingTerminationsRequest; +import com.workup.shared.commands.contracts.requests.InitiateContractRequest; +import com.workup.shared.commands.contracts.responses.GetPendingTerminationsResponse; +import com.workup.shared.commands.contracts.responses.InitiateContractResponse; +import com.workup.shared.enums.HttpStatusCode; +import com.workup.shared.enums.ServiceQueueNames; +import com.workup.shared.enums.contracts.TerminationRequestStatus; +import java.util.ArrayList; +import java.util.HashMap; +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 GetPendingTerminationsTests { + + @Autowired TerminationRequestRepository terminationRequestRepository; + + public void invalidContract(AmqpTemplate template) { + GetPendingTerminationsRequest request = + GetPendingTerminationsRequest.builder() + .withContractId(UUID.randomUUID().toString()) + .build(); + + GetPendingTerminationsResponse response = + (GetPendingTerminationsResponse) + template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request); + assertNotNull(response); + assertEquals(HttpStatusCode.BAD_REQUEST, response.getStatusCode()); + } + + public void successTest(AmqpTemplate template) { + // create contract + 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(new ArrayList<>()) + .build(); + + InitiateContractResponse contractResponse = + (InitiateContractResponse) + template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, initiateContractRequest); + + assertNotNull(contractResponse); + assertEquals(HttpStatusCode.CREATED, contractResponse.getStatusCode()); + + String request1 = UUID.randomUUID().toString(), request2 = UUID.randomUUID().toString(); + TerminationRequest clientTerminationRequest = + TerminationRequest.builder() + .withRequestId(UUID.fromString(request1)) + .withReason("m4 3agebny el 4o8l da1") + .withContractId(contractResponse.getContractId()) + .withRequesterId(clientId) + .withStatus(TerminationRequestStatus.PENDING) + .build(); + + TerminationRequest freelancerTerminationRequest = + TerminationRequest.builder() + .withRequestId(UUID.fromString(request2)) + .withReason("m4 3agebny el 4o8l da2") + .withContractId(contractResponse.getContractId()) + .withRequesterId(freelancerId) + .withStatus(TerminationRequestStatus.PENDING) + .build(); + + HashMap expectedTerminationRequests = new HashMap<>(); + expectedTerminationRequests.put(freelancerId, freelancerTerminationRequest); + expectedTerminationRequests.put(clientId, clientTerminationRequest); + + try { + terminationRequestRepository.save(freelancerTerminationRequest); + terminationRequestRepository.save(clientTerminationRequest); + + // Now Get the pending requests :D + GetPendingTerminationsRequest request = + GetPendingTerminationsRequest.builder() + .withContractId(contractResponse.getContractId()) + .build(); + + GetPendingTerminationsResponse response = + (GetPendingTerminationsResponse) + template.convertSendAndReceive(ServiceQueueNames.CONTRACTS, request); + + assertNotNull(response); + assertEquals(HttpStatusCode.OK, response.getStatusCode()); + + response + .getTerminationRequests() + .forEach( + req -> { + TerminationRequest correspondent = + expectedTerminationRequests.get(req.getRequesterId()); + assertEquals(correspondent.getStatus(), req.getStatus()); + assertEquals(correspondent.getContractId(), req.getContractId()); + assertEquals(correspondent.getReason(), req.getReason()); + assertEquals(correspondent.getRequestId().toString(), req.getRequestId()); + assertEquals(correspondent.getRequesterId(), req.getRequesterId()); + }); + } catch (Exception e) { + fail("Error has occurred in the GetPending TerminationRequests Tests"); + } + } +}